From 7a938eb5414a2019759c61bfd6d6147b9c3b0771 Mon Sep 17 00:00:00 2001 From: Jeff Grafton Date: Fri, 8 Feb 2019 14:15:16 -0800 Subject: [PATCH] Create work-around wrappers for pure attribute on go_binary and go_test This enables cgo when cross-compiling certain tests and binaries to Linux, while disabling cgo for Windows and Darwin. --- build/BUILD | 4 +- build/go.bzl | 103 +++++++++++++++++++++++++++++++ cluster/images/conformance/BUILD | 2 +- cmd/gendocs/BUILD | 6 +- cmd/genkubedocs/BUILD | 5 +- cmd/genman/BUILD | 6 +- cmd/genswaggertypedocs/BUILD | 6 +- cmd/genyaml/BUILD | 6 +- cmd/hyperkube/BUILD | 6 +- cmd/kubelet/BUILD | 6 +- cmd/kubemark/BUILD | 6 +- cmd/linkcheck/BUILD | 6 +- test/e2e/BUILD | 18 ++---- test/e2e_kubeadm/BUILD | 16 ++--- test/e2e_node/BUILD | 16 ++--- 15 files changed, 150 insertions(+), 62 deletions(-) create mode 100644 build/go.bzl diff --git a/build/BUILD b/build/BUILD index 340f9236b6c..e015f503324 100644 --- a/build/BUILD +++ b/build/BUILD @@ -143,8 +143,8 @@ filegroup( "//cmd/genyaml", "//cmd/kubemark", # TODO: server platforms only "//cmd/linkcheck", - "//test/e2e:e2e.test", - "//test/e2e_node:e2e_node.test", # TODO: server platforms only + "//test/e2e:e2e.test_binary", + "//test/e2e_node:e2e_node.test_binary", # TODO: server platforms only "//vendor/github.com/onsi/ginkgo/ginkgo", ], ) diff --git a/build/go.bzl b/build/go.bzl new file mode 100644 index 00000000000..58ce9c101b0 --- /dev/null +++ b/build/go.bzl @@ -0,0 +1,103 @@ +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test") + +# Defines several go_binary rules to work around a Bazel issue which makes +# the pure attribute on go_binary not configurable. +# The name provided will have cgo enabled if targeting Linux, and will +# be a pure go binary otherwise. Additionally, if targeting Windows, the +# output filename will have a .exe suffix. +def go_binary_conditional_pure(name, tags = None, **kwargs): + tags = tags or [] + tags.append("manual") + go_binary( + name = "_%s-cgo" % name, + out = name, + pure = "off", + tags = tags, + **kwargs + ) + + # Define a rule for both Unix and Windows exe suffixes. + [go_binary( + name = "_%s-pure" % out, + out = out, + pure = "on", + tags = tags, + **kwargs + ) for out in [name, name + ".exe"]] + + # The real magic, where we work around the pure attribute not being + # configurable: select the appropriate go_binary rule above based on the + # configured platform. + native.alias( + name = name, + actual = select({ + "@io_bazel_rules_go//go/platform:linux": ":_%s-cgo" % name, + "@io_bazel_rules_go//go/platform:windows": ":_%s.exe-pure" % name, + "//conditions:default": ":_%s-pure" % name, + }), + ) + +# Defines several go_test rules to work around a Bazel issue which makes +# the pure attribute on go_test not configurable. +# This also defines genrules to produce test binaries named ${out} and +# ${out}.exe, and an alias named ${out}_binary which automatically selects +# the correct filename suffix (i.e. with a .exe on Windows). +def go_test_conditional_pure(name, out, tags = None, **kwargs): + tags = tags or [] + tags.append("manual") + + go_test( + name = "_%s-cgo" % name, + pure = "off", + testonly = False, + tags = tags, + **kwargs + ) + + go_test( + name = "_%s-pure" % name, + pure = "on", + testonly = False, + tags = tags, + **kwargs + ) + + native.alias( + name = name, + actual = select({ + "@io_bazel_rules_go//go/platform:linux": ":_%s-cgo" % name, + "//conditions:default": ":_%s-pure" % name, + }), + ) + + [native.genrule( + name = "gen_%s" % o, + srcs = [name], + outs = [o], + cmd = "cp $< $@;", + output_to_bindir = True, + executable = True, + tags = tags, + ) for o in [out, out + ".exe"]] + + native.alias( + name = "%s_binary" % out, + actual = select({ + "@io_bazel_rules_go//go/platform:windows": ":gen_%s.exe" % out, + "//conditions:default": ":gen_%s" % out, + }), + ) diff --git a/cluster/images/conformance/BUILD b/cluster/images/conformance/BUILD index 5968c20b1ca..399127f2dfd 100644 --- a/cluster/images/conformance/BUILD +++ b/cluster/images/conformance/BUILD @@ -12,7 +12,7 @@ container_layer( directory = "/usr/local/bin", files = [ "//cmd/kubectl", - "//test/e2e:e2e.test", + "//test/e2e:e2e.test_binary", "//vendor/github.com/onsi/ginkgo/ginkgo", ], ) diff --git a/cmd/gendocs/BUILD b/cmd/gendocs/BUILD index 04897fb3126..6b2af89afb8 100644 --- a/cmd/gendocs/BUILD +++ b/cmd/gendocs/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "gendocs", diff --git a/cmd/genkubedocs/BUILD b/cmd/genkubedocs/BUILD index 6e461f7df36..deddd64c7fb 100644 --- a/cmd/genkubedocs/BUILD +++ b/cmd/genkubedocs/BUILD @@ -1,8 +1,11 @@ package(default_visibility = ["//visibility:public"]) +load( + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", +) load( "@io_bazel_rules_go//go:def.bzl", - "go_binary", "go_library", "go_test", ) diff --git a/cmd/genman/BUILD b/cmd/genman/BUILD index 31c33b4f0c3..09a7a7573d7 100644 --- a/cmd/genman/BUILD +++ b/cmd/genman/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "genman", diff --git a/cmd/genswaggertypedocs/BUILD b/cmd/genswaggertypedocs/BUILD index 5fabe41559d..846f92857cd 100644 --- a/cmd/genswaggertypedocs/BUILD +++ b/cmd/genswaggertypedocs/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "genswaggertypedocs", diff --git a/cmd/genyaml/BUILD b/cmd/genyaml/BUILD index 924e6144d2d..b659a379622 100644 --- a/cmd/genyaml/BUILD +++ b/cmd/genyaml/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "genyaml", diff --git a/cmd/hyperkube/BUILD b/cmd/hyperkube/BUILD index 22f9f3d4330..369cc94fc3f 100644 --- a/cmd/hyperkube/BUILD +++ b/cmd/hyperkube/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") load("//pkg/version:def.bzl", "version_x_defs") go_binary( diff --git a/cmd/kubelet/BUILD b/cmd/kubelet/BUILD index e2900d3cf32..b0afbcc7880 100644 --- a/cmd/kubelet/BUILD +++ b/cmd/kubelet/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") load("//pkg/version:def.bzl", "version_x_defs") go_binary( diff --git a/cmd/kubemark/BUILD b/cmd/kubemark/BUILD index 7dedb9587f0..56f362c1e28 100644 --- a/cmd/kubemark/BUILD +++ b/cmd/kubemark/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "kubemark", diff --git a/cmd/linkcheck/BUILD b/cmd/linkcheck/BUILD index 00d36ada089..7aa32491ce8 100644 --- a/cmd/linkcheck/BUILD +++ b/cmd/linkcheck/BUILD @@ -1,10 +1,10 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", + "//build:go.bzl", + go_binary = "go_binary_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_binary( name = "linkcheck", diff --git a/test/e2e/BUILD b/test/e2e/BUILD index b0d0e531ec8..7e464dde316 100644 --- a/test/e2e/BUILD +++ b/test/e2e/BUILD @@ -1,14 +1,15 @@ package(default_visibility = ["//visibility:public"]) load( - "@io_bazel_rules_go//go:def.bzl", - "go_library", - "go_test", + "//build:go.bzl", + go_test = "go_test_conditional_pure", ) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_test( name = "go_default_test", srcs = ["e2e_test.go"], + out = "e2e.test", embed = [":go_default_library"], tags = ["e2e"], deps = [ @@ -78,17 +79,6 @@ go_library( ], ) -# This is a handwritten rule. Do not delete, it will not be regenerated by -# update-bazel.sh. -genrule( - name = "gen_e2e.test", - testonly = 1, - srcs = [":go_default_test"], - outs = ["e2e.test"], - cmd = "srcs=($(SRCS)); cp $$(dirname $${srcs[0]})/go_default_test $@;", - output_to_bindir = 1, -) - filegroup( name = "package-srcs", srcs = glob(["**"]), diff --git a/test/e2e_kubeadm/BUILD b/test/e2e_kubeadm/BUILD index eba7037ba01..bf37bd72c2a 100644 --- a/test/e2e_kubeadm/BUILD +++ b/test/e2e_kubeadm/BUILD @@ -1,6 +1,10 @@ package(default_visibility = ["//visibility:public"]) -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load( + "//build:go.bzl", + go_test = "go_test_conditional_pure", +) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_test( name = "go_default_test", @@ -8,6 +12,7 @@ go_test( "e2e_kubeadm_suite_test.go", "kubeadm_test.go", ], + out = "e2e_kubeadm.test", embed = [":go_default_library"], tags = ["e2e"], deps = [ @@ -32,15 +37,6 @@ filegroup( visibility = ["//visibility:private"], ) -genrule( - name = "gen_e2e_kubeadm.test", - testonly = 1, - srcs = [":go_default_test"], - outs = ["e2e_kubeadm.test"], - cmd = "srcs=($(SRCS)); cp $$(dirname $${srcs[0]})/go_default_test $@;", - output_to_bindir = 1, -) - filegroup( name = "all-srcs", srcs = [ diff --git a/test/e2e_node/BUILD b/test/e2e_node/BUILD index 4c81963967f..a8cf1c6b3c3 100644 --- a/test/e2e_node/BUILD +++ b/test/e2e_node/BUILD @@ -1,6 +1,10 @@ package(default_visibility = ["//visibility:public"]) -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load( + "//build:go.bzl", + go_test = "go_test_conditional_pure", +) +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", @@ -106,6 +110,7 @@ go_test( "summary_test.go", "volume_manager_test.go", ], + out = "e2e_node.test", embed = [":go_default_library"], tags = ["e2e"], deps = [ @@ -180,15 +185,6 @@ go_test( }), ) -genrule( - name = "gen_e2e_node.test", - testonly = 1, - srcs = [":go_default_test"], - outs = ["e2e_node.test"], - cmd = "srcs=($(SRCS)); cp $$(dirname $${srcs[0]})/go_default_test $@;", - output_to_bindir = 1, -) - filegroup( name = "package-srcs", srcs = glob(["**"]),