|
| 1 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 2 | + |
| 3 | +def forward_binary_from_transition(ctx): |
| 4 | + binary = ctx.attr.dep[0] |
| 5 | + default_info = binary[DefaultInfo] |
| 6 | + original_executable = default_info.files_to_run.executable |
| 7 | + if not original_executable: |
| 8 | + fail("Cannot transition a target that is not executable") |
| 9 | + |
| 10 | + (_, extension) = paths.split_extension(original_executable.basename) |
| 11 | + new_executable = ctx.actions.declare_file(ctx.label.name + extension) |
| 12 | + inputs = [original_executable] |
| 13 | + command = "cp %s %s" % (original_executable.path, new_executable.path) |
| 14 | + |
| 15 | + providers = [] |
| 16 | + if OutputGroupInfo in binary: |
| 17 | + pdb_file = getattr(binary[OutputGroupInfo], "pdb_file", None) |
| 18 | + if pdb_file: |
| 19 | + (pdb_file,) = pdb_file.to_list() |
| 20 | + linked_pdb_file = ctx.actions.declare_file(ctx.label.name + ".pdb") |
| 21 | + ctx.actions.symlink(target_file = pdb_file, output = linked_pdb_file) |
| 22 | + inputs.append(linked_pdb_file) |
| 23 | + providers.append(binary[OutputGroupInfo]) |
| 24 | + |
| 25 | + ctx.actions.run_shell( |
| 26 | + inputs = inputs, |
| 27 | + outputs = [new_executable], |
| 28 | + command = command, |
| 29 | + ) |
| 30 | + files = depset(direct = [new_executable]) |
| 31 | + runfiles = default_info.default_runfiles.merge(ctx.runfiles([new_executable])) |
| 32 | + providers.append( |
| 33 | + DefaultInfo( |
| 34 | + files = files, |
| 35 | + runfiles = runfiles, |
| 36 | + executable = new_executable, |
| 37 | + ), |
| 38 | + ) |
| 39 | + return providers |
| 40 | + |
| 41 | +def get_transition_attrs(transition_rule): |
| 42 | + return { |
| 43 | + "_allowlist_function_transition": attr.label( |
| 44 | + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 45 | + ), |
| 46 | + "dep": attr.label(mandatory = True, cfg = transition_rule), |
| 47 | + } |
0 commit comments