@@ -7,7 +7,9 @@ BazelDeps
7
7
8
8
The ``BazelDeps `` is the dependencies generator for Bazel. Generates a *<REPOSITORY>/BUILD.bazel * file per dependency,
9
9
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).
11
13
12
14
The ``BazelDeps `` generator can be used by name in conanfiles:
13
15
@@ -59,9 +61,12 @@ Every :command:`conan install` generates these files:
59
61
60
62
* *BUILD.bazel *: An empty file aimed to be alongside the *dependencies.bzl * one.
61
63
More information `here <https://bazel.build/concepts/build-files >`__.
62
- * *dependencies.bzl *: this file tells your Bazel *WORKSPACE * how to load the dependencies.
63
64
* *zlib/BUILD.bazel *: contains all the targets that you can load from any of your *BUILD * files. More information in
64
65
: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.
65
70
66
71
Let's check the content of the files created:
67
72
@@ -89,6 +94,45 @@ Given the example above, and imagining that your WORKSPACE is at the same direct
89
94
load_conan_dependencies()
90
95
91
96
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
+
92
136
.. code-block :: python
93
137
:caption: zlib/ BUILD .bazel
94
138
@@ -160,6 +204,8 @@ Running again the :command:`conan install` command, we now get this structure:
160
204
├── conan
161
205
│ ├── BUILD.bazel
162
206
│ ├── dependencies.bzl
207
+ │ ├── conan_deps_module_extension.bzl
208
+ │ ├── conan_deps_repo_rules.bzl
163
209
│ └── zlib
164
210
│ └── BUILD.bazel
165
211
└── conanfile.py
@@ -241,6 +287,8 @@ Running the :command:`conan install` command, the structure created is as follow
241
287
│ ├── BUILD.bazel
242
288
│ ├── build-my_tool
243
289
│ │ └── BUILD.bazel
290
+ │ ├── conan_deps_module_extension.bzl
291
+ │ ├── conan_deps_repo_rules.bzl
244
292
│ └── dependencies.bzl
245
293
└── conanfile.py
246
294
0 commit comments