1*61c4878aSAndroid Build Coastguard Worker.. _docs-bazel-integration: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker=================================================== 4*61c4878aSAndroid Build Coastguard WorkerUsing a Pigweed module in an existing Bazel project 5*61c4878aSAndroid Build Coastguard Worker=================================================== 6*61c4878aSAndroid Build Coastguard WorkerThis guide explains how to start using a Pigweed module in your existing 7*61c4878aSAndroid Build Coastguard WorkerBazel-based C or C++ project. We'll assume you're familiar with the build 8*61c4878aSAndroid Build Coastguard Workersystem at the level of the `Bazel tutorial <https://bazel.build/start/cpp>`__. 9*61c4878aSAndroid Build Coastguard Worker 10*61c4878aSAndroid Build Coastguard Worker------------------------------------- 11*61c4878aSAndroid Build Coastguard WorkerAdd Pigweed as a WORKSPACE dependency 12*61c4878aSAndroid Build Coastguard Worker------------------------------------- 13*61c4878aSAndroid Build Coastguard WorkerAdd Pigweed as a `git_repository 14*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/rules/lib/repo/git#git_repository>`__ in your 15*61c4878aSAndroid Build Coastguard Worker``WORKSPACE``: 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker git_repository( 20*61c4878aSAndroid Build Coastguard Worker name = "pigweed", 21*61c4878aSAndroid Build Coastguard Worker commit = "c00e9e430addee0c8add16c32eb6d8ab94189b9e", 22*61c4878aSAndroid Build Coastguard Worker remote = "https://pigweed.googlesource.com/pigweed/pigweed.git", 23*61c4878aSAndroid Build Coastguard Worker ) 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker(You can find the latest tip-of-tree commit in the History tab in `CodeSearch 26*61c4878aSAndroid Build Coastguard Worker<https://cs.opensource.google/pigweed/pigweed>`__.) 27*61c4878aSAndroid Build Coastguard Worker 28*61c4878aSAndroid Build Coastguard WorkerIf you manage your dependencies as submodules, you can add Pigweed as a 29*61c4878aSAndroid Build Coastguard Workersubmodule, too, and then add it to the ``WORKSPACE`` as a `local_repository 30*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/reference/be/workspace#local_repository>`__: 31*61c4878aSAndroid Build Coastguard Worker 32*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 33*61c4878aSAndroid Build Coastguard Worker 34*61c4878aSAndroid Build Coastguard Worker local_repository( 35*61c4878aSAndroid Build Coastguard Worker name = "pigweed", 36*61c4878aSAndroid Build Coastguard Worker path = "third_party/pigweed", 37*61c4878aSAndroid Build Coastguard Worker ) 38*61c4878aSAndroid Build Coastguard Worker 39*61c4878aSAndroid Build Coastguard WorkerWe don't yet publish releases that could be pulled in using `http_archive 40*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/rules/lib/repo/http#http_archive>`__. 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard WorkerWe don't support `bzlmod <https://bazel.build/external/overview#bzlmod>`__ yet. 43*61c4878aSAndroid Build Coastguard WorkerSee :bug:`258836641`. 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard WorkerIf either of these limitations is important to you, please reach out to us on 46*61c4878aSAndroid Build Coastguard Worker`chat <https://discord.gg/M9NSeTA>`__. 47*61c4878aSAndroid Build Coastguard Worker 48*61c4878aSAndroid Build Coastguard Worker--------------------------- 49*61c4878aSAndroid Build Coastguard WorkerUse Pigweed in your project 50*61c4878aSAndroid Build Coastguard Worker--------------------------- 51*61c4878aSAndroid Build Coastguard WorkerLet's say you want to use ``pw::Vector`` from :ref:`module-pw_containers`, our 52*61c4878aSAndroid Build Coastguard Workerembedded-friendly replacement for ``std::vector``. 53*61c4878aSAndroid Build Coastguard Worker 54*61c4878aSAndroid Build Coastguard Worker#. Include the header you want in your code: 55*61c4878aSAndroid Build Coastguard Worker 56*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 57*61c4878aSAndroid Build Coastguard Worker 58*61c4878aSAndroid Build Coastguard Worker #include "pw_containers/vector.h" 59*61c4878aSAndroid Build Coastguard Worker 60*61c4878aSAndroid Build Coastguard Worker#. Look at the module's `build file 61*61c4878aSAndroid Build Coastguard Worker <https://cs.opensource.google/pigweed/pigweed/+/main:pw_containers/BUILD.bazel>`__ 62*61c4878aSAndroid Build Coastguard Worker to figure out which build target you need to provide the header and 63*61c4878aSAndroid Build Coastguard Worker implementation. For ``pw_containers/vector.h``, it's 64*61c4878aSAndroid Build Coastguard Worker ``//pw_containers:vector``. 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker#. Add this target to the ``deps`` of your 67*61c4878aSAndroid Build Coastguard Worker `cc_library <https://bazel.build/reference/be/c-cpp#cc_library>`__ or 68*61c4878aSAndroid Build Coastguard Worker `cc_binary <https://bazel.build/reference/be/c-cpp#cc_binary>`__: 69*61c4878aSAndroid Build Coastguard Worker 70*61c4878aSAndroid Build Coastguard Worker .. code-block:: python 71*61c4878aSAndroid Build Coastguard Worker 72*61c4878aSAndroid Build Coastguard Worker cc_library( 73*61c4878aSAndroid Build Coastguard Worker name = "my_library", 74*61c4878aSAndroid Build Coastguard Worker srcs = ["my_library.cc"], 75*61c4878aSAndroid Build Coastguard Worker hdrs = ["my_library.h"], 76*61c4878aSAndroid Build Coastguard Worker deps = [ 77*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_containers:vector", # <-- The new dependency 78*61c4878aSAndroid Build Coastguard Worker ], 79*61c4878aSAndroid Build Coastguard Worker ) 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker#. Add a dependency on ``@pigweed//pw_build:default_link_extra_lib`` to your 82*61c4878aSAndroid Build Coastguard Worker final *binary* target. See :ref:`docs-build_system-bazel_link-extra-lib` 83*61c4878aSAndroid Build Coastguard Worker for a discussion of why this is necessary, and what the alternatives are. 84*61c4878aSAndroid Build Coastguard Worker 85*61c4878aSAndroid Build Coastguard Worker .. code-block:: python 86*61c4878aSAndroid Build Coastguard Worker 87*61c4878aSAndroid Build Coastguard Worker cc_binary( 88*61c4878aSAndroid Build Coastguard Worker name = "my_binary", 89*61c4878aSAndroid Build Coastguard Worker srcs = ["my_binary.cc"], 90*61c4878aSAndroid Build Coastguard Worker deps = [ 91*61c4878aSAndroid Build Coastguard Worker ":my_library", 92*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_build:default_link_extra_lib", # <-- The new dependency 93*61c4878aSAndroid Build Coastguard Worker ], 94*61c4878aSAndroid Build Coastguard Worker ) 95*61c4878aSAndroid Build Coastguard Worker 96*61c4878aSAndroid Build Coastguard Worker---------------------------- 97*61c4878aSAndroid Build Coastguard WorkerSet the required Bazel flags 98*61c4878aSAndroid Build Coastguard Worker---------------------------- 99*61c4878aSAndroid Build Coastguard WorkerPigweed projects need to set certain flags in their ``.bazelrc``. These 100*61c4878aSAndroid Build Coastguard Workergenerally pre-adopt Bazel features that will become default in the future and 101*61c4878aSAndroid Build Coastguard Workerimprove cache performance, disambiguate Python imports, etc. These flags are 102*61c4878aSAndroid Build Coastguard Workerlisted below. Unfortunately there's no way to automatically import them, see 103*61c4878aSAndroid Build Coastguard Worker:bug:`353750350`. 104*61c4878aSAndroid Build Coastguard Worker 105*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: ../../pw_build/pigweed.bazelrc 106*61c4878aSAndroid Build Coastguard Worker 107*61c4878aSAndroid Build Coastguard Worker-------------------------------------------- 108*61c4878aSAndroid Build Coastguard WorkerConfigure backends for facades you depend on 109*61c4878aSAndroid Build Coastguard Worker-------------------------------------------- 110*61c4878aSAndroid Build Coastguard WorkerPigweed makes extensive use of :ref:`docs-facades`, and any module you choose 111*61c4878aSAndroid Build Coastguard Workerto use will likely have a transitive dependency on some facade (typically 112*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_assert` or :ref:`module-pw_log`). Continuing with our example, 113*61c4878aSAndroid Build Coastguard Worker``pw::Vector`` depends on :ref:`module-pw_assert`. 114*61c4878aSAndroid Build Coastguard Worker 115*61c4878aSAndroid Build Coastguard WorkerIn Bazel, facades already have a default backend (implementation) that works 116*61c4878aSAndroid Build Coastguard Workerfor host builds (builds targeting your local development machine). But to build 117*61c4878aSAndroid Build Coastguard Workera binary for your embedded target, you'll need to select a suitable backend 118*61c4878aSAndroid Build Coastguard Workeryourself. 119*61c4878aSAndroid Build Coastguard Worker 120*61c4878aSAndroid Build Coastguard WorkerFortunately, the default backend for :ref:`module-pw_assert` is 121*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_assert_basic`, which is a suitable place to start for most 122*61c4878aSAndroid Build Coastguard Workerembedded targets, too. But it depends on :ref:`module-pw_sys_io`, another 123*61c4878aSAndroid Build Coastguard Workerfacade for which you *will* have to choose a backend yourself. 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard WorkerThe simplest way to do so is to set the corresponding `label flag 126*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/extending/config#label-typed-build-settings>`__ when 127*61c4878aSAndroid Build Coastguard Workerinvoking Bazel. For example, to use the 128*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_sys_io_baremetal_stm32f429` backend for :ref:`module-pw_sys_io` 129*61c4878aSAndroid Build Coastguard Workerprovided in upstream Pigweed: 130*61c4878aSAndroid Build Coastguard Worker 131*61c4878aSAndroid Build Coastguard Worker.. code-block:: console 132*61c4878aSAndroid Build Coastguard Worker 133*61c4878aSAndroid Build Coastguard Worker $ bazel build \ 134*61c4878aSAndroid Build Coastguard Worker --@pigweed//targets/pw_sys_io_backend=@pigweed//pw_sys_io_baremetal_stm32f429 \ 135*61c4878aSAndroid Build Coastguard Worker //path/to/your:target 136*61c4878aSAndroid Build Coastguard Worker 137*61c4878aSAndroid Build Coastguard WorkerYou can also define backends within your own project. (If Pigweed doesn't 138*61c4878aSAndroid Build Coastguard Workerinclude a :ref:`module-pw_sys_io` backend suitable for your embedded platform, 139*61c4878aSAndroid Build Coastguard Workerthat's what you should do now.) See 140*61c4878aSAndroid Build Coastguard Worker:ref:`docs-build_system-bazel_configuration` for a tutorial that dives deeper 141*61c4878aSAndroid Build Coastguard Workerinto facade configuration with Bazel. 142