bazel icon indicating copy to clipboard operation
bazel copied to clipboard

cc_toolchain invokes ar unnecessarily and without arguments

Open calebzulawski opened this issue 3 years ago • 2 comments

Description of the bug:

I'm new to bazel (trialing it for my company) so please excuse if this isn't a legitimate bug.

To summarize, adding a custom cc_toolchain introduces two issues:

  • introduces building a cc_library as liblibrary.a, which doesn't happen with the default toolchain
  • fails to call ar correctly, omitting all arguments

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

I have a simple BUILD as follows:

cc_binary(
    name = "library",
    linkshared = True,
    deps = [
        ":library_impl",
    ],
)

cc_library(
    name = "library_impl",
    srcs = ["library.cpp"],
    hdrs = ["library.h"],
)

This results in two subcommands, one for compiling and one for linking the shared object.

Next, I import @rules_company via http_archive, which has this BUILD:

package(default_visibility = ["//visibility:public"])

load("//:toolchain.bzl", "cc_toolchain_config")
load("@rules_cc//cc:defs.bzl", "cc_toolchain")

filegroup(name = "empty")

cc_toolchain_config(
    name = "x86_64-unknown-linux-gnu-toolchain-config",
    target = "x86_64-unknown-linux-gnu",
)
cc_toolchain(
    name = "x86_64_unknown_linux_gnu_cc_toolchain",
    toolchain_identifier = "x86_64-unknown-linux-gnu-toolchain",
    toolchain_config = ":x86_64-unknown-linux-gnu-toolchain-config",
    all_files = "@build-env//:contents",
    compiler_files = "@build-env//:contents",
    ar_files = "@build-env//:conda/bin/ar",
    dwp_files = ":empty",
    linker_files = "@build-env//:contents",
    objcopy_files = "@build-env//:conda/bin/objcopy",
    strip_files = "@build-env//:conda/bin/strip",
    supports_header_parsing = True,
    supports_param_files = True,
)

toolchain(
    name = "x86_64_unknown_linux_gnu_toolchain",
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    toolchain = ":x86_64_unknown_linux_gnu_cc_toolchain",
    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

toolchain.bzl contains:

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", "flag_set")

def _cc_toolchain_config(ctx):
    def action(name, tool, flags = None):
        return action_config(
            action_name = name,
            tools = [
                struct(
                    type_name = "tool",
                    tool = tool,
                    flag_sets = flags,
                ),
            ],
        )

    actions = [
        action(ACTION_NAMES.cpp_compile, ctx.file._cpp),
        action(ACTION_NAMES.cpp_link_executable, ctx.file._cpp),
        action(ACTION_NAMES.cpp_link_static_library, ctx.file._ar),
    ]

    toolchain_include_directories_feature = feature(
        name = "toolchain_include_directories",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = [
                    ACTION_NAMES.assemble,
                    ACTION_NAMES.preprocess_assemble,
                    ACTION_NAMES.linkstamp_compile,
                    ACTION_NAMES.c_compile,
                    ACTION_NAMES.cpp_compile,
                    ACTION_NAMES.cpp_header_parsing,
                    ACTION_NAMES.cpp_module_compile,
                    ACTION_NAMES.cpp_module_codegen,
                    ACTION_NAMES.lto_backend,
                    ACTION_NAMES.clif_match,
                ],
                flag_groups = [
                    flag_group(
                        flags = [
                            "-isystem",
                            "external/build-env/conda/libexec/toolchain/gcc/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/include/c++/12.2.0/x86_64-unknown-linux-gnu",
                            "-isystem",
                            "external/build-env/conda/libexec/toolchain/llvm/lib/clang/15.0.7/include",
                            "-isystem",
                            "external/build-env/conda/libexec/toolchain/gcc/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/sysroot/usr/include",
                            "-isystem",
                            "external/build-env/conda/libexec/toolchain/gcc/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/include/c++/12.2.0",
                            "-isystem",
                            "external/build-env/conda/libexec/toolchain/gcc/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/include/c++/12.2.0/bits",
                        ],
                    ),
                ],
            ),
        ],
    )

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        toolchain_identifier = ctx.attr.target + "-toolchain",
        host_system_name = "local",
        target_system_name = "x86_64-unknown-linux-gnu",
        target_cpu = "unknown",
        target_libc = "unknown",
        compiler = "clang",
        abi_version = "unknown",
        abi_libc_version = "unknown",
        action_configs = actions,
        features = [toolchain_include_directories_feature],
    )

cc_toolchain_config = rule(
    implementation = _cc_toolchain_config,
    attrs = {
        "target": attr.string(mandatory = True),
        "_cpp": attr.label(
            default = "@build-env//:conda/bin/c++",
            allow_single_file = True,
        ),
        "_ar": attr.label(
            default = "@build-env//:conda/bin/ar",
            allow_single_file = True,
        ),
    },
    provides = [CcToolchainConfigInfo],
)

I update my WORKSPACE to contain the repository rule that creates @build-env, as well as add native.register_toolchains(":x86_64_unknown_linux_gnu_toolchain"). This introduces the failing ar invocation:

SUBCOMMAND: # //library:library_impl [action 'Linking library/liblibrary_impl.a', configuration: ef9a557bd1eb5399777db96cd9aa4c37f61235478a9e027fdfb32cb2a3b13c24, execution platform: @local_config_platform//:host]
(cd /home/caleb/.cache/bazel/_bazel_caleb/df4ed872939f267aea83c95366fcb7f1/execroot/__main__ && \
  exec env - \
    PATH=/home/caleb/.cache/bazelisk/downloads/bazelbuild/bazel-6.2.1-linux-x86_64/bin:/home/caleb/.local/bin:/home/caleb/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/caleb/.fzf/bin \
    PWD=/proc/self/cwd \
  external/build-env/conda/bin/ar)
# Configuration: ef9a557bd1eb5399777db96cd9aa4c37f61235478a9e027fdfb32cb2a3b13c24
# Execution platform: @local_config_platform//:host
ERROR: /home/caleb/rules_company/example/library/BUILD:9:11: Linking library/liblibrary_impl.a failed: (Exit 1): ar failed: error executing command (from target //library:library_impl) external/build-env/conda/bin/ar

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
external/build-env/conda/bin/ar: error: an archive name must be specified
Target //library:library failed to build

Which operating system are you running Bazel on?

Linux (Ubuntu)

What is the output of bazel info release?

release 6.2.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

I've found information on how to set up toolchains, which I've followed, but nothing on this specific issue

Any other information, logs, or outputs that you want to share?

No response

calebzulawski avatar Jun 23 '23 23:06 calebzulawski

I was having a similar issue, and implying the "archiver_flags" on the link action did the trick for me.

See the following snippet of code: https://github.com/bazelbuild/bazel/blob/07e0d316a345a3cb2593f98525320590bbc56e30/tools/cpp/windows_cc_toolchain_config.bzl#L151

eribas1 avatar Sep 12 '23 03:09 eribas1

Hello @oquenchil, I'm hitting the same issue. Could you perhaps elaborate on the reason why this is not a bug? What in the usage made by @calebzulawski is incorrect? Is the workaround from @eribas1 the official way of using cc_toolchain?

Something that is even stranger, is that, since I'm using rules_cc to define my toolchain, if I do not specify an archiver tool, one is automatically added. When this dummy tool (which doesn't exist) is invoked, the arguments passed to it are correct. Which means that somehow the arguments depend on the path to the archiver. That cannot be good...

jdebache avatar Aug 31 '24 13:08 jdebache