bazel icon indicating copy to clipboard operation
bazel copied to clipboard

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

Open zedzhu opened this issue 3 years ago • 5 comments

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

zedzhu avatar Jun 22 '23 10:06 zedzhu

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

sgowroji avatar Jun 22 '23 10:06 sgowroji

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

zedzhu avatar Jun 22 '23 10:06 zedzhu

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

  • Change your linker flag from -lstdc++ to lc++
  • Add the path /opt/homebrew/opt/llvm/include/c++/v1 that Bazel suggests is missing to the cxx_builtin_include_directories list.

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 :)

jdebache avatar Aug 04 '23 18:08 jdebache

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.

github-actions[bot] avatar Oct 20 '24 01:10 github-actions[bot]

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please post @bazelbuild/triage in a comment here and we'll take a look. Thanks!

github-actions[bot] avatar Jan 19 '25 01:01 github-actions[bot]