blob: 685c616975818c8745de1381e54f31ef0b3926c8 [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,
Jan Schär230a31a2025-05-05 12:28:50 +00009 "//build/platforms/linkmode:static": True,
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",
Jan Schär230a31a2025-05-05 12:28:50 +000017 "//build/platforms/linkmode:static",
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.
Tim Windelschmidt08054ca2025-04-04 01:11:56 +020063 "@io_bazel_rules_go//go/config:static": False,
Jan Schär230a31a2025-05-05 12:28:50 +000064 "//build/platforms/linkmode:static": False,
Tim Windelschmidt08054ca2025-04-04 01:11:56 +020065}
66
67def _ignore_unused_configuration_impl(_settings, _attr):
68 return _new_settings
69
70# Transition to flip all known-unimportant but varying configuration options to
71# a known, stable value.
72# This is to prevent Bazel from creating extra configurations for possible
73# combinations of options in case the linux_image rule is pulled through build
74# graph fragments that have different options set.
75#
76# Ideally, Bazel would let us mark in a list that we only care about some set
77# of options (or at least let us mark those that we explicitly don't care
78# about, instead of manually setting them to some value). However, this doesn't
79# seem to be possible, thus this transition is a bit of a hack.
80ignore_unused_configuration = transition(
81 implementation = _ignore_unused_configuration_impl,
82 inputs = [],
83 outputs = list(_new_settings.keys()),
84)
Jan Schär778cc332025-04-29 16:31:40 +000085
86ignore_unused_configuration_target = rule(
87 cfg = ignore_unused_configuration,
Jan Schär219c2c62025-04-30 08:14:25 +000088 implementation = forward_impl,
Jan Schär778cc332025-04-29 16:31:40 +000089 attrs = {
90 "dep": attr.label(mandatory = True),
91 },
92 doc = """Applies ignore_unused_configuration transition to a target.""",
93)