|
| 1 | +"""Internal utility for transitioning a target to -c dbg.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 4 | + |
| 5 | +def _transition_dbg_impl(_1, _2): |
| 6 | + return {"//command_line_option:compilation_mode": "dbg"} |
| 7 | + |
| 8 | +_transition_dbg = transition( |
| 9 | + implementation = _transition_dbg_impl, |
| 10 | + inputs = [], |
| 11 | + outputs = ["//command_line_option:compilation_mode"], |
| 12 | +) |
| 13 | + |
| 14 | +def _dbg_rust_binary_impl(ctx): |
| 15 | + # We need to forward the DefaultInfo provider from the underlying rule. |
| 16 | + # Unfortunately, we can't do this directly, because Bazel requires that the executable to run |
| 17 | + # is actually generated by this rule, so we need to symlink to it, and generate a synthetic |
| 18 | + # forwarding DefaultInfo. |
| 19 | + |
| 20 | + result = [] |
| 21 | + binary = ctx.attr.binary[0] |
| 22 | + |
| 23 | + default_info = binary[DefaultInfo] |
| 24 | + files = default_info.files |
| 25 | + new_executable = None |
| 26 | + original_executable = default_info.files_to_run.executable |
| 27 | + runfiles = default_info.default_runfiles |
| 28 | + |
| 29 | + if not original_executable: |
| 30 | + fail("Cannot transition a 'binary' that is not executable") |
| 31 | + |
| 32 | + new_executable_name = original_executable.basename |
| 33 | + |
| 34 | + # In order for the symlink to have the same basename as the original |
| 35 | + # executable (important in the case of proto plugins), put it in a |
| 36 | + # subdirectory named after the label to prevent collisions. |
| 37 | + new_executable = ctx.actions.declare_file(paths.join(ctx.label.name, new_executable_name)) |
| 38 | + |
| 39 | + ctx.actions.symlink( |
| 40 | + output = new_executable, |
| 41 | + target_file = original_executable, |
| 42 | + is_executable = True, |
| 43 | + ) |
| 44 | + |
| 45 | + files = depset(direct = [new_executable]) |
| 46 | + runfiles = ctx.runfiles([new_executable]) |
| 47 | + |
| 48 | + result.append( |
| 49 | + DefaultInfo( |
| 50 | + files = files, |
| 51 | + runfiles = runfiles, |
| 52 | + executable = new_executable, |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + return result |
| 57 | + |
| 58 | +dbg_rust_binary = rule( |
| 59 | + doc = "Transitions the binary to use the provided platform.", |
| 60 | + implementation = _dbg_rust_binary_impl, |
| 61 | + attrs = { |
| 62 | + "binary": attr.label( |
| 63 | + doc = "The target to transition.", |
| 64 | + allow_files = True, |
| 65 | + cfg = _transition_dbg, |
| 66 | + mandatory = True, |
| 67 | + ), |
| 68 | + "_allowlist_function_transition": attr.label( |
| 69 | + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 70 | + ), |
| 71 | + }, |
| 72 | + executable = True, |
| 73 | +) |
0 commit comments