Skip to content

Commit

Permalink
feat: Add bazel 7.1 documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendel Johannes (XC-CP/PJ-ADC-ECI) committed May 3, 2024
1 parent 4948b8d commit ecc3147
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions reference/tools/google/bazeldeps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ BazelDeps

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

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

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

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

Let's check the content of the files created:

Expand Down Expand Up @@ -89,6 +94,45 @@ Given the example above, and imagining that your WORKSPACE is at the same direct
load_conan_dependencies()
.. code-block:: python
:caption conan_deps_module_extension.bzl
# This module provides a repo for each requires-dependency in your conanfile.
# It's generated by the ConanDepsGenerator, and should be used in your Module.bazel file.
load(":conan_deps_repo_rules.bzl", "conan_dependency_repo")
def _load_dependenies_impl(mctx):
conan_dependency_repo(
name = "zlib",
package_path = "/path/to/conan/package/folder/",
build_file_path = "/your/current/working/directory/zlib/BUILD.bazel",
)
return mctx.extension_metadata(
root_module_direct_deps = None,
root_module_direct_dev_deps = None,
# Prevent writing function content to lockfiles https://bazel.build/rules/lib/builtins/module_ctx#extension_metadata
# Important for remote build. Actually it's not reproducible, as local paths will be different on different machines.
# But we assume that conan works correctly here.
# IMPORTANT: Not compatible with bazel < 7.1
reproducible = True,
)
load_dependencies = module_extension(
implementation = _load_dependenies_impl,
os_dependent = True,
arch_dependent = True,
)
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:

.. code-block:: python
:caption: Module.bazel
load_conan_dependencies = use_extension("//:conan_deps_module_extension.bzl", "load_dependencies")
use_repo(load_conan_dependencies, "zlib")
.. code-block:: python
:caption: zlib/BUILD.bazel
Expand Down Expand Up @@ -160,6 +204,8 @@ Running again the :command:`conan install` command, we now get this structure:
├── conan
│ ├── BUILD.bazel
│ ├── dependencies.bzl
│ ├── conan_deps_module_extension.bzl
│ ├── conan_deps_repo_rules.bzl
│ └── zlib
│ └── BUILD.bazel
└── conanfile.py
Expand Down Expand Up @@ -241,6 +287,8 @@ Running the :command:`conan install` command, the structure created is as follow
│ ├── BUILD.bazel
│ ├── build-my_tool
│ │ └── BUILD.bazel
│ ├── conan_deps_module_extension.bzl
│ ├── conan_deps_repo_rules.bzl
│ └── dependencies.bzl
└── conanfile.py
Expand Down

0 comments on commit ecc3147

Please sign in to comment.