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

Fix unix toolchain for macos arm64 platform #13683

Conversation

meteorcloudy
Copy link
Member

@meteorcloudy meteorcloudy commented Jul 14, 2021

Cherry-pick for b4b0c32 for #13558

This allow the basic unix toolchain to work on Apple silicon without
Xcode installed.

Fixes #13514

Closes #13515.

PiperOrigin-RevId: 375711139

This allow the basic unix toolchain to work on Apple silicon without
Xcode installed.

Fixes bazelbuild#13514

Closes bazelbuild#13515.

PiperOrigin-RevId: 375711139
@google-cla google-cla bot added the cla: yes label Jul 14, 2021
@meteorcloudy meteorcloudy requested a review from katre July 14, 2021 15:48
Copy link
Member

@katre katre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll merge this once the CI tests pass.

We don't have any coverage for darwin arm64 in CI, do we? Can you verify that you tested this on actual hardware?

@meteorcloudy
Copy link
Member Author

We don't have any coverage for darwin arm64 in CI, do we? Can you verify that you tested this on actual hardware?

Good point! I manually verified this on a M1 machine, it works correctly! Thanks!

@katre katre merged commit f013c84 into bazelbuild:release-4.2.0-patches Jul 15, 2021
@katre katre mentioned this pull request Jul 16, 2021
9 tasks
@alexeagle
Copy link
Contributor

@meteorcloudy thanks for the fix - I observe that this works with bazel-4.2.0rc3-darwin-x86_64, but not with bazel-4.2.0rc3-darwin-arm64 - the latter still fails to find the toolchain. Is that still expected? Do you recall which architecture Bazel binary you used for testing?

ERROR: /private/var/tmp/_bazel_alexeagle/f366b2ea298a1eb16435a50617f648a1/external/local_config_cc/BUILD:48:19: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'darwin_arm64'

@meteorcloudy
Copy link
Member Author

meteorcloudy commented Aug 3, 2021

@alexeagle

Hmm, can you show me the content of /private/var/tmp/_bazel_alexeagle/f366b2ea298a1eb16435a50617f648a1/external/local_config_cc/BUILD?

I uninstalled my Xcode and tested again with the arm64 Bazel version on a M1 machine, it seems to work.

pcloudy@pcloudy-macbookpro2:~/workspace/my_tests/bazel
$ bazel version
Build label: 4.2.0rc3
Build target: bazel-out/darwin_arm64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Aug 2 11:58:42 2021 (1627905522)
Build timestamp: 1627905522
Build timestamp as int: 1627905522

pcloudy@pcloudy-macbookpro2:~/workspace/my_tests/bazel
$ lipo -info $(which bazel)
Non-fat file: /Users/pcloudy/bin/bazel is architecture: arm64

pcloudy@pcloudy-macbookpro2:~/workspace/my_tests/bazel
$ xcode-select -p
/Library/Developer/CommandLineTools

pcloudy@pcloudy-macbookpro2:~/workspace/my_tests/bazel
$ bazel build examples/cpp:hello-world
INFO: Invocation ID: 6586a020-0c31-413c-86f5-2b136aafa2ee
INFO: Reading 'startup' options from /Users/pcloudy/.bazelrc: --host_jvm_args=-Djava.net.preferIPv6Addresses=true
INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=281
INFO: Reading rc options for 'build' from /Users/pcloudy/workspace/my_tests/bazel/.bazelrc:
  Inherited 'common' options: --enable_platform_specific_config
INFO: Reading rc options for 'build' from /Users/pcloudy/.bazelrc:
  'build' options: --verbose_failures --announce_rc --disk_cache=/tmp/bazel_disk_cache --repository_cache=/tmp/bazel_repository_cache
INFO: Found applicable config definition build:macos in file /Users/pcloudy/workspace/my_tests/bazel/.bazelrc: --macos_minimum_os=10.10
DEBUG: /private/var/tmp/_bazel_pcloudy/169b23e1f950a49deea4bc42c808fd53/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:103:14: rbe_ubuntu1804_java11 not using checked in configs as detect_java_home was set to True
DEBUG: /private/var/tmp/_bazel_pcloudy/169b23e1f950a49deea4bc42c808fd53/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:103:14: rbe_ubuntu1604_java8 not using checked in configs as detect_java_home was set to True
INFO: Analyzed target //examples/cpp:hello-world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //examples/cpp:hello-world up-to-date:
  bazel-bin/examples/cpp/hello-world
INFO: Elapsed time: 0.097s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

pcloudy@pcloudy-macbookpro2:~/workspace/my_tests/bazel
$ cat bazel-bazel/external/local_config_cc/BUILD
# Copyright 2016 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This becomes the BUILD file for @local_config_cc// under non-BSD unixes.

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

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

licenses(["notice"])  # Apache 2.0

cc_library(
    name = "malloc",
)

filegroup(
    name = "empty",
    srcs = [],
)

filegroup(
    name = "cc_wrapper",
    srcs = ["cc_wrapper.sh"],
)

filegroup(
    name = "compiler_deps",
    srcs = glob(["extra_tools/**"], allow_empty = True) + [":builtin_include_directory_paths",
    ":cc_wrapper"],
)

# This is the entry point for --crosstool_top.  Toolchains are found
# by lopping off the name of --crosstool_top and searching for
# the "${CPU}" entry in the toolchains attribute.
cc_toolchain_suite(
    name = "toolchain",
    toolchains = {
        "darwin_arm64|clang": ":cc-compiler-darwin_arm64",
        "darwin_arm64": ":cc-compiler-darwin_arm64",
        "armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a",
        "armeabi-v7a": ":cc-compiler-armeabi-v7a",
    },
)

cc_toolchain(
    name = "cc-compiler-darwin_arm64",
    toolchain_identifier = "local",
    toolchain_config = ":local",
    all_files = ":compiler_deps",
    ar_files = ":compiler_deps",
    as_files = ":compiler_deps",
    compiler_files = ":compiler_deps",
    dwp_files = ":empty",
    linker_files = ":compiler_deps",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 1,
    module_map = ":module.modulemap",
)

cc_toolchain_config(
    name = "local",
    cpu = "darwin_arm64",
    compiler = "clang",
    toolchain_identifier = "local",
    host_system_name = "local",
    target_system_name = "local",
    target_libc = "macosx",
    abi_version = "local",
    abi_libc_version = "local",
    cxx_builtin_include_directories = ["/Library/Developer/CommandLineTools/usr/lib/clang/12.0.5/include",
    "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
    "/Library/Developer/CommandLineTools/usr/include",
    "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
    "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.5/share",
    "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1"],
    tool_paths = {"ar": "/usr/bin/libtool",
        "ld": "/usr/bin/ld",
        "llvm-cov": "None",
        "cpp": "/usr/bin/cpp",
        "gcc": "cc_wrapper.sh",
        "dwp": "/usr/bin/dwp",
        "gcov": "/usr/bin/gcov",
        "nm": "/usr/bin/nm",
        "objcopy": "/usr/bin/objcopy",
        "objdump": "/usr/bin/objdump",
        "strip": "/usr/bin/strip"},
    compile_flags = ["-U_FORTIFY_SOURCE",
    "-fstack-protector",
    "-Wall",
    "-Wthread-safety",
    "-Wself-assign",
    "-fcolor-diagnostics",
    "-fno-omit-frame-pointer"],
    opt_compile_flags = ["-g0",
    "-O2",
    "-D_FORTIFY_SOURCE=1",
    "-DNDEBUG",
    "-ffunction-sections",
    "-fdata-sections"],
    dbg_compile_flags = ["-g"],
    cxx_flags = ["-std=c++0x"],
    link_flags = ["-undefined",
    "dynamic_lookup",
    "-headerpad_max_install_names"],
    link_libs = ["-lstdc++",
    "-lm"],
    opt_link_flags = [],
    unfiltered_compile_flags = ["-no-canonical-prefixes",
    "-Wno-builtin-macro-redefined",
    "-D__DATE__=\"redacted\"",
    "-D__TIMESTAMP__=\"redacted\"",
    "-D__TIME__=\"redacted\""],
    coverage_compile_flags = ["-fprofile-instr-generate",  "-fcoverage-mapping"],
    coverage_link_flags = ["-fprofile-instr-generate"],
    supports_start_end_lib = False,
)

# Android tooling requires a default toolchain for the armeabi-v7a cpu.
cc_toolchain(
    name = "cc-compiler-armeabi-v7a",
    toolchain_identifier = "stub_armeabi-v7a",
    toolchain_config = ":stub_armeabi-v7a",
    all_files = ":empty",
    ar_files = ":empty",
    as_files = ":empty",
    compiler_files = ":empty",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 1,
)

armeabi_cc_toolchain_config(name = "stub_armeabi-v7a")

@alexeagle
Copy link
Contributor

Sorry to let this go so long @meteorcloudy

% alias bazel=/Users/alexeagle/Downloads/bazel-4.2.0rc3-darwin-arm64
% bazel test //...
ERROR: /private/var/tmp/_bazel_alexeagle/60269631d49c6e1fcd2f98db2e749015/external/remote_coverage_tools/BUILD:9:12: Target '@remote_coverage_tools//:Main' depends on toolchain '@local_config_cc//:cc-compiler-darwin', which cannot be found: no such target '@local_config_cc//:cc-compiler-darwin': target 'cc-compiler-darwin' not declared in package '' defined by /private/var/tmp/_bazel_alexeagle/60269631d49c6e1fcd2f98db2e749015/external/local_config_cc/BUILD'

content of that file: https://gist.github.com/alexeagle/a19cee9b7e0f0d49902e0a1b74a92cc3

Verified I get the same with the 4.2.0 final release

Come ping me on bazel slack if you have a chance to get more real-time on this

@alexeagle
Copy link
Contributor

The obvious question is the diff between our generated files. I get /usr/local/include in the generated file, but yours didn't.

% diff !$ /private/var/tmp/_bazel_alexeagle/60269631d49c6e1fcd2f98db2e749015/external/local_config_cc/BUILD
diff /tmp/meteorcloudy /private/var/tmp/_bazel_alexeagle/60269631d49c6e1fcd2f98db2e749015/external/local_config_cc/BUILD
84c84,85
<     cxx_builtin_include_directories = ["/Library/Developer/CommandLineTools/usr/lib/clang/12.0.5/include",
---
>     cxx_builtin_include_directories = ["/usr/local/include",
>     "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.5/include",

However even if I modify my /private/var/tmp/_bazel_alexeagle/60269631d49c6e1fcd2f98db2e749015/external/local_config_cc/BUILD to match yours, I still get the same error.

@alexeagle
Copy link
Contributor

Well, shoot. After doing a bazel clean --expunge I can't reproduce a problem anymore. So I guess there was some cross-architecture issue with invalidating a repository cache, but it doesn't seem worth tracking that down. Never mind :)

@meteorcloudy
Copy link
Member Author

OK, if it reappears with an easy way to reproduce, I'm happy to look into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants