Build a simple Bazel 7.x project using Conan

Warning

This example is Bazel >= 7.1 compatible.

In this example, we are going to create a Hello World program that uses one of the most popular C++ libraries: fmt.

Note

This example is based on the Build a simple CMake project using Conan tutorial. So we highly recommend reading it before trying out this one.

We’ll use Bazel as the build system and helper tool in this case, so you should get it installed before going forward with this example. See how to install Bazel.

Please, first clone the sources to recreate this project. You can find them in the examples2 repository in GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/examples/tools/google/bazeltoolchain/7_x/string_formatter

We start from a very simple C++ language project with this structure:

  1. .
  2. ├── MODULE.bazel
  3. ├── conanfile.txt
  4. └── main
  5. ├── BUILD
  6. └── demo.cpp

This project contains a MODULE.bazel file loading the Conan dependencies (in this case only fmt) and a main/BUILD file which defines the demo bazel target and it’s in charge of using fmt to build a simple Hello World program.

Let’s have a look at each file’s content:

main/demo.cpp

  1. #include <cstdlib>
  2. #include <fmt/core.h>
  3. int main() {
  4. fmt::print("{} - The C++ Package Manager!\n", "Conan");
  5. return EXIT_SUCCESS;
  6. }

MODULE.bazel

  1. load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension")
  2. use_repo(load_conan_dependencies, "fmt")

main/BUILD

  1. cc_binary(
  2. name = "demo",
  3. srcs = ["demo.cpp"],
  4. deps = [
  5. "@fmt//:fmt"
  6. ],
  7. )

conanfile.txt

  1. [requires]
  2. fmt/10.1.1
  3. [generators]
  4. BazelDeps
  5. BazelToolchain
  6. [layout]
  7. bazel_layout

Conan uses the BazelToolchain to generate a conan_bzl.rc file which defines the conan-config bazel-build configuration. This file and the configuration are passed as parameters to the bazel build command. Apart from that, Conan uses the BazelDeps generator to create all the bazel files ([DEP]/BUILD.bazel, conan_deps_module_extension.bzl and conan_deps_repo_rules.bzl) which define the rule and all the dependencies to create/load them as Bazel repositories. The MODULE.bazel above is ready to load the conan_deps_module_extension.bzl file which will tell the main/BUILD all the information about the @fmt//:fmt bazel target.

As the first step, we should install all the dependencies listed in the conanfile.txt. The command conan install does not only install the fmt package, it also builds it from sources in case your profile does not match with a pre-built binary in your remotes. Furthermore, it will save all the files created by the generators listed in the conanfile.txt in a folder named conan/ (default folder defined by the bazel_layout).

  1. $ conan install . --build=missing
  2. # ...
  3. ======== Finalizing install (deploy, generators) ========
  4. conanfile.txt: Writing generators to /Users/user/develop/examples2/examples/tools/google/bazeltoolchain/7_x/string_formatter/conan
  5. conanfile.txt: Generator 'BazelDeps' calling 'generate()'
  6. conanfile.txt: Generator 'BazelToolchain' calling 'generate()'
  7. conanfile.txt: Generating aggregated env files
  8. conanfile.txt: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
  9. Install finished successfully

Now we are ready to build and run our application:

  1. $ bazel --bazelrc=./conan/conan_bzl.rc build --config=conan-config //main:demo
  2. Computing main repo mapping:
  3. Loading:
  4. Loading: 0 packages loaded
  5. Analyzing: target //main:demo (1 packages loaded, 0 targets configured)
  6. Analyzing: target //main:demo (1 packages loaded, 0 targets configured)
  7. [0 / 1] [Prepa] BazelWorkspaceStatusAction stable-status.txt
  8. INFO: Analyzed target //main:demo (69 packages loaded, 369 targets configured).
  9. [5 / 7] Compiling main/demo.cpp; 0s darwin-sandbox
  10. INFO: Found 1 target...
  11. Target //main:demo up-to-date:
  12. bazel-bin/main/demo
  13. INFO: Elapsed time: 2.955s, Critical Path: 1.70s
  14. INFO: 7 processes: 5 internal, 2 darwin-sandbox.
  15. INFO: Build completed successfully, 7 total actions
  1. $ ./bazel-bin/main/demo
  2. Conan - The C++ Package Manager!