Skip to content

Commit ecc3147

Browse files
author
Wendel Johannes (XC-CP/PJ-ADC-ECI)
committed
feat: Add bazel 7.1 documentation
1 parent 4948b8d commit ecc3147

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

reference/tools/google/bazeldeps.rst

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ BazelDeps
77

88
The ``BazelDeps`` is the dependencies generator for Bazel. Generates a *<REPOSITORY>/BUILD.bazel* file per dependency,
99
where the *<REPOSITORY>/* folder is the Conan recipe reference name by default, e.g., *mypkg/BUILD.bazel*. Apart from
10-
that, it also generates a *dependencies.bzl* file which contains a Bazel function to load all your Conan dependencies.
10+
that, it also generates a *dependencies.bzl* file which contains a Bazel function to load all your Conan dependencies
11+
when using Bazel < 7 and the files conan_deps_module_extension.bzl and conan_deps_repo_rules.bzl for the usage with
12+
Bazel > 7.1 (7.0 is not supported right now).
1113

1214
The ``BazelDeps`` generator can be used by name in conanfiles:
1315

@@ -59,9 +61,12 @@ Every :command:`conan install` generates these files:
5961

6062
* *BUILD.bazel*: An empty file aimed to be alongside the *dependencies.bzl* one.
6163
More information `here <https://bazel.build/concepts/build-files>`__.
62-
* *dependencies.bzl*: this file tells your Bazel *WORKSPACE* how to load the dependencies.
6364
* *zlib/BUILD.bazel*: contains all the targets that you can load from any of your *BUILD* files. More information in
6465
:ref:`conan_tools_google_bazeldeps_customization`.
66+
* *dependencies.bzl*: this file tells your Bazel *WORKSPACE* how to load the dependencies.
67+
* *conan_deps_module_extension.bzl*: this file is used to load each dependency as repository, in Bazel 7.1 and above.
68+
* *conan_deps_repo_rules.bzl*: The rule provided by this file is used to create a repository, in Bazel 7.1 and above.
69+
It is not intended to be used by consumer, but by the conan_deps_module_extension.bzl.
6570

6671
Let's check the content of the files created:
6772

@@ -89,6 +94,45 @@ Given the example above, and imagining that your WORKSPACE is at the same direct
8994
load_conan_dependencies()
9095
9196
97+
.. code-block:: python
98+
:caption conan_deps_module_extension.bzl
99+
100+
# This module provides a repo for each requires-dependency in your conanfile.
101+
# It's generated by the ConanDepsGenerator, and should be used in your Module.bazel file.
102+
load(":conan_deps_repo_rules.bzl", "conan_dependency_repo")
103+
104+
def _load_dependenies_impl(mctx):
105+
conan_dependency_repo(
106+
name = "zlib",
107+
package_path = "/path/to/conan/package/folder/",
108+
build_file_path = "/your/current/working/directory/zlib/BUILD.bazel",
109+
)
110+
111+
return mctx.extension_metadata(
112+
root_module_direct_deps = None,
113+
root_module_direct_dev_deps = None,
114+
115+
# Prevent writing function content to lockfiles https://bazel.build/rules/lib/builtins/module_ctx#extension_metadata
116+
# Important for remote build. Actually it's not reproducible, as local paths will be different on different machines.
117+
# But we assume that conan works correctly here.
118+
# IMPORTANT: Not compatible with bazel < 7.1
119+
reproducible = True,
120+
)
121+
122+
load_dependencies = module_extension(
123+
implementation = _load_dependenies_impl,
124+
os_dependent = True,
125+
arch_dependent = True,
126+
)
127+
128+
Given the example above, a Bazel version above 7, and imagining that your Module.bazel is at the same directory, you would have to add these lines in there:
129+
130+
.. code-block:: python
131+
:caption: Module.bazel
132+
133+
load_conan_dependencies = use_extension("//:conan_deps_module_extension.bzl", "load_dependencies")
134+
use_repo(load_conan_dependencies, "zlib")
135+
92136
.. code-block:: python
93137
:caption: zlib/BUILD.bazel
94138
@@ -160,6 +204,8 @@ Running again the :command:`conan install` command, we now get this structure:
160204
├── conan
161205
│ ├── BUILD.bazel
162206
│ ├── dependencies.bzl
207+
│ ├── conan_deps_module_extension.bzl
208+
│ ├── conan_deps_repo_rules.bzl
163209
│ └── zlib
164210
│ └── BUILD.bazel
165211
└── conanfile.py
@@ -241,6 +287,8 @@ Running the :command:`conan install` command, the structure created is as follow
241287
│ ├── BUILD.bazel
242288
│ ├── build-my_tool
243289
│ │ └── BUILD.bazel
290+
│ ├── conan_deps_module_extension.bzl
291+
│ ├── conan_deps_repo_rules.bzl
244292
│ └── dependencies.bzl
245293
└── conanfile.py
246294

0 commit comments

Comments
 (0)