| Jan Schär | 1b6cd6f | 2025-04-29 15:30:22 +0000 | [diff] [blame] | 1 | load("@bazel_skylib//lib:paths.bzl", "paths") |
| 2 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 3 | def _build_static_transition_impl(_settings, _attr): |
| Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 4 | """ |
| Jan Schär | 0fd36f4 | 2025-04-29 10:26:03 +0000 | [diff] [blame] | 5 | Transition that enables static build of Go and C binaries. |
| Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 6 | """ |
| 7 | return { |
| 8 | "@io_bazel_rules_go//go/config:static": True, |
| Lorenz Brun | 043c0b4 | 2025-03-13 21:09:36 +0100 | [diff] [blame] | 9 | "@toolchain_cc_mngn//buildmode:static": True, |
| Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | build_static_transition = transition( |
| 13 | implementation = _build_static_transition_impl, |
| 14 | inputs = [], |
| 15 | outputs = [ |
| 16 | "@io_bazel_rules_go//go/config:static", |
| Lorenz Brun | 043c0b4 | 2025-03-13 21:09:36 +0100 | [diff] [blame] | 17 | "@toolchain_cc_mngn//buildmode:static", |
| Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 18 | ], |
| 19 | ) |
| Tim Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame] | 20 | |
| Jan Schär | 219c2c6 | 2025-04-30 08:14:25 +0000 | [diff] [blame] | 21 | def forward_impl(ctx): |
| Jan Schär | 1b6cd6f | 2025-04-29 15:30:22 +0000 | [diff] [blame] | 22 | # 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 | |
| 47 | build_static_target = rule( |
| 48 | cfg = build_static_transition, |
| Jan Schär | 219c2c6 | 2025-04-30 08:14:25 +0000 | [diff] [blame] | 49 | implementation = forward_impl, |
| Jan Schär | 1b6cd6f | 2025-04-29 15:30:22 +0000 | [diff] [blame] | 50 | attrs = { |
| 51 | "dep": attr.label(mandatory = True), |
| 52 | }, |
| 53 | doc = """Applies build_static_transition to a target.""", |
| 54 | ) |
| 55 | |
| Tim Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame] | 56 | _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 Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame] | 63 | "@io_bazel_rules_go//go/config:static": False, |
| Lorenz Brun | 043c0b4 | 2025-03-13 21:09:36 +0100 | [diff] [blame] | 64 | "@toolchain_cc_mngn//buildmode:static": False, |
| Tim Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | def _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. |
| 80 | ignore_unused_configuration = transition( |
| 81 | implementation = _ignore_unused_configuration_impl, |
| 82 | inputs = [], |
| 83 | outputs = list(_new_settings.keys()), |
| 84 | ) |
| Jan Schär | 778cc33 | 2025-04-29 16:31:40 +0000 | [diff] [blame] | 85 | |
| 86 | ignore_unused_configuration_target = rule( |
| 87 | cfg = ignore_unused_configuration, |
| Jan Schär | 219c2c6 | 2025-04-30 08:14:25 +0000 | [diff] [blame] | 88 | implementation = forward_impl, |
| Jan Schär | 778cc33 | 2025-04-29 16:31:40 +0000 | [diff] [blame] | 89 | attrs = { |
| 90 | "dep": attr.label(mandatory = True), |
| 91 | }, |
| 92 | doc = """Applies ignore_unused_configuration transition to a target.""", |
| 93 | ) |