BazelToolchain

Warning

This feature is experimental and subject to breaking changes. See the Conan stability section for more information.

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:

conanfile.py

  1. class Pkg(ConanFile):
  2. generators = "BazelToolchain"

conanfile.txt

  1. [generators]
  2. BazelToolchain

And it can also be fully instantiated in the conanfile generate() method:

conanfile.py

  1. from conan import ConanFile
  2. from conan.tools.google import BazelToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def generate(self):
  6. tc = BazelToolchain(self)
  7. tc.generate()

Generated files

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):

conan_bzl.rc

  1. # Automatic bazelrc file created by Conan
  2. build:conan-config --cxxopt=-std=gnu++17
  3. build:conan-config --dynamic_mode=off
  4. 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>.

Reference

class BazelToolchain(conanfile)

  • Parameters:

    conanfile< ConanFile object > The current recipe object. Always use self.

  • force_pic

    Boolean used to add –force_pic=True. Depends on self.options.shared and self.options.fPIC values

  • dynamic_mode

    String used to add –dynamic_mode=[“fully”|”off”]. Depends on self.options.shared value.

  • cppstd

    String used to add –cppstd=[FLAG]. Depends on your settings.

  • copt

    List of flags used to add –copt=flag1 … –copt=flagN

  • conlyopt

    List of flags used to add –conlyopt=flag1 … –conlyopt=flagN

  • cxxopt

    List of flags used to add –cxxopt=flag1 … –cxxopt=flagN

  • linkopt

    List of flags used to add –linkopt=flag1 … –linkopt=flagN

  • compilation_mode

    String used to add –compilation_mode=[“opt”|”dbg”]. Depends on self.settings.build_type

  • compiler

    String used to add –compiler=xxxx.

  • cpu

    String used to add –cpu=xxxxx. At the moment, it’s only added if cross-building.

  • crosstool_top

    String used to add –crosstool_top.

  • generate()

    Creates a conan_bzl.rc file with some bazel-build configuration. This last mentioned is put as conan-config.

conf

BazelToolchain is affected by these [conf] variables:

  • tools.build:cxxflags list of extra C++ flags that will be used by cxxopt.

  • tools.build:cflags list of extra of pure C flags that will be used by conlyopt.

  • tools.build:sharedlinkflags list of extra linker flags that will be used by linkopt.

  • tools.build:exelinkflags list of extra linker flags that will be used by linkopt.

  • tools.build:linker_scripts list of linker scripts, each of which will be prefixed with -T and added to linkopt.

See also