Bazel build on macOS failed: no member named 'syscall_impl' in namespace '__llvm_libc'

I am building LLVM (the most recent main branch) using Bazel on my M1 MacBook Pro using the generic_clang build configuration.

  • 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

After applying the trick as described in this issue comment, bazel building can keep running until the following error.

(base) ✘-INT ~/w/llvm-project/utils/bazel [main ↓·6|✚ 1]
19:37 $ USE_BAZEL_VERSION=5.0.0 /opt/homebrew/bin/bazel build --config=generic_clang @llvm-project//... --sandbox_debug
INFO: Build options --host_linkopt and --linkopt have changed, discarding analysis cache.
INFO: Analyzed 3731 targets (0 packages loaded, 21793 targets configured).
INFO: Found 3731 targets...
ERROR: /private/var/tmp/_bazel_y/8c037591f8528cb3cd2621d674d2347d/external/llvm-project/libc/BUILD.bazel:1369:14: Compiling libc/src/unistd/linux/ftruncate.cpp failed: (Aborted): sandbox-exec failed: error executing command
  (cd /private/var/tmp/_bazel_y/8c037591f8528cb3cd2621d674d2347d/sandbox/darwin-sandbox/5464/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/5464/sandbox.sb /var/tmp/_bazel_y/install/c8706c49991751b43417f135b97a0616/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/wrapped_clang_pp '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG '-std=c++11' 'DEBUG_PREFIX_MAP_PWD=.' -iquote external/llvm-project -iquote bazel-out/darwin_arm64-fastbuild/bin/external/llvm-project -isystem external/llvm-project/libc -isystem bazel-out/darwin_arm64-fastbuild/bin/external/llvm-project/libc -MD -MF bazel-out/darwin_arm64-fastbuild/bin/external/llvm-project/libc/_objs/ftruncate.__internal__/ftruncate.pic.d -fPIC '-frandom-seed=bazel-out/darwin_arm64-fastbuild/bin/external/llvm-project/libc/_objs/ftruncate.__internal__/ftruncate.pic.o' -isysroot __BAZEL_XCODE_SDKROOT__ -F__BAZEL_XCODE_SDKROOT__/System/Library/Frameworks -F__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/Library/Frameworks '-mmacosx-version-min=13.0' -no-canonical-prefixes -pthread -Wall -Wno-deprecated '-std=c++17' -Wno-range-loop-analysis -O3 -fno-builtin -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -target arm64-apple-macosx -c external/llvm-project/libc/src/unistd/linux/ftruncate.cpp -o bazel-out/darwin_arm64-fastbuild/bin/external/llvm-project/libc/_objs/ftruncate.__internal__/ftruncate.pic.o)
external/llvm-project/libc/src/unistd/linux/ftruncate.cpp:21:26: error: no member named 'syscall_impl' in namespace '__llvm_libc'
  int ret = __llvm_libc::syscall_impl(SYS_ftruncate, fd, len);
            ~~~~~~~~~~~~~^
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
INFO: Elapsed time: 496.885s, Critical Path: 45.82s
INFO: 4842 processes: 755 internal, 4087 darwin-sandbox.
FAILED: Build did NOT complete successfully

The above message tells that the error was from this building rule

libc_function(
    name = "ftruncate",
    srcs = ["src/unistd/linux/ftruncate.cpp"],
    hdrs = ["src/unistd/ftruncate.h"],
    deps = [
        ":__support_common",
        ":__support_osutil",
    ],
)

There are other rules near the above; they all build source files in unistd/linux. The build process would continue if I commented out these rules.

OK, I read the file /libc/src/CMakeLists.txt. It turns out that some sub-directories in /libc are built only for Linux.

if(${LIBC_TARGET_OS} STREQUAL "linux")
  add_subdirectory(dirent)
  add_subdirectory(fcntl)
  add_subdirectory(pthread)
  add_subdirectory(sched)
  add_subdirectory(sys)
  add_subdirectory(termios)
  add_subdirectory(unistd)
endif()

if(NOT LLVM_LIBC_FULL_BUILD)
  return()
endif()

And this conditional is not implemented in utils/libc/BUILD.bazel. Does it seem like the Bazel build rules were written to work on Linux only? Could anyone who knows the history check my understanding, please?

My assumption is just that this hasn’t been needed on macOS with bazel so far

1 Like

TensorFlow is building LLVM/MLIR with Bazel on MacOS on a day-to-day basis, but that does not include libc (which is the source of the failure here)

1 Like

Thank you @mehdi_amini ! Yes, I got it after listing LLVM targets on which TensorFlow depends in this Quip doc. It seems that TensorFlow depends on no more targets than LLVM and MLIR.