platform.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright 2017 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package rule
  13. import (
  14. "sort"
  15. )
  16. // Platform represents a GOOS/GOARCH pair. When Platform is used to describe
  17. // sources, dependencies, or flags, either OS or Arch may be empty.
  18. type Platform struct {
  19. OS, Arch string
  20. }
  21. // String returns OS, Arch, or "OS_Arch" if both are set. This must match
  22. // the names of config_setting rules in @io_bazel_rules_go//go/platform.
  23. func (p Platform) String() string {
  24. switch {
  25. case p.OS != "" && p.Arch != "":
  26. return p.OS + "_" + p.Arch
  27. case p.OS != "":
  28. return p.OS
  29. case p.Arch != "":
  30. return p.Arch
  31. default:
  32. return ""
  33. }
  34. }
  35. // KnownPlatforms is the set of target platforms that Go supports. Gazelle
  36. // will generate multi-platform build files using these tags. rules_go and
  37. // Bazel may not actually support all of these.
  38. var KnownPlatforms = []Platform{
  39. {"android", "386"},
  40. {"android", "amd64"},
  41. {"android", "arm"},
  42. {"android", "arm64"},
  43. {"darwin", "386"},
  44. {"darwin", "amd64"},
  45. {"darwin", "arm"},
  46. {"darwin", "arm64"},
  47. {"dragonfly", "amd64"},
  48. {"freebsd", "386"},
  49. {"freebsd", "amd64"},
  50. {"freebsd", "arm"},
  51. {"linux", "386"},
  52. {"linux", "amd64"},
  53. {"linux", "arm"},
  54. {"linux", "arm64"},
  55. {"linux", "mips"},
  56. {"linux", "mips64"},
  57. {"linux", "mips64le"},
  58. {"linux", "mipsle"},
  59. {"linux", "ppc64"},
  60. {"linux", "ppc64le"},
  61. {"linux", "s390x"},
  62. {"nacl", "386"},
  63. {"nacl", "amd64p32"},
  64. {"nacl", "arm"},
  65. {"netbsd", "386"},
  66. {"netbsd", "amd64"},
  67. {"netbsd", "arm"},
  68. {"openbsd", "386"},
  69. {"openbsd", "amd64"},
  70. {"openbsd", "arm"},
  71. {"plan9", "386"},
  72. {"plan9", "amd64"},
  73. {"plan9", "arm"},
  74. {"solaris", "amd64"},
  75. {"windows", "386"},
  76. {"windows", "amd64"},
  77. }
  78. var (
  79. // KnownOSs is the sorted list of operating systems that Go supports.
  80. KnownOSs []string
  81. // KnownOSSet is the set of operating systems that Go supports.
  82. KnownOSSet map[string]bool
  83. // KnownArchs is the sorted list of architectures that Go supports.
  84. KnownArchs []string
  85. // KnownArchSet is the set of architectures that Go supports.
  86. KnownArchSet map[string]bool
  87. // KnownOSArchs is a map from OS to the archictures they run on.
  88. KnownOSArchs map[string][]string
  89. // KnownArchOSs is a map from architectures to that OSs that run on them.
  90. KnownArchOSs map[string][]string
  91. )
  92. func init() {
  93. KnownOSSet = make(map[string]bool)
  94. KnownArchSet = make(map[string]bool)
  95. KnownOSArchs = make(map[string][]string)
  96. KnownArchOSs = make(map[string][]string)
  97. for _, p := range KnownPlatforms {
  98. KnownOSSet[p.OS] = true
  99. KnownArchSet[p.Arch] = true
  100. KnownOSArchs[p.OS] = append(KnownOSArchs[p.OS], p.Arch)
  101. KnownArchOSs[p.Arch] = append(KnownArchOSs[p.Arch], p.OS)
  102. }
  103. KnownOSs = make([]string, 0, len(KnownOSSet))
  104. KnownArchs = make([]string, 0, len(KnownArchSet))
  105. for os := range KnownOSSet {
  106. KnownOSs = append(KnownOSs, os)
  107. }
  108. for arch := range KnownArchSet {
  109. KnownArchs = append(KnownArchs, arch)
  110. }
  111. sort.Strings(KnownOSs)
  112. sort.Strings(KnownArchs)
  113. }