blob: b047d8b299fd6fd004e8a329ef2f5704d746be55 [file] [log] [blame]
Jan Schär1b6cd6f2025-04-29 15:30:22 +00001load("@bazel_skylib//lib:paths.bzl", "paths")
2
Tim Windelschmidt156248b2025-01-10 00:27:45 +01003def _build_static_transition_impl(_settings, _attr):
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +02004 """
Jan Schär0fd36f42025-04-29 10:26:03 +00005 Transition that enables static build of Go and C binaries.
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +02006 """
7 return {
8 "@io_bazel_rules_go//go/config:static": True,
Leopoldbc93c2b2023-01-14 13:12:23 +01009 "//command_line_option:platforms": "//build/platforms:linux_amd64_static",
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020010 }
11
12build_static_transition = transition(
13 implementation = _build_static_transition_impl,
14 inputs = [],
15 outputs = [
16 "@io_bazel_rules_go//go/config:static",
Leopoldbc93c2b2023-01-14 13:12:23 +010017 "//command_line_option:platforms",
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020018 ],
19)
Tim Windelschmidt08054ca2025-04-04 01:11:56 +020020
Jan Schär219c2c62025-04-30 08:14:25 +000021def forward_impl(ctx):
Jan Schär1b6cd6f2025-04-29 15:30:22 +000022 # We can't pass DefaultInfo through as-is, since Bazel forbids executable
23 # if it's a file declared in a different target. To emulate that, symlink
24 # to the original executable, if there is one.
25 default_info = ctx.attr.dep[DefaultInfo]
26 new_executable = None
27 original_executable = default_info.files_to_run.executable
28 runfiles = default_info.default_runfiles
29 if original_executable:
30 # In order for the symlink to have the same basename as the original
31 # executable (important in the case of proto plugins), put it in a
32 # subdirectory named after the label to prevent collisions.
33 new_executable = ctx.actions.declare_file(paths.join(ctx.label.name, original_executable.basename))
34 ctx.actions.symlink(
35 output = new_executable,
36 target_file = original_executable,
37 is_executable = True,
38 )
39 runfiles = runfiles.merge(ctx.runfiles([new_executable]))
40
41 return [DefaultInfo(
42 files = default_info.files,
43 runfiles = runfiles,
44 executable = new_executable,
45 )]
46
47build_static_target = rule(
48 cfg = build_static_transition,
Jan Schär219c2c62025-04-30 08:14:25 +000049 implementation = forward_impl,
Jan Schär1b6cd6f2025-04-29 15:30:22 +000050 attrs = {
51 "dep": attr.label(mandatory = True),
52 },
53 doc = """Applies build_static_transition to a target.""",
54)
55
Tim Windelschmidt08054ca2025-04-04 01:11:56 +020056_new_settings = {
57 # This list should be expanded with any configuration options that end
58 # up reaching this rule with different values across different build
59 # graph paths, but that do not actually influence the kernel build.
60 # Force-setting them to a stable value forces the build configuration
61 # to a stable hash.
62 # See the transition's comment block for more information.
Jan Schär8ffa54e2025-04-29 11:33:39 +000063 "@io_bazel_rules_go//go/config:race": False,
Tim Windelschmidt08054ca2025-04-04 01:11:56 +020064 "@io_bazel_rules_go//go/config:pure": False,
65 "@io_bazel_rules_go//go/config:static": False,
66
67 # Note: this toolchain is not actually used to perform the build.
68 "//command_line_option:platforms": "//build/platforms:linux_amd64_static",
69}
70
71def _ignore_unused_configuration_impl(_settings, _attr):
72 return _new_settings
73
74# Transition to flip all known-unimportant but varying configuration options to
75# a known, stable value.
76# This is to prevent Bazel from creating extra configurations for possible
77# combinations of options in case the linux_image rule is pulled through build
78# graph fragments that have different options set.
79#
80# Ideally, Bazel would let us mark in a list that we only care about some set
81# of options (or at least let us mark those that we explicitly don't care
82# about, instead of manually setting them to some value). However, this doesn't
83# seem to be possible, thus this transition is a bit of a hack.
84ignore_unused_configuration = transition(
85 implementation = _ignore_unused_configuration_impl,
86 inputs = [],
87 outputs = list(_new_settings.keys()),
88)
Jan Schär778cc332025-04-29 16:31:40 +000089
90ignore_unused_configuration_target = rule(
91 cfg = ignore_unused_configuration,
Jan Schär219c2c62025-04-30 08:14:25 +000092 implementation = forward_impl,
Jan Schär778cc332025-04-29 16:31:40 +000093 attrs = {
94 "dep": attr.label(mandatory = True),
95 },
96 doc = """Applies ignore_unused_configuration transition to a target.""",
97)