treewide: replace platform_transition_binary
platform_transition_binary changes the platform, but does not set
@io_bazel_rules_go//go/config:static. Instead, build_static_transition
is now used, which sets both.
A second benefit of this change is that we have fewer places where the
amd64 architecture is mentioned, which should make it easier to enable
multi-platform builds.
Change-Id: Id01e0d942a12770b8b34b0e6825f314128149b40
Reviewed-on: https://review.monogon.dev/c/monogon/+/4162
Tested-by: Jenkins CI
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
diff --git a/osbase/build/def.bzl b/osbase/build/def.bzl
index 7a6fbd7..f1665b5 100644
--- a/osbase/build/def.bzl
+++ b/osbase/build/def.bzl
@@ -1,3 +1,5 @@
+load("@bazel_skylib//lib:paths.bzl", "paths")
+
def _build_static_transition_impl(_settings, _attr):
"""
Transition that enables static build of Go and C binaries.
@@ -16,6 +18,41 @@
],
)
+def _forward_impl(ctx):
+ # We can't pass DefaultInfo through as-is, since Bazel forbids executable
+ # if it's a file declared in a different target. To emulate that, symlink
+ # to the original executable, if there is one.
+ default_info = ctx.attr.dep[DefaultInfo]
+ new_executable = None
+ original_executable = default_info.files_to_run.executable
+ runfiles = default_info.default_runfiles
+ if original_executable:
+ # In order for the symlink to have the same basename as the original
+ # executable (important in the case of proto plugins), put it in a
+ # subdirectory named after the label to prevent collisions.
+ new_executable = ctx.actions.declare_file(paths.join(ctx.label.name, original_executable.basename))
+ ctx.actions.symlink(
+ output = new_executable,
+ target_file = original_executable,
+ is_executable = True,
+ )
+ runfiles = runfiles.merge(ctx.runfiles([new_executable]))
+
+ return [DefaultInfo(
+ files = default_info.files,
+ runfiles = runfiles,
+ executable = new_executable,
+ )]
+
+build_static_target = rule(
+ cfg = build_static_transition,
+ implementation = _forward_impl,
+ attrs = {
+ "dep": attr.label(mandatory = True),
+ },
+ doc = """Applies build_static_transition to a target.""",
+)
+
_new_settings = {
# This list should be expanded with any configuration options that end
# up reaching this rule with different values across different build