bazelbuild/bazel

cc_toolchain invokes ar unnecessarily and without arguments

Open

#18.762 aberto em 23 de jun. de 2023

Ver no GitHub
 (2 comments) (0 reactions) (0 assignees)Java (4.465 forks)batch import
P3help wantedteam-Rules-CPPtype: support / not a bug (process)

Métricas do repositório

Stars
 (25.384 stars)
Métricas de merge de PR
 (Mesclagem média 22d 20h) (77 fundiu PRs em 30d)

Description

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

Guia do colaborador