github.com/wolfd/bazel-gazelle@v0.14.0/internal/rule/platform.go (about)

     1  /* Copyright 2017 The Bazel Authors. All rights reserved.
     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  
    16  package rule
    17  
    18  import (
    19  	"sort"
    20  )
    21  
    22  // Platform represents a GOOS/GOARCH pair. When Platform is used to describe
    23  // sources, dependencies, or flags, either OS or Arch may be empty.
    24  type Platform struct {
    25  	OS, Arch string
    26  }
    27  
    28  // String returns OS, Arch, or "OS_Arch" if both are set. This must match
    29  // the names of config_setting rules in @io_bazel_rules_go//go/platform.
    30  func (p Platform) String() string {
    31  	switch {
    32  	case p.OS != "" && p.Arch != "":
    33  		return p.OS + "_" + p.Arch
    34  	case p.OS != "":
    35  		return p.OS
    36  	case p.Arch != "":
    37  		return p.Arch
    38  	default:
    39  		return ""
    40  	}
    41  }
    42  
    43  // KnownPlatforms is the set of target platforms that Go supports. Gazelle
    44  // will generate multi-platform build files using these tags. rules_go and
    45  // Bazel may not actually support all of these.
    46  var KnownPlatforms = []Platform{
    47  	{"android", "386"},
    48  	{"android", "amd64"},
    49  	{"android", "arm"},
    50  	{"android", "arm64"},
    51  	{"darwin", "386"},
    52  	{"darwin", "amd64"},
    53  	{"darwin", "arm"},
    54  	{"darwin", "arm64"},
    55  	{"dragonfly", "amd64"},
    56  	{"freebsd", "386"},
    57  	{"freebsd", "amd64"},
    58  	{"freebsd", "arm"},
    59  	{"linux", "386"},
    60  	{"linux", "amd64"},
    61  	{"linux", "arm"},
    62  	{"linux", "arm64"},
    63  	{"linux", "mips"},
    64  	{"linux", "mips64"},
    65  	{"linux", "mips64le"},
    66  	{"linux", "mipsle"},
    67  	{"linux", "ppc64"},
    68  	{"linux", "ppc64le"},
    69  	{"linux", "s390x"},
    70  	{"nacl", "386"},
    71  	{"nacl", "amd64p32"},
    72  	{"nacl", "arm"},
    73  	{"netbsd", "386"},
    74  	{"netbsd", "amd64"},
    75  	{"netbsd", "arm"},
    76  	{"openbsd", "386"},
    77  	{"openbsd", "amd64"},
    78  	{"openbsd", "arm"},
    79  	{"plan9", "386"},
    80  	{"plan9", "amd64"},
    81  	{"plan9", "arm"},
    82  	{"solaris", "amd64"},
    83  	{"windows", "386"},
    84  	{"windows", "amd64"},
    85  }
    86  
    87  var (
    88  	// KnownOSs is the sorted list of operating systems that Go supports.
    89  	KnownOSs []string
    90  
    91  	// KnownOSSet is the set of operating systems that Go supports.
    92  	KnownOSSet map[string]bool
    93  
    94  	// KnownArchs is the sorted list of architectures that Go supports.
    95  	KnownArchs []string
    96  
    97  	// KnownArchSet is the set of architectures that Go supports.
    98  	KnownArchSet map[string]bool
    99  
   100  	// KnownOSArchs is a map from OS to the archictures they run on.
   101  	KnownOSArchs map[string][]string
   102  
   103  	// KnownArchOSs is a map from architectures to that OSs that run on them.
   104  	KnownArchOSs map[string][]string
   105  )
   106  
   107  func init() {
   108  	KnownOSSet = make(map[string]bool)
   109  	KnownArchSet = make(map[string]bool)
   110  	KnownOSArchs = make(map[string][]string)
   111  	KnownArchOSs = make(map[string][]string)
   112  	for _, p := range KnownPlatforms {
   113  		KnownOSSet[p.OS] = true
   114  		KnownArchSet[p.Arch] = true
   115  		KnownOSArchs[p.OS] = append(KnownOSArchs[p.OS], p.Arch)
   116  		KnownArchOSs[p.Arch] = append(KnownArchOSs[p.Arch], p.OS)
   117  	}
   118  	KnownOSs = make([]string, 0, len(KnownOSSet))
   119  	KnownArchs = make([]string, 0, len(KnownArchSet))
   120  	for os := range KnownOSSet {
   121  		KnownOSs = append(KnownOSs, os)
   122  	}
   123  	for arch := range KnownArchSet {
   124  		KnownArchs = append(KnownArchs, arch)
   125  	}
   126  	sort.Strings(KnownOSs)
   127  	sort.Strings(KnownArchs)
   128  }