9

How can you tell bazel to use a different C++ compiler on OS X?

bazel build --action_env CC=/path/to/compiler //:target

works on linux.

But -s shows that bazel always runs with external/local_config_cc/wrapped_clang (clang) on OSX regardless of what CC is.

Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
  • 4
    To whoever voted to close this, it is absolutely not an unclear question! With C++, bazel builds with a compiler. I'm asking how to change it on OSX. – Ryan Burn Dec 04 '17 at 00:13

3 Answers3

8

CC correctly works only when you use the C++-only toolchain. If you have Xcode installed, bazel will detect this and automatically pick a different toolchain, the one that supports both C++ and ObjC. This toolchain can only use Xcode-provided clang.

This is unfortunate and I propose two solutions:

  1. File a feature request for bazel to make it possible to select which toolchain is used. This will allow you to tell bazel that even though you have Xcode installed, you want to use C++ only toolchain with a custom compiler. This is quite simple and doable in a short time.
  2. File a feature request for bazel to make it possible to select which compiler is used with C++/ObjC toolchain. I cannot comment on viability of this, I know next to nothing about osx, and I have no idea if it makes any sense to compile ObjC with a compiler that is not provided with Xcode...
hlopko
  • 3,100
  • 22
  • 27
  • Thanks. I put in [#4231](https://github.com/bazelbuild/bazel/issues/4231) to follow up on (1). – Ryan Burn Dec 04 '17 at 21:04
  • 1
    The issue is fixed. Once bazel is released, and you specify `BAZEL_USE_CPP_ONLY_TOOLCHAIN=1` env variable, bazel will pick C++-only toolchain. – hlopko Dec 12 '17 at 15:57
8

Actually with the latest version of bazel specifying

BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
build --action_env CC=/path/to/compiler  [...]

does work, in the sense that the specified compiler is used. However there is still a problem with the compiler flags. If the compiler flags of the old compiler are incompatible with the new one, there is a problem. I still have to find out how to change compiler flags.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • Yeah, I was noticing that too. It seems like there's still some work to be done to make this workflow usable. It's especially a problem when using a different compiler (e.g. gcc on OS X instead of clang). – Ryan Burn Apr 22 '18 at 21:32
1

Use --crosstool_top.

See also --host_crosstool_top and --apple_crosstool_top.

Jin
  • 12,748
  • 3
  • 36
  • 41
László
  • 3,973
  • 1
  • 13
  • 26