|
| 1 | +.. _examples_tools_bazel_7x_toolchain_build_simple_bazel_project: |
| 2 | + |
| 3 | +Build a simple Bazel 7.x project using Conan |
| 4 | +============================================ |
| 5 | + |
| 6 | +.. warning:: |
| 7 | + |
| 8 | + This example is Bazel >= 7.1 compatible. |
| 9 | + |
| 10 | +In this example, we are going to create a Hello World program |
| 11 | +that uses one of the most popular C++ libraries: `fmt <https://fmt.dev/latest/index.html/>`_. |
| 12 | + |
| 13 | +.. note:: |
| 14 | + |
| 15 | + This example is based on the :ref:`Build a simple CMake project using Conan<consuming_packages_build_simple_cmake_project>` |
| 16 | + tutorial. So we highly recommend reading it before trying out this one. |
| 17 | + |
| 18 | + |
| 19 | +We'll use Bazel as the build system and helper tool in this case, so you should get it installed |
| 20 | +before going forward with this example. See `how to install Bazel <https://bazel.build/install>`_. |
| 21 | + |
| 22 | +Please, first clone the sources to recreate this project. You can find them in the |
| 23 | +`examples2 repository <https://github.com/conan-io/examples2>`_ in GitHub: |
| 24 | + |
| 25 | +.. code-block:: bash |
| 26 | +
|
| 27 | + $ git clone https://github.com/conan-io/examples2.git |
| 28 | + $ cd examples2/examples/tools/google/bazeltoolchain/7_x/string_formatter |
| 29 | +
|
| 30 | +
|
| 31 | +We start from a very simple C++ language project with this structure: |
| 32 | + |
| 33 | +.. code-block:: text |
| 34 | +
|
| 35 | + . |
| 36 | + ├── MODULE.bazel |
| 37 | + ├── conanfile.txt |
| 38 | + └── main |
| 39 | + ├── BUILD |
| 40 | + └── demo.cpp |
| 41 | +
|
| 42 | +This project contains a *MODULE.bazel* file loading the Conan dependencies (in this case only ``fmt``) |
| 43 | +and a *main/BUILD* file which defines the *demo* bazel target and it's in charge of using ``fmt`` to build a |
| 44 | +simple Hello World program. |
| 45 | + |
| 46 | +Let's have a look at each file's content: |
| 47 | + |
| 48 | +.. code-block:: cpp |
| 49 | + :caption: **main/demo.cpp** |
| 50 | +
|
| 51 | + #include <cstdlib> |
| 52 | + #include <fmt/core.h> |
| 53 | +
|
| 54 | + int main() { |
| 55 | + fmt::print("{} - The C++ Package Manager!\n", "Conan"); |
| 56 | + return EXIT_SUCCESS; |
| 57 | + } |
| 58 | +
|
| 59 | +.. code-block:: python |
| 60 | + :caption: **MODULE.bazel** |
| 61 | +
|
| 62 | + load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension") |
| 63 | + use_repo(load_conan_dependencies, "fmt") |
| 64 | +
|
| 65 | +
|
| 66 | +.. code-block:: python |
| 67 | + :caption: **main/BUILD** |
| 68 | +
|
| 69 | + cc_binary( |
| 70 | + name = "demo", |
| 71 | + srcs = ["demo.cpp"], |
| 72 | + deps = [ |
| 73 | + "@fmt//:fmt" |
| 74 | + ], |
| 75 | + ) |
| 76 | +
|
| 77 | +
|
| 78 | +.. code-block:: ini |
| 79 | + :caption: **conanfile.txt** |
| 80 | +
|
| 81 | + [requires] |
| 82 | + fmt/10.1.1 |
| 83 | +
|
| 84 | + [generators] |
| 85 | + BazelDeps |
| 86 | + BazelToolchain |
| 87 | +
|
| 88 | + [layout] |
| 89 | + bazel_layout |
| 90 | +
|
| 91 | +
|
| 92 | +Conan uses the :ref:`conan_tools_google_bazeltoolchain` to generate a ``conan_bzl.rc`` file which defines the |
| 93 | +``conan-config`` bazel-build configuration. This file and the configuration are passed as parameters to the |
| 94 | +``bazel build`` command. Apart from that, Conan uses the :ref:`conan_tools_google_bazeldeps` generator |
| 95 | +to create all the bazel files (*[DEP]/BUILD.bazel*, *conan_deps_module_extension.bzl* and |
| 96 | +*conan_deps_repo_rules.bzl*) which define the rule and all the dependencies to create/load them as Bazel repositories. |
| 97 | +The *MODULE.bazel* above is ready to load the *conan_deps_module_extension.bzl* file which will tell the |
| 98 | +*main/BUILD* all the information about the ``@fmt//:fmt`` bazel target. |
| 99 | + |
| 100 | +As the first step, we should install all the dependencies listed in the ``conanfile.txt``. |
| 101 | +The command :ref:`conan install<reference_commands_install>` does not only install the ``fmt`` package, |
| 102 | +it also builds it from sources in case your profile does not match with a pre-built binary in your remotes. |
| 103 | +Furthermore, it will save all the files created by the generators listed in the ``conanfile.txt`` |
| 104 | +in a folder named *conan/* (default folder defined by the ``bazel_layout``). |
| 105 | + |
| 106 | +.. code-block:: bash |
| 107 | +
|
| 108 | + $ conan install . --build=missing |
| 109 | + # ... |
| 110 | + ======== Finalizing install (deploy, generators) ======== |
| 111 | + conanfile.txt: Writing generators to /Users/user/develop/examples2/examples/tools/google/bazeltoolchain/7_x/string_formatter/conan |
| 112 | + conanfile.txt: Generator 'BazelDeps' calling 'generate()' |
| 113 | + conanfile.txt: Generator 'BazelToolchain' calling 'generate()' |
| 114 | + conanfile.txt: Generating aggregated env files |
| 115 | + conanfile.txt: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh'] |
| 116 | + Install finished successfully |
| 117 | +
|
| 118 | +Now we are ready to build and run our application: |
| 119 | + |
| 120 | +.. code-block:: bash |
| 121 | +
|
| 122 | + $ bazel --bazelrc=./conan/conan_bzl.rc build --config=conan-config //main:demo |
| 123 | + Computing main repo mapping: |
| 124 | + Loading: |
| 125 | + Loading: 0 packages loaded |
| 126 | + Analyzing: target //main:demo (1 packages loaded, 0 targets configured) |
| 127 | + Analyzing: target //main:demo (1 packages loaded, 0 targets configured) |
| 128 | + [0 / 1] [Prepa] BazelWorkspaceStatusAction stable-status.txt |
| 129 | + INFO: Analyzed target //main:demo (69 packages loaded, 369 targets configured). |
| 130 | + [5 / 7] Compiling main/demo.cpp; 0s darwin-sandbox |
| 131 | + INFO: Found 1 target... |
| 132 | + Target //main:demo up-to-date: |
| 133 | + bazel-bin/main/demo |
| 134 | + INFO: Elapsed time: 2.955s, Critical Path: 1.70s |
| 135 | + INFO: 7 processes: 5 internal, 2 darwin-sandbox. |
| 136 | + INFO: Build completed successfully, 7 total actions |
| 137 | +
|
| 138 | +
|
| 139 | +.. code-block:: bash |
| 140 | +
|
| 141 | + $ ./bazel-bin/main/demo |
| 142 | + Conan - The C++ Package Manager! |
0 commit comments