Skip to content

Commit ed73430

Browse files
Neeeflixfranramirez688czoido
authored
feat: Add bazel 7.1 documentation (#3707)
* feat: Add bazel 7.1 documentation * Typos * Updating bazel 7.x docs * Useless rules * Added example * Updated ref * Update examples/tools/google/bazeltoolchain/build_simple_bazel_7x_project.rst * Fixed --------- Co-authored-by: Francisco Ramirez de Anton <franchuti688@gmail.com> Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
1 parent d8bbe7f commit ed73430

File tree

8 files changed

+272
-11
lines changed

8 files changed

+272
-11
lines changed

examples/tools/google/bazel.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Bazel
88
:maxdepth: 2
99

1010
bazeltoolchain/build_simple_bazel_project
11+
bazeltoolchain/build_simple_bazel_7x_project
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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!

examples/tools/google/bazeltoolchain/build_simple_bazel_project.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Build a simple Bazel project using Conan
44
========================================
55

6+
.. warning::
7+
8+
This example is Bazel 6.x compatible.
9+
610
In this example, we are going to create a Hello World program
711
that uses one of the most popular C++ libraries: `fmt <https://fmt.dev/latest/index.html/>`_.
812

@@ -21,7 +25,7 @@ Please, first clone the sources to recreate this project. You can find them in t
2125
.. code-block:: bash
2226
2327
$ git clone https://github.com/conan-io/examples2.git
24-
$ cd examples2/examples/tools/google/bazeltoolchain/string_formatter
28+
$ cd examples2/examples/tools/google/bazeltoolchain/6_x/string_formatter
2529
2630
2731
We start from a very simple C++ language project with this structure:
@@ -62,8 +66,6 @@ Let's have a look at each file's content:
6266
.. code-block:: python
6367
:caption: **main/BUILD**
6468
65-
load("@rules_cc//cc:defs.bzl", "cc_binary")
66-
6769
cc_binary(
6870
name = "demo",
6971
srcs = ["demo.cpp"],
@@ -105,7 +107,7 @@ in a folder named *conan/* (default folder defined by the ``bazel_layout``).
105107
$ conan install . --build=missing
106108
# ...
107109
======== Finalizing install (deploy, generators) ========
108-
conanfile.txt: Writing generators to /Users/franchuti/develop/examples2/examples/tools/google/bazeltoolchain/string_formatter/conan
110+
conanfile.txt: Writing generators to /Users/user/develop/examples2/examples/tools/google/bazeltoolchain/6_x/string_formatter/conan
109111
conanfile.txt: Generator 'BazelDeps' calling 'generate()'
110112
conanfile.txt: Generator 'BazelToolchain' calling 'generate()'
111113
conanfile.txt: Generating aggregated env files

integrations/bazel.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ imported from ``conan.tools.google``. The most relevant tools are:
2121
- Reference for :ref:`conan_tools_google_bazeltoolchain`.
2222
- Reference for :ref:`conan_tools_google_bazel`.
2323
- :ref:`examples_tools_bazel_toolchain_build_simple_bazel_project`
24+
- :ref:`examples_tools_bazel_7x_toolchain_build_simple_bazel_project`
2425

2526

2627
.. |bazel_logo| image:: ../images/integrations/conan-bazel-logo.png

reference/tools/google/bazel.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ conf
5454
.. seealso::
5555

5656
- :ref:`examples_tools_bazel_toolchain_build_simple_bazel_project`
57+
- :ref:`examples_tools_bazel_7x_toolchain_build_simple_bazel_project`

0 commit comments

Comments
 (0)