Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel + BazelToolchain + BazelDeps #3457

Merged
merged 10 commits into from
Nov 8, 2023
2 changes: 1 addition & 1 deletion reference/conanfile/tools/gnu/pkgconfigdeps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ By default, the ``*.pc`` files will be named following these rules:
* For packages, it uses the package name, e.g., package ``zlib/1.2.11`` -> ``zlib.pc``.
* For components, the package name + hyphen + component name, e.g., ``openssl/3.0.0`` with ``self.cpp_info.components["crypto"]`` -> ``openssl-crypto.pc``.

You can change that default behavior with the ``pkg_config_name`` and ``pkg_config_aliases`` properties. For instance, ``openssl/3.0.0``` recipe has these ``pkg_config_name`` properties already declared:
You can change that default behavior with the ``pkg_config_name`` and ``pkg_config_aliases`` properties. For instance, ``openssl/3.0.0`` recipe has these ``pkg_config_name`` properties already declared:

.. code:: python

Expand Down
136 changes: 5 additions & 131 deletions reference/conanfile/tools/google.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,135 +9,9 @@ conan.tools.google
some breaking changes might still happen if necessary to prepare for the *Conan 2.0 release*.


BazelDeps
---------
.. toctree::
:maxdepth: 2

Available since: `1.37.0 <https://github.com/conan-io/conan/releases/tag/1.37.0>`_

The ``BazelDeps`` helper will generate one **conandeps/xxxx/BUILD** file per dependency. This dependencies will be
automatically added to the project if you add the following lines to the project's **WORKSPACE** file:


.. code-block:: text

load("@//conandeps:dependencies.bzl", "load_conan_dependencies")
load_conan_dependencies()


The dependencies should be added to the **conanfile.py** file as usual:

.. code-block:: python

class BazelExampleConan(ConanFile):
name = "bazel-example"
...
generators = "BazelDeps", "BazelToolchain"
requires = "boost/1.76.0"


BazelToolchain
--------------

Available since: `1.37.0 <https://github.com/conan-io/conan/releases/tag/1.37.0>`_

The ``BazelToolchain`` is the toolchain generator for Bazel. It will generate a file called
``conanbuild.conf`` containing two keys:

- **bazelrc_path**: defining Bazel rc-path. Can be set using the conf ``tools.google.bazel:bazelrc_path``.
- **bazel_configs**: defining the configs to be activated in the ``bazelrc_path``.
Can be set with the conf ``tools.google.bazel:configs``.

.. code-block:: text
:caption: **Example of a custom bazelrc file at '/path/to/mybazelrc':**

build:Release -c opt
build:RelWithDebInfo -c opt --copt=-O3 --copt=-DNDEBUG
build:MinSizeRel -c opt --copt=-Os --copt=-DNDEBUG
build --color=yes
build:withTimeStamps --show_timestamps

.. code-block:: ini
:caption: **Example of a Release profile:**

[settings]
...
build_type=Release

[conf]
tools.google.bazel:bazelrc_path=/path/to/mybazelrc
tools.google.bazel:configs=["Release"]


The Bazel build helper will use that ``conanbuild.conf`` file to seamlessly call
the configure and make script using these precalculated arguments. Note that the file can have a
different name if you set the namespace argument in the constructor as explained below.

It supports the following methods and attributes:

constructor
+++++++++++

.. code:: python

def __init__(self, conanfile, namespace=None):

- ``conanfile``: the current recipe object. Always use ``self``.
- ``namespace``: this argument avoids collisions when you have multiple toolchain calls in the same
recipe. By setting this argument, the *conanbuild.conf* file used to pass information to the
build helper will be named as: *<namespace>_conanbuild.conf*. The default value is ``None`` meaning that
the name of the generated file is *conanbuild.conf*. This namespace must be also set with the same
value in the constructor of the ``Bazel`` build helper so that it reads the information from the proper
file.


Bazel
-----

Available since: `1.37.0 <https://github.com/conan-io/conan/releases/tag/1.37.0>`_

The ``Bazel`` build helper is a wrapper around the command line invocation of bazel. It will abstract the
calls like ``bazel build //main:hello-world`` into Python method calls.

The helper is intended to be used in the ``build()`` method, to call Bazel commands automatically
when a package is being built directly by Conan (create, install)


.. code-block:: python

from conan import ConanFile
from conan.tools.google import Bazel

class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"

def build(self):
bazel = Bazel(self)
bazel.configure()
bazel.build(label="//main:hello-world")

It supports the following methods:

constructor
+++++++++++

.. code:: python

def __init__(self, conanfile, namespace=None):

- ``conanfile``: the current recipe object. Always use ``self``.
- ``namespace``: this argument avoids collisions when you have multiple toolchain calls in the same
recipe. By setting this argument, the *conanbuild.conf* file used to pass information to the
toolchain will be named as: *<namespace>_conanbuild.conf*. The default value is ``None`` meaning that
the name of the generated file is *conanbuild.conf*. This namespace must be also set with the same
value in the constructor of ``BazelToolchain`` so that it reads the information from the proper file.


build()
+++++++

.. code:: python

def build(self, args=None, label=None):


Calls the build system. Equivalent to :command:`bazel build {label}` in the build folder.
google/bazeldeps
google/bazeltoolchain
google/bazel
94 changes: 94 additions & 0 deletions reference/conanfile/tools/google/bazel.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
.. _conan_tools_google_bazel:


Bazel
-----

Available since: `1.37.0 <https://github.com/conan-io/conan/releases/tag/1.37.0>`_

The ``Bazel`` build helper is a wrapper around the command line invocation of bazel. It will abstract the
calls like ``bazel <rcpaths> build <configs> <targets>`` into Python method calls.

The helper is intended to be used in the *conanfile.py* ``build()`` method, to call Bazel commands automatically
when a package is being built directly by Conan (create, install)


.. code-block:: python

from conan import ConanFile
from conan.tools.google import Bazel

class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"

def build(self):
bz = Bazel(self)
bz.build(target="//main:hello-world")


It supports the following methods:

constructor
+++++++++++

.. code:: python

def __init__(self, conanfile, namespace=None):

- ``conanfile``: the current recipe object. Always use ``self``.
- ``namespace``: Deprecated since Conan 1.62. It only keeps backward compatibility.


build()
+++++++

.. code-block:: python

def build(self, args=None, label=None, target="//...", clean=True):


Equivalent to run the :command:`bazel <rcpaths> build <configs> <args> <targets>` command in the build folder.

The parameters are:

* ``args`` (defaulted to ``None``): List of extra arguments to add to the Bazel build command.
* ``label`` (defaulted to ``None``): Deprecated since Conan 1.62, superseded by ``target`` one. The Bazel target name.
* ``target`` (defaulted to ``//...``): The Bazel target name. By default, it runs all the targets under your WORKSPACE.
* ``clean`` (defaulted to ``True``): Runs a :command:`bazel clean` command before running every :command:`bazel build`.
It helps to keep your Bazel cache up to date.

test()
+++++++

Available since: `1.62.0 <https://github.com/conan-io/conan/releases/tag/1.62.0>`_

.. code-block:: python

def test(self, target=None):


Equivalent to :command:`bazel test <target>` in the build folder.

The parameters are:

* ``args`` (defaulted to ``None``): List of extra arguments to add to the Bazel build command.
* ``target`` (defaulted to ``//...``): The Bazel target name. By default, it runs all the targets under your WORKSPACE.


Properties
++++++++++

Available since: `1.62.0 <https://github.com/conan-io/conan/releases/tag/1.62.0>`_

The following properties affect the ``BazelDeps`` generator:

- ``tools.build:skip_test=<bool>`` (boolean) if ``True``, it runs the ``bazel test <target>``.


conf
+++++

``BazelToolchain`` is affected by these :ref:`[conf]<global_conf>` variables:

- ``tools.google.bazel:bazelrc_path``: List of paths to other bazelrc files to be used as :command:`bazel --bazelrc=rcpath1 ... build`.
- ``tools.google.bazel:configs``: List of Bazel configurations to be used as :command:`bazel build --config=config1 ...`.
Loading