1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-gn: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard WorkerGN / Ninja 4*61c4878aSAndroid Build Coastguard Worker========== 5*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage:: 6*61c4878aSAndroid Build Coastguard Worker :name: pw_build 7*61c4878aSAndroid Build Coastguard Worker 8*61c4878aSAndroid Build Coastguard WorkerThe GN / Ninja build system is the primary build system used for upstream 9*61c4878aSAndroid Build Coastguard WorkerPigweed development, and is the most tested and feature-rich build system 10*61c4878aSAndroid Build Coastguard WorkerPigweed offers. 11*61c4878aSAndroid Build Coastguard Worker 12*61c4878aSAndroid Build Coastguard WorkerThis module's ``build.gn`` file contains a number of C/C++ ``config`` 13*61c4878aSAndroid Build Coastguard Workerdeclarations that are used by upstream Pigweed to set some architecture-agnostic 14*61c4878aSAndroid Build Coastguard Workercompiler defaults. (See Pigweed's ``//BUILDCONFIG.gn``) 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Worker``pw_build`` also provides several useful GN templates that are used throughout 17*61c4878aSAndroid Build Coastguard WorkerPigweed. 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard WorkerBuilding upstream Pigweed 20*61c4878aSAndroid Build Coastguard Worker------------------------- 21*61c4878aSAndroid Build Coastguard WorkerSee Pigweed's :ref:`docs-get-started-upstream` guide for a high-level introduction 22*61c4878aSAndroid Build Coastguard Workerto using the GN build. 23*61c4878aSAndroid Build Coastguard Worker 24*61c4878aSAndroid Build Coastguard WorkerPigweed's root ``BUILD.gn`` file contains a variety of groups to help you 25*61c4878aSAndroid Build Coastguard Workercontrol what parts of the project you'd like to build. 26*61c4878aSAndroid Build Coastguard Worker 27*61c4878aSAndroid Build Coastguard Worker* ``default``: Same as just calling ``ninja -C out``. Builds Pigweed's 28*61c4878aSAndroid Build Coastguard Worker documentation, recommended tests, and python linting, and static analysis. 29*61c4878aSAndroid Build Coastguard Worker* ``extended_default``: Everything in ``default``, plus some other useful 30*61c4878aSAndroid Build Coastguard Worker configurations that are tested in CQ. 31*61c4878aSAndroid Build Coastguard Worker* ``all``: Attempts to build everything in Pigweed. Note that ``pw package`` may 32*61c4878aSAndroid Build Coastguard Worker need to be used to enable some branches of the build. 33*61c4878aSAndroid Build Coastguard Worker* ``docs``: Only build Pigweed's documentation. 34*61c4878aSAndroid Build Coastguard Worker* ``stm32f429i``: Only build for the STMicroelectronics STM32F429I-DISC1 board. 35*61c4878aSAndroid Build Coastguard Worker* ``host``: Only build for the host. 36*61c4878aSAndroid Build Coastguard Worker 37*61c4878aSAndroid Build Coastguard WorkerThere are a variety of other groups in the root ``BUILD.gn`` file that may be 38*61c4878aSAndroid Build Coastguard Workerhelpful for covering more areas of the build, or for reducing iteration time 39*61c4878aSAndroid Build Coastguard Workerby only building a subset of the default build. 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard WorkerSome currently broken groups are gated behind the ``pw_BUILD_BROKEN_GROUPS`` 42*61c4878aSAndroid Build Coastguard Workerbuild argument. You can set this to ``true`` using ``gn args out`` to try to 43*61c4878aSAndroid Build Coastguard Workerbuild and debug known broken build configurations. 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard WorkerBuild system philosophies 46*61c4878aSAndroid Build Coastguard Worker------------------------- 47*61c4878aSAndroid Build Coastguard WorkerWhile Pigweed's GN build is not hermetic, it strives to adhere to principles of 48*61c4878aSAndroid Build Coastguard Worker`hermeticity <https://bazel.build/concepts/hermeticity>`_. Some guidelines to 49*61c4878aSAndroid Build Coastguard Workermove towards the ideal of hermeticity include: 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker* Only rely on pre-compiled tools provided by CIPD (or some other versioned, 52*61c4878aSAndroid Build Coastguard Worker pre-compiled binary distribution mechanism). This eliminates build artifact 53*61c4878aSAndroid Build Coastguard Worker differences caused by different tool versions or variations (e.g. same tool 54*61c4878aSAndroid Build Coastguard Worker version built with slightly different compilation flags). 55*61c4878aSAndroid Build Coastguard Worker* Do not use absolute paths in Ninja commands. Typically, these appear when 56*61c4878aSAndroid Build Coastguard Worker using ``rebase_path("//path/to/my_script.py")``. Most of the time, Ninja 57*61c4878aSAndroid Build Coastguard Worker steps should be passed paths rebased relative to the build directory (i.e. 58*61c4878aSAndroid Build Coastguard Worker ``rebase_path("//path/to/my_script.py", root_build_dir)``). This ensures build 59*61c4878aSAndroid Build Coastguard Worker commands are the same across different machines. 60*61c4878aSAndroid Build Coastguard Worker* Prevent produced artifacts from relying on or referencing system state. This 61*61c4878aSAndroid Build Coastguard Worker includes time stamps, writing absolute paths to generated artifacts, or 62*61c4878aSAndroid Build Coastguard Worker producing artifacts that reference system state in a way that prevents them 63*61c4878aSAndroid Build Coastguard Worker from working the same way on a different machine. 64*61c4878aSAndroid Build Coastguard Worker* Isolate build actions to the build directory. In general, the build system 65*61c4878aSAndroid Build Coastguard Worker should not add or modify files outside of the build directory. This can cause 66*61c4878aSAndroid Build Coastguard Worker confusion to users, and makes the concept of a clean build more ambiguous. 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard WorkerTarget types 69*61c4878aSAndroid Build Coastguard Worker------------ 70*61c4878aSAndroid Build Coastguard Worker.. code-block:: 71*61c4878aSAndroid Build Coastguard Worker 72*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/target_types.gni") 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker pw_source_set("my_library") { 75*61c4878aSAndroid Build Coastguard Worker sources = [ "lib.cc" ] 76*61c4878aSAndroid Build Coastguard Worker } 77*61c4878aSAndroid Build Coastguard Worker 78*61c4878aSAndroid Build Coastguard WorkerPigweed defines wrappers around the four basic GN binary types ``source_set``, 79*61c4878aSAndroid Build Coastguard Worker``executable``, ``static_library``, and ``shared_library``. These templates 80*61c4878aSAndroid Build Coastguard Workerdo several things: 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker#. **Add default configs/deps** 83*61c4878aSAndroid Build Coastguard Worker 84*61c4878aSAndroid Build Coastguard Worker Rather than binding the majority of compiler flags related to C++ standard, 85*61c4878aSAndroid Build Coastguard Worker cross-compilation, warning/error policy, etc. directly to toolchain 86*61c4878aSAndroid Build Coastguard Worker invocations, these flags are applied as configs to all ``pw_*`` C/C++ target 87*61c4878aSAndroid Build Coastguard Worker types. The primary motivations for this are to allow some targets to modify 88*61c4878aSAndroid Build Coastguard Worker the default set of flags when needed by specifying ``remove_configs``, and to 89*61c4878aSAndroid Build Coastguard Worker reduce the complexity of building novel toolchains. 90*61c4878aSAndroid Build Coastguard Worker 91*61c4878aSAndroid Build Coastguard Worker Pigweed's global default configs are set in ``pw_build/default.gni``, and 92*61c4878aSAndroid Build Coastguard Worker individual platform-specific toolchains extend the list by appending to the 93*61c4878aSAndroid Build Coastguard Worker ``default_configs`` build argument. 94*61c4878aSAndroid Build Coastguard Worker 95*61c4878aSAndroid Build Coastguard Worker Default deps were added to support polyfill, which has since been deprecated. 96*61c4878aSAndroid Build Coastguard Worker Default dependency functionality continues to exist for backwards 97*61c4878aSAndroid Build Coastguard Worker compatibility. 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard Worker#. **Optionally add link-time binding** 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Worker Some libraries like pw_assert and pw_log are borderline impossible to 102*61c4878aSAndroid Build Coastguard Worker implement well without introducing circular dependencies. One solution for 103*61c4878aSAndroid Build Coastguard Worker addressing this is to break apart the libraries into an interface with 104*61c4878aSAndroid Build Coastguard Worker minimal dependencies, and an implementation with the bulk of the 105*61c4878aSAndroid Build Coastguard Worker dependencies that would typically create dependency cycles. In order for the 106*61c4878aSAndroid Build Coastguard Worker implementation to be linked in, it must be added to the dependency tree of 107*61c4878aSAndroid Build Coastguard Worker linked artifacts (e.g. ``pw_executable``, ``pw_static_library``). Since 108*61c4878aSAndroid Build Coastguard Worker there's no way for the libraries themselves to just happily pull in the 109*61c4878aSAndroid Build Coastguard Worker implementation if someone depends on the interface, the implementation is 110*61c4878aSAndroid Build Coastguard Worker instead late-bound by adding it as a direct dependency of the final linked 111*61c4878aSAndroid Build Coastguard Worker artifact. This is all managed through ``pw_build_LINK_DEPS``, which is global 112*61c4878aSAndroid Build Coastguard Worker for each toolchain and applied to every ``pw_executable``, 113*61c4878aSAndroid Build Coastguard Worker ``pw_static_library``, and ``pw_shared_library``. 114*61c4878aSAndroid Build Coastguard Worker 115*61c4878aSAndroid Build Coastguard Worker#. **Apply a default visibility policy** 116*61c4878aSAndroid Build Coastguard Worker 117*61c4878aSAndroid Build Coastguard Worker Projects can globally control the default visibility of pw_* target types by 118*61c4878aSAndroid Build Coastguard Worker specifying ``pw_build_DEFAULT_VISIBILITY``. This template applies that as the 119*61c4878aSAndroid Build Coastguard Worker default visibility for any pw_* targets that do not explicitly specify a 120*61c4878aSAndroid Build Coastguard Worker visibility. 121*61c4878aSAndroid Build Coastguard Worker 122*61c4878aSAndroid Build Coastguard Worker#. **Add source file names as metadata** 123*61c4878aSAndroid Build Coastguard Worker 124*61c4878aSAndroid Build Coastguard Worker All source file names are collected as 125*61c4878aSAndroid Build Coastguard Worker `GN metadata <https://gn.googlesource.com/gn/+/main/docs/reference.md#metadata_collection>`_. 126*61c4878aSAndroid Build Coastguard Worker This list can be writen to a file at build time using ``generated_file``. The 127*61c4878aSAndroid Build Coastguard Worker primary use case for this is to generate a token database containing all the 128*61c4878aSAndroid Build Coastguard Worker source files. This allows :c:macro:`PW_ASSERT` to emit filename tokens even 129*61c4878aSAndroid Build Coastguard Worker though it can't add them to the elf file because of the reasons described at 130*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_assert-assert-api`. 131*61c4878aSAndroid Build Coastguard Worker 132*61c4878aSAndroid Build Coastguard Worker .. note:: 133*61c4878aSAndroid Build Coastguard Worker ``pw_source_files``, if not rebased will default to outputing module 134*61c4878aSAndroid Build Coastguard Worker relative paths from a ``generated_file`` target. This is likely not 135*61c4878aSAndroid Build Coastguard Worker useful. Adding a ``rebase`` argument to ``generated_file`` such as 136*61c4878aSAndroid Build Coastguard Worker ``rebase = root_build_dir`` will result in usable paths. For an example, 137*61c4878aSAndroid Build Coastguard Worker see ``//pw_tokenizer/database.gni``'s ``pw_tokenizer_filename_database`` 138*61c4878aSAndroid Build Coastguard Worker template. 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard WorkerThe ``pw_executable`` template provides additional functionality around building 141*61c4878aSAndroid Build Coastguard Workercomplete binaries. As Pigweed is a collection of libraries, it does not know how 142*61c4878aSAndroid Build Coastguard Workerits final targets are built. ``pw_executable`` solves this by letting each user 143*61c4878aSAndroid Build Coastguard Workerof Pigweed specify a global executable template for their target, and have 144*61c4878aSAndroid Build Coastguard WorkerPigweed build against it. This is controlled by the build variable 145*61c4878aSAndroid Build Coastguard Worker``pw_executable_config.target_type``, specifying the name of the executable 146*61c4878aSAndroid Build Coastguard Workertemplate for a project. 147*61c4878aSAndroid Build Coastguard Worker 148*61c4878aSAndroid Build Coastguard WorkerIn some uncommon cases, a project's ``pw_executable`` template definition may 149*61c4878aSAndroid Build Coastguard Workerneed to stamp out some ``pw_source_set``\s. Since a pw_executable template can't 150*61c4878aSAndroid Build Coastguard Workerimport ``$dir_pw_build/target_types.gni`` due to circular imports, it should 151*61c4878aSAndroid Build Coastguard Workerimport ``$dir_pw_build/cc_library.gni`` instead. 152*61c4878aSAndroid Build Coastguard Worker 153*61c4878aSAndroid Build Coastguard Worker.. tip:: 154*61c4878aSAndroid Build Coastguard Worker 155*61c4878aSAndroid Build Coastguard Worker Prefer to use ``pw_executable`` over plain ``executable`` targets to allow 156*61c4878aSAndroid Build Coastguard Worker cleanly building the same code for multiple target configs. 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard WorkerArguments 159*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 160*61c4878aSAndroid Build Coastguard WorkerAll of the ``pw_*`` target type overrides accept any arguments supported by 161*61c4878aSAndroid Build Coastguard Workerthe underlying native types, as they simply forward them through to the 162*61c4878aSAndroid Build Coastguard Workerunderlying target. 163*61c4878aSAndroid Build Coastguard Worker 164*61c4878aSAndroid Build Coastguard WorkerAdditionally, the following arguments are also supported: 165*61c4878aSAndroid Build Coastguard Worker 166*61c4878aSAndroid Build Coastguard Worker* **remove_configs**: (optional) A list of configs / config patterns to remove 167*61c4878aSAndroid Build Coastguard Worker from the set of default configs specified by the current toolchain 168*61c4878aSAndroid Build Coastguard Worker configuration. 169*61c4878aSAndroid Build Coastguard Worker* **remove_public_deps**: (optional) A list of targets to remove from the set of 170*61c4878aSAndroid Build Coastguard Worker default public_deps specified by the current toolchain configuration. 171*61c4878aSAndroid Build Coastguard Worker 172*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-link-deps: 173*61c4878aSAndroid Build Coastguard Worker 174*61c4878aSAndroid Build Coastguard WorkerLink-only deps 175*61c4878aSAndroid Build Coastguard Worker-------------- 176*61c4878aSAndroid Build Coastguard WorkerIt may be necessary to specify additional link-time dependencies that may not be 177*61c4878aSAndroid Build Coastguard Workerexplicitly depended on elsewhere in the build. One example of this is a 178*61c4878aSAndroid Build Coastguard Worker``pw_assert`` backend, which may need to leave out dependencies to avoid 179*61c4878aSAndroid Build Coastguard Workercircular dependencies. Its dependencies need to be linked for executables and 180*61c4878aSAndroid Build Coastguard Workerlibraries, even if they aren't pulled in elsewhere. 181*61c4878aSAndroid Build Coastguard Worker 182*61c4878aSAndroid Build Coastguard WorkerThe ``pw_build_LINK_DEPS`` build arg is a list of dependencies to add to all 183*61c4878aSAndroid Build Coastguard Worker``pw_executable``, ``pw_static_library``, and ``pw_shared_library`` targets. 184*61c4878aSAndroid Build Coastguard WorkerThis should only be used as a last resort when dependencies cannot be properly 185*61c4878aSAndroid Build Coastguard Workerexpressed in the build. 186*61c4878aSAndroid Build Coastguard Worker 187*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-third-party: 188*61c4878aSAndroid Build Coastguard Worker 189*61c4878aSAndroid Build Coastguard WorkerThird party libraries 190*61c4878aSAndroid Build Coastguard Worker--------------------- 191*61c4878aSAndroid Build Coastguard WorkerPigweed includes build files for a selection of third-party libraries. For a 192*61c4878aSAndroid Build Coastguard Workergiven library, these include: 193*61c4878aSAndroid Build Coastguard Worker 194*61c4878aSAndroid Build Coastguard Worker* ``third_party/<library>/library.gni``: Declares build arguments like 195*61c4878aSAndroid Build Coastguard Worker ``dir_pw_third_party_<library>`` that default to ``""`` but can be set to the 196*61c4878aSAndroid Build Coastguard Worker absolute path of the library in order to use it. 197*61c4878aSAndroid Build Coastguard Worker* ``third_party/<library>/BUILD.gn``: Describes how to build the library. This 198*61c4878aSAndroid Build Coastguard Worker should import ``third_party/<library>/library.gni`` and refer to source paths 199*61c4878aSAndroid Build Coastguard Worker relative to ``dir_pw_third_party_<library>``. 200*61c4878aSAndroid Build Coastguard Worker 201*61c4878aSAndroid Build Coastguard WorkerTo add or update GN build files for libraries that only offer Bazel build files, 202*61c4878aSAndroid Build Coastguard Workerthe Python script at ``pw_build/py/pw_build/bazel_to_gn.py`` may be used. 203*61c4878aSAndroid Build Coastguard Worker 204*61c4878aSAndroid Build Coastguard Worker.. note:: 205*61c4878aSAndroid Build Coastguard Worker The ``bazel_to_gn.py`` script is experimental, and may not work on an 206*61c4878aSAndroid Build Coastguard Worker arbitrary Bazel library. 207*61c4878aSAndroid Build Coastguard Worker 208*61c4878aSAndroid Build Coastguard WorkerTo generate or update the GN offered by Pigweed from an Bazel upstream project, 209*61c4878aSAndroid Build Coastguard Workerfirst create a ``third_party/<library>/bazel_to_gn.json`` file. This file should 210*61c4878aSAndroid Build Coastguard Workerdescribe a single JSON object, with the following fields: 211*61c4878aSAndroid Build Coastguard Worker 212*61c4878aSAndroid Build Coastguard Worker* ``repo``: Required string containing the Bazel repository name. 213*61c4878aSAndroid Build Coastguard Worker 214*61c4878aSAndroid Build Coastguard Worker .. code-block:: 215*61c4878aSAndroid Build Coastguard Worker 216*61c4878aSAndroid Build Coastguard Worker "repo": "com_google_absl" 217*61c4878aSAndroid Build Coastguard Worker 218*61c4878aSAndroid Build Coastguard Worker* ``targets``: Optional list of Bazel targets to convert, relative to the repo. 219*61c4878aSAndroid Build Coastguard Worker 220*61c4878aSAndroid Build Coastguard Worker .. code-block:: 221*61c4878aSAndroid Build Coastguard Worker 222*61c4878aSAndroid Build Coastguard Worker "targets": [ "//pkg1:target1", "//pkg2:target2" ] 223*61c4878aSAndroid Build Coastguard Worker 224*61c4878aSAndroid Build Coastguard Worker* ``defaults``: Optional object mapping attribute names to lists of strings. 225*61c4878aSAndroid Build Coastguard Worker These values are treated as implied for the attribute, and will be skipped 226*61c4878aSAndroid Build Coastguard Worker when converting a Bazel rule into a GN target. 227*61c4878aSAndroid Build Coastguard Worker 228*61c4878aSAndroid Build Coastguard Worker .. code-block:: 229*61c4878aSAndroid Build Coastguard Worker 230*61c4878aSAndroid Build Coastguard Worker "defaults": { 231*61c4878aSAndroid Build Coastguard Worker "copts": [ "-Wconversion" ] 232*61c4878aSAndroid Build Coastguard Worker } 233*61c4878aSAndroid Build Coastguard Worker 234*61c4878aSAndroid Build Coastguard Worker* ``generate``: Optional boolean indicating whether to generate GN build files a 235*61c4878aSAndroid Build Coastguard Worker third party library. Default is true. This flag may be useful for a third 236*61c4878aSAndroid Build Coastguard Worker party library whose GN build files are manually maintained, but which is 237*61c4878aSAndroid Build Coastguard Worker referenced by another library whose GN build files are generated. 238*61c4878aSAndroid Build Coastguard Worker 239*61c4878aSAndroid Build Coastguard Worker .. code-block:: 240*61c4878aSAndroid Build Coastguard Worker 241*61c4878aSAndroid Build Coastguard Worker "generate": true 242*61c4878aSAndroid Build Coastguard Worker 243*61c4878aSAndroid Build Coastguard WorkerGN build files may be generated using the following command: 244*61c4878aSAndroid Build Coastguard Worker 245*61c4878aSAndroid Build Coastguard Worker.. code-block:: 246*61c4878aSAndroid Build Coastguard Worker 247*61c4878aSAndroid Build Coastguard Worker python3 pw_build/py/pw_build/bazel_to_gn.py <library> 248*61c4878aSAndroid Build Coastguard Worker 249*61c4878aSAndroid Build Coastguard WorkerFor Bazel, ``http_archive`` rules should be added or updated in the project 250*61c4878aSAndroid Build Coastguard WorkerWORKSPACE file. 251*61c4878aSAndroid Build Coastguard Worker 252*61c4878aSAndroid Build Coastguard Worker.. code-block:: 253*61c4878aSAndroid Build Coastguard Worker 254*61c4878aSAndroid Build Coastguard Worker http_archive( 255*61c4878aSAndroid Build Coastguard Worker name = "com_google_absl", 256*61c4878aSAndroid Build Coastguard Worker sha256 = "0ddd37f347c58d89f449dd189a645bfd97bcd85c5284404a3af27a3ca3476f39", 257*61c4878aSAndroid Build Coastguard Worker strip_prefix = "abseil-cpp-fad946221cec37175e762c399760f54b9de9a9fa", 258*61c4878aSAndroid Build Coastguard Worker url = "https://github.com/abseil/abseil-cpp/archive/fad946221cec37175e762c399760f54b9de9a9fa.tar.gz", 259*61c4878aSAndroid Build Coastguard Worker ) 260*61c4878aSAndroid Build Coastguard Worker 261*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-python-packages: 262*61c4878aSAndroid Build Coastguard Worker 263*61c4878aSAndroid Build Coastguard WorkerPython packages 264*61c4878aSAndroid Build Coastguard Worker--------------- 265*61c4878aSAndroid Build Coastguard WorkerGN templates for :ref:`Python build automation <docs-python-build>` are 266*61c4878aSAndroid Build Coastguard Workerdescribed in :ref:`module-pw_build-python`. 267*61c4878aSAndroid Build Coastguard Worker 268*61c4878aSAndroid Build Coastguard Worker.. toctree:: 269*61c4878aSAndroid Build Coastguard Worker :hidden: 270*61c4878aSAndroid Build Coastguard Worker 271*61c4878aSAndroid Build Coastguard Worker python 272*61c4878aSAndroid Build Coastguard Worker 273*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-cc_blob_library: 274*61c4878aSAndroid Build Coastguard Worker 275*61c4878aSAndroid Build Coastguard Workerpw_cc_blob_library 276*61c4878aSAndroid Build Coastguard Worker------------------ 277*61c4878aSAndroid Build Coastguard WorkerThe ``pw_cc_blob_library`` template is useful for embedding binary data into a 278*61c4878aSAndroid Build Coastguard Workerprogram. The template takes in a mapping of symbol names to file paths, and 279*61c4878aSAndroid Build Coastguard Workergenerates a set of C++ source and header files that embed the contents of the 280*61c4878aSAndroid Build Coastguard Workerpassed-in files as arrays of ``std::byte``. 281*61c4878aSAndroid Build Coastguard Worker 282*61c4878aSAndroid Build Coastguard WorkerThe blob byte arrays are constant initialized and are safe to access at any 283*61c4878aSAndroid Build Coastguard Workertime, including before ``main()``. 284*61c4878aSAndroid Build Coastguard Worker 285*61c4878aSAndroid Build Coastguard Worker``pw_cc_blob_library`` is also available in the CMake build. It is provided by 286*61c4878aSAndroid Build Coastguard Worker``pw_build/cc_blob_library.cmake``. 287*61c4878aSAndroid Build Coastguard Worker 288*61c4878aSAndroid Build Coastguard WorkerArguments 289*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 290*61c4878aSAndroid Build Coastguard Worker* ``blobs``: A list of GN scopes, where each scope corresponds to a binary blob 291*61c4878aSAndroid Build Coastguard Worker to be transformed from file to byte array. This is a required field. Blob 292*61c4878aSAndroid Build Coastguard Worker fields include: 293*61c4878aSAndroid Build Coastguard Worker 294*61c4878aSAndroid Build Coastguard Worker * ``symbol_name``: The C++ symbol for the byte array. 295*61c4878aSAndroid Build Coastguard Worker * ``file_path``: The file path for the binary blob. 296*61c4878aSAndroid Build Coastguard Worker * ``linker_section``: If present, places the byte array in the specified 297*61c4878aSAndroid Build Coastguard Worker linker section. 298*61c4878aSAndroid Build Coastguard Worker * ``alignas``: If present, uses the specified string or integer verbatim in 299*61c4878aSAndroid Build Coastguard Worker the ``alignas()`` specifier for the byte array. 300*61c4878aSAndroid Build Coastguard Worker 301*61c4878aSAndroid Build Coastguard Worker* ``out_header``: The header file to generate. Users will include this file 302*61c4878aSAndroid Build Coastguard Worker exactly as it is written here to reference the byte arrays. 303*61c4878aSAndroid Build Coastguard Worker* ``namespace``: An optional (but highly recommended!) C++ namespace to place 304*61c4878aSAndroid Build Coastguard Worker the generated blobs within. 305*61c4878aSAndroid Build Coastguard Worker 306*61c4878aSAndroid Build Coastguard WorkerExample 307*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 308*61c4878aSAndroid Build Coastguard Worker**BUILD.gn** 309*61c4878aSAndroid Build Coastguard Worker 310*61c4878aSAndroid Build Coastguard Worker.. code-block:: 311*61c4878aSAndroid Build Coastguard Worker 312*61c4878aSAndroid Build Coastguard Worker pw_cc_blob_library("foo_bar_blobs") { 313*61c4878aSAndroid Build Coastguard Worker blobs: [ 314*61c4878aSAndroid Build Coastguard Worker { 315*61c4878aSAndroid Build Coastguard Worker symbol_name: "kFooBlob" 316*61c4878aSAndroid Build Coastguard Worker file_path: "${target_out_dir}/stuff/bin/foo.bin" 317*61c4878aSAndroid Build Coastguard Worker }, 318*61c4878aSAndroid Build Coastguard Worker { 319*61c4878aSAndroid Build Coastguard Worker symbol_name: "kBarBlob" 320*61c4878aSAndroid Build Coastguard Worker file_path: "//stuff/bin/bar.bin" 321*61c4878aSAndroid Build Coastguard Worker linker_section: ".bar_section" 322*61c4878aSAndroid Build Coastguard Worker }, 323*61c4878aSAndroid Build Coastguard Worker ] 324*61c4878aSAndroid Build Coastguard Worker out_header: "my/stuff/foo_bar_blobs.h" 325*61c4878aSAndroid Build Coastguard Worker namespace: "my::stuff" 326*61c4878aSAndroid Build Coastguard Worker deps = [ ":generate_foo_bin" ] 327*61c4878aSAndroid Build Coastguard Worker } 328*61c4878aSAndroid Build Coastguard Worker 329*61c4878aSAndroid Build Coastguard Worker.. note:: If the binary blobs are generated as part of the build, be sure to 330*61c4878aSAndroid Build Coastguard Worker list them as deps to the pw_cc_blob_library target. 331*61c4878aSAndroid Build Coastguard Worker 332*61c4878aSAndroid Build Coastguard Worker**Generated Header** 333*61c4878aSAndroid Build Coastguard Worker 334*61c4878aSAndroid Build Coastguard Worker.. code-block:: 335*61c4878aSAndroid Build Coastguard Worker 336*61c4878aSAndroid Build Coastguard Worker #pragma once 337*61c4878aSAndroid Build Coastguard Worker 338*61c4878aSAndroid Build Coastguard Worker #include <array> 339*61c4878aSAndroid Build Coastguard Worker #include <cstddef> 340*61c4878aSAndroid Build Coastguard Worker 341*61c4878aSAndroid Build Coastguard Worker namespace my::stuff { 342*61c4878aSAndroid Build Coastguard Worker 343*61c4878aSAndroid Build Coastguard Worker extern const std::array<std::byte, 100> kFooBlob; 344*61c4878aSAndroid Build Coastguard Worker 345*61c4878aSAndroid Build Coastguard Worker extern const std::array<std::byte, 50> kBarBlob; 346*61c4878aSAndroid Build Coastguard Worker 347*61c4878aSAndroid Build Coastguard Worker } // namespace my::stuff 348*61c4878aSAndroid Build Coastguard Worker 349*61c4878aSAndroid Build Coastguard Worker**Generated Source** 350*61c4878aSAndroid Build Coastguard Worker 351*61c4878aSAndroid Build Coastguard Worker.. code-block:: 352*61c4878aSAndroid Build Coastguard Worker 353*61c4878aSAndroid Build Coastguard Worker #include "my/stuff/foo_bar_blobs.h" 354*61c4878aSAndroid Build Coastguard Worker 355*61c4878aSAndroid Build Coastguard Worker #include <array> 356*61c4878aSAndroid Build Coastguard Worker #include <cstddef> 357*61c4878aSAndroid Build Coastguard Worker 358*61c4878aSAndroid Build Coastguard Worker #include "pw_preprocessor/compiler.h" 359*61c4878aSAndroid Build Coastguard Worker 360*61c4878aSAndroid Build Coastguard Worker namespace my::stuff { 361*61c4878aSAndroid Build Coastguard Worker 362*61c4878aSAndroid Build Coastguard Worker const std::array<std::byte, 100> kFooBlob = { ... }; 363*61c4878aSAndroid Build Coastguard Worker 364*61c4878aSAndroid Build Coastguard Worker PW_PLACE_IN_SECTION(".bar_section") 365*61c4878aSAndroid Build Coastguard Worker const std::array<std::byte, 50> kBarBlob = { ... }; 366*61c4878aSAndroid Build Coastguard Worker 367*61c4878aSAndroid Build Coastguard Worker } // namespace my::stuff 368*61c4878aSAndroid Build Coastguard Worker 369*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-facade: 370*61c4878aSAndroid Build Coastguard Worker 371*61c4878aSAndroid Build Coastguard Workerpw_facade 372*61c4878aSAndroid Build Coastguard Worker--------- 373*61c4878aSAndroid Build Coastguard WorkerIn their simplest form, a :ref:`facade<docs-module-structure-facades>` is a GN 374*61c4878aSAndroid Build Coastguard Workerbuild arg used to change a dependency at compile time. Pigweed targets configure 375*61c4878aSAndroid Build Coastguard Workerthese facades as needed. 376*61c4878aSAndroid Build Coastguard Worker 377*61c4878aSAndroid Build Coastguard WorkerThe ``pw_facade`` template bundles a ``pw_source_set`` with a facade build arg. 378*61c4878aSAndroid Build Coastguard WorkerThis allows the facade to provide header files, compilation options or anything 379*61c4878aSAndroid Build Coastguard Workerelse a GN ``source_set`` provides. 380*61c4878aSAndroid Build Coastguard Worker 381*61c4878aSAndroid Build Coastguard WorkerThe ``pw_facade`` template declares one or two targets: 382*61c4878aSAndroid Build Coastguard Worker 383*61c4878aSAndroid Build Coastguard Worker* ``$target_name``: The public-facing ``pw_source_set``, with a ``public_dep`` 384*61c4878aSAndroid Build Coastguard Worker on the backend. Always declared. 385*61c4878aSAndroid Build Coastguard Worker* ``$target_name.facade``: Target with ``public`` headers, ``public_deps``, and 386*61c4878aSAndroid Build Coastguard Worker ``public_configs`` shared between the public-facing ``pw_source_set`` and 387*61c4878aSAndroid Build Coastguard Worker backend to avoid circular dependencies. Only declared if ``public``, 388*61c4878aSAndroid Build Coastguard Worker ``public_deps``, or ``public_configs`` are provided. 389*61c4878aSAndroid Build Coastguard Worker 390*61c4878aSAndroid Build Coastguard Worker.. code-block:: 391*61c4878aSAndroid Build Coastguard Worker 392*61c4878aSAndroid Build Coastguard Worker # Declares ":foo" and ":foo.facade" GN targets 393*61c4878aSAndroid Build Coastguard Worker pw_facade("foo") { 394*61c4878aSAndroid Build Coastguard Worker backend = pw_log_BACKEND 395*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":public_include_path" ] 396*61c4878aSAndroid Build Coastguard Worker public = [ "public/pw_foo/foo.h" ] 397*61c4878aSAndroid Build Coastguard Worker } 398*61c4878aSAndroid Build Coastguard Worker 399*61c4878aSAndroid Build Coastguard WorkerLow-level facades like ``pw_assert`` cannot express all of their dependencies 400*61c4878aSAndroid Build Coastguard Workerdue to the potential for dependency cycles. Facades with this issue may require 401*61c4878aSAndroid Build Coastguard Workerbackends to place their implementations in a separate build target to be listed 402*61c4878aSAndroid Build Coastguard Workerin ``pw_build_LINK_DEPS`` (see :ref:`module-pw_build-link-deps`). The 403*61c4878aSAndroid Build Coastguard Worker``require_link_deps`` variable in ``pw_facade`` asserts that all specified build 404*61c4878aSAndroid Build Coastguard Workertargets are present in ``pw_build_LINK_DEPS`` if the facade's backend variable 405*61c4878aSAndroid Build Coastguard Workeris set. 406*61c4878aSAndroid Build Coastguard Worker 407*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-python-action: 408*61c4878aSAndroid Build Coastguard Worker 409*61c4878aSAndroid Build Coastguard Workerpw_python_action 410*61c4878aSAndroid Build Coastguard Worker---------------- 411*61c4878aSAndroid Build Coastguard Worker.. seealso:: 412*61c4878aSAndroid Build Coastguard Worker - :ref:`module-pw_build-python` for all of Pigweed's Python build GN templates. 413*61c4878aSAndroid Build Coastguard Worker - :ref:`docs-python-build` for details on how the GN Python build works. 414*61c4878aSAndroid Build Coastguard Worker 415*61c4878aSAndroid Build Coastguard WorkerThe ``pw_python_action`` template is a convenience wrapper around GN's `action 416*61c4878aSAndroid Build Coastguard Workerfunction <https://gn.googlesource.com/gn/+/main/docs/reference.md#func_action>`_ 417*61c4878aSAndroid Build Coastguard Workerfor running Python scripts. The main benefit it provides is resolution of GN 418*61c4878aSAndroid Build Coastguard Workertarget labels to compiled binary files. This allows Python scripts to be written 419*61c4878aSAndroid Build Coastguard Workerindependently of GN, taking only filesystem paths as arguments. 420*61c4878aSAndroid Build Coastguard Worker 421*61c4878aSAndroid Build Coastguard WorkerAnother convenience provided by the template is to allow running scripts without 422*61c4878aSAndroid Build Coastguard Workerany outputs. Sometimes scripts run in a build do not directly produce output 423*61c4878aSAndroid Build Coastguard Workerfiles, but GN requires that all actions have an output. ``pw_python_action`` 424*61c4878aSAndroid Build Coastguard Workersolves this by accepting a boolean ``stamp`` argument which tells it to create a 425*61c4878aSAndroid Build Coastguard Workerplaceholder output file for the action. 426*61c4878aSAndroid Build Coastguard Worker 427*61c4878aSAndroid Build Coastguard WorkerArguments 428*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 429*61c4878aSAndroid Build Coastguard Worker``pw_python_action`` accepts all of the arguments of a regular ``action`` 430*61c4878aSAndroid Build Coastguard Workertarget. Additionally, it has some of its own arguments: 431*61c4878aSAndroid Build Coastguard Worker 432*61c4878aSAndroid Build Coastguard Worker* ``module``: Run the specified Python module instead of a script. Either 433*61c4878aSAndroid Build Coastguard Worker ``script`` or ``module`` must be specified, but not both. 434*61c4878aSAndroid Build Coastguard Worker* ``capture_output``: Optional boolean. If true, script output is hidden unless 435*61c4878aSAndroid Build Coastguard Worker the script fails with an error. Defaults to true. 436*61c4878aSAndroid Build Coastguard Worker* ``stamp``: Optional variable indicating whether to automatically create a 437*61c4878aSAndroid Build Coastguard Worker placeholder output file for the script. This allows running scripts without 438*61c4878aSAndroid Build Coastguard Worker specifying ``outputs``. If ``stamp`` is true, a generic output file is 439*61c4878aSAndroid Build Coastguard Worker used. If ``stamp`` is a file path, that file is used as a stamp file. Like any 440*61c4878aSAndroid Build Coastguard Worker output file, ``stamp`` must be in the build directory. Defaults to false. 441*61c4878aSAndroid Build Coastguard Worker* ``environment``: Optional list of strings. Environment variables to set, 442*61c4878aSAndroid Build Coastguard Worker passed as NAME=VALUE strings. 443*61c4878aSAndroid Build Coastguard Worker* ``working_directory``: Optional file path. When provided the current working 444*61c4878aSAndroid Build Coastguard Worker directory will be set to this location before the Python module or script is 445*61c4878aSAndroid Build Coastguard Worker run. 446*61c4878aSAndroid Build Coastguard Worker* ``command_launcher``: Optional string. Arguments to prepend to the Python 447*61c4878aSAndroid Build Coastguard Worker command, e.g. ``'/usr/bin/fakeroot --'`` will run the Python script within a 448*61c4878aSAndroid Build Coastguard Worker fakeroot environment. 449*61c4878aSAndroid Build Coastguard Worker* ``venv``: Optional gn target of the pw_python_venv that should be used to run 450*61c4878aSAndroid Build Coastguard Worker this action. 451*61c4878aSAndroid Build Coastguard Worker* ``python_deps``: Extra dependencies that are required for running the Python 452*61c4878aSAndroid Build Coastguard Worker script for the ``action``. This must be used with ``module`` to specify the 453*61c4878aSAndroid Build Coastguard Worker build dependency of the ``module`` if it is user-defined code. 454*61c4878aSAndroid Build Coastguard Worker* ``python_metadata_deps``: Extra dependencies that are ensured completed before 455*61c4878aSAndroid Build Coastguard Worker generating a Python package metadata manifest, not the overall Python script 456*61c4878aSAndroid Build Coastguard Worker ``action``. This should rarely be used by non-Pigweed code. 457*61c4878aSAndroid Build Coastguard Worker 458*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-python-action-test: 459*61c4878aSAndroid Build Coastguard Worker 460*61c4878aSAndroid Build Coastguard Workerpw_python_action_test 461*61c4878aSAndroid Build Coastguard Worker--------------------- 462*61c4878aSAndroid Build Coastguard WorkerThe ``pw_python_action_test`` extends :ref:`module-pw_build-python-action` to 463*61c4878aSAndroid Build Coastguard Workercreate a test that is run by a Python script, and its associated test metadata. 464*61c4878aSAndroid Build Coastguard Worker 465*61c4878aSAndroid Build Coastguard WorkerInclude action tests in the :ref:`module-pw_unit_test-pw_test_group` to produce 466*61c4878aSAndroid Build Coastguard Workerthe JSON metadata that :ref:`module-pw_build-test-info` adds. 467*61c4878aSAndroid Build Coastguard Worker 468*61c4878aSAndroid Build Coastguard WorkerThis template derives several additional targets: 469*61c4878aSAndroid Build Coastguard Worker 470*61c4878aSAndroid Build Coastguard Worker* ``<target_name>.metadata`` produces the test metadata when included in a 471*61c4878aSAndroid Build Coastguard Worker ``pw_test_group``. This metadata includes the Ninja target that runs the test. 472*61c4878aSAndroid Build Coastguard Worker* If``action`` is not provided as a label, ``<target_name>.script`` wraps a 473*61c4878aSAndroid Build Coastguard Worker ``pw_python_action`` to run the test as a standalone ``pw_python_package``. 474*61c4878aSAndroid Build Coastguard Worker* ``<target_name>.group`` creates a ``pw_python_group`` in order to apply tools, 475*61c4878aSAndroid Build Coastguard Worker e.g. linters, to the standalone package. 476*61c4878aSAndroid Build Coastguard Worker* ``<target_name>.lib`` is an empty group for compatibility with 477*61c4878aSAndroid Build Coastguard Worker ``pw_test_group``. 478*61c4878aSAndroid Build Coastguard Worker* ``<target_name>.run`` invokes the test. 479*61c4878aSAndroid Build Coastguard Worker 480*61c4878aSAndroid Build Coastguard WorkerTargets defined using this template will produce test metadata with a 481*61c4878aSAndroid Build Coastguard Worker``test_type`` of "action_test" and a ``ninja_target`` value that will invoke the 482*61c4878aSAndroid Build Coastguard Workertest when passed to Ninja, i.e. ``ninja -C out <ninja_target>``. 483*61c4878aSAndroid Build Coastguard Worker 484*61c4878aSAndroid Build Coastguard WorkerArguments 485*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 486*61c4878aSAndroid Build Coastguard Worker``pw_python_action_test`` accepts the following arguments: 487*61c4878aSAndroid Build Coastguard Worker 488*61c4878aSAndroid Build Coastguard Worker* All of the arguments of :ref:`module-pw_unit_test-pw_test`. 489*61c4878aSAndroid Build Coastguard Worker* ``action``: An optional string or scope. If a string, this should be a label 490*61c4878aSAndroid Build Coastguard Worker to a ``pw_python_action`` target that performs the test. If a scope, this has 491*61c4878aSAndroid Build Coastguard Worker the same meaning as for ``pw_python_script``. 492*61c4878aSAndroid Build Coastguard Worker* Optionally, the ``test_type`` and ``extra_metadata`` arguments of the 493*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_build-test-info` template. 494*61c4878aSAndroid Build Coastguard Worker* Optionally, all of the arguments of the :ref:`module-pw_build-python-action` 495*61c4878aSAndroid Build Coastguard Worker template except ``module``, ``capture_output``, ``stamp``, and 496*61c4878aSAndroid Build Coastguard Worker ``python_metadata_deps``. 497*61c4878aSAndroid Build Coastguard Worker* Optionally, all of the arguments of the ``pw_python_package`` template except 498*61c4878aSAndroid Build Coastguard Worker ``setup``, ``generate_setup``, ``tests``, ``python_test_deps``, and 499*61c4878aSAndroid Build Coastguard Worker ``proto_library``. 500*61c4878aSAndroid Build Coastguard Worker 501*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-test-info: 502*61c4878aSAndroid Build Coastguard Worker 503*61c4878aSAndroid Build Coastguard Workerpw_test_info 504*61c4878aSAndroid Build Coastguard Worker------------ 505*61c4878aSAndroid Build Coastguard Worker``pw_test_info`` generates metadata describing tests. To produce a JSON file 506*61c4878aSAndroid Build Coastguard Workercontaining this metadata: 507*61c4878aSAndroid Build Coastguard Worker 508*61c4878aSAndroid Build Coastguard Worker#. For new modules, add a :ref:`module-pw_unit_test-pw_test_group` to the 509*61c4878aSAndroid Build Coastguard Worker BUILD.gn file. All modules are required to have a ``tests`` target. 510*61c4878aSAndroid Build Coastguard Worker#. Include one or more tests or test groups via ``tests`` or ``group_deps``, 511*61c4878aSAndroid Build Coastguard Worker respectively, in the ``pw_test_group``. 512*61c4878aSAndroid Build Coastguard Worker#. Set ``output_metadata`` to ``true`` in the ``pw_test_group`` definition. 513*61c4878aSAndroid Build Coastguard Worker 514*61c4878aSAndroid Build Coastguard WorkerThis template does not typically need to be used directly, unless adding new 515*61c4878aSAndroid Build Coastguard Workertypes of tests. It is typically used by other templates, such as the 516*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_unit_test-pw_test` and the 517*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_unit_test-pw_test_group`. 518*61c4878aSAndroid Build Coastguard Worker 519*61c4878aSAndroid Build Coastguard WorkerArguments 520*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 521*61c4878aSAndroid Build Coastguard Worker* ``test_type``: One of "test_group", "unit_test", "action_test", "perf_test", 522*61c4878aSAndroid Build Coastguard Worker or "fuzz_test". 523*61c4878aSAndroid Build Coastguard Worker* ``test_name``: Name of the test. Defaults to the target name. 524*61c4878aSAndroid Build Coastguard Worker* ``build_label``: GN label for the test. Defaults to the test name. 525*61c4878aSAndroid Build Coastguard Worker* ``extra_metadata``: Additional variables to add to the metadata. 526*61c4878aSAndroid Build Coastguard Worker 527*61c4878aSAndroid Build Coastguard WorkerSpecific test templates add additional details using ``extra_metadata``. For 528*61c4878aSAndroid Build Coastguard Workerexample: 529*61c4878aSAndroid Build Coastguard Worker 530*61c4878aSAndroid Build Coastguard Worker* The :ref:`module-pw_unit_test-pw_test_group` includes its collected list of 531*61c4878aSAndroid Build Coastguard Worker tests and test groups as ``deps``. 532*61c4878aSAndroid Build Coastguard Worker* The :ref:`module-pw_unit_test-pw_test` and the 533*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_perf_test-pw_perf_test` includes the ``test_directory`` 534*61c4878aSAndroid Build Coastguard Worker that contains the test executable. 535*61c4878aSAndroid Build Coastguard Worker* The :ref:`module-pw_build-python-action-test` includes the Ninja target that 536*61c4878aSAndroid Build Coastguard Worker can be used to invoke the Python action and run the test. 537*61c4878aSAndroid Build Coastguard Worker 538*61c4878aSAndroid Build Coastguard WorkerExample 539*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 540*61c4878aSAndroid Build Coastguard WorkerLet ``//my_module/BUILD.gn`` contain the following: 541*61c4878aSAndroid Build Coastguard Worker 542*61c4878aSAndroid Build Coastguard Worker.. code-block:: 543*61c4878aSAndroid Build Coastguard Worker 544*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/python_action_test.gni") 545*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_perf_test/perf_test.gni") 546*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_unit_test/test.gni") 547*61c4878aSAndroid Build Coastguard Worker 548*61c4878aSAndroid Build Coastguard Worker pw_test("my_unit_test") { 549*61c4878aSAndroid Build Coastguard Worker sources = [ ... ] 550*61c4878aSAndroid Build Coastguard Worker deps = [ ... ] 551*61c4878aSAndroid Build Coastguard Worker } 552*61c4878aSAndroid Build Coastguard Worker 553*61c4878aSAndroid Build Coastguard Worker pw_python_action_test("my_action_test") { 554*61c4878aSAndroid Build Coastguard Worker script = [ ... ] 555*61c4878aSAndroid Build Coastguard Worker args = [ ... ] 556*61c4878aSAndroid Build Coastguard Worker deps = [ ... ] 557*61c4878aSAndroid Build Coastguard Worker } 558*61c4878aSAndroid Build Coastguard Worker 559*61c4878aSAndroid Build Coastguard Worker pw_python_action_test("my_integration_test") { 560*61c4878aSAndroid Build Coastguard Worker script = [ ... ] 561*61c4878aSAndroid Build Coastguard Worker args = [ ... ] 562*61c4878aSAndroid Build Coastguard Worker deps = [ ... ] 563*61c4878aSAndroid Build Coastguard Worker tags = [ "integration" ] 564*61c4878aSAndroid Build Coastguard Worker } 565*61c4878aSAndroid Build Coastguard Worker 566*61c4878aSAndroid Build Coastguard Worker pw_perf_test("my_perf_test") { 567*61c4878aSAndroid Build Coastguard Worker sources = [ ... ] 568*61c4878aSAndroid Build Coastguard Worker deps = [ ... ] 569*61c4878aSAndroid Build Coastguard Worker } 570*61c4878aSAndroid Build Coastguard Worker 571*61c4878aSAndroid Build Coastguard Worker pw_test_group("tests") { 572*61c4878aSAndroid Build Coastguard Worker tests = [ 573*61c4878aSAndroid Build Coastguard Worker ":my_unit_test", 574*61c4878aSAndroid Build Coastguard Worker ":my_action_test", 575*61c4878aSAndroid Build Coastguard Worker ":my_integration_test", 576*61c4878aSAndroid Build Coastguard Worker ] 577*61c4878aSAndroid Build Coastguard Worker } 578*61c4878aSAndroid Build Coastguard Worker 579*61c4878aSAndroid Build Coastguard WorkerLet `//BUILD.gn`` contain the following: 580*61c4878aSAndroid Build Coastguard Worker 581*61c4878aSAndroid Build Coastguard Worker.. code-block:: 582*61c4878aSAndroid Build Coastguard Worker 583*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_unit_test/test.gni") 584*61c4878aSAndroid Build Coastguard Worker 585*61c4878aSAndroid Build Coastguard Worker group("run_tests") { 586*61c4878aSAndroid Build Coastguard Worker deps = [ ":my_module_tests(//targets/my_targets:my_toolchain)" ] 587*61c4878aSAndroid Build Coastguard Worker } 588*61c4878aSAndroid Build Coastguard Worker 589*61c4878aSAndroid Build Coastguard Worker pw_test_group("my_module_tests") { 590*61c4878aSAndroid Build Coastguard Worker group_deps = [ "//my_module:tests" ] 591*61c4878aSAndroid Build Coastguard Worker output_metadata = true 592*61c4878aSAndroid Build Coastguard Worker } 593*61c4878aSAndroid Build Coastguard Worker 594*61c4878aSAndroid Build Coastguard WorkerThen running ``gn gen out`` will produce the following JSON file at 595*61c4878aSAndroid Build Coastguard Worker``out/my_toolchain/my_module_tests.testinfo.json``: 596*61c4878aSAndroid Build Coastguard Worker 597*61c4878aSAndroid Build Coastguard Worker.. code-block:: json 598*61c4878aSAndroid Build Coastguard Worker 599*61c4878aSAndroid Build Coastguard Worker [ 600*61c4878aSAndroid Build Coastguard Worker { 601*61c4878aSAndroid Build Coastguard Worker "build_label": "//my_module:my_unit_test", 602*61c4878aSAndroid Build Coastguard Worker "test_directory": "my_toolchain/obj/my_module/test", 603*61c4878aSAndroid Build Coastguard Worker "test_name": "my_unit_test", 604*61c4878aSAndroid Build Coastguard Worker "test_type": "unit_test" 605*61c4878aSAndroid Build Coastguard Worker }, 606*61c4878aSAndroid Build Coastguard Worker { 607*61c4878aSAndroid Build Coastguard Worker "build_label": "//my_module:my_action_test", 608*61c4878aSAndroid Build Coastguard Worker "ninja_target": "my_toolchain/obj/my_module/my_action_test.run.stamp", 609*61c4878aSAndroid Build Coastguard Worker "test_name": "my_action_test", 610*61c4878aSAndroid Build Coastguard Worker "test_type": "action_test" 611*61c4878aSAndroid Build Coastguard Worker }, 612*61c4878aSAndroid Build Coastguard Worker { 613*61c4878aSAndroid Build Coastguard Worker "build_label": "//my_module:my_integration_test", 614*61c4878aSAndroid Build Coastguard Worker "ninja_target": "my_toolchain/obj/my_module/my_integration_test.run.stamp", 615*61c4878aSAndroid Build Coastguard Worker "tags": [ 616*61c4878aSAndroid Build Coastguard Worker "integration" 617*61c4878aSAndroid Build Coastguard Worker ], 618*61c4878aSAndroid Build Coastguard Worker "test_name": "my_integration_test", 619*61c4878aSAndroid Build Coastguard Worker "test_type": "action_test" 620*61c4878aSAndroid Build Coastguard Worker }, 621*61c4878aSAndroid Build Coastguard Worker { 622*61c4878aSAndroid Build Coastguard Worker "build_label": "//my_module:my_perf_test", 623*61c4878aSAndroid Build Coastguard Worker "test_directory": "my_toolchain/obj/my_module/test", 624*61c4878aSAndroid Build Coastguard Worker "test_name": "my_perf_test", 625*61c4878aSAndroid Build Coastguard Worker "test_type": "perf_test" 626*61c4878aSAndroid Build Coastguard Worker }, 627*61c4878aSAndroid Build Coastguard Worker { 628*61c4878aSAndroid Build Coastguard Worker "build_label": "//my_module:tests", 629*61c4878aSAndroid Build Coastguard Worker "deps": [ 630*61c4878aSAndroid Build Coastguard Worker "//my_module:my_unit_test", 631*61c4878aSAndroid Build Coastguard Worker "//my_module:my_action_test", 632*61c4878aSAndroid Build Coastguard Worker "//my_module:my_integration_test", 633*61c4878aSAndroid Build Coastguard Worker ], 634*61c4878aSAndroid Build Coastguard Worker "test_name": "my_module/tests", 635*61c4878aSAndroid Build Coastguard Worker "test_type": "test_group" 636*61c4878aSAndroid Build Coastguard Worker }, 637*61c4878aSAndroid Build Coastguard Worker { 638*61c4878aSAndroid Build Coastguard Worker "build_label": "//:my_module_tests", 639*61c4878aSAndroid Build Coastguard Worker "deps": [ 640*61c4878aSAndroid Build Coastguard Worker "//my_module:tests", 641*61c4878aSAndroid Build Coastguard Worker ], 642*61c4878aSAndroid Build Coastguard Worker "test_name": "my_module_tests", 643*61c4878aSAndroid Build Coastguard Worker "test_type": "test_group" 644*61c4878aSAndroid Build Coastguard Worker } 645*61c4878aSAndroid Build Coastguard Worker ] 646*61c4878aSAndroid Build Coastguard Worker 647*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-python-action-expressions: 648*61c4878aSAndroid Build Coastguard Worker 649*61c4878aSAndroid Build Coastguard WorkerExpressions 650*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^ 651*61c4878aSAndroid Build Coastguard Worker``pw_python_action`` evaluates expressions in ``args``, the arguments passed to 652*61c4878aSAndroid Build Coastguard Workerthe script. These expressions function similarly to generator expressions in 653*61c4878aSAndroid Build Coastguard WorkerCMake. Expressions may be passed as a standalone argument or as part of another 654*61c4878aSAndroid Build Coastguard Workerargument. A single argument may contain multiple expressions. 655*61c4878aSAndroid Build Coastguard Worker 656*61c4878aSAndroid Build Coastguard WorkerGenerally, these expressions are used within templates rather than directly in 657*61c4878aSAndroid Build Coastguard WorkerBUILD.gn files. This allows build code to use GN labels without having to worry 658*61c4878aSAndroid Build Coastguard Workerabout converting them to files. 659*61c4878aSAndroid Build Coastguard Worker 660*61c4878aSAndroid Build Coastguard Worker.. note:: 661*61c4878aSAndroid Build Coastguard Worker 662*61c4878aSAndroid Build Coastguard Worker We intend to replace these expressions with native GN features when possible. 663*61c4878aSAndroid Build Coastguard Worker See `b/234886742 <http://issuetracker.google.com/234886742>`_. 664*61c4878aSAndroid Build Coastguard Worker 665*61c4878aSAndroid Build Coastguard WorkerThe following expressions are supported: 666*61c4878aSAndroid Build Coastguard Worker 667*61c4878aSAndroid Build Coastguard Worker.. describe:: <TARGET_FILE(gn_target)> 668*61c4878aSAndroid Build Coastguard Worker 669*61c4878aSAndroid Build Coastguard Worker Evaluates to the output file of the provided GN target. For example, the 670*61c4878aSAndroid Build Coastguard Worker expression 671*61c4878aSAndroid Build Coastguard Worker 672*61c4878aSAndroid Build Coastguard Worker .. code-block:: 673*61c4878aSAndroid Build Coastguard Worker 674*61c4878aSAndroid Build Coastguard Worker "<TARGET_FILE(//foo/bar:static_lib)>" 675*61c4878aSAndroid Build Coastguard Worker 676*61c4878aSAndroid Build Coastguard Worker might expand to 677*61c4878aSAndroid Build Coastguard Worker 678*61c4878aSAndroid Build Coastguard Worker .. code-block:: 679*61c4878aSAndroid Build Coastguard Worker 680*61c4878aSAndroid Build Coastguard Worker "/home/User/project_root/out/obj/foo/bar/static_lib.a" 681*61c4878aSAndroid Build Coastguard Worker 682*61c4878aSAndroid Build Coastguard Worker ``TARGET_FILE`` parses the ``.ninja`` file for the GN target, so it should 683*61c4878aSAndroid Build Coastguard Worker always find the correct output file, regardless of the toolchain's or target's 684*61c4878aSAndroid Build Coastguard Worker configuration. Some targets, such as ``source_set`` and ``group`` targets, do 685*61c4878aSAndroid Build Coastguard Worker not have an output file, and attempting to use ``TARGET_FILE`` with them 686*61c4878aSAndroid Build Coastguard Worker results in an error. 687*61c4878aSAndroid Build Coastguard Worker 688*61c4878aSAndroid Build Coastguard Worker ``TARGET_FILE`` only resolves GN target labels to their outputs. To resolve 689*61c4878aSAndroid Build Coastguard Worker paths generally, use the standard GN approach of applying the 690*61c4878aSAndroid Build Coastguard Worker ``rebase_path(path, root_build_dir)`` function. This function 691*61c4878aSAndroid Build Coastguard Worker converts the provided GN path or list of paths to be relative to the build 692*61c4878aSAndroid Build Coastguard Worker directory, from which all build commands and scripts are executed. 693*61c4878aSAndroid Build Coastguard Worker 694*61c4878aSAndroid Build Coastguard Worker.. describe:: <TARGET_FILE_IF_EXISTS(gn_target)> 695*61c4878aSAndroid Build Coastguard Worker 696*61c4878aSAndroid Build Coastguard Worker ``TARGET_FILE_IF_EXISTS`` evaluates to the output file of the provided GN 697*61c4878aSAndroid Build Coastguard Worker target, if the output file exists. If the output file does not exist, the 698*61c4878aSAndroid Build Coastguard Worker entire argument that includes this expression is omitted, even if there is 699*61c4878aSAndroid Build Coastguard Worker other text or another expression. 700*61c4878aSAndroid Build Coastguard Worker 701*61c4878aSAndroid Build Coastguard Worker For example, consider this expression: 702*61c4878aSAndroid Build Coastguard Worker 703*61c4878aSAndroid Build Coastguard Worker .. code-block:: 704*61c4878aSAndroid Build Coastguard Worker 705*61c4878aSAndroid Build Coastguard Worker "--database=<TARGET_FILE_IF_EXISTS(//alpha/bravo)>" 706*61c4878aSAndroid Build Coastguard Worker 707*61c4878aSAndroid Build Coastguard Worker If the ``//alpha/bravo`` target file exists, this might expand to the 708*61c4878aSAndroid Build Coastguard Worker following: 709*61c4878aSAndroid Build Coastguard Worker 710*61c4878aSAndroid Build Coastguard Worker .. code-block:: 711*61c4878aSAndroid Build Coastguard Worker 712*61c4878aSAndroid Build Coastguard Worker "--database=/home/User/project/out/obj/alpha/bravo/bravo.elf" 713*61c4878aSAndroid Build Coastguard Worker 714*61c4878aSAndroid Build Coastguard Worker If the ``//alpha/bravo`` target file does not exist, the entire 715*61c4878aSAndroid Build Coastguard Worker ``--database=`` argument is omitted from the script arguments. 716*61c4878aSAndroid Build Coastguard Worker 717*61c4878aSAndroid Build Coastguard Worker.. describe:: <TARGET_OBJECTS(gn_target)> 718*61c4878aSAndroid Build Coastguard Worker 719*61c4878aSAndroid Build Coastguard Worker Evaluates to the object files of the provided GN target. Expands to a separate 720*61c4878aSAndroid Build Coastguard Worker argument for each object file. If the target has no object files, the argument 721*61c4878aSAndroid Build Coastguard Worker is omitted entirely. Because it does not expand to a single expression, the 722*61c4878aSAndroid Build Coastguard Worker ``<TARGET_OBJECTS(...)>`` expression may not have leading or trailing text. 723*61c4878aSAndroid Build Coastguard Worker 724*61c4878aSAndroid Build Coastguard Worker For example, the expression 725*61c4878aSAndroid Build Coastguard Worker 726*61c4878aSAndroid Build Coastguard Worker .. code-block:: 727*61c4878aSAndroid Build Coastguard Worker 728*61c4878aSAndroid Build Coastguard Worker "<TARGET_OBJECTS(//foo/bar:a_source_set)>" 729*61c4878aSAndroid Build Coastguard Worker 730*61c4878aSAndroid Build Coastguard Worker might expand to multiple separate arguments: 731*61c4878aSAndroid Build Coastguard Worker 732*61c4878aSAndroid Build Coastguard Worker .. code-block:: 733*61c4878aSAndroid Build Coastguard Worker 734*61c4878aSAndroid Build Coastguard Worker "/home/User/project_root/out/obj/foo/bar/a_source_set.file_a.cc.o" 735*61c4878aSAndroid Build Coastguard Worker "/home/User/project_root/out/obj/foo/bar/a_source_set.file_b.cc.o" 736*61c4878aSAndroid Build Coastguard Worker "/home/User/project_root/out/obj/foo/bar/a_source_set.file_c.cc.o" 737*61c4878aSAndroid Build Coastguard Worker 738*61c4878aSAndroid Build Coastguard WorkerExample 739*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 740*61c4878aSAndroid Build Coastguard Worker.. code-block:: 741*61c4878aSAndroid Build Coastguard Worker 742*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/python_action.gni") 743*61c4878aSAndroid Build Coastguard Worker 744*61c4878aSAndroid Build Coastguard Worker pw_python_action("postprocess_main_image") { 745*61c4878aSAndroid Build Coastguard Worker script = "py/postprocess_binary.py" 746*61c4878aSAndroid Build Coastguard Worker args = [ 747*61c4878aSAndroid Build Coastguard Worker "--database", 748*61c4878aSAndroid Build Coastguard Worker rebase_path("my/database.csv", root_build_dir), 749*61c4878aSAndroid Build Coastguard Worker "--binary=<TARGET_FILE(//firmware/images:main)>", 750*61c4878aSAndroid Build Coastguard Worker ] 751*61c4878aSAndroid Build Coastguard Worker stamp = true 752*61c4878aSAndroid Build Coastguard Worker } 753*61c4878aSAndroid Build Coastguard Worker 754*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-evaluate-path-expressions: 755*61c4878aSAndroid Build Coastguard Worker 756*61c4878aSAndroid Build Coastguard Workerpw_evaluate_path_expressions 757*61c4878aSAndroid Build Coastguard Worker---------------------------- 758*61c4878aSAndroid Build Coastguard WorkerIt is not always feasible to pass information to a script through command line 759*61c4878aSAndroid Build Coastguard Workerarguments. If a script requires a large amount of input data, writing to a file 760*61c4878aSAndroid Build Coastguard Workeris often more convenient. However, doing so bypasses ``pw_python_action``'s GN 761*61c4878aSAndroid Build Coastguard Workertarget label resolution, preventing such scripts from working with build 762*61c4878aSAndroid Build Coastguard Workerartifacts in a build system-agnostic manner. 763*61c4878aSAndroid Build Coastguard Worker 764*61c4878aSAndroid Build Coastguard Worker``pw_evaluate_path_expressions`` is designed to address this use case. It takes 765*61c4878aSAndroid Build Coastguard Workera list of input files and resolves target expressions within them, modifying the 766*61c4878aSAndroid Build Coastguard Workerfiles in-place. 767*61c4878aSAndroid Build Coastguard Worker 768*61c4878aSAndroid Build Coastguard WorkerRefer to ``pw_python_action``'s :ref:`module-pw_build-python-action-expressions` 769*61c4878aSAndroid Build Coastguard Workersection for the list of supported expressions. 770*61c4878aSAndroid Build Coastguard Worker 771*61c4878aSAndroid Build Coastguard Worker.. note:: 772*61c4878aSAndroid Build Coastguard Worker 773*61c4878aSAndroid Build Coastguard Worker ``pw_evaluate_path_expressions`` is typically used as an intermediate 774*61c4878aSAndroid Build Coastguard Worker sub-target of a larger template, rather than a standalone build target. 775*61c4878aSAndroid Build Coastguard Worker 776*61c4878aSAndroid Build Coastguard WorkerArguments 777*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 778*61c4878aSAndroid Build Coastguard Worker* ``files``: A list of scopes, each containing a ``source`` file to process and 779*61c4878aSAndroid Build Coastguard Worker a ``dest`` file to which to write the result. 780*61c4878aSAndroid Build Coastguard Worker 781*61c4878aSAndroid Build Coastguard WorkerExample 782*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 783*61c4878aSAndroid Build Coastguard WorkerThe following template defines an executable target which additionally outputs 784*61c4878aSAndroid Build Coastguard Workerthe list of object files from which it was compiled, making use of 785*61c4878aSAndroid Build Coastguard Worker``pw_evaluate_path_expressions`` to resolve their paths. 786*61c4878aSAndroid Build Coastguard Worker 787*61c4878aSAndroid Build Coastguard Worker.. code-block:: 788*61c4878aSAndroid Build Coastguard Worker 789*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/evaluate_path_expressions.gni") 790*61c4878aSAndroid Build Coastguard Worker 791*61c4878aSAndroid Build Coastguard Worker template("executable_with_artifacts") { 792*61c4878aSAndroid Build Coastguard Worker executable("${target_name}.exe") { 793*61c4878aSAndroid Build Coastguard Worker sources = invoker.sources 794*61c4878aSAndroid Build Coastguard Worker if defined(invoker.deps) { 795*61c4878aSAndroid Build Coastguard Worker deps = invoker.deps 796*61c4878aSAndroid Build Coastguard Worker } 797*61c4878aSAndroid Build Coastguard Worker } 798*61c4878aSAndroid Build Coastguard Worker 799*61c4878aSAndroid Build Coastguard Worker _artifacts_input = "$target_gen_dir/${target_name}_artifacts.json.in" 800*61c4878aSAndroid Build Coastguard Worker _artifacts_output = "$target_gen_dir/${target_name}_artifacts.json" 801*61c4878aSAndroid Build Coastguard Worker _artifacts = { 802*61c4878aSAndroid Build Coastguard Worker binary = "<TARGET_FILE(:${target_name}.exe)>" 803*61c4878aSAndroid Build Coastguard Worker objects = "<TARGET_OBJECTS(:${target_name}.exe)>" 804*61c4878aSAndroid Build Coastguard Worker } 805*61c4878aSAndroid Build Coastguard Worker write_file(_artifacts_input, _artifacts, "json") 806*61c4878aSAndroid Build Coastguard Worker 807*61c4878aSAndroid Build Coastguard Worker pw_evaluate_path_expressions("${target_name}.evaluate") { 808*61c4878aSAndroid Build Coastguard Worker files = [ 809*61c4878aSAndroid Build Coastguard Worker { 810*61c4878aSAndroid Build Coastguard Worker source = _artifacts_input 811*61c4878aSAndroid Build Coastguard Worker dest = _artifacts_output 812*61c4878aSAndroid Build Coastguard Worker }, 813*61c4878aSAndroid Build Coastguard Worker ] 814*61c4878aSAndroid Build Coastguard Worker } 815*61c4878aSAndroid Build Coastguard Worker 816*61c4878aSAndroid Build Coastguard Worker group(target_name) { 817*61c4878aSAndroid Build Coastguard Worker deps = [ 818*61c4878aSAndroid Build Coastguard Worker ":${target_name}.exe", 819*61c4878aSAndroid Build Coastguard Worker ":${target_name}.evaluate", 820*61c4878aSAndroid Build Coastguard Worker ] 821*61c4878aSAndroid Build Coastguard Worker } 822*61c4878aSAndroid Build Coastguard Worker } 823*61c4878aSAndroid Build Coastguard Worker 824*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-pw_exec: 825*61c4878aSAndroid Build Coastguard Worker 826*61c4878aSAndroid Build Coastguard Workerpw_exec 827*61c4878aSAndroid Build Coastguard Worker------- 828*61c4878aSAndroid Build Coastguard Worker``pw_exec`` allows for execution of arbitrary programs. It is a wrapper around 829*61c4878aSAndroid Build Coastguard Worker``pw_python_action`` but allows for specifying the program to execute. 830*61c4878aSAndroid Build Coastguard Worker 831*61c4878aSAndroid Build Coastguard Worker.. note:: 832*61c4878aSAndroid Build Coastguard Worker 833*61c4878aSAndroid Build Coastguard Worker Prefer to use ``pw_python_action`` instead of calling out to shell 834*61c4878aSAndroid Build Coastguard Worker scripts, as the Python will be more portable. ``pw_exec`` should generally 835*61c4878aSAndroid Build Coastguard Worker only be used for interacting with legacy/existing scripts. 836*61c4878aSAndroid Build Coastguard Worker 837*61c4878aSAndroid Build Coastguard WorkerArguments 838*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 839*61c4878aSAndroid Build Coastguard Worker* ``program``: The program to run. Can be a full path or just a name (in which 840*61c4878aSAndroid Build Coastguard Worker case $PATH is searched). 841*61c4878aSAndroid Build Coastguard Worker* ``args``: Optional list of arguments to the program. 842*61c4878aSAndroid Build Coastguard Worker* ``deps``: Dependencies for this target. 843*61c4878aSAndroid Build Coastguard Worker* ``public_deps``: Public dependencies for this target. In addition to outputs 844*61c4878aSAndroid Build Coastguard Worker from this target, outputs generated by public dependencies can be used as 845*61c4878aSAndroid Build Coastguard Worker inputs from targets that depend on this one. This is not the case for private 846*61c4878aSAndroid Build Coastguard Worker deps. 847*61c4878aSAndroid Build Coastguard Worker* ``inputs``: Optional list of build inputs to the program. 848*61c4878aSAndroid Build Coastguard Worker* ``outputs``: Optional list of artifacts produced by the program's execution. 849*61c4878aSAndroid Build Coastguard Worker* ``env``: Optional list of key-value pairs defining environment variables for 850*61c4878aSAndroid Build Coastguard Worker the program. 851*61c4878aSAndroid Build Coastguard Worker* ``env_file``: Optional path to a file containing a list of newline-separated 852*61c4878aSAndroid Build Coastguard Worker key-value pairs defining environment variables for the program. 853*61c4878aSAndroid Build Coastguard Worker* ``args_file``: Optional path to a file containing additional positional 854*61c4878aSAndroid Build Coastguard Worker arguments to the program. Each line of the file is appended to the 855*61c4878aSAndroid Build Coastguard Worker invocation. Useful for specifying arguments from GN metadata. 856*61c4878aSAndroid Build Coastguard Worker* ``skip_empty_args``: If args_file is provided, boolean indicating whether to 857*61c4878aSAndroid Build Coastguard Worker skip running the program if the file is empty. Used to avoid running 858*61c4878aSAndroid Build Coastguard Worker commands which error when called without arguments. 859*61c4878aSAndroid Build Coastguard Worker* ``capture_output``: If true, output from the program is hidden unless the 860*61c4878aSAndroid Build Coastguard Worker program exits with an error. Defaults to true. 861*61c4878aSAndroid Build Coastguard Worker* ``working_directory``: The working directory to execute the subprocess with. 862*61c4878aSAndroid Build Coastguard Worker If not specified it will not be set and the subprocess will have whatever 863*61c4878aSAndroid Build Coastguard Worker the parent current working directory is. 864*61c4878aSAndroid Build Coastguard Worker* ``venv``: Python virtualenv to pass along to the underlying 865*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_build-pw_python_action`. 866*61c4878aSAndroid Build Coastguard Worker* ``visibility``: GN visibility to apply to the underlying target. 867*61c4878aSAndroid Build Coastguard Worker 868*61c4878aSAndroid Build Coastguard WorkerExample 869*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 870*61c4878aSAndroid Build Coastguard Worker.. code-block:: 871*61c4878aSAndroid Build Coastguard Worker 872*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/exec.gni") 873*61c4878aSAndroid Build Coastguard Worker 874*61c4878aSAndroid Build Coastguard Worker pw_exec("hello_world") { 875*61c4878aSAndroid Build Coastguard Worker program = "/bin/sh" 876*61c4878aSAndroid Build Coastguard Worker args = [ 877*61c4878aSAndroid Build Coastguard Worker "-c", 878*61c4878aSAndroid Build Coastguard Worker "echo hello \$WORLD", 879*61c4878aSAndroid Build Coastguard Worker ] 880*61c4878aSAndroid Build Coastguard Worker env = [ 881*61c4878aSAndroid Build Coastguard Worker "WORLD=world", 882*61c4878aSAndroid Build Coastguard Worker ] 883*61c4878aSAndroid Build Coastguard Worker } 884*61c4878aSAndroid Build Coastguard Worker 885*61c4878aSAndroid Build Coastguard Workerpw_input_group 886*61c4878aSAndroid Build Coastguard Worker-------------- 887*61c4878aSAndroid Build Coastguard Worker``pw_input_group`` defines a group of input files which are not directly 888*61c4878aSAndroid Build Coastguard Workerprocessed by the build but are still important dependencies of later build 889*61c4878aSAndroid Build Coastguard Workersteps. This is commonly used alongside metadata to propagate file dependencies 890*61c4878aSAndroid Build Coastguard Workerthrough the build graph and force rebuilds on file modifications. 891*61c4878aSAndroid Build Coastguard Worker 892*61c4878aSAndroid Build Coastguard WorkerFor example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs 893*61c4878aSAndroid Build Coastguard Workermetadata from a list of input files. The metadata file is not actually part of 894*61c4878aSAndroid Build Coastguard Workerthe build, and so changes to any of the input files do not trigger a rebuild. 895*61c4878aSAndroid Build Coastguard WorkerThis is problematic, as targets that depend on the metadata should rebuild when 896*61c4878aSAndroid Build Coastguard Workerthe inputs are modified but GN cannot express this dependency. 897*61c4878aSAndroid Build Coastguard Worker 898*61c4878aSAndroid Build Coastguard Worker``pw_input_group`` solves this problem by allowing a list of files to be listed 899*61c4878aSAndroid Build Coastguard Workerin a target that does not output any build artifacts, causing all dependent 900*61c4878aSAndroid Build Coastguard Workertargets to correctly rebuild. 901*61c4878aSAndroid Build Coastguard Worker 902*61c4878aSAndroid Build Coastguard WorkerArguments 903*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 904*61c4878aSAndroid Build Coastguard Worker``pw_input_group`` accepts all arguments that can be passed to a ``group`` 905*61c4878aSAndroid Build Coastguard Workertarget, as well as requiring one extra: 906*61c4878aSAndroid Build Coastguard Worker 907*61c4878aSAndroid Build Coastguard Worker* ``inputs``: List of input files. 908*61c4878aSAndroid Build Coastguard Worker 909*61c4878aSAndroid Build Coastguard WorkerExample 910*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 911*61c4878aSAndroid Build Coastguard Worker.. code-block:: 912*61c4878aSAndroid Build Coastguard Worker 913*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/input_group.gni") 914*61c4878aSAndroid Build Coastguard Worker 915*61c4878aSAndroid Build Coastguard Worker pw_input_group("foo_metadata") { 916*61c4878aSAndroid Build Coastguard Worker metadata = { 917*61c4878aSAndroid Build Coastguard Worker files = [ 918*61c4878aSAndroid Build Coastguard Worker "x.foo", 919*61c4878aSAndroid Build Coastguard Worker "y.foo", 920*61c4878aSAndroid Build Coastguard Worker "z.foo", 921*61c4878aSAndroid Build Coastguard Worker ] 922*61c4878aSAndroid Build Coastguard Worker } 923*61c4878aSAndroid Build Coastguard Worker inputs = metadata.files 924*61c4878aSAndroid Build Coastguard Worker } 925*61c4878aSAndroid Build Coastguard Worker 926*61c4878aSAndroid Build Coastguard WorkerTargets depending on ``foo_metadata`` will rebuild when any of the ``.foo`` 927*61c4878aSAndroid Build Coastguard Workerfiles are modified. 928*61c4878aSAndroid Build Coastguard Worker 929*61c4878aSAndroid Build Coastguard Workerpw_zip 930*61c4878aSAndroid Build Coastguard Worker------ 931*61c4878aSAndroid Build Coastguard Worker``pw_zip`` is a target that allows users to zip up a set of input files and 932*61c4878aSAndroid Build Coastguard Workerdirectories into a single output ``.zip`` file—a simple automation of a 933*61c4878aSAndroid Build Coastguard Workerpotentially repetitive task. 934*61c4878aSAndroid Build Coastguard Worker 935*61c4878aSAndroid Build Coastguard WorkerArguments 936*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 937*61c4878aSAndroid Build Coastguard Worker* ``inputs``: List of source files as well as the desired relative zip 938*61c4878aSAndroid Build Coastguard Worker destination. See below for the input syntax. 939*61c4878aSAndroid Build Coastguard Worker* ``dirs``: List of entire directories to be zipped as well as the desired 940*61c4878aSAndroid Build Coastguard Worker relative zip destination. See below for the input syntax. 941*61c4878aSAndroid Build Coastguard Worker* ``output``: Filename of output ``.zip`` file. 942*61c4878aSAndroid Build Coastguard Worker* ``deps``: List of dependencies for the target. 943*61c4878aSAndroid Build Coastguard Worker 944*61c4878aSAndroid Build Coastguard WorkerInput Syntax 945*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^ 946*61c4878aSAndroid Build Coastguard WorkerInputs all need to follow the correct syntax: 947*61c4878aSAndroid Build Coastguard Worker 948*61c4878aSAndroid Build Coastguard Worker#. Path to source file or directory. Directories must end with a ``/``. 949*61c4878aSAndroid Build Coastguard Worker#. The delimiter (defaults to ``>``). 950*61c4878aSAndroid Build Coastguard Worker#. The desired destination of the contents within the ``.zip``. Must start 951*61c4878aSAndroid Build Coastguard Worker with ``/`` to indicate the zip root. Any number of subdirectories are 952*61c4878aSAndroid Build Coastguard Worker allowed. If the source is a file it can be put into any subdirectory of the 953*61c4878aSAndroid Build Coastguard Worker root. If the source is a file, the zip copy can also be renamed by ending 954*61c4878aSAndroid Build Coastguard Worker the zip destination with a filename (no trailing ``/``). 955*61c4878aSAndroid Build Coastguard Worker 956*61c4878aSAndroid Build Coastguard WorkerThus, it should look like the following: ``"[source file or dir] > /"``. 957*61c4878aSAndroid Build Coastguard Worker 958*61c4878aSAndroid Build Coastguard WorkerExample 959*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 960*61c4878aSAndroid Build Coastguard WorkerLet's say we have the following structure for a ``//source/`` directory: 961*61c4878aSAndroid Build Coastguard Worker 962*61c4878aSAndroid Build Coastguard Worker.. code-block:: 963*61c4878aSAndroid Build Coastguard Worker 964*61c4878aSAndroid Build Coastguard Worker source/ 965*61c4878aSAndroid Build Coastguard Worker ├── file1.txt 966*61c4878aSAndroid Build Coastguard Worker ├── file2.txt 967*61c4878aSAndroid Build Coastguard Worker ├── file3.txt 968*61c4878aSAndroid Build Coastguard Worker └── some_dir/ 969*61c4878aSAndroid Build Coastguard Worker ├── file4.txt 970*61c4878aSAndroid Build Coastguard Worker └── some_other_dir/ 971*61c4878aSAndroid Build Coastguard Worker └── file5.txt 972*61c4878aSAndroid Build Coastguard Worker 973*61c4878aSAndroid Build Coastguard WorkerAnd we create the following build target: 974*61c4878aSAndroid Build Coastguard Worker 975*61c4878aSAndroid Build Coastguard Worker.. code-block:: 976*61c4878aSAndroid Build Coastguard Worker 977*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_build/zip.gni") 978*61c4878aSAndroid Build Coastguard Worker 979*61c4878aSAndroid Build Coastguard Worker pw_zip("target_name") { 980*61c4878aSAndroid Build Coastguard Worker inputs = [ 981*61c4878aSAndroid Build Coastguard Worker "//source/file1.txt > /", # Copied to the zip root dir. 982*61c4878aSAndroid Build Coastguard Worker "//source/file2.txt > /renamed.txt", # File renamed. 983*61c4878aSAndroid Build Coastguard Worker "//source/file3.txt > /bar/", # File moved to the /bar/ dir. 984*61c4878aSAndroid Build Coastguard Worker ] 985*61c4878aSAndroid Build Coastguard Worker 986*61c4878aSAndroid Build Coastguard Worker dirs = [ 987*61c4878aSAndroid Build Coastguard Worker "//source/some_dir/ > /bar/some_dir/", # All /some_dir/ contents copied 988*61c4878aSAndroid Build Coastguard Worker # as /bar/some_dir/. 989*61c4878aSAndroid Build Coastguard Worker ] 990*61c4878aSAndroid Build Coastguard Worker 991*61c4878aSAndroid Build Coastguard Worker # Note on output: if the specific output directory isn't defined 992*61c4878aSAndroid Build Coastguard Worker # (such as output = "zoo.zip") then the .zip will output to the 993*61c4878aSAndroid Build Coastguard Worker # same directory as the BUILD.gn file that called the target. 994*61c4878aSAndroid Build Coastguard Worker output = "//$target_out_dir/foo.zip" # Where the foo.zip will end up 995*61c4878aSAndroid Build Coastguard Worker } 996*61c4878aSAndroid Build Coastguard Worker 997*61c4878aSAndroid Build Coastguard WorkerThis will result in a ``.zip`` file called ``foo.zip`` stored in 998*61c4878aSAndroid Build Coastguard Worker``//$target_out_dir`` with the following structure: 999*61c4878aSAndroid Build Coastguard Worker 1000*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1001*61c4878aSAndroid Build Coastguard Worker 1002*61c4878aSAndroid Build Coastguard Worker foo.zip 1003*61c4878aSAndroid Build Coastguard Worker ├── bar/ 1004*61c4878aSAndroid Build Coastguard Worker │ ├── file3.txt 1005*61c4878aSAndroid Build Coastguard Worker │ └── some_dir/ 1006*61c4878aSAndroid Build Coastguard Worker │ ├── file4.txt 1007*61c4878aSAndroid Build Coastguard Worker │ └── some_other_dir/ 1008*61c4878aSAndroid Build Coastguard Worker │ └── file5.txt 1009*61c4878aSAndroid Build Coastguard Worker ├── file1.txt 1010*61c4878aSAndroid Build Coastguard Worker └── renamed.txt 1011*61c4878aSAndroid Build Coastguard Worker 1012*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-relative-source-file-names: 1013*61c4878aSAndroid Build Coastguard Worker 1014*61c4878aSAndroid Build Coastguard Workerpw_relative_source_file_names 1015*61c4878aSAndroid Build Coastguard Worker----------------------------- 1016*61c4878aSAndroid Build Coastguard WorkerThis template recursively walks the listed dependencies and collects the names 1017*61c4878aSAndroid Build Coastguard Workerof all the headers and source files required by the targets, and then transforms 1018*61c4878aSAndroid Build Coastguard Workerthem such that they reflect the ``__FILE__`` when pw_build's ``relative_paths`` 1019*61c4878aSAndroid Build Coastguard Workerconfig is applied. This is primarily intended for side-band generation of 1020*61c4878aSAndroid Build Coastguard Workerpw_tokenizer tokens so file name tokens can be utilized in places where 1021*61c4878aSAndroid Build Coastguard Workerpw_tokenizer is unable to embed token information as part of C/C++ compilation. 1022*61c4878aSAndroid Build Coastguard Worker 1023*61c4878aSAndroid Build Coastguard WorkerThis template produces a JSON file containing an array of strings (file paths 1024*61c4878aSAndroid Build Coastguard Workerwith ``-ffile-prefix-map``-like transformations applied) that can be used to 1025*61c4878aSAndroid Build Coastguard Worker:ref:`generate a token database <module-pw_tokenizer-database-creation>`. 1026*61c4878aSAndroid Build Coastguard Worker 1027*61c4878aSAndroid Build Coastguard WorkerArguments 1028*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 1029*61c4878aSAndroid Build Coastguard Worker* ``deps``: A required list of targets to recursively extract file names from. 1030*61c4878aSAndroid Build Coastguard Worker* ``outputs``: A required array with a single element: the path to write the 1031*61c4878aSAndroid Build Coastguard Worker final JSON file to. 1032*61c4878aSAndroid Build Coastguard Worker 1033*61c4878aSAndroid Build Coastguard WorkerExample 1034*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 1035*61c4878aSAndroid Build Coastguard WorkerLet's say we have the following project structure: 1036*61c4878aSAndroid Build Coastguard Worker 1037*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1038*61c4878aSAndroid Build Coastguard Worker 1039*61c4878aSAndroid Build Coastguard Worker project root 1040*61c4878aSAndroid Build Coastguard Worker ├── foo/ 1041*61c4878aSAndroid Build Coastguard Worker │ ├── foo.h 1042*61c4878aSAndroid Build Coastguard Worker │ └── foo.cc 1043*61c4878aSAndroid Build Coastguard Worker ├── bar/ 1044*61c4878aSAndroid Build Coastguard Worker │ ├── bar.h 1045*61c4878aSAndroid Build Coastguard Worker │ └── bar.cc 1046*61c4878aSAndroid Build Coastguard Worker ├── unused/ 1047*61c4878aSAndroid Build Coastguard Worker │ ├── unused.h 1048*61c4878aSAndroid Build Coastguard Worker │ └── unused.cc 1049*61c4878aSAndroid Build Coastguard Worker └── main.cc 1050*61c4878aSAndroid Build Coastguard Worker 1051*61c4878aSAndroid Build Coastguard WorkerAnd a BUILD.gn at the root: 1052*61c4878aSAndroid Build Coastguard Worker 1053*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1054*61c4878aSAndroid Build Coastguard Worker 1055*61c4878aSAndroid Build Coastguard Worker pw_source_set("bar") { 1056*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":bar_headers" ] 1057*61c4878aSAndroid Build Coastguard Worker public = [ "bar/bar.h" ] 1058*61c4878aSAndroid Build Coastguard Worker sources = [ "bar/bar.cc" ] 1059*61c4878aSAndroid Build Coastguard Worker } 1060*61c4878aSAndroid Build Coastguard Worker 1061*61c4878aSAndroid Build Coastguard Worker pw_source_set("foo") { 1062*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":foo_headers" ] 1063*61c4878aSAndroid Build Coastguard Worker public = [ "foo/foo.h" ] 1064*61c4878aSAndroid Build Coastguard Worker sources = [ "foo/foo.cc" ] 1065*61c4878aSAndroid Build Coastguard Worker deps = [ ":bar" ] 1066*61c4878aSAndroid Build Coastguard Worker } 1067*61c4878aSAndroid Build Coastguard Worker 1068*61c4878aSAndroid Build Coastguard Worker 1069*61c4878aSAndroid Build Coastguard Worker pw_source_set("unused") { 1070*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":unused_headers" ] 1071*61c4878aSAndroid Build Coastguard Worker public = [ "unused/unused.h" ] 1072*61c4878aSAndroid Build Coastguard Worker sources = [ "unused/unused.cc" ] 1073*61c4878aSAndroid Build Coastguard Worker deps = [ ":bar" ] 1074*61c4878aSAndroid Build Coastguard Worker } 1075*61c4878aSAndroid Build Coastguard Worker 1076*61c4878aSAndroid Build Coastguard Worker pw_executable("main") { 1077*61c4878aSAndroid Build Coastguard Worker sources = [ "main.cc" ] 1078*61c4878aSAndroid Build Coastguard Worker deps = [ ":foo" ] 1079*61c4878aSAndroid Build Coastguard Worker } 1080*61c4878aSAndroid Build Coastguard Worker 1081*61c4878aSAndroid Build Coastguard Worker pw_relative_source_file_names("main_source_files") { 1082*61c4878aSAndroid Build Coastguard Worker deps = [ ":main" ] 1083*61c4878aSAndroid Build Coastguard Worker outputs = [ "$target_gen_dir/main_source_files.json" ] 1084*61c4878aSAndroid Build Coastguard Worker } 1085*61c4878aSAndroid Build Coastguard Worker 1086*61c4878aSAndroid Build Coastguard WorkerThe json file written to `out/gen/main_source_files.json` will contain: 1087*61c4878aSAndroid Build Coastguard Worker 1088*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1089*61c4878aSAndroid Build Coastguard Worker 1090*61c4878aSAndroid Build Coastguard Worker [ 1091*61c4878aSAndroid Build Coastguard Worker "bar/bar.cc", 1092*61c4878aSAndroid Build Coastguard Worker "bar/bar.h", 1093*61c4878aSAndroid Build Coastguard Worker "foo/foo.cc", 1094*61c4878aSAndroid Build Coastguard Worker "foo/foo.h", 1095*61c4878aSAndroid Build Coastguard Worker "main.cc" 1096*61c4878aSAndroid Build Coastguard Worker ] 1097*61c4878aSAndroid Build Coastguard Worker 1098*61c4878aSAndroid Build Coastguard WorkerSince ``unused`` isn't a transitive dependency of ``main``, its source files 1099*61c4878aSAndroid Build Coastguard Workerare not included. Similarly, even though ``bar`` is not a direct dependency of 1100*61c4878aSAndroid Build Coastguard Worker``main``, its source files *are* included because ``foo`` brings in ``bar`` as 1101*61c4878aSAndroid Build Coastguard Workera transitive dependency. 1102*61c4878aSAndroid Build Coastguard Worker 1103*61c4878aSAndroid Build Coastguard WorkerNote how the file paths in this example are relative to the project root rather 1104*61c4878aSAndroid Build Coastguard Workerthan being absolute paths (e.g. ``/home/user/ralph/coding/my_proj/main.cc``). 1105*61c4878aSAndroid Build Coastguard WorkerThis is a result of transformations applied to strip absolute pathing prefixes, 1106*61c4878aSAndroid Build Coastguard Workermatching the behavior of pw_build's ``$dir_pw_build:relative_paths`` config. 1107*61c4878aSAndroid Build Coastguard Worker 1108*61c4878aSAndroid Build Coastguard WorkerBuild time errors: pw_error and pw_build_assert 1109*61c4878aSAndroid Build Coastguard Worker----------------------------------------------- 1110*61c4878aSAndroid Build Coastguard WorkerIn Pigweed's complex, multi-toolchain GN build it is not possible to build every 1111*61c4878aSAndroid Build Coastguard Workertarget in every configuration. GN's ``assert`` statement is not ideal for 1112*61c4878aSAndroid Build Coastguard Workerenforcing the correct configuration because it may prevent the GN build files or 1113*61c4878aSAndroid Build Coastguard Workertargets from being referred to at all, even if they aren't used. 1114*61c4878aSAndroid Build Coastguard Worker 1115*61c4878aSAndroid Build Coastguard WorkerThe ``pw_error`` GN template results in an error if it is executed during the 1116*61c4878aSAndroid Build Coastguard Workerbuild. These error targets can exist in the build graph, but cannot be depended 1117*61c4878aSAndroid Build Coastguard Workeron without an error. 1118*61c4878aSAndroid Build Coastguard Worker 1119*61c4878aSAndroid Build Coastguard Worker``pw_build_assert`` evaluates to a ``pw_error`` if a condition fails or nothing 1120*61c4878aSAndroid Build Coastguard Worker(an empty group) if the condition passes. Targets can add a dependency on a 1121*61c4878aSAndroid Build Coastguard Worker``pw_build_assert`` to enforce a condition at build time. 1122*61c4878aSAndroid Build Coastguard Worker 1123*61c4878aSAndroid Build Coastguard WorkerThe templates for build time errors are defined in ``pw_build/error.gni``. 1124*61c4878aSAndroid Build Coastguard Worker 1125*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-gn-pw_coverage_report: 1126*61c4878aSAndroid Build Coastguard Worker 1127*61c4878aSAndroid Build Coastguard WorkerGenerate code coverage reports: ``pw_coverage_report`` 1128*61c4878aSAndroid Build Coastguard Worker------------------------------------------------------ 1129*61c4878aSAndroid Build Coastguard WorkerPigweed supports generating coverage reports, in a variety of formats, for C/C++ 1130*61c4878aSAndroid Build Coastguard Workercode using the ``pw_coverage_report`` GN template. 1131*61c4878aSAndroid Build Coastguard Worker 1132*61c4878aSAndroid Build Coastguard WorkerCoverage Caveats 1133*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^ 1134*61c4878aSAndroid Build Coastguard WorkerThere are currently two code coverage caveats when enabled: 1135*61c4878aSAndroid Build Coastguard Worker 1136*61c4878aSAndroid Build Coastguard Worker#. Coverage reports are only populated based on host tests that use a ``clang`` 1137*61c4878aSAndroid Build Coastguard Worker toolchain. 1138*61c4878aSAndroid Build Coastguard Worker 1139*61c4878aSAndroid Build Coastguard Worker#. Coverage reports will only show coverage information for headers included in 1140*61c4878aSAndroid Build Coastguard Worker a test binary. 1141*61c4878aSAndroid Build Coastguard Worker 1142*61c4878aSAndroid Build Coastguard WorkerThese two caveats mean that all device-specific code that cannot be compiled for 1143*61c4878aSAndroid Build Coastguard Workerand run on the host will not be able to have reports generated for them, and 1144*61c4878aSAndroid Build Coastguard Workerthat the existence of these files will not appear in any coverage report. 1145*61c4878aSAndroid Build Coastguard Worker 1146*61c4878aSAndroid Build Coastguard WorkerTry to ensure that your code can be written in a way that it can be compiled 1147*61c4878aSAndroid Build Coastguard Workerinto a host test for the purpose of coverage reporting, although this is 1148*61c4878aSAndroid Build Coastguard Workersometimes impossible due to requiring hardware-specific APIs to be available. 1149*61c4878aSAndroid Build Coastguard Worker 1150*61c4878aSAndroid Build Coastguard WorkerCoverage Instrumentation 1151*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^ 1152*61c4878aSAndroid Build Coastguard WorkerFor the ``pw_coverage_report`` to generate meaningful output, you must ensure 1153*61c4878aSAndroid Build Coastguard Workerthat it is invoked by a toolchain that instruments tests for code coverage 1154*61c4878aSAndroid Build Coastguard Workercollection and output. 1155*61c4878aSAndroid Build Coastguard Worker 1156*61c4878aSAndroid Build Coastguard WorkerInstrumentation is controlled by two GN build arguments: 1157*61c4878aSAndroid Build Coastguard Worker 1158*61c4878aSAndroid Build Coastguard Worker- ``pw_toolchain_COVERAGE_ENABLED`` being set to ``true``. 1159*61c4878aSAndroid Build Coastguard Worker- ``pw_toolchain_PROFILE_SOURCE_FILES`` is an optional argument that provides a 1160*61c4878aSAndroid Build Coastguard Worker list of source files to selectively collect coverage. 1161*61c4878aSAndroid Build Coastguard Worker 1162*61c4878aSAndroid Build Coastguard Worker.. note:: 1163*61c4878aSAndroid Build Coastguard Worker 1164*61c4878aSAndroid Build Coastguard Worker It is possible to also instrument binaries for UBSAN, ASAN, or TSAN at the 1165*61c4878aSAndroid Build Coastguard Worker same time as coverage. However, TSAN will find issues in the coverage 1166*61c4878aSAndroid Build Coastguard Worker instrumentation code and fail to properly build. 1167*61c4878aSAndroid Build Coastguard Worker 1168*61c4878aSAndroid Build Coastguard WorkerThis can most easily be done by using the ``host_clang_coverage`` toolchain 1169*61c4878aSAndroid Build Coastguard Workerprovided in :ref:`module-pw_toolchain`, but you can also create custom 1170*61c4878aSAndroid Build Coastguard Workertoolchains that manually set these GN build arguments as well. 1171*61c4878aSAndroid Build Coastguard Worker 1172*61c4878aSAndroid Build Coastguard Worker``pw_coverage_report`` 1173*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^ 1174*61c4878aSAndroid Build Coastguard Worker``pw_coverage_report`` is basically a GN frontend to the ``llvm-cov`` 1175*61c4878aSAndroid Build Coastguard Worker`tool <https://llvm.org/docs/CommandGuide/llvm-cov.html>`_ that can be 1176*61c4878aSAndroid Build Coastguard Workerintegrated into the normal build. 1177*61c4878aSAndroid Build Coastguard Worker 1178*61c4878aSAndroid Build Coastguard WorkerIt can be found at ``pw_build/coverage_report.gni`` and is available through 1179*61c4878aSAndroid Build Coastguard Worker``import("$dir_pw_build/coverage_report.gni")``. 1180*61c4878aSAndroid Build Coastguard Worker 1181*61c4878aSAndroid Build Coastguard WorkerThe supported report formats are: 1182*61c4878aSAndroid Build Coastguard Worker 1183*61c4878aSAndroid Build Coastguard Worker- ``text``: A text representation of the code coverage report. This 1184*61c4878aSAndroid Build Coastguard Worker format is not suitable for further machine manipulation and is instead only 1185*61c4878aSAndroid Build Coastguard Worker useful for cases where a human needs to interpret the report. The text format 1186*61c4878aSAndroid Build Coastguard Worker provides a nice summary, but if you desire to drill down into the coverage 1187*61c4878aSAndroid Build Coastguard Worker details more, please consider using ``html`` instead. 1188*61c4878aSAndroid Build Coastguard Worker 1189*61c4878aSAndroid Build Coastguard Worker - This is equivalent to ``llvm-cov show --format text`` and similar to 1190*61c4878aSAndroid Build Coastguard Worker ``llvm-cov report``. 1191*61c4878aSAndroid Build Coastguard Worker 1192*61c4878aSAndroid Build Coastguard Worker- ``html``: A static HTML site that provides an overall coverage summary and 1193*61c4878aSAndroid Build Coastguard Worker per-file information. This format is not suitable for further machine 1194*61c4878aSAndroid Build Coastguard Worker manipulation and is instead only useful for cases where a human needs to 1195*61c4878aSAndroid Build Coastguard Worker interpret the report. 1196*61c4878aSAndroid Build Coastguard Worker 1197*61c4878aSAndroid Build Coastguard Worker - This is equivalent to ``llvm-cov show --format html``. 1198*61c4878aSAndroid Build Coastguard Worker 1199*61c4878aSAndroid Build Coastguard Worker- ``lcov``: A machine-friendly coverage report format. This format is not human- 1200*61c4878aSAndroid Build Coastguard Worker friendly. If that is necessary, use ``text`` or ``html`` instead. 1201*61c4878aSAndroid Build Coastguard Worker 1202*61c4878aSAndroid Build Coastguard Worker - This is equivalent to ``llvm-cov export --format lcov``. 1203*61c4878aSAndroid Build Coastguard Worker 1204*61c4878aSAndroid Build Coastguard Worker- ``json``: A machine-friendly coverage report format. This format is not human- 1205*61c4878aSAndroid Build Coastguard Worker friendly. If that is necessary, use ``text`` or ``html`` instead. 1206*61c4878aSAndroid Build Coastguard Worker 1207*61c4878aSAndroid Build Coastguard Worker - This is equivalent to ``llvm-cov export --format json``. 1208*61c4878aSAndroid Build Coastguard Worker 1209*61c4878aSAndroid Build Coastguard WorkerArguments 1210*61c4878aSAndroid Build Coastguard Worker""""""""" 1211*61c4878aSAndroid Build Coastguard WorkerThere are three classes of ``template`` arguments: build, coverage, and test. 1212*61c4878aSAndroid Build Coastguard Worker 1213*61c4878aSAndroid Build Coastguard Worker**Build Arguments:** 1214*61c4878aSAndroid Build Coastguard Worker 1215*61c4878aSAndroid Build Coastguard Worker- ``enable_if`` (optional): Conditionally activates coverage report generation when set to 1216*61c4878aSAndroid Build Coastguard Worker a boolean expression that evaluates to ``true``. This can be used to allow 1217*61c4878aSAndroid Build Coastguard Worker project builds to conditionally enable or disable coverage reports to minimize 1218*61c4878aSAndroid Build Coastguard Worker work needed for certain build configurations. 1219*61c4878aSAndroid Build Coastguard Worker 1220*61c4878aSAndroid Build Coastguard Worker- ``failure_mode`` (optional/unstable): Specify the failure mode for 1221*61c4878aSAndroid Build Coastguard Worker ``llvm-profdata`` (used to merge inidividual profraw files from ``pw_test`` 1222*61c4878aSAndroid Build Coastguard Worker runs). Available options are ``"any"`` (default) or ``"all"``. 1223*61c4878aSAndroid Build Coastguard Worker 1224*61c4878aSAndroid Build Coastguard Worker - This should be considered an unstable/deprecated argument that should only 1225*61c4878aSAndroid Build Coastguard Worker be used as a last resort to get a build working again. Using 1226*61c4878aSAndroid Build Coastguard Worker ``failure_mode = "all"`` usually indicates that there are underlying 1227*61c4878aSAndroid Build Coastguard Worker problems in the build or test infrastructure that should be independently 1228*61c4878aSAndroid Build Coastguard Worker resolved. Please reach out to the Pigweed team for assistance. 1229*61c4878aSAndroid Build Coastguard Worker 1230*61c4878aSAndroid Build Coastguard Worker**Coverage Arguments:** 1231*61c4878aSAndroid Build Coastguard Worker 1232*61c4878aSAndroid Build Coastguard Worker- ``filter_paths`` (optional): List of file paths to include when generating the 1233*61c4878aSAndroid Build Coastguard Worker coverage report. These cannot be regular expressions, but can be concrete file 1234*61c4878aSAndroid Build Coastguard Worker or folder paths. Folder paths will allow all files in that directory or any 1235*61c4878aSAndroid Build Coastguard Worker recursive child directory. 1236*61c4878aSAndroid Build Coastguard Worker 1237*61c4878aSAndroid Build Coastguard Worker - These are passed to ``llvm-cov`` by the optional trailing positional 1238*61c4878aSAndroid Build Coastguard Worker ``[SOURCES]`` arguments. 1239*61c4878aSAndroid Build Coastguard Worker 1240*61c4878aSAndroid Build Coastguard Worker- ``ignore_filename_patterns`` (optional): List of file path regular expressions 1241*61c4878aSAndroid Build Coastguard Worker to ignore when generating the coverage report. 1242*61c4878aSAndroid Build Coastguard Worker 1243*61c4878aSAndroid Build Coastguard Worker - These are passed to ``llvm-cov`` via ``--ignore-filename-regex`` named 1244*61c4878aSAndroid Build Coastguard Worker parameters. 1245*61c4878aSAndroid Build Coastguard Worker 1246*61c4878aSAndroid Build Coastguard Worker**Test Arguments (one of these is required to be provided):** 1247*61c4878aSAndroid Build Coastguard Worker 1248*61c4878aSAndroid Build Coastguard Worker- ``tests``: A list of ``pw_test`` :ref:`targets<module-pw_unit_test-pw_test>`. 1249*61c4878aSAndroid Build Coastguard Worker 1250*61c4878aSAndroid Build Coastguard Worker- ``group_deps``: A list of ``pw_test_group`` 1251*61c4878aSAndroid Build Coastguard Worker :ref:`targets<module-pw_unit_test-pw_test_group>`. 1252*61c4878aSAndroid Build Coastguard Worker 1253*61c4878aSAndroid Build Coastguard Worker.. note:: 1254*61c4878aSAndroid Build Coastguard Worker 1255*61c4878aSAndroid Build Coastguard Worker ``tests`` and ``group_deps`` are treated exactly the same by 1256*61c4878aSAndroid Build Coastguard Worker ``pw_coverage_report``, so it is not that important to ensure they are used 1257*61c4878aSAndroid Build Coastguard Worker properly. 1258*61c4878aSAndroid Build Coastguard Worker 1259*61c4878aSAndroid Build Coastguard WorkerTarget Expansion 1260*61c4878aSAndroid Build Coastguard Worker"""""""""""""""" 1261*61c4878aSAndroid Build Coastguard Worker``pw_coverage_report(<target_name>)`` expands to one concrete target for each 1262*61c4878aSAndroid Build Coastguard Workerreport format. 1263*61c4878aSAndroid Build Coastguard Worker 1264*61c4878aSAndroid Build Coastguard Worker- ``<target_name>.text``: Generates the ``text`` coverage report. 1265*61c4878aSAndroid Build Coastguard Worker 1266*61c4878aSAndroid Build Coastguard Worker- ``<target_name>.html``: Generates the ``html`` coverage report. 1267*61c4878aSAndroid Build Coastguard Worker 1268*61c4878aSAndroid Build Coastguard Worker- ``<target_name>.lcov``: Generates the ``lcov`` coverage report. 1269*61c4878aSAndroid Build Coastguard Worker 1270*61c4878aSAndroid Build Coastguard Worker- ``<target_name>.json``: Generates the ``json`` coverage report. 1271*61c4878aSAndroid Build Coastguard Worker 1272*61c4878aSAndroid Build Coastguard WorkerTo use any of these targets, you need only to add a dependency on the desired 1273*61c4878aSAndroid Build Coastguard Workertarget somewhere in your build. 1274*61c4878aSAndroid Build Coastguard Worker 1275*61c4878aSAndroid Build Coastguard WorkerThere is also a ``<target_name>`` target generated that is a ``group`` that adds 1276*61c4878aSAndroid Build Coastguard Workera dependency on all of the format-specific targets listed above. 1277*61c4878aSAndroid Build Coastguard Worker 1278*61c4878aSAndroid Build Coastguard Worker.. note:: 1279*61c4878aSAndroid Build Coastguard Worker These targets are always available, even when the toolchain executing the 1280*61c4878aSAndroid Build Coastguard Worker target does not support coverage or coverage is not enabled. In these cases, 1281*61c4878aSAndroid Build Coastguard Worker the targets are set to empty groups. 1282*61c4878aSAndroid Build Coastguard Worker 1283*61c4878aSAndroid Build Coastguard WorkerCoverage Output 1284*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^ 1285*61c4878aSAndroid Build Coastguard WorkerCoverage reports are currently generated and placed into the build output 1286*61c4878aSAndroid Build Coastguard Workerdirectory associated with the path to the GN file where the 1287*61c4878aSAndroid Build Coastguard Worker``pw_coverage_report`` is used in a subfolder named 1288*61c4878aSAndroid Build Coastguard Worker``<target_name>.<report_type>``. 1289*61c4878aSAndroid Build Coastguard Worker 1290*61c4878aSAndroid Build Coastguard Worker.. note:: 1291*61c4878aSAndroid Build Coastguard Worker 1292*61c4878aSAndroid Build Coastguard Worker Due to limitations with telling GN the entire output of coverage reports 1293*61c4878aSAndroid Build Coastguard Worker (stemming from per-source-file generation for HTML and text representations), 1294*61c4878aSAndroid Build Coastguard Worker it is not as simple as using GN's built-in ``copy`` to be able to move these 1295*61c4878aSAndroid Build Coastguard Worker coverage reports to another output location. However, it seems possible to add 1296*61c4878aSAndroid Build Coastguard Worker a target that can use Python to copy the entire output directory. 1297*61c4878aSAndroid Build Coastguard Worker 1298*61c4878aSAndroid Build Coastguard WorkerImproved Ninja interface 1299*61c4878aSAndroid Build Coastguard Worker------------------------ 1300*61c4878aSAndroid Build Coastguard WorkerNinja includes a basic progress display, showing in a single line the number of 1301*61c4878aSAndroid Build Coastguard Workertargets finished, the total number of targets, and the name of the most recent 1302*61c4878aSAndroid Build Coastguard Workertarget it has either started or finished. 1303*61c4878aSAndroid Build Coastguard Worker 1304*61c4878aSAndroid Build Coastguard WorkerFor additional insight into the status of the build, Pigweed includes a Ninja 1305*61c4878aSAndroid Build Coastguard Workerwrapper, ``pw-wrap-ninja``, that displays additional real-time information about 1306*61c4878aSAndroid Build Coastguard Workerthe progress of the build. The wrapper is invoked the same way you'd normally 1307*61c4878aSAndroid Build Coastguard Workerinvoke Ninja: 1308*61c4878aSAndroid Build Coastguard Worker 1309*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh 1310*61c4878aSAndroid Build Coastguard Worker 1311*61c4878aSAndroid Build Coastguard Worker pw-wrap-ninja -C out 1312*61c4878aSAndroid Build Coastguard Worker 1313*61c4878aSAndroid Build Coastguard WorkerThe script lists the progress of the build, as well as the list of targets that 1314*61c4878aSAndroid Build Coastguard WorkerNinja is currently building, along with a timer that measures how long each 1315*61c4878aSAndroid Build Coastguard Workertarget has been building for: 1316*61c4878aSAndroid Build Coastguard Worker 1317*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1318*61c4878aSAndroid Build Coastguard Worker 1319*61c4878aSAndroid Build Coastguard Worker [51.3s] Building [8924/10690] ... 1320*61c4878aSAndroid Build Coastguard Worker [10.4s] c++ pw_strict_host_clang_debug/obj/pw_string/string_test.lib.string_test.cc.o 1321*61c4878aSAndroid Build Coastguard Worker [ 9.5s] ACTION //pw_console/py:py.lint.mypy(//pw_build/python_toolchain:python) 1322*61c4878aSAndroid Build Coastguard Worker [ 9.4s] ACTION //pw_console/py:py.lint.pylint(//pw_build/python_toolchain:python) 1323*61c4878aSAndroid Build Coastguard Worker [ 6.1s] clang-tidy ../pw_log_rpc/log_service.cc 1324*61c4878aSAndroid Build Coastguard Worker [ 6.1s] clang-tidy ../pw_log_rpc/log_service_test.cc 1325*61c4878aSAndroid Build Coastguard Worker [ 6.1s] clang-tidy ../pw_log_rpc/rpc_log_drain.cc 1326*61c4878aSAndroid Build Coastguard Worker [ 6.1s] clang-tidy ../pw_log_rpc/rpc_log_drain_test.cc 1327*61c4878aSAndroid Build Coastguard Worker [ 5.4s] c++ pw_strict_host_clang_debug/obj/BUILD_DIR/pw_strict_host_clang_debug/gen/pw... 1328*61c4878aSAndroid Build Coastguard Worker ... and 109 more 1329*61c4878aSAndroid Build Coastguard Worker 1330*61c4878aSAndroid Build Coastguard WorkerThis allows you to, at a glance, know what Ninja's currently building, which 1331*61c4878aSAndroid Build Coastguard Workertargets are bottlenecking the rest of the build, and which targets are taking 1332*61c4878aSAndroid Build Coastguard Workeran unusually long time to complete. 1333*61c4878aSAndroid Build Coastguard Worker 1334*61c4878aSAndroid Build Coastguard Worker``pw-wrap-ninja`` includes other useful functionality as well. The 1335*61c4878aSAndroid Build Coastguard Worker``--write-trace`` option writes a build trace to the specified path, which can 1336*61c4878aSAndroid Build Coastguard Workerbe viewed in the `Perfetto UI <https://ui.perfetto.dev/>`_, or via Chrome's 1337*61c4878aSAndroid Build Coastguard Workerbuilt-in ``chrome://tracing`` tool. 1338*61c4878aSAndroid Build Coastguard Worker 1339*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-gn-pw_linker_script: 1340*61c4878aSAndroid Build Coastguard Worker 1341*61c4878aSAndroid Build Coastguard Workerpw_linker_script 1342*61c4878aSAndroid Build Coastguard Worker---------------- 1343*61c4878aSAndroid Build Coastguard WorkerPreprocess a linker script and turn it into a target. 1344*61c4878aSAndroid Build Coastguard Worker 1345*61c4878aSAndroid Build Coastguard WorkerIn lieu of direct GN support for linker scripts, this template makes it 1346*61c4878aSAndroid Build Coastguard Workerpossible to run the C Preprocessor on a linker script file so defines can 1347*61c4878aSAndroid Build Coastguard Workerbe properly evaluated before the linker script is passed to linker. 1348*61c4878aSAndroid Build Coastguard Worker 1349*61c4878aSAndroid Build Coastguard WorkerArguments 1350*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 1351*61c4878aSAndroid Build Coastguard Worker- ``linker_script``: The linker script to send through the C preprocessor. 1352*61c4878aSAndroid Build Coastguard Worker 1353*61c4878aSAndroid Build Coastguard Worker- ``defines``: Preprocessor defines to apply when running the C preprocessor. 1354*61c4878aSAndroid Build Coastguard Worker 1355*61c4878aSAndroid Build Coastguard Worker- ``cflags``: Flags to pass to the C compiler. 1356*61c4878aSAndroid Build Coastguard Worker 1357*61c4878aSAndroid Build Coastguard Worker- ``includes``: Include these files when running the C preprocessor. 1358*61c4878aSAndroid Build Coastguard Worker 1359*61c4878aSAndroid Build Coastguard Worker- ``inputs``: Files that, when changed, should trigger a re-build of the linker 1360*61c4878aSAndroid Build Coastguard Worker script. linker_script and includes are implicitly added to this by the 1361*61c4878aSAndroid Build Coastguard Worker template. 1362*61c4878aSAndroid Build Coastguard Worker 1363*61c4878aSAndroid Build Coastguard WorkerExample 1364*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 1365*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1366*61c4878aSAndroid Build Coastguard Worker 1367*61c4878aSAndroid Build Coastguard Worker pw_linker_script("generic_linker_script") { 1368*61c4878aSAndroid Build Coastguard Worker defines = [ 1369*61c4878aSAndroid Build Coastguard Worker "PW_HEAP_SIZE=1K", 1370*61c4878aSAndroid Build Coastguard Worker "PW_NOINIT_SIZE=512" 1371*61c4878aSAndroid Build Coastguard Worker ] 1372*61c4878aSAndroid Build Coastguard Worker linker_script = "basic_script.ld" 1373*61c4878aSAndroid Build Coastguard Worker } 1374*61c4878aSAndroid Build Coastguard Worker 1375*61c4878aSAndroid Build Coastguard Worker 1376*61c4878aSAndroid Build Coastguard Workerpw_copy_and_patch_file 1377*61c4878aSAndroid Build Coastguard Worker---------------------- 1378*61c4878aSAndroid Build Coastguard WorkerProvides the ability to patch a file as part of the build. 1379*61c4878aSAndroid Build Coastguard Worker 1380*61c4878aSAndroid Build Coastguard WorkerThe source file will not be patched in place, but instead copied into the 1381*61c4878aSAndroid Build Coastguard Workeroutput directory before patching. The output of this target will be the 1382*61c4878aSAndroid Build Coastguard Workerpatched file. 1383*61c4878aSAndroid Build Coastguard Worker 1384*61c4878aSAndroid Build Coastguard WorkerArguments 1385*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 1386*61c4878aSAndroid Build Coastguard Worker- ``source``: The source file to be patched. 1387*61c4878aSAndroid Build Coastguard Worker 1388*61c4878aSAndroid Build Coastguard Worker- ``out``: The output file containing the patched contents. 1389*61c4878aSAndroid Build Coastguard Worker 1390*61c4878aSAndroid Build Coastguard Worker- ``patch_file``: The patch file. 1391*61c4878aSAndroid Build Coastguard Worker 1392*61c4878aSAndroid Build Coastguard Worker- ``root``: The root directory for applying the path. 1393*61c4878aSAndroid Build Coastguard Worker 1394*61c4878aSAndroid Build Coastguard WorkerExample 1395*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 1396*61c4878aSAndroid Build Coastguard Worker 1397*61c4878aSAndroid Build Coastguard WorkerTo apply the patch `changes.patch` to the file `data/file.txt` which is located 1398*61c4878aSAndroid Build Coastguard Workerin the packages directory `<PW_ROOT>/environment/packages/external_sdk`. 1399*61c4878aSAndroid Build Coastguard Worker 1400*61c4878aSAndroid Build Coastguard Worker.. code-block:: 1401*61c4878aSAndroid Build Coastguard Worker 1402*61c4878aSAndroid Build Coastguard Worker pw_copy_and_patch_file("apply_patch") { 1403*61c4878aSAndroid Build Coastguard Worker source = "$EXTERNAL_SDK/data/file.txt" 1404*61c4878aSAndroid Build Coastguard Worker out = "data/patched_file.txt" 1405*61c4878aSAndroid Build Coastguard Worker patch_file = "changes.patch" 1406*61c4878aSAndroid Build Coastguard Worker root = "$EXTERNAL_SDK" 1407*61c4878aSAndroid Build Coastguard Worker } 1408