llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.81k stars 11.45k forks source link

Bazel build on macOS failed with -fuse-ld=lld #59387

Open wangkuiyi opened 1 year ago

wangkuiyi commented 1 year ago

In Short

When I used Bazel on my M1 MacBook Pro to build the main branch, I got the following error.

clang: error: invalid linker name in argument '-fuse-ld=lld'

I then built lld using CMake and installed it to /usr/local/bin and reset the PATH environment variable. After all these, the above error is still there.

Details

Chip: Apple M1 Max macOS: Ventura 13.0.1 Xcode version: 14.1 (14B47b) MacOS SDK: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk

I followed the steps in https://github.com/llvm/llvm-project/blob/main/utils/bazel/README.md to build LLVM on the above system.

git clone --recursive https://github.com/llvm/llvm-project
cd llvm-project/utils/bazel
USE_BAZEL_VERSION=5.0.0 /opt/homebrew/bin/bazel build --config=generic_clang @llvm-project//... --sandbox_debug

Here attaches the bazel build command-line, which includes the --sandbox_debug option so to print more details.

(base) ✘-1 ~/w/llvm-project/utils/bazel [main ↓·4|✔]
18:45 $ USE_BAZEL_VERSION=5.0.0 /opt/homebrew/bin/bazel build --config=generic_clang @llvm-project//... --sandbox_debug
INFO: Analyzed 3731 targets (0 packages loaded, 0 targets configured).
INFO: Found 3731 targets...
ERROR: /private/var/tmp/_bazel_y/8c037591f8528cb3cd2621d674d2347d/external/llvm-project/mlir/BUILD.bazel:7250:10: Linking external/llvm-project/mlir/mlir-tblgen failed: (Aborted): sandbox-exec failed: error executing command
  (cd /private/var/tmp/_bazel_y/8c037591f8528cb3cd2621d674d2347d/sandbox/darwin-sandbox/1350/execroot/__main__ && \
  exec env - \
    APPLE_SDK_PLATFORM=MacOSX \
    APPLE_SDK_VERSION_OVERRIDE=13.0 \
    DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
    PATH=/Users/y/Library/Caches/bazelisk/downloads/bazelbuild/bazel-5.0.0-darwin-arm64/bin:/usr/local/bin:/Users/y/.cargo/bin:/opt/homebrew/opt/python@3.9/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/y/anaconda3/bin:/Users/y/anaconda3/condabin:/opt/homebrew/opt/python@3.9/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/y/bin \
    SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk \
    TMPDIR=/var/folders/hd/6q8jftdn7b1fygsrzdkp5ww40000gn/T/ \
    XCODE_VERSION_OVERRIDE=14.1.0.14B47b \
    ZERO_AR_DATE=1 \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_y/8c037591f8528cb3cd2621d674d2347d/sandbox/darwin-sandbox/1350/sandbox.sb /var/tmp/_bazel_y/install/c8706c49991751b43417f135b97a0616/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/cc_wrapper.sh @bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/llvm-project/mlir/mlir-tblgen-2.params)
clang: error: invalid linker name in argument '-fuse-ld=lld'
Error in child process '/usr/bin/xcrun'. 1
external/local_config_cc/cc_wrapper.sh: line 69: 70306 Abort trap: 6           "$(/usr/bin/dirname "$0")"/wrapped_clang "$@"
INFO: Elapsed time: 0.553s, Critical Path: 0.07s
INFO: 16 processes: 14 internal, 2 darwin-sandbox.
FAILED: Build did NOT complete successfully

I assumed that I do not have lld on my system, because when I ran Clang to build a hello world C program, it complains

clang: error: invalid linker name in argument '-fuse-ld=lld'

I then built lld using CMake by following this document https://lld.llvm.org/. I copied ld.lld from the ./build directory to /usr/local/bin and reset the PATH environment variable to /usr/local/bin:$PATH. Unfortunately, the above problem is still there.

wangkuiyi commented 1 year ago

The error went away after I commented out the -fuse-ld=ldd option from the .bazelrc file.

Is it a good idea to add a paragraph to https://github.com/llvm/llvm-project/blob/main/utils/bazel/README.md to explain this trick to contributors working on and for macOS?

aaronmondal commented 1 year ago

@wangkuiyi I'm not too familiar with MacOS, but I've seen that there are two variants of Clang on MacOS. a stripped-down "AppleClang" and a "Clang" that you'd get from building Clang yourself. My guess is that generic_clang will only work with the latter.

If the above is correct, WDYT about an apple_clang config for users using "AppleClang"?