J-B-Blankenship
J-B-Blankenship

Reputation: 49

How do I specify portable build configurations for different operating systems for Bazel?

I am posting this question here because I could not find the specific answer already. By suggestion of another user, I am moving my answer in another post that answers this specifically to here to make it more clear as to what it answers.

What: The deployment of my application is on Linux and Windows. Build results and overall characteristics (i.e. prioritizing speed vs. memory, warnings, etc.) should be standard across operating systems and users of the repository.

Why: A developer should be focused on development, not tweaking build configurations or fighting it to work in the majority of cases. The capability to build on different systems should be as simple as specifying a single flag or automatic (preferably), centralized, and standardized for the entire repository.

Upvotes: 0

Views: 599

Answers (1)

J-B-Blankenship
J-B-Blankenship

Reputation: 49

The .bazelrc file can be utilized to offer a centralized source for specifying build configurations specific to operating system and build objectives.

In the .bazelrc file, the specification of build follow by :[name] allows you to specify any build options following thereafter on the same line. The name can be anything. In my case, I specified the compiler used that corresponds to the operating system.

Consider the following:

#In the .bazelrc file
build:msvc --cxxopt='-std:c++latest' -c opt

--cxxopt= is Bazel's flag to specify build options. -c is the flag to utilize a set of flags pre-set by Google with opt being optimal for speed in this case. All of this can be found in the documentation.

Now to use the same flags for testing without causing a complete rebuild, use the following:

test:msvc -c opt --test_output=all

There are nuances in this regard because Bazel does not like when flags are specified for a second time, even if it is the same flag. I cannot entirely remember my experiments with customizing on top of the -c opt, so you will have to test if ordering matters for yourself. I do not think it does though.

Combined, it will look like the following:

build:msvc --cxxopt='-std:c++latest' -c opt
test:msvc -c opt --test_output=all

On the command line, you would do the following to choose one of the options:

bazel build --config=[name] //...

with the names used above:

bazel build --config=msvc //...

and to test swap out the word "build" with "test":

bazel test --config=msvc //...

A snippet from my own own project:

# To build|test Windows: bazel build|test --config=msvc
# To build|test Linux: bazel build|test --config=gcc
build:msvc --cxxopt='-std:c++latest' -c opt
test:msvc -c opt --test_output=all

build:gcc --cxxopt='-std=c++2a' -c opt
test:gcc -c opt --test_output=all

build:gccProfile --cxxopt='-std=c++2a' --cxxopt='-fno-omit-frame-pointer' -c dbg

Notice the subtle differences between the gcc and msvc specifications. The flags specified are specific to the compiler used, not generic specifications by Bazel. Also, Bazel pulls the first compiler it finds on your operating system. I tried some experiments of building with clang on the same operating machine, but it requires a whole host of other specifications and so forth to make it work. gcc was the first compiler installed by me and used by Bazel, and it kept it as the default compiler.

Upvotes: 1

Related Questions