bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.69k stars 3.98k forks source link

BAZEL_USE_CPP_ONLY_TOOLCHAIN doesn't work without Xcode / Xcode CLT installed. #16009

Open uri-canva opened 1 year ago

uri-canva commented 1 year ago

Description of the bug:

BAZEL_USE_CPP_ONLY_TOOLCHAIN on Darwin is meant to disable the Xcode toolchain resolution and use the common Unix toolchain resolution based on what binaries are available on the PATH and set with environment variables. However it will still hardcode several apple tools, like /usr/bin/libtool, /usr/bin/gcc and /usr/bin/install_name_tool (in cc_wrapper.sh).

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

# Either have an install of macOS without Xcode or Xcode CLT installed, or:
ln -s $(xcode-select -p) invalid
sudo xcode-select -s invalid
rm invalid

# try building a cc target with BAZEL_USE_CPP_ONLY_TOOLCHAIN having `libtool`, `install_name_tool` and `gcc` on your `PATH`:
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 bazel build //tools/cpp/runfiles

Expected output: successful build using the binaries on your PATH. Actual output:

ERROR: /Users/uri/github/bazelbuild/bazel/tools/cpp/runfiles/BUILD:51:11: Compiling tools/cpp/runfiles/runfiles_src.cc failed: (Exit 1): cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics ... (remaining 20 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
xcrun: error: active developer path ("/Users/uri/github/bazelbuild/bazel/invalid") does not exist
Use `sudo xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools, or use `xcode-select --install` to install the standalone command line developer tools.
See `man xcode-select` for more details.
Target //tools/cpp/runfiles:runfiles failed to build

Which operating system are you running Bazel on?

macOS 12.5 21G72

What is the output of bazel info release?

release 5.2.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

git@github.com:bazelbuild/bazel.git
c282526c071389cd6f88cb77565283b257316267
2e8458b7810eab7829fc7d28af5c45b9af91ed7c

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

makslevental commented 1 year ago

there's an additional problem here:

ERROR: /Users/mlevental/dev_projects/xls/xls/passes/BUILD:179:11: Linking xls/passes/libunroll_pass.pic.a failed: (Exit 1): sandbox-exec failed: error executing command 
  (cd /private/var/tmp/_bazel_mlevental/77aa60ef93768a50da7e847fcf47eca8/sandbox/darwin-sandbox/17524/execroot/com_google_xls && \
  exec env - \
    BAZEL_CXXOPTS='-std=c++17' \
    BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
    PATH='/Users/mlevental/.cabal/bin:/Users/mlevental/.ghcup/bin:/opt/homebrew/opt/binutils/bin:/opt/homebrew/opt/llvm/bin:/opt/homebrew/opt/bison/bin:/opt/homebrew/opt/ruby/bin:/opt/homebrew/opt/qt@5/bin:/opt/homebrew/bin:/opt/homebrew/opt/bison/bin:/opt/homebrew/bin/:/opt/homebrew/opt/libtool/libexec/gnubin:/Users/mlevental/miniforge3/bin:/Users/mlevental/miniforge3/condabin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/opt/local/bin:/opt/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/mlevental/.cargo/bin:/opt/homebrew/opt/ccache/libexec:/Users/mlevental/Library/Application Support/JetBrains/Toolbox/scripts' \
    PWD=/proc/self/cwd \
    TMPDIR=/var/folders/5r/nhx6kb2j2bq9zh1lng031l800000gn/T/ \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_mlevental/77aa60ef93768a50da7e847fcf47eca8/sandbox/darwin-sandbox/17524/sandbox.sb /var/tmp/_bazel_mlevental/install/492267a38d620c777a8b281ba7e366a6/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/private/var/tmp/_bazel_mlevental/77aa60ef93768a50da7e847fcf47eca8/sandbox/darwin-sandbox/17524/stats.out' /opt/homebrew/opt/libtool/libexec/gnubin/libtool @bazel-out/darwin_arm64-opt/bin/xls/passes/libunroll_pass.pic.a-2.params)
libtool:   error: you must specify an output file
Usage: /opt/homebrew/opt/libtool/libexec/gnubin/libtool [OPTION]... [MODE-ARG]...
Try 'libtool --help' for more information.

I.e., that while the GNU libtool is found, it's not passed the correct argument; I tried patching libtool itself (because I don't know how to configure which options to pass it through bazel itself) and in addition to the mode (link) you need to pass --tag and the output file name. The lack of these three things (and possibly others) is preventing me from compiling a bazel project on my M1.

Uninstalled gnu libtool then gets me back to apple's libtool but then symbols aren't resolved at runtime (despite successful linking during the link stage).

github-actions[bot] commented 1 month ago

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.

uri-canva commented 1 month ago

Just checked, there are still references to /usr/bin/xcrun in the wrapper.

keith commented 1 month ago

Fwiw I don't think there was ever a goal of supporting builds without even the CLT. I imagine the best solution if you wanted that would be to use a custom cc toolchain like toolchains_llvm. I think some changes would still be required though.

uri-canva commented 1 month ago

I think builds without that environment variable already work with CLT and without Xcode though. I thought it was meant to support toolchains provided through other means like brew etc.

If not then maybe it's pointless to have it?

keith commented 1 month ago

The point is to allow you to use the CLT without a full Xcode install (and has the side benefit of making your build more similar to Linux builds if you care about cross platform, at the cost of some platform specific improvements)

uri-canva commented 1 month ago

What I mean is that bazel never needed full Xcode install as far as I remember, regardless of whether you use BAZEL_USE_CPP_ONLY_TOOLCHAIN. Maybe it was necessary in older, pre-open source versions?