go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/generators/platform.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package generators
    16  
    17  import (
    18  	"runtime"
    19  	"strings"
    20  
    21  	"go.chromium.org/luci/common/system/environ"
    22  )
    23  
    24  // Platforms is the cross-compile platform tuple.
    25  type Platforms struct {
    26  	Build  Platform
    27  	Host   Platform
    28  	Target Platform
    29  }
    30  
    31  // Platform defines the environment outside the build system, which can't be
    32  // identified in the environment variables or commands, but will affect the
    33  // outputs. Platform must includes operating system and architecture, but
    34  // also support any attributes that may affect the results (e.g. glibcABI).
    35  type Platform struct {
    36  	// (ab)use the Env implementation since it's almost same as the key-value
    37  	// platform attributes.
    38  	// TODO(fancl): properly implement it.
    39  	attributes environ.Env
    40  }
    41  
    42  // NewPlatform returns a Platform with os and arch set.
    43  func NewPlatform(os, arch string) Platform {
    44  	p := Platform{attributes: environ.New(nil)}
    45  	p.Set("os", os)
    46  	p.Set("arch", arch)
    47  	return p
    48  }
    49  
    50  // Get will return the value of the attribute
    51  func (p Platform) Get(key string) string {
    52  	return p.attributes.Get(key)
    53  }
    54  
    55  // Set will set the attribute.
    56  func (p Platform) Set(key, val string) {
    57  	if strings.Contains(key, ",") || strings.Contains(val, ",") {
    58  		panic("comma is not allowd in the platform attributes")
    59  	}
    60  	p.attributes.Set(key, val)
    61  }
    62  
    63  func (p Platform) OS() string {
    64  	return p.attributes.Get("os")
    65  }
    66  
    67  func (p Platform) Arch() string {
    68  	return p.attributes.Get("arch")
    69  }
    70  
    71  func (p Platform) String() string {
    72  	attrs := p.attributes.Sorted()
    73  	return strings.Join(attrs, ",")
    74  }
    75  
    76  // CurrentPlatform returns a Platform with os and arch set to runtime.GOOS and
    77  // runtime.GOARCH.
    78  func CurrentPlatform() Platform {
    79  	return NewPlatform(runtime.GOOS, runtime.GOARCH)
    80  }
    81  
    82  // PlatformFromCIPD converts cipd platform to Platform.
    83  func PlatformFromCIPD(cipdPlat string) Platform {
    84  	ss := strings.SplitN(cipdPlat, "-", 2)
    85  	os, arch := ss[0], ss[1]
    86  	if os == "mac" {
    87  		os = "darwin"
    88  	}
    89  
    90  	// From https://source.chromium.org/chromium/infra/infra/+/main:recipes/recipe_modules/support_3pp/create.py;l=61;drc=3e18f7231543c975ecb0a620bb466471b0ea2c2b
    91  	if arch == "armv6l" {
    92  		arch = "arm"
    93  	}
    94  	return NewPlatform(os, arch)
    95  }