platform.go 3.7 KB

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