go.bzl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2019 The Kubernetes 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. load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
  15. # Defines several go_binary rules to work around a Bazel issue which makes
  16. # the pure attribute on go_binary not configurable.
  17. # The name provided will have cgo enabled if targeting Linux, and will
  18. # be a pure go binary otherwise. Additionally, if targeting Windows, the
  19. # output filename will have a .exe suffix.
  20. def go_binary_conditional_pure(name, tags = None, **kwargs):
  21. tags = tags or []
  22. tags.append("manual")
  23. go_binary(
  24. name = "_%s-cgo" % name,
  25. out = name,
  26. pure = "off",
  27. tags = tags,
  28. **kwargs
  29. )
  30. # Define a rule for both Unix and Windows exe suffixes.
  31. [go_binary(
  32. name = "_%s-pure" % out,
  33. out = out,
  34. pure = "on",
  35. tags = tags,
  36. **kwargs
  37. ) for out in [name, name + ".exe"]]
  38. # The real magic, where we work around the pure attribute not being
  39. # configurable: select the appropriate go_binary rule above based on the
  40. # configured platform.
  41. native.alias(
  42. name = name,
  43. actual = select({
  44. "@io_bazel_rules_go//go/platform:linux": ":_%s-cgo" % name,
  45. "@io_bazel_rules_go//go/platform:windows": ":_%s.exe-pure" % name,
  46. "//conditions:default": ":_%s-pure" % name,
  47. }),
  48. )
  49. # Defines several go_test rules to work around a Bazel issue which makes
  50. # the pure attribute on go_test not configurable.
  51. # This also defines genrules to produce test binaries named ${out} and
  52. # ${out}.exe, and an alias named ${out}_binary which automatically selects
  53. # the correct filename suffix (i.e. with a .exe on Windows).
  54. def go_test_conditional_pure(name, out, tags = None, **kwargs):
  55. tags = tags or []
  56. tags.append("manual")
  57. go_test(
  58. name = "_%s-cgo" % name,
  59. pure = "off",
  60. testonly = False,
  61. tags = tags,
  62. **kwargs
  63. )
  64. go_test(
  65. name = "_%s-pure" % name,
  66. pure = "on",
  67. testonly = False,
  68. tags = tags,
  69. **kwargs
  70. )
  71. native.alias(
  72. name = name,
  73. actual = select({
  74. "@io_bazel_rules_go//go/platform:linux": ":_%s-cgo" % name,
  75. "//conditions:default": ":_%s-pure" % name,
  76. }),
  77. )
  78. [native.genrule(
  79. name = "gen_%s" % o,
  80. srcs = [name],
  81. outs = [o],
  82. cmd = "cp $< $@;",
  83. output_to_bindir = True,
  84. executable = True,
  85. tags = tags,
  86. ) for o in [out, out + ".exe"]]
  87. native.alias(
  88. name = "%s_binary" % out,
  89. actual = select({
  90. "@io_bazel_rules_go//go/platform:windows": ":gen_%s.exe" % out,
  91. "//conditions:default": ":gen_%s" % out,
  92. }),
  93. )