xref: /aosp_15_r20/external/pigweed/pw_toolchain/gn.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain-gn:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker============================
4*61c4878aSAndroid Build Coastguard WorkerGN build system integrations
5*61c4878aSAndroid Build Coastguard Worker============================
6*61c4878aSAndroid Build Coastguard Worker``pw_toolchain`` module provides GN toolchains that may be used to build
7*61c4878aSAndroid Build Coastguard WorkerPigweed. GN toolchains function both as a set of tools for compilation and as a
8*61c4878aSAndroid Build Coastguard Workerworkspace for evaluating build files. The same compilations and actions can be
9*61c4878aSAndroid Build Coastguard Workerexecuted by different toolchains. Each toolchain maintains its own set of
10*61c4878aSAndroid Build Coastguard Worker`build args <https://gn.googlesource.com/gn/+/main/docs/reference.md#buildargs>`_,
11*61c4878aSAndroid Build Coastguard Workerand build steps from all toolchains can be executed in parallel.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard WorkerVarious GCC and Clang toolchains for multiple platforms are provided.
14*61c4878aSAndroid Build Coastguard WorkerToolchains names typically include the compiler (``clang`` or ``gcc`` and
15*61c4878aSAndroid Build Coastguard Workeroptimization level (``debug``, ``size_optimized``, ``speed_optimized``).
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker--------------------
18*61c4878aSAndroid Build Coastguard WorkerNon-C/C++ toolchains
19*61c4878aSAndroid Build Coastguard Worker--------------------
20*61c4878aSAndroid Build Coastguard Worker``pw_toolchain/non_c_toolchain.gni`` provides the ``pw_non_c_toolchain``
21*61c4878aSAndroid Build Coastguard Workertemplate. This template creates toolchains that cannot compile C/C++ source
22*61c4878aSAndroid Build Coastguard Workercode. These toolchains may only be used to execute GN actions or declare groups
23*61c4878aSAndroid Build Coastguard Workerof targets in other toolchains. Attempting to compile C/C++ code with either of
24*61c4878aSAndroid Build Coastguard Workerthese toolchains results in errors.
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard WorkerNon-C/C++ toolchains can be used to consolidate actions that should only occur
27*61c4878aSAndroid Build Coastguard Workeronce in a multi-toolchain build. Build targets from all toolchains can refer to
28*61c4878aSAndroid Build Coastguard Workerthese actions in a non-C/C++ toolchain so they only execute once instead of once
29*61c4878aSAndroid Build Coastguard Workerper toolchain.
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard WorkerFor example, Pigweed runs protobuf compilation and Python package actions like
32*61c4878aSAndroid Build Coastguard Workerinstallation and Pylint in toolchains created with ``pw_non_c_toolchain``. This
33*61c4878aSAndroid Build Coastguard Workerallows all toolchains to cleanly share the same protobuf and Python declarations
34*61c4878aSAndroid Build Coastguard Workerwithout any duplicated work.
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker-------------------------------
37*61c4878aSAndroid Build Coastguard WorkerTesting other compiler versions
38*61c4878aSAndroid Build Coastguard Worker-------------------------------
39*61c4878aSAndroid Build Coastguard WorkerThe clang-based toolchain provided by Pigweed can be substituted with another
40*61c4878aSAndroid Build Coastguard Workerversion by modifying the ``pw_toolchain_CLANG_PREFIX`` GN build argument to
41*61c4878aSAndroid Build Coastguard Workerpoint to the directory that contains the desired clang, clang++, and llvm-ar
42*61c4878aSAndroid Build Coastguard Workerbinaries. This should only be used for debugging purposes. Pigweed does not
43*61c4878aSAndroid Build Coastguard Workerofficially support any compilers other than those provided by Pigweed.
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker------------------------------
46*61c4878aSAndroid Build Coastguard WorkerRunning static analysis checks
47*61c4878aSAndroid Build Coastguard Worker------------------------------
48*61c4878aSAndroid Build Coastguard Worker``clang-tidy`` can be run as a compiler replacement, to analyze all sources
49*61c4878aSAndroid Build Coastguard Workerbuilt for a target. ``pw_toolchain/static_analysis_toolchain.gni`` provides
50*61c4878aSAndroid Build Coastguard Workerthe ``pw_static_analysis_toolchain`` template. This template creates toolchains
51*61c4878aSAndroid Build Coastguard Workerthat execute ``clang-tidy`` for C/C++ sources, and mock implementations of
52*61c4878aSAndroid Build Coastguard Workerthe ``link``, ``alink`` and ``solink`` tools.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard WorkerIn addition to the standard toolchain requirements (``cc``, ``cxx``, etc.), the
55*61c4878aSAndroid Build Coastguard Worker``pw_static_analysis_toolchain`` template requires a scope ``static_analysis``
56*61c4878aSAndroid Build Coastguard Workerto be defined on the invoker.
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker.. code-block::
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker   static_analysis = {
61*61c4878aSAndroid Build Coastguard Worker    # Configure whether static_analysis should be enabled for invoker toolchain.
62*61c4878aSAndroid Build Coastguard Worker    # This is must be set true if using pw_static_analysis_toolchain.
63*61c4878aSAndroid Build Coastguard Worker    enabled = true
64*61c4878aSAndroid Build Coastguard Worker    # Optionally override clang-tidy binary to use by setting to proper path.
65*61c4878aSAndroid Build Coastguard Worker    clang_tidy_path = ""
66*61c4878aSAndroid Build Coastguard Worker    # Optionally specify additional command(s) to run as part of cc tool.
67*61c4878aSAndroid Build Coastguard Worker    cc_post = ""
68*61c4878aSAndroid Build Coastguard Worker    # Optionally specify additional command(s) to run as part of cxx tool.
69*61c4878aSAndroid Build Coastguard Worker    cxx_post = ""
70*61c4878aSAndroid Build Coastguard Worker   }
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard WorkerThe ``generate_toolchain`` supports the above mentioned ``static_analysis``
73*61c4878aSAndroid Build Coastguard Workerscope, which if specified must at the very least define the bool ``enabled``
74*61c4878aSAndroid Build Coastguard Workerwithin the scope. If the ``static_analysis`` scope is provided and
75*61c4878aSAndroid Build Coastguard Worker``static_analysis.enabled = true``, the derived toolchain
76*61c4878aSAndroid Build Coastguard Worker``${target_name}.static_analysis`` will be generated using
77*61c4878aSAndroid Build Coastguard Worker``pw_generate_static_analysis_toolchain`` and the toolchain options.
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard WorkerAn example on the utility of the ``static_analysis`` scope args is shown in the
80*61c4878aSAndroid Build Coastguard Workersnippet below where we enable clang-tidy caching and add ``//.clang-tidy`` as a
81*61c4878aSAndroid Build Coastguard Workerdependency to the generated ``.d`` files for the
82*61c4878aSAndroid Build Coastguard Worker``pw_static_analysis_toolchain``.
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker.. code-block::
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker   static_analysis = {
87*61c4878aSAndroid Build Coastguard Worker    clang_tidy_path = "//third_party/ctcache/clang-tidy"
88*61c4878aSAndroid Build Coastguard Worker    _clang_tidy_cfg_path = rebase_path("//.clang-tidy", root_build_dir)
89*61c4878aSAndroid Build Coastguard Worker    cc_post = "echo '-: $_clang_tidy_cfg_path' >> {{output}}.d"
90*61c4878aSAndroid Build Coastguard Worker    cxx_post = "echo '-: $_clang_tidy_cfg_path' >> {{output}}.d"
91*61c4878aSAndroid Build Coastguard Worker   }
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard WorkerExcluding files from checks
94*61c4878aSAndroid Build Coastguard Worker===========================
95*61c4878aSAndroid Build Coastguard WorkerThe build argument ``pw_toolchain_STATIC_ANALYSIS_SKIP_SOURCES_RES`` is used
96*61c4878aSAndroid Build Coastguard Workerto exclude source files from the analysis. The list must contain regular
97*61c4878aSAndroid Build Coastguard Workerexpressions matching individual files, rather than directories. For example,
98*61c4878aSAndroid Build Coastguard Workerprovide ``"the_path/.*"`` to exclude all files in all directories under
99*61c4878aSAndroid Build Coastguard Worker``the_path``.
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard WorkerThe build argument ``pw_toolchain_STATIC_ANALYSIS_SKIP_INCLUDE_PATHS`` is used
102*61c4878aSAndroid Build Coastguard Workerused to exclude header files from the analysis. This argument must be a list of
103*61c4878aSAndroid Build Coastguard WorkerPOSIX-style path suffixes for include paths, or regular expressions matching
104*61c4878aSAndroid Build Coastguard Workerinclude paths. For example, passing ``the_path/include`` excludes all header
105*61c4878aSAndroid Build Coastguard Workerfiles that are accessed from include paths ending in ``the_path/include``,
106*61c4878aSAndroid Build Coastguard Workerwhile passing ``.*/third_party/.*`` excludes all third-party header files.
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard WorkerNote that ``pw_toolchain_STATIC_ANALYSIS_SKIP_INCLUDE_PATHS`` operates on
109*61c4878aSAndroid Build Coastguard Workerinclude paths, not header file paths. For example, say your compile commands
110*61c4878aSAndroid Build Coastguard Workerinclude ``-Idrivers``, and this results in a file at ``drivers/public/i2c.h``
111*61c4878aSAndroid Build Coastguard Workerbeing included. You can skip this header by adding ``drivers`` or ``drivers.*``
112*61c4878aSAndroid Build Coastguard Workerto ``pw_toolchain_STATIC_ANALYSIS_SKIP_INCLUDE_PATHS``, but *not* by adding
113*61c4878aSAndroid Build Coastguard Worker``drivers/.*``: this last regex matches the header file path, but not the
114*61c4878aSAndroid Build Coastguard Workerinclude path.
115*61c4878aSAndroid Build Coastguard Worker
116*61c4878aSAndroid Build Coastguard WorkerProvided toolchains
117*61c4878aSAndroid Build Coastguard Worker===================
118*61c4878aSAndroid Build Coastguard Worker``pw_toolchain`` provides static analysis GN toolchains that may be used to
119*61c4878aSAndroid Build Coastguard Workertest host targets:
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_host_clang.debug.static_analysis
122*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_host_clang.speed_optimized.static_analysis
123*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_host_clang.size_optimized.static_analysis
124*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_host_clang.fuzz.static_analysis
125*61c4878aSAndroid Build Coastguard Worker  (if pw_toolchain_OSS_FUZZ_ENABLED is false)
126*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_arm_clang.debug.static_analysis
127*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_arm_clang.speed_optimized.static_analysis
128*61c4878aSAndroid Build Coastguard Worker- pw_toolchain_arm_clang.size_optimized.static_analysis
129*61c4878aSAndroid Build Coastguard Worker
130*61c4878aSAndroid Build Coastguard WorkerFor example, to run ``clang-tidy`` on all source dependencies of the
131*61c4878aSAndroid Build Coastguard Worker``default`` target:
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker.. code-block::
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker   generate_toolchain("my_toolchain") {
136*61c4878aSAndroid Build Coastguard Worker     ..
137*61c4878aSAndroid Build Coastguard Worker     static_analysis = {
138*61c4878aSAndroid Build Coastguard Worker      enabled = true
139*61c4878aSAndroid Build Coastguard Worker     }
140*61c4878aSAndroid Build Coastguard Worker   }
141*61c4878aSAndroid Build Coastguard Worker
142*61c4878aSAndroid Build Coastguard Worker   group("static_analysis") {
143*61c4878aSAndroid Build Coastguard Worker     deps = [ ":default(my_toolchain.static_analysis)" ]
144*61c4878aSAndroid Build Coastguard Worker   }
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker.. warning::
147*61c4878aSAndroid Build Coastguard Worker
148*61c4878aSAndroid Build Coastguard Worker   The status of the static analysis checks might change when
149*61c4878aSAndroid Build Coastguard Worker   any relevant .clang-tidy file is updated. You should
150*61c4878aSAndroid Build Coastguard Worker   clean the output directory before invoking
151*61c4878aSAndroid Build Coastguard Worker   ``clang-tidy``.
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker-------------
154*61c4878aSAndroid Build Coastguard WorkerTarget traits
155*61c4878aSAndroid Build Coastguard Worker-------------
156*61c4878aSAndroid Build Coastguard WorkerPigweed targets expose a set of constants that describe properties of the target
157*61c4878aSAndroid Build Coastguard Workeror the toolchain compiling code for it. These are referred to as target traits.
158*61c4878aSAndroid Build Coastguard Worker
159*61c4878aSAndroid Build Coastguard WorkerIn GN, these traits are exposed as GN args and are prefixed with
160*61c4878aSAndroid Build Coastguard Worker``pw_toolchain_`` (e.g. ``pw_toolchain_CXX_STANDARD``). They are defined in
161*61c4878aSAndroid Build Coastguard Worker``pw_toolchain/traits.gni``.
162*61c4878aSAndroid Build Coastguard Worker
163*61c4878aSAndroid Build Coastguard WorkerTraits must never be set by the user (e.g. with ``gn args``). Traits are always
164*61c4878aSAndroid Build Coastguard Workerset by the target.
165*61c4878aSAndroid Build Coastguard Worker
166*61c4878aSAndroid Build Coastguard Worker.. warning::
167*61c4878aSAndroid Build Coastguard Worker
168*61c4878aSAndroid Build Coastguard Worker   This feature is under development and is likely to change significantly.
169*61c4878aSAndroid Build Coastguard Worker   See `b/234883746 <http://issuetracker.google.com/issues/234883746>`_.
170*61c4878aSAndroid Build Coastguard Worker
171*61c4878aSAndroid Build Coastguard WorkerList of traits
172*61c4878aSAndroid Build Coastguard Worker==============
173*61c4878aSAndroid Build Coastguard Worker- ``CXX_STANDARD``. The C++ standard used by the toolchain. The value must be an
174*61c4878aSAndroid Build Coastguard Worker  integer value matching one of the standard values for the ``__cplusplus``
175*61c4878aSAndroid Build Coastguard Worker  macro. For example, ``201703`` corresponds to C++17. See
176*61c4878aSAndroid Build Coastguard Worker  https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros for
177*61c4878aSAndroid Build Coastguard Worker  further details.
178