From ed0cadc1050bb5a7be381721760da850ffc559f9 Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Tue, 4 Jun 2024 16:52:49 +0200 Subject: [PATCH] Updating bazel 7.x docs --- reference/tools/google/bazeldeps.rst | 113 +++++++++++++++++++++------ 1 file changed, 87 insertions(+), 26 deletions(-) diff --git a/reference/tools/google/bazeldeps.rst b/reference/tools/google/bazeldeps.rst index 787471b9ccb7..3faecad2ada5 100644 --- a/reference/tools/google/bazeldeps.rst +++ b/reference/tools/google/bazeldeps.rst @@ -7,9 +7,9 @@ BazelDeps The ``BazelDeps`` is the dependencies generator for Bazel. Generates a */BUILD.bazel* file per dependency, where the */* 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 -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). +that, it also generates Bazel 6.x compatible file like *dependencies.bzl*, and other Bazel >= 7.1 compatible ones +like *conan_deps_module_extension.bzl* and *conan_deps_repo_rules.bzl*. All of them contain the logic to load +all your Conan dependencies through your *WORKSPACE* | *MODULE.bazel*. The ``BazelDeps`` generator can be used by name in conanfiles: @@ -64,13 +64,18 @@ Every :command:`conan install` generates these files: More information `here `__. * *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. +* *dependencies.bzl*: (Bazel 6.x compatible) this file tells your Bazel *WORKSPACE* how to load the dependencies. +* *conan_deps_module_extension.bzl*: (since `Conan 2.4.0 `_)(Bazel >= 7.1 compatible) + This file is used to load each dependency as repository. +* *conan_deps_repo_rules.bzl*: (since `Conan 2.4.0 `_)(Bazel >= 7.1 compatible) + The rule provided by this file is used to create a repository. + It is not intended to be used by consumers but by *conan_deps_module_extension.bzl*. Let's check the content of the files created: +**Bazel 6.x compatible** + + .. code-block:: python :caption: dependencies.bzl @@ -86,20 +91,47 @@ Let's check the content of the files created: build_file="/your/current/working/directory/zlib/BUILD.bazel", ) -Given the example above, and imagining that your WORKSPACE is at the same directory, you would have to add these lines in there: +**Bazel >= 7.1 compatible** .. code-block:: python - :caption: WORKSPACE + :caption: conan_deps_repo_rules.bzl + + # This bazel repository rule is used to load Conan dependencies into the Bazel workspace. + # It's used by a generated module file that provides information about the conan packages. + # Each conan package is loaded into a bazel repository rule, with having the name of the + # package. The whole method is based on symlinks to not copy the whole package into the + # Bazel workspace, which is expensive. + def _conan_dependency_repo(rctx): + package_path = rctx.workspace_root.get_child(rctx.attr.package_path) + + child_packages = package_path.readdir() + for child in child_packages: + rctx.symlink(child, child.basename) + + rctx.symlink(rctx.attr.build_file_path, "BUILD.bazel") + + conan_dependency_repo = repository_rule( + implementation = _conan_dependency_repo, + attrs = { + "package_path": attr.string( + mandatory = True, + doc = "The path to the Conan package in conan cache.", + ), + "build_file_path": attr.string( + mandatory = True, + doc = "The path to the BUILD file.", + ), + }, + ) - load("@//:dependencies.bzl", "load_conan_dependencies") - 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. + # It's generated by the BazelDeps, and should be used in your Module.bazel file. load(":conan_deps_repo_rules.bzl", "conan_dependency_repo") def _load_dependenies_impl(mctx): @@ -110,36 +142,55 @@ Given the example above, and imagining that your WORKSPACE is at the same direct ) 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. + # It will only warn you if any direct + # dependency is not imported by the 'use_repo' or even it is imported + # but not created. Notice that root_module_direct_dev_deps can not be None as we + # are giving 'all' value to root_module_direct_deps. + # Fix the 'use_repo' calls by running 'bazel mod tidy' + root_module_direct_deps = 'all', + root_module_direct_dev_deps = [], + + # 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( + conan_extension = 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: + +Given the examples above, and imagining that your *WORKSPACE* | *MODULE.bazel* is at the same directory, +you would have to add these lines in there: + +**Bazel 6.x compatible** + +.. code-block:: python + :caption: WORKSPACE + + load("@//:dependencies.bzl", "load_conan_dependencies") + load_conan_dependencies() + + +**Bazel >= 7.1 compatible** .. code-block:: python - :caption: Module.bazel + :caption: MODULE.bazel - load_conan_dependencies = use_extension("//:conan_deps_module_extension.bzl", "load_dependencies") + load_conan_dependencies = use_extension("//:conan_deps_module_extension.bzl", "conan_extension") use_repo(load_conan_dependencies, "zlib") +As you can observe, the *zlib/BUILD.bazel* defines these global targets: + .. code-block:: python :caption: zlib/BUILD.bazel - load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library") - # Components precompiled libs # Root package precompiled libs cc_import( @@ -172,7 +223,6 @@ Given the example above, a Bazel version above 7, and imagining that your Module visibility = ["//visibility:public"], ) -As you can observe, the *zlib/BUILD.bazel* defines these global targets: * ``zlib``: bazel library target. The label used to depend on it would be ``@zlib//:zlib``. * ``zlib_binaries``: bazel filegroup target. The label used to depend on it would be ``@zlib//:zlib_binaries``. @@ -213,7 +263,7 @@ Running again the :command:`conan install` command, we now get this structure: └── conanfile.py -Now your Conan-bazel files were generated in the *conan/* folder, so your WORKSPACE will look like: +Now your Conan-bazel files were generated in the *conan/* folder, your WORKSPACE will look like: .. code-block:: python :caption: WORKSPACE @@ -222,6 +272,17 @@ Now your Conan-bazel files were generated in the *conan/* folder, so your WORKSP load_conan_dependencies() +Or your MODULE.bazel: + +.. code-block:: python + :caption: MODULE.bazel + + load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension") + use_repo(load_conan_dependencies, "zlib") + + + + .. _conan_tools_google_bazeldeps_customization: Customization