Skip to content

Proposal to change tools to have absolute paths #96

@widiba03304

Description

@widiba03304

Hi, I am building low-level software with this repository, and I found some problems here.

Some binaries do not return absolute paths

This can be problematic when some bazel rules use cc_common.get_tool_for_action and execute them.
This is a use case that compiles a C code into an object and objcopy the content.

def _gen_c_bin_impl(ctx):
    """
    A rule that compiles C sources into an object, then extracts the .text section into a .bin file using objcopy.
    """
    toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]

    feature_configuration = cc_common.configure_features(
        ctx = ctx,
        cc_toolchain = toolchain,
        requested_features = ctx.features,
        unsupported_features = ctx.disabled_features,
    )

    _, compilation_outputs = cc_common.compile(
        actions = ctx.actions,
        feature_configuration = feature_configuration,
        cc_toolchain = toolchain,
        name = ctx.label.name,
        srcs = ctx.files.srcs,
        public_hdrs = ctx.files.hdrs,
    )

    object_files = compilation_outputs.objects + compilation_outputs.pic_objects
    if len(object_files) == 0:
        fail("No object files produced from srcs = %s" % (ctx.files.srcs))
    object_file = object_files[0]

    bin_file = ctx.actions.declare_file(ctx.label.name + ".bin")

    objcopy_tool = cc_common.get_tool_for_action(
        feature_configuration = feature_configuration,
        action_name = "objcopy_embed_data",
    )
    if objcopy_tool == None:
        fail("The C/C++ toolchain does not provide an objcopy tool.")

    ctx.actions.run_shell(
        inputs = [object_file],
        outputs = [bin_file],
        command = """
            '{objcopy}' --only-section=.text -O binary '{in_o}' '{out_bin}'
        """.format(
            objcopy = objcopy_tool,
            in_o = object_file.path,
            out_bin = bin_file.path,
        ),
        mnemonic = "Objcopy",
        progress_message = "Extracting .text section from %s" % (object_file.path),
    )

    return [
        DefaultInfo(
            files = depset([bin_file]),
        ),
    ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions