BazelToolchainο
Important
Some of the features used in this section are still under development, while they are recommended and usable and we will try not to break them in future releases, some breaking changes might still happen if necessary to prepare for the Conan 2.0 release.
Available since: 1.37.0
BazelToolchainο
The BazelToolchain is the toolchain generator for Bazel. It will generate a conan_bzl.rc file that contains
a build configuration conan-config to inject all the parameters into the bazel build command.
The BazelToolchain generator can be used by name in conanfiles:
class Pkg(ConanFile):
generators = "BazelToolchain"
[generators]
BazelToolchain
And it can also be fully instantiated in the conanfile generate() method:
from conan import ConanFile
from conan.tools.google import BazelToolchain
class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"
def generate(self):
tc = BazelToolchain(self)
tc.generate()
After running conan install command, the BazelToolchain generates the conan_bzl.rc file
that contains Bazel build parameters (it will depend on your current Conan settings and options from your default profile):
# Automatic bazelrc file created by Conan
build:conan-config --cxxopt=-std=gnu++17
build:conan-config --dynamic_mode=off
build:conan-config --compilation_mode=opt
The Bazel build helper will use that conan_bzl.rc file to perform a call using this
configuration. The outcoming command will look like this bazel --bazelrc=/path/to/conan_bzl.rc build --config=conan-config <target>.
The toolchain supports the following methods and attributes:
constructorο
def __init__(self, conanfile, namespace=None):
conanfile: the current recipe object. Always useself.namespace: Deprecated since Conan 1.62. It only keeps backward compatibility.
Attributesο
You can change some attributes before calling the generate() method if you want to change some of the precalculated
values:
from conan import ConanFile
from conan.tools.google import BazelToolchain
class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"
def generate(self):
tc = BazelToolchain(self)
tc.cxxopt.append("--my_flag")
tc.generate()
These attributes are processed and passed as part of build:conan-config:
force_pic (defaulted to
fpicifoptions.shared == Falseandoptions.fpic == TrueelseNone): Injected to the--force_picparameter.dynamic_mode (defaulted to
fullyif shared, elseoff): Injected to the--dynamic_modeparameter.cppstd (defaulted to
Noneif your settings does not havesettings.compiler.cppstdcopt (defaulted to
[]): They will be part of the--coptparameter.conlyopt (defaulted to
[]): They will be part of the--conlyoptparameter.cxxopt (defaulted to
[]): They will be part of the--cxxoptparameter.linkopt (defaulted to
[]): They will be part of the--linkoptparameter.compilation_mode (defaulted to
optifsettings.build_type == "Release", otherwise, ifsettings.build_type == "Debug", itβll bedbg): Injected to the--compilation_modeparameter.compiler (defaulted to
None): Injected to the--compilerparameter.cpu (defaulted to
None): Injected to the--cpuparameter.crosstool_top (defaulted to
None): Injected to the--crosstool_topparameter.
confο
BazelToolchain is affected by these [conf] variables:
tools.build:cxxflagslist of extra C++ flags that will be used bycxxopt.tools.build:cflagslist of extra of pure C flags that will be used byconlyopt.tools.build:sharedlinkflagslist of extra linker flags that will be used bylinkopt.tools.build:exelinkflagslist of extra linker flags that will be used bylinkopt.tools.build:linker_scriptslist of linker scripts, each of which will be prefixed with-Tand added tolinkopt.