Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change compiler command line with Bazel

Tags:

c++

bazel

I'd like to have complete control over the command line arguments Bazel passes to the compiler when compiling and linking C++ files. For example, I'd like to use a G++ from a custom path, I'd like to change -std=c++0x to -std=c++14 and remove -fno-omit-frame-pointer - with the following constraints:

  • Avoid setting the compiler via CC envvar, as it is fragile
  • Avoid using --crosstool_top et. al, as I understand Bazel is moving away from it
  • Be compatible with --all_incompatible_changes
  • Do not simply override those args via copts, as it is fragile and makes error prone commands

Preferably, I'd like to get the auto detected, generated toolchain, modify it, and commit it to my workspace, to be used by every C++ target in the workspace, including imported, from source compiled workspaces.

I looked at Toolchains, Configuring C++ toolchain, rules_cc - but I couldn't figure it out.

like image 510
erenon Avatar asked Mar 10 '20 20:03

erenon


People also ask

What compiler does Bazel use?

Build C++ with Clang From 0.29. 0, Bazel supports building with LLVM's MSVC-compatible compiler driver ( clang-cl.exe ). Requirement: To build with Clang, you have to install both LLVM and Visual C++ Build tools, because although you use clang-cl.exe as compiler, you still need to link to Visual C++ libraries.

How do I run Bazel commands?

To run Bazel, go to your base workspace directory or any of its subdirectories and type bazel . See build if you need to make a new workspace.

What is Bazel C++?

Bazel is a build system from google. C++ is one of the languages that bazel supports. One of the nice things about bazel is, you specify the files and bazel figures out the dependency tree.

Is Bazel any good?

Bazel is fast and builds correctly - It can cache build results and only rebuild what it needs to, which make it fast. Its platform independent - It can run on Linux, macOS, Windows. Bazel scales - It can handle large source files easily. It works with various repositories and user bases in the tens of thousands.


2 Answers

The default arguments (e.g: -fno-omit-frame-pointer or -std=c++0x) can be removed by disabling the default_compile_flags feature that provides them:

$ bazel build ... --features=-default_compile_flags
like image 146
erenon Avatar answered Sep 23 '22 18:09

erenon


Add a .bazelrc to your project. Add the lines

build:your_config --cxxopt=-std=c++14

Build your code:

bazel build --config=your_config //...
like image 45
Vertexwahn Avatar answered Sep 22 '22 18:09

Vertexwahn