output go_binary rule directly from go_binary_conditional_pure

And same for go_test_conditional_pure.

Instead of aliasing. Aliases are annoying in a number of ways. This is
specifically bugging me now because they make the action graph harder to
analyze programmatically. By using aliases here, we would need to handle
potentially aliased go_binary targets and dereference to the effective
target.

The comment references an issue with `pure = select(...)` which appears
to be resolved considering this now builds.
This commit is contained in:
Mike Danese 2020-09-09 12:05:07 -07:00
parent 5a72bb001f
commit d182ecc1f9
4 changed files with 23 additions and 77 deletions

View File

@ -155,7 +155,7 @@ filegroup(
srcs = select(for_platforms(
for_server = [
"//cmd/kubemark",
"//test/e2e_node:e2e_node.test_binary",
"//test/e2e_node:e2e_node.test",
],
for_test = [
"//cmd/gendocs",
@ -164,7 +164,7 @@ filegroup(
"//cmd/genswaggertypedocs",
"//cmd/genyaml",
"//cmd/linkcheck",
"//test/e2e:e2e.test_binary",
"//test/e2e:e2e.test",
"//vendor/github.com/onsi/ginkgo/ginkgo",
"//cluster/images/conformance/go-runner",
],

View File

@ -14,90 +14,36 @@
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")
# Defines a go_binary rule that enables cgo on platform builds targeting Linux,
# and otherwise builds a pure go binary.
def go_binary_conditional_pure(name, tags = [], **kwargs):
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,
pure = select({
"@io_bazel_rules_go//go/platform:linux": "off",
"//conditions:default": "on",
}),
tags = ["manual"] + tags,
**kwargs
)
# 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 []
# Defines a go_test rule that enables cgo on platform builds targeting Linux,
# and otherwise builds a pure go binary.
def go_test_conditional_pure(name, out, tags = [], **kwargs):
tags.append("manual")
go_test(
name = "_%s-cgo" % name,
pure = "off",
testonly = False,
tags = tags,
**kwargs
)
go_test(
name = "_%s-pure" % name,
pure = "on",
name = out,
pure = select({
"@io_bazel_rules_go//go/platform:linux": "off",
"//conditions:default": "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,
}),
name = "name",
actual = out,
)

View File

@ -15,7 +15,7 @@ container_layer(
files = [
"//cluster/images/conformance/go-runner",
"//cmd/kubectl",
"//test/e2e:e2e.test_binary",
"//test/e2e:e2e.test",
"//vendor/github.com/onsi/ginkgo/ginkgo",
],
)

View File

@ -45,11 +45,11 @@ genrule(
srcs = [
"//test/e2e:all-srcs",
"//test/e2e_node:all-srcs",
"//test/e2e:e2e.test_binary",
"//test/e2e:e2e.test",
"//vendor/github.com/onsi/ginkgo/ginkgo",
],
outs = ["specsummaries.json"],
cmd = "$(location //vendor/github.com/onsi/ginkgo/ginkgo) --dryRun=true --focus=[Conformance] $(location //test/e2e:e2e.test_binary) -- --spec-dump $$(pwd)/$@ > /dev/null",
cmd = "$(location //vendor/github.com/onsi/ginkgo/ginkgo) --dryRun=true --focus=[Conformance] $(location //test/e2e:e2e.test) -- --spec-dump $$(pwd)/$@ > /dev/null",
message = "Getting all conformance spec summaries.",
)