bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.14k stars 4.05k forks source link

[bazel.build] Problem with Bazel Tutorial: Configure C++ Toolchains #18739

Open zedzhu opened 1 year ago

zedzhu commented 1 year ago

Page link:

https://bazel.build/tutorials/ccp-toolchain-config?hl=en

example link: https://github.com/bazelbuild/examples/tree/main/cpp-tutorial/stage3

Problem description (include actual vs expected text, if applicable):

I followed the instructions of the documentation on my macOS Monterey(12.3) with Apple M1 Pro chipset and failed to build:

$ bazel build --config=clang_config --verbose_failures //main:hello-world INFO: Analyzed target //main:hello-world (1 packages loaded, 10 targets configured). INFO: Found 1 target... ERROR: /Users/carlzhu/github/fun_algs/bazel/cpp-tutorial/stage5/main/BUILD:3:11: Compiling main/hello-greet.cc failed: undeclared inclusion(s) in rule '//main:hello-greet': this rule is missing dependency declarations for the following files included by 'main/hello-greet.cc': '/opt/homebrew/opt/llvm/include/c++/v1/string' '/opt/homebrew/opt/llvm/include/c++/v1/algorithm/max.h' '/opt/homebrew/opt/llvm/include/c++/v1/algorithm/comp.h' '/opt/homebrew/opt/llvm/include/c++/v1/config' '/opt/homebrew/opt/llvm/include/c++/v1/config_site' '/opt/homebrew/opt/llvm/include/c++/v1/algorithm/comp_ref_type.h' '/opt/homebrew/opt/llvm/include/c++/v1/debug' '/opt/homebrew/opt/llvm/include/c++/v1/assert' '/opt/homebrew/opt/llvm/include/c++/v1/verbose_abort' '/opt/homebrew/opt/llvm/include/c++/v1/availability' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/is_constant_evaluated.h' '/opt/homebrew/opt/llvm/include/c++/v1/cstddef' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/enable_if.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/integral_constant.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/is_integral.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/remove_cv.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/remove_const.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/remove_volatile.h' '/opt/homebrew/opt/llvm/include/c++/v1/version' '/opt/homebrew/opt/llvm/include/c++/v1/stddef.h' '/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include/stddef.h' '/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include/stddef_max_align_t.h' '/opt/homebrew/opt/llvm/include/c++/v1/utility/declval.h' '/opt/homebrew/opt/llvm/include/c++/v1/algorithm/max_element.h' '/opt/homebrew/opt/llvm/include/c++/v1/iterator/iterator_traits.h' '/opt/homebrew/opt/llvm/include/c++/v1/concepts/arithmetic.h' '/opt/homebrew/opt/llvm/include/c++/v1/type_traits/is_floating_point.h'

Where do you see this issue? (include link to specific section of the page, if applicable)

No response

Any other information you'd like to share?

.bazelrc file content:

# Use our custom-configured c++ toolchain.

build:clang_config --crosstool_top=//toolchain:clang_suite

# Use --cpu as a differentiator.

build:clang_config --cpu=arm64

# Use the default Bazel C++ toolchain to build the tools used during the
# build.

build:clang_config --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

toolchain/BUILD file content:

load(":cc_toolchain_config.bzl", "cc_toolchain_config")

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

cc_toolchain_suite(
    name = "clang_suite",
    toolchains = {
        "arm64": ":arm64_toolchain",
    },
)

filegroup(name = "empty")

cc_toolchain(
    name = "arm64_toolchain",
    toolchain_identifier = "arm64-toolchain",
    toolchain_config = ":arm64_toolchain_config",
    all_files = ":empty",
    compiler_files = ":empty",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 0,
)

cc_toolchain_config(name = "arm64_toolchain_config")

toolchain/cc_toolchain_config file content:

# NEW
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")

# NEW
load(
    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
    "feature",
    "flag_group",
    "flag_set",
    "tool_path",
)

all_link_actions = [ # NEW
    ACTION_NAMES.cpp_link_executable,
    ACTION_NAMES.cpp_link_dynamic_library,
    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]

def _impl(ctx):
    tool_paths = [
        tool_path(
            name = "gcc",
            # path = "/usr/bin/clang",
            path = "/opt/homebrew/opt/llvm/bin/clang",
        ),
        tool_path(
            name = "ld",
            path = "/usr/bin/ld",
        ),
        tool_path(
            name = "ar",
            path = "/bin/false",
        ),
        tool_path(
            name = "cpp",
            path = "/bin/false",
        ),
        tool_path(
            name = "gcov",
            path = "/bin/false",
        ),
        tool_path(
            name = "nm",
            path = "/bin/false",
        ),
        tool_path(
            name = "objdump",
            path = "/bin/false",
        ),
        tool_path(
            name = "strip",
            path = "/bin/false",
        ),
    ]

    features = [ # NEW
        feature(
            name = "default_linker_flags",
            enabled = True,
            flag_sets = [
                flag_set(
                    actions = all_link_actions,
                    flag_groups = ([
                        flag_group(
                            flags = [
                                "-lstdc++",
                            ],
                        ),
                    ]),
                ),
            ],
        ),
    ]

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        features = features, # NEW
        cxx_builtin_include_directories = [
            # "/usr/lib/llvm-9/lib/clang/9.0.1/include",
            # "/usr/include",
            # "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/", 
            # "/opt/homebrew/opt/llvm/include/c++/v1",
            # "/opt/homebrew/opt/llvm/include",
            # "/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include",
        ],
        toolchain_identifier = "local",
        host_system_name = "local",
        target_system_name = "local",
        target_cpu = "arm64",
        target_libc = "unknown",
        compiler = "clang",
        abi_version = "unknown",
        abi_libc_version = "unknown",
        tool_paths = tool_paths,
    )

cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
)
sgowroji commented 1 year ago

Hi @zedzhu, Can you please update the issue with complete information along with a clear title. Thanks !

zedzhu commented 1 year ago

OK, updated, could you see if it's ok now?

hypdeb commented 1 year ago

If I'm not mistaken, macOS has the libc++ implementation of the standard library by default. So I would try two things:

Disclosure: I'm a beginner so this might be not the right way of solving this issue, I'm here because I have the same issue in a different context :)

github-actions[bot] commented 3 days ago

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.