llvm / llvm-project

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

Cannot build the toy language example with bazel #71514

Open tisonkun opened 8 months ago

tisonkun commented 8 months ago

Reproduce

The reproduce is simple, with file structure:

toy/
toy/BUILD
toy/MODULE.bazel
toy/WORKSPACE
toy/toy.cpp

where

  1. toy.cpp contains the example code
  2. WORKSPACE is empty.
  3. BUILD contains:
cc_binary(
  name = "toy",
  srcs = ["toy.cpp"],
  deps = [
    "@org_llvm_project//llvm:Core",
    "@org_llvm_project//llvm:Support",
  ]
)
  1. MODULE.bazel contains:
module(name = "toy", version = "0.1.0")
bazel_dep(name = "llvm-project", version = "17.0.3", repo_name = "org_llvm_project")

when build with:

$ bazel build --enable_bzlmod --action_env=BAZEL_CXXOPTS="-std=c++17" --sandbox_debug --verbose_failures //:toy

it fails with:

INFO: Analyzed target //:toy (58 packages loaded, 1490 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_tison/c244744f015d9943cebd9b4c9b18d7b0/external/llvm-project~17.0.3/llvm/BUILD.bazel:600:10: Linking external/llvm-project~17.0.3/llvm/llvm-min-tblgen [for tool] failed: (Exit 254): sandbox-exec failed: error executing command 
  (cd /private/var/tmp/_bazel_tison/c244744f015d9943cebd9b4c9b18d7b0/sandbox/darwin-sandbox/338/execroot/_main && \
  exec env - \
    PATH='...' \
    PWD=/proc/self/cwd \
    TMPDIR=/var/folders/ts/160w4k_j5mv5bjk9rnjkzjpm0000gn/T/ \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_tison/c244744f015d9943cebd9b4c9b18d7b0/sandbox/darwin-sandbox/338/sandbox.sb /var/tmp/_bazel_tison/install/ba45a1a358416fb2acebb17d65c986f3/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/private/var/tmp/_bazel_tison/c244744f015d9943cebd9b4c9b18d7b0/sandbox/darwin-sandbox/338/stats.out' external/bazel_tools~cc_configure_extension~local_config_cc/cc_wrapper.sh @bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/llvm-project~17.0.3/llvm/llvm-min-tblgen-2.params)
clang: error: unable to execute command: Segmentation fault: 11
clang: error: linker command failed due to signal (use -v to see invocation)
Target //:toy failed to build
INFO: Elapsed time: 40.140s, Critical Path: 2.66s
INFO: 381 processes: 44 internal, 337 darwin-sandbox.

while if built with:

$ clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy

It succeeds with preinstalled LLVM@17.

Environment

$ uname -a
Darwin xxx Darwin Kernel Version 22.5.0: Mon Apr 24 20:53:19 PDT 2023; root:xnu-xxx/RELEASE_ARM64_T6020 arm64

$ clang -v
Homebrew clang version 17.0.4
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Reference

tisonkun commented 8 months ago

It seems a script returns value other than 0. I tried to debug and get:

$ /private/var/tmp/_bazel_tison/c244744f015d9943cebd9b4c9b18d7b0/sandbox/darwin-sandbox/338/execroot/_main/external/bazel_tools~cc_configure_extension~local_config_cc/cc_wrapper.sh bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/llvm-project\~17.0.3/llvm/llvm-min-tblgen-2.params 
ld: warning: ignoring file bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/llvm-project~17.0.3/llvm/llvm-min-tblgen-2.params, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x2D 0x6F 0x0A 0x62 0x61 0x7A 0x65 0x6C 0x2D 0x6F 0x75 0x74 0x2F 0x64 0x61 0x72 )
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)