Create a Bazel C++ toolchain configuration for remote actions¶
To construct C/C++ commands that can run remotely, Bazel needs a C++ toolchain configuration.
For local builds, Bazel configures this automatically based on toolchains installed on your host system. This may not be desirable: different developers may have different toolchains installed.
For remote builds, you need to configure a toolchain explicitly because Bazel doesn't know what's installed in the remote environment.
This page explains how to configure a Bazel C++ toolchain configuration for remote execution. You must have already created a container image with your C++ toolchain installed. You'll also need a host machine with Docker installed with the same OS and architecture as the remote execution environment.
Generating configuration with rbe_configs_gen
¶
rbe_configs_gen
is a tool that can automatically configure a toolchain for you, given a Docker image tag. Visit that page and follow the instructions there to build and run it.
rbe_configs_gen
works for Linux only. You may need to install additional tools such as Java in your container image, or pass flags like --generate_java_configs=false
for rbe_configs_gen
to work.
Generating configuration with Bazel¶
You can generate your C++ toolchain configuration with Bazel by building a C++ target locally in a container, then extracting the automatic configuration. This works more reliably and has fewer restrictions than rbe_configs_gen
, but it's a more manual process.
You can find a complete example workspace at https://github.com/EngFlow/example. The remote toolchains are in the platform/linux_x64 and platform/windows_x64 directories.
Before you begin, you'll need a Docker tag for your container image. We'll use engflow-container-image
in the instructions here. If it's in a remote registry, you must be able to docker pull
from that registry (docker login
first).
To build your configuration:
-
Run a new container based on your image. The next few steps should be performed inside the container.
-
Install Bazelisk.
-
Create a Bazel C++ workspace, then build the target. You may need to set the
CC
environment variable if Bazel doesn't find your compiler automatically.Bash -
Find the
local_config_cc
repository, and copy the files. They are short enough that you can read them, then copy from your terminal. -
On the host machine, create a directory in the repository where you want to use remote execution. The directory name is up to you, but its name should reflect the remote environment. If you use remote execution on multiple platforms, you'll probably want multiple directories. In our example repository, we have
platform/linux_x64
andplatform/windows_x64
. Paste thelocal_config_cc
files into that new directory. -
Modify the
BUILD
file, keeping only the toolchains you need, discarding others. You may combine it with an existingBUILD
file, perhaps containing other toolchains and aplatform
definition. In particular, keep the following targets:cc_toolchain_suite
toolchain
: this is the top-level definition for the C++ toolchains. It has atoolchains
attribute mappingcpu|compiler
andcpu
entries tocc_toolchain
labels. Keep only the entries forcc_toolchain
targets you need. The--crosstool_top
flag will refer to this target.cc_toolchain
cc-compiler-k8
: this contains high-level information for an individual toolchain. There may be multiple toolchains, and the name may be different, depending on your CPU architecture (k8
isx86_64
). You can drop thearmeabi-v7a
target; this is for Android only.cc_toolchain_config
local
: this describes your toolchain in detail. It lists paths of commands, paths of include directories, and flags to pass. You can modify this if you want to change which flags are passed automatically.filegroup
empty
: used in places where the toolchain doesn't depend on anything inside the Bazel workspace. You can replace":compiler_deps"
with":empty"
.
-
Add a
toolchain
target to theBUILD
file. This is part of Bazel's toolchain system, and adding this will help Bazel select your toolchain automatically. Make sure thetoolchain
attribute refers to acc_toolchain
target.Python -
In your project's
.bazelrc
file, ensure that your toolchain is registered when you are using your remote configuration. Ensure that--crosstool_top
and--host_crosstool_top
refer to yourcc_toolchain_suite
target, and that--cpu
,--host_cpu
,--compiler
, and--host_compiler
refer to the CPU and compiler fortoolchains
entry. -
Verify your new toolchain configuration works by building the same C++ target using your new remote configuration from a compatible host environment. You may need to configure a
platform
and add additional flags to.bazelrc
to enable remote execution, if you haven't done so already. Refer to Bazel First-Time Setup.