Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zedzhu opened this issue Jun 22, 2023 · 4 comments
Open

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

zedzhu opened this issue Jun 22, 2023 · 4 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) stale Issues or PRs that are stale (no activity for 30 days) team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-CPP Issues for C++ rules type: support / not a bug (process)

Comments

@zedzhu
Copy link

zedzhu commented Jun 22, 2023

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 zedzhu added team-Documentation Documentation improvements that cannot be directly linked to other team labels type: documentation (cleanup) untriaged labels Jun 22, 2023
@zedzhu zedzhu changed the title [bazel.build] Problem with /docs/cc-toolchain-config-reference [bazel.build] Problem with Bazel Tutorial: Configure C++ Toolchains Jun 22, 2023
@zedzhu
Copy link
Author

zedzhu commented Jun 22, 2023

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

@meteorcloudy meteorcloudy added team-Documentation Documentation improvements that cannot be directly linked to other team labels and removed team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website labels Aug 1, 2023
@hypdeb
Copy link

hypdeb commented Aug 4, 2023

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

@buildbreaker2021 buildbreaker2021 added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Aug 16, 2023
Copy link

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 github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) stale Issues or PRs that are stale (no activity for 30 days) team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-CPP Issues for C++ rules type: support / not a bug (process)
Projects
None yet
Development

No branches or pull requests

6 participants
@meteorcloudy @zedzhu @hypdeb @buildbreaker2021 @Pavank1992 and others