"""Internal utility for transitioning a target to -c dbg.""" load("@bazel_skylib//lib:paths.bzl", "paths") def _transition_dbg_impl(_1, _2): return {"//command_line_option:compilation_mode": "dbg"} _transition_dbg = transition( implementation = _transition_dbg_impl, inputs = [], outputs = ["//command_line_option:compilation_mode"], ) def _dbg_rust_binary_impl(ctx): # We need to forward the DefaultInfo provider from the underlying rule. # Unfortunately, we can't do this directly, because Bazel requires that the executable to run # is actually generated by this rule, so we need to symlink to it, and generate a synthetic # forwarding DefaultInfo. result = [] binary = ctx.attr.binary[0] default_info = binary[DefaultInfo] files = default_info.files new_executable = None original_executable = default_info.files_to_run.executable runfiles = default_info.default_runfiles if not original_executable: fail("Cannot transition a 'binary' that is not executable") new_executable_name = original_executable.basename # In order for the symlink to have the same basename as the original # executable (important in the case of proto plugins), put it in a # subdirectory named after the label to prevent collisions. new_executable = ctx.actions.declare_file(paths.join(ctx.label.name, new_executable_name)) ctx.actions.symlink( output = new_executable, target_file = original_executable, is_executable = True, ) files = depset(direct = [new_executable]) runfiles = ctx.runfiles([new_executable]) result.append( DefaultInfo( files = files, runfiles = runfiles, executable = new_executable, ), ) return result dbg_rust_binary = rule( doc = "Transitions the binary to use the provided platform.", implementation = _dbg_rust_binary_impl, attrs = { "binary": attr.label( doc = "The target to transition.", allow_files = True, cfg = _transition_dbg, mandatory = True, ), "_allowlist_function_transition": attr.label( default = "@bazel_tools//tools/allowlists/function_transition_allowlist", ), }, executable = True, )