rules_android_ndk icon indicating copy to clipboard operation
rules_android_ndk copied to clipboard

Change `get_tool_for_action` to return absolute paths

Open widiba03304 opened this issue 11 months ago • 0 comments

Problem

When getting the path of executables in a toolchain by calling get_tool_for_action, it returns a string containing relative paths, thereby prohibiting defining rules like below:

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]),
        ),
    ]

gen_c_kernel = rule(
    implementation = _gen_c_kernel_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = [".c"]),
        "hdrs": attr.label_list(allow_files = [".h"]),
        "_cc_toolchain": attr.label(
            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
            providers = [cc_common.CcToolchainInfo],
        ),
    },
    fragments = ["cpp"],
    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
)

INFO: Analyzed 5 targets (3 packages loaded, 5140 targets configured).
ERROR: /workspaces/orchestra-project/orchestra/data/BUILD:26:23: Extracting .text section from bazel-out/k8-fastbuild/bin/orchestra/data/_objs/test2/test2.o failed: (Exit 127): process-wrapper failed: error executing Objcopy command 
  (cd /root/.cache/bazel/_bazel_root/cdd8e368207b5916b649be52f184b097/sandbox/processwrapper-sandbox/574/execroot/_main && \
  exec env - \
    TMPDIR=/tmp \
  /root/.cache/bazel/_bazel_root/install/78ccbcf59b0fe2de7bbdccc470af78df/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/root/.cache/bazel/_bazel_root/cdd8e368207b5916b649be52f184b097/sandbox/processwrapper-sandbox/574/stats.out' /bin/bash -c '
            '\''external/rules_android_ndk~~android_ndk_repository_extension~androidndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objcopy'\'' --only-section=.text -O binary '\''bazel-out/k8-fastbuild/bin/orchestra/data/_objs/test2/test2.o'\'' '\''bazel-out/k8-fastbuild/bin/orchestra/data/test2.bin'\''
        ')
/bin/bash: line 2: external/rules_android_ndk~~android_ndk_repository_extension~androidndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objcopy: No such file or directory
ERROR: /workspaces/orchestra-project/orchestra/data/BUILD:20:23: Extracting .text section from bazel-out/k8-fastbuild/bin/orchestra/data/_objs/test1/test1.o failed: (Exit 127): process-wrapper failed: error executing Objcopy command 
  (cd /root/.cache/bazel/_bazel_root/cdd8e368207b5916b649be52f184b097/sandbox/processwrapper-sandbox/573/execroot/_main && \
  exec env - \
    TMPDIR=/tmp \
  /root/.cache/bazel/_bazel_root/install/78ccbcf59b0fe2de7bbdccc470af78df/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/root/.cache/bazel/_bazel_root/cdd8e368207b5916b649be52f184b097/sandbox/processwrapper-sandbox/573/stats.out' /bin/bash -c '
            '\''external/rules_android_ndk~~android_ndk_repository_extension~androidndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objcopy'\'' --only-section=.text -O binary '\''bazel-out/k8-fastbuild/bin/orchestra/data/_objs/test1/test1.o'\'' '\''bazel-out/k8-fastbuild/bin/orchestra/data/test1.bin'\''
        ')
/bin/bash: line 2: external/rules_android_ndk~~android_ndk_repository_extension~androidndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objcopy: No such file or directory

The directory run_shell runs in the sandboxed directory /root/.cache/bazel/_bazel_root/cdd8e368207b5916b649be52f184b097/sandbox/processwrapper-sandbox/573/execroot/_main so bin/objcopy could not be found.

List of changes

  • Add ndk_path to be propagated to ndk_cc_toolchain_config.bzl
  • Add clang_directory to be propagated to ndk_cc_toolchain_config.bzl
  • Revise tools and cxx_builtin_include_directories to have absolute paths
  • Resolves #96

widiba03304 avatar Jan 25 '25 09:01 widiba03304