1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain_bazel-get-started: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker=================================== 4*61c4878aSAndroid Build Coastguard WorkerGet started with pw_toolchain_bazel 5*61c4878aSAndroid Build Coastguard Worker=================================== 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_toolchain_bazel 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard Worker----------- 10*61c4878aSAndroid Build Coastguard WorkerQuick start 11*61c4878aSAndroid Build Coastguard Worker----------- 12*61c4878aSAndroid Build Coastguard WorkerThe fastest way to get started using ``pw_toolchain_bazel`` is to use Pigweed's 13*61c4878aSAndroid Build Coastguard Workerupstream toolchains. 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard Worker1. Enable required features in your project's ``//.bazelrc`` file: 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Worker .. code-block:: sh 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker # Required for new toolchain resolution API. 20*61c4878aSAndroid Build Coastguard Worker build --incompatible_enable_cc_toolchain_resolution 21*61c4878aSAndroid Build Coastguard Worker 22*61c4878aSAndroid Build Coastguard Worker # Do not attempt to configure an autodetected (local) toolchain. 23*61c4878aSAndroid Build Coastguard Worker common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker2. Configure ``pw_toolchain_bazel`` in your project's ``//WORKSPACE`` file: 26*61c4878aSAndroid Build Coastguard Worker 27*61c4878aSAndroid Build Coastguard Worker .. code-block:: py 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker # Add Pigweed itself, as a submodule from `//third_party/pigweed`. 30*61c4878aSAndroid Build Coastguard Worker # 31*61c4878aSAndroid Build Coastguard Worker # TODO: b/300695111 - Support depending on Pigweed as a git_repository, 32*61c4878aSAndroid Build Coastguard Worker # even if you use pw_toolchain. 33*61c4878aSAndroid Build Coastguard Worker local_repository( 34*61c4878aSAndroid Build Coastguard Worker name = "pigweed", 35*61c4878aSAndroid Build Coastguard Worker path = "third_party/pigweed", 36*61c4878aSAndroid Build Coastguard Worker ) 37*61c4878aSAndroid Build Coastguard Worker local_repository( 38*61c4878aSAndroid Build Coastguard Worker name = "pw_toolchain", 39*61c4878aSAndroid Build Coastguard Worker path = "third_party/pigweed/pw_toolchain_bazel", 40*61c4878aSAndroid Build Coastguard Worker ) 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard Worker # Set up CIPD. 43*61c4878aSAndroid Build Coastguard Worker load( 44*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_env_setup/bazel/cipd_setup:cipd_rules.bzl", 45*61c4878aSAndroid Build Coastguard Worker "cipd_client_repository", 46*61c4878aSAndroid Build Coastguard Worker "cipd_repository", 47*61c4878aSAndroid Build Coastguard Worker ) 48*61c4878aSAndroid Build Coastguard Worker 49*61c4878aSAndroid Build Coastguard Worker cipd_client_repository() 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker # Set up and register Pigweed's toolchains. 52*61c4878aSAndroid Build Coastguard Worker load( 53*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_toolchain:register_toolchains.bzl", 54*61c4878aSAndroid Build Coastguard Worker "register_pigweed_cxx_toolchains" 55*61c4878aSAndroid Build Coastguard Worker ) 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard Worker register_pigweed_cxx_toolchains() 58*61c4878aSAndroid Build Coastguard Worker 59*61c4878aSAndroid Build Coastguard WorkerAnd you're done! You should now be able to compile for macOS, Linux, and ARM 60*61c4878aSAndroid Build Coastguard WorkerCortex-M devices. 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain_bazel-get-started-overview: 63*61c4878aSAndroid Build Coastguard Worker 64*61c4878aSAndroid Build Coastguard Worker-------- 65*61c4878aSAndroid Build Coastguard WorkerOverview 66*61c4878aSAndroid Build Coastguard Worker-------- 67*61c4878aSAndroid Build Coastguard WorkerThis guide shows you how to use ``pw_toolchain_bazel`` to assemble a fully 68*61c4878aSAndroid Build Coastguard Workerworking toolchain. There are three core elements in a C/C++ toolchain in 69*61c4878aSAndroid Build Coastguard WorkerBazel: 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker#. The underlying tools used to perform compile and link actions. 72*61c4878aSAndroid Build Coastguard Worker#. Flag declarations that may or may not apply to a given toolchain 73*61c4878aSAndroid Build Coastguard Worker configuration. 74*61c4878aSAndroid Build Coastguard Worker#. The final toolchain definition that binds tools and flag declarations 75*61c4878aSAndroid Build Coastguard Worker together to produce working C/C++ compile and link commands. 76*61c4878aSAndroid Build Coastguard Worker 77*61c4878aSAndroid Build Coastguard WorkerThis guide assumes you have a good grasp on writing Bazel build files, and also 78*61c4878aSAndroid Build Coastguard Workerassumes you have a working understanding of what flags are typically passed to 79*61c4878aSAndroid Build Coastguard Workervarious compile and link tool invocations. 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker-------------------------------- 82*61c4878aSAndroid Build Coastguard WorkerAdding Pigweed to your WORKSPACE 83*61c4878aSAndroid Build Coastguard Worker-------------------------------- 84*61c4878aSAndroid Build Coastguard WorkerBefore you can use Pigweed and ``pw_toolchain_bazel`` in your project, you must 85*61c4878aSAndroid Build Coastguard Workerregister Pigweed in your ``//WORKSPACE`` file: 86*61c4878aSAndroid Build Coastguard Worker 87*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 88*61c4878aSAndroid Build Coastguard Worker 89*61c4878aSAndroid Build Coastguard Worker # Add Pigweed itself, as a submodule from `//third_party/pigweed`. 90*61c4878aSAndroid Build Coastguard Worker # 91*61c4878aSAndroid Build Coastguard Worker # TODO: b/300695111 - Support depending on Pigweed as a git_repository, 92*61c4878aSAndroid Build Coastguard Worker # even if you use pw_toolchain. 93*61c4878aSAndroid Build Coastguard Worker local_repository( 94*61c4878aSAndroid Build Coastguard Worker name = "pigweed", 95*61c4878aSAndroid Build Coastguard Worker path = "third_party/pigweed", 96*61c4878aSAndroid Build Coastguard Worker ) 97*61c4878aSAndroid Build Coastguard Worker local_repository( 98*61c4878aSAndroid Build Coastguard Worker name = "pw_toolchain", 99*61c4878aSAndroid Build Coastguard Worker path = "third_party/pigweed/pw_toolchain_bazel", 100*61c4878aSAndroid Build Coastguard Worker ) 101*61c4878aSAndroid Build Coastguard Worker 102*61c4878aSAndroid Build Coastguard Worker.. admonition:: Note 103*61c4878aSAndroid Build Coastguard Worker 104*61c4878aSAndroid Build Coastguard Worker `b/300695111 <https://issues.pigweed.dev/300695111>`_\: You must add Pigweed 105*61c4878aSAndroid Build Coastguard Worker as a submodule to use Pigweed in a Bazel project. Pigweed does not yet work 106*61c4878aSAndroid Build Coastguard Worker when added as a ``http_repository``. 107*61c4878aSAndroid Build Coastguard Worker 108*61c4878aSAndroid Build Coastguard Worker------------------ 109*61c4878aSAndroid Build Coastguard WorkerConfigure .bazelrc 110*61c4878aSAndroid Build Coastguard Worker------------------ 111*61c4878aSAndroid Build Coastguard WorkerTo use this module's toolchain rules, you must first add a couple 112*61c4878aSAndroid Build Coastguard Workerflags that tell Bazel how to find toolchain definitions. Bazel's ``.bazelrc`` 113*61c4878aSAndroid Build Coastguard Workerlives at the root of your project, and is the source of truth for your 114*61c4878aSAndroid Build Coastguard Workerproject-specific build flags that control Bazel's behavior. 115*61c4878aSAndroid Build Coastguard Worker 116*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh 117*61c4878aSAndroid Build Coastguard Worker 118*61c4878aSAndroid Build Coastguard Worker # Required for new toolchain resolution API. 119*61c4878aSAndroid Build Coastguard Worker build --incompatible_enable_cc_toolchain_resolution 120*61c4878aSAndroid Build Coastguard Worker 121*61c4878aSAndroid Build Coastguard Worker # Do not attempt to configure an autodetected (local) toolchain. We vendor 122*61c4878aSAndroid Build Coastguard Worker # all our toolchains, and CI VMs may not have any local toolchain to detect. 123*61c4878aSAndroid Build Coastguard Worker common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain_bazel-assemble-a-tool-suite: 126*61c4878aSAndroid Build Coastguard Worker 127*61c4878aSAndroid Build Coastguard Worker--------------------- 128*61c4878aSAndroid Build Coastguard WorkerAssemble a tool suite 129*61c4878aSAndroid Build Coastguard Worker--------------------- 130*61c4878aSAndroid Build Coastguard WorkerThe fastest way to get started is using a toolchain tool repository template. 131*61c4878aSAndroid Build Coastguard Worker``pw_toolchain_bazel`` provides pre-assembled templates for ``clang`` and 132*61c4878aSAndroid Build Coastguard Worker``arm-none-eabi-gcc`` toolchains in the 133*61c4878aSAndroid Build Coastguard Worker`@pw_toolchain//build_external <https://cs.opensource.google/pigweed/pigweed/+/main:pw_toolchain_bazel/build_external/>`_ 134*61c4878aSAndroid Build Coastguard Workerpackage. These build files can be attached to an external repository in your 135*61c4878aSAndroid Build Coastguard Worker``WORKSPACE`` file using the ``build_file`` attribute of ``http_archive``, 136*61c4878aSAndroid Build Coastguard Worker``git_repository``, or ``cipd_repository``. 137*61c4878aSAndroid Build Coastguard Worker 138*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker # Declare a toolchain tool suite for Linux. 141*61c4878aSAndroid Build Coastguard Worker http_archive( 142*61c4878aSAndroid Build Coastguard Worker name = "linux_clang_toolchain", 143*61c4878aSAndroid Build Coastguard Worker build_file = "@pw_toolchain//build_external:llvm_clang_legacy.BUILD", 144*61c4878aSAndroid Build Coastguard Worker sha256 = "884ee67d647d77e58740c1e645649e29ae9e8a6fe87c1376be0f3a30f3cc9ab3", 145*61c4878aSAndroid Build Coastguard Worker strip_prefix = "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04", 146*61c4878aSAndroid Build Coastguard Worker url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz", 147*61c4878aSAndroid Build Coastguard Worker ) 148*61c4878aSAndroid Build Coastguard Worker 149*61c4878aSAndroid Build Coastguard Worker--------------------------- 150*61c4878aSAndroid Build Coastguard WorkerCreate toolchain definition 151*61c4878aSAndroid Build Coastguard Worker--------------------------- 152*61c4878aSAndroid Build Coastguard WorkerTo set up a complete toolchain definition, you'll need ``toolchain`` and 153*61c4878aSAndroid Build Coastguard Worker``pw_cc_toolchain`` rules that serve as the core of your toolchain. 154*61c4878aSAndroid Build Coastguard WorkerA simplified example is provided below. 155*61c4878aSAndroid Build Coastguard Worker 156*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard Worker load("@pw_toolchain//cc_toolchain:defs.bzl", "pw_cc_toolchain") 159*61c4878aSAndroid Build Coastguard Worker 160*61c4878aSAndroid Build Coastguard Worker pw_cc_toolchain( 161*61c4878aSAndroid Build Coastguard Worker name = "host_toolchain", 162*61c4878aSAndroid Build Coastguard Worker action_configs = [ 163*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:ar", 164*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:clang", 165*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:clang++", 166*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:lld", 167*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:llvm-cov", 168*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:llvm-objcopy", 169*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:llvm-objdump", 170*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:llvm-strip", 171*61c4878aSAndroid Build Coastguard Worker ], 172*61c4878aSAndroid Build Coastguard Worker cxx_builtin_include_directories = [ 173*61c4878aSAndroid Build Coastguard Worker "%package(@linux_clang_toolchain//)%/include/x86_64-unknown-linux-gnu/c++/v1", 174*61c4878aSAndroid Build Coastguard Worker "%package(@linux_clang_toolchain//)%/include/c++/v1", 175*61c4878aSAndroid Build Coastguard Worker "%package(@linux_clang_toolchain//)%/lib/clang/17/include", 176*61c4878aSAndroid Build Coastguard Worker ], 177*61c4878aSAndroid Build Coastguard Worker toolchain_identifier = "host-linux-toolchain", 178*61c4878aSAndroid Build Coastguard Worker flag_sets = [ 179*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:c++17", 180*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:debugging", 181*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:no_canonical_prefixes", 182*61c4878aSAndroid Build Coastguard Worker ], 183*61c4878aSAndroid Build Coastguard Worker ) 184*61c4878aSAndroid Build Coastguard Worker 185*61c4878aSAndroid Build Coastguard Worker toolchain( 186*61c4878aSAndroid Build Coastguard Worker name = "host_cc_toolchain_linux", 187*61c4878aSAndroid Build Coastguard Worker # This is the list of constraints that must be satisfied for the suite of 188*61c4878aSAndroid Build Coastguard Worker # toolchain tools to be determined as runnable on the current machine. 189*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = [ 190*61c4878aSAndroid Build Coastguard Worker "@platforms//os:linux", 191*61c4878aSAndroid Build Coastguard Worker ], 192*61c4878aSAndroid Build Coastguard Worker # This is the list of constraints that dictates compatibility of the final 193*61c4878aSAndroid Build Coastguard Worker # artifacts produced by this toolchain. 194*61c4878aSAndroid Build Coastguard Worker target_compatible_with = [ 195*61c4878aSAndroid Build Coastguard Worker "@platforms//os:linux", 196*61c4878aSAndroid Build Coastguard Worker ], 197*61c4878aSAndroid Build Coastguard Worker toolchain = ":host_toolchain", 198*61c4878aSAndroid Build Coastguard Worker toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 199*61c4878aSAndroid Build Coastguard Worker ) 200*61c4878aSAndroid Build Coastguard Worker 201*61c4878aSAndroid Build Coastguard WorkerThe ``toolchain`` rule 202*61c4878aSAndroid Build Coastguard Worker====================== 203*61c4878aSAndroid Build Coastguard WorkerThe ``toolchain`` rule communicates to Bazel what kind of toolchains are 204*61c4878aSAndroid Build Coastguard Workeravailable, what environments the tools can run on, and what environment the 205*61c4878aSAndroid Build Coastguard Workerartifacts are intended for. A quick overview of the critical parts of this 206*61c4878aSAndroid Build Coastguard Workerrule are outlined below. 207*61c4878aSAndroid Build Coastguard Worker 208*61c4878aSAndroid Build Coastguard Worker- ``name``: The name of the toolchain rule. This is the label that you 209*61c4878aSAndroid Build Coastguard Worker reference when registering a toolchain so Bazel knows it may use this 210*61c4878aSAndroid Build Coastguard Worker toolchain. 211*61c4878aSAndroid Build Coastguard Worker- ``toolchain_type``: The language this toolchain is designed for. Today, 212*61c4878aSAndroid Build Coastguard Worker ``pw_toolchain_bazel`` only supports C/C++ toolchains via the 213*61c4878aSAndroid Build Coastguard Worker ``@bazel_tools//tools/cpp:toolchain_type`` type. 214*61c4878aSAndroid Build Coastguard Worker- ``exec_compatible_with``: What constraints must be satisfied for this 215*61c4878aSAndroid Build Coastguard Worker toolchain to be compatible with the execution environment. In simpler terms, 216*61c4878aSAndroid Build Coastguard Worker if the machine that is currently running the build is a Linux x86_64 machine, 217*61c4878aSAndroid Build Coastguard Worker it can only use toolchains designed to run on that OS and architecture. 218*61c4878aSAndroid Build Coastguard Worker ``exec_compatible_with`` is what prevents a Linux machine from trying to 219*61c4878aSAndroid Build Coastguard Worker compile using tools designed for a Windows machine and vice versa. 220*61c4878aSAndroid Build Coastguard Worker- ``target_compatible_with``: What constraints must be satisfied for this 221*61c4878aSAndroid Build Coastguard Worker toolchain to be compatible with the targeted environment. Rather than 222*61c4878aSAndroid Build Coastguard Worker specifying whether the *tools* are compatible, this specifies the 223*61c4878aSAndroid Build Coastguard Worker compatibility of the final artifacts produced by this toolchain. 224*61c4878aSAndroid Build Coastguard Worker For example, ``target_compatible_with`` is what tells Bazel that a toolchain 225*61c4878aSAndroid Build Coastguard Worker is building firmware for a Cortex-M4. 226*61c4878aSAndroid Build Coastguard Worker- ``toolchain``: The rule that implements the toolchain behavior. When using 227*61c4878aSAndroid Build Coastguard Worker ``pw_toolchain_bazel``, this points to a :py:class:`pw_cc_toolchain` rule. 228*61c4878aSAndroid Build Coastguard Worker Multiple ``toolchain`` rules can point to the same 229*61c4878aSAndroid Build Coastguard Worker :py:class:`pw_cc_toolchain`, which can be useful for creating parameterized 230*61c4878aSAndroid Build Coastguard Worker toolchains that have a lot in common. 231*61c4878aSAndroid Build Coastguard Worker 232*61c4878aSAndroid Build Coastguard Worker 233*61c4878aSAndroid Build Coastguard WorkerThe ``pw_cc_toolchain`` rule 234*61c4878aSAndroid Build Coastguard Worker============================ 235*61c4878aSAndroid Build Coastguard WorkerThis is the heart of your C/C++ toolchain configuration, and has two main 236*61c4878aSAndroid Build Coastguard Workerconfiguration surfaces of interest. 237*61c4878aSAndroid Build Coastguard Worker 238*61c4878aSAndroid Build Coastguard Worker- :py:attr:`pw_cc_toolchain.action_configs`\: This is a list of bindings that 239*61c4878aSAndroid Build Coastguard Worker map various toolchain actions to the appropriate tools. Typically you'll just 240*61c4878aSAndroid Build Coastguard Worker want to list all of the :py:class:`pw_cc_action_config` rules included in your 241*61c4878aSAndroid Build Coastguard Worker toolchain repository from 242*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_toolchain_bazel-assemble-a-tool-suite`. If you need to swap 243*61c4878aSAndroid Build Coastguard Worker out a particular tool, you can just create a custom 244*61c4878aSAndroid Build Coastguard Worker :py:class:`pw_cc_tool` and :py:class:`pw_cc_action_config` and list it here. 245*61c4878aSAndroid Build Coastguard Worker- :py:attr:`pw_cc_toolchain.flag_sets`\: This lists all the flags 246*61c4878aSAndroid Build Coastguard Worker that are applied when compiling with this toolchain. Each 247*61c4878aSAndroid Build Coastguard Worker :py:class:`pw_cc_flag_set` listed here includes at least one flag that applies 248*61c4878aSAndroid Build Coastguard Worker to at least one kind of action. 249*61c4878aSAndroid Build Coastguard Worker 250*61c4878aSAndroid Build Coastguard WorkerWhile the other attributes of a :py:class:`pw_cc_toolchain` are still required, 251*61c4878aSAndroid Build Coastguard Workertheir behaviors are less interesting from a configuration perspective and are 252*61c4878aSAndroid Build Coastguard Workerrequired for correctness and completeness reasons. See the full 253*61c4878aSAndroid Build Coastguard WorkerAPI reference for :py:class:`pw_cc_toolchain` for more information. 254*61c4878aSAndroid Build Coastguard Worker 255*61c4878aSAndroid Build Coastguard Worker----------------------- 256*61c4878aSAndroid Build Coastguard WorkerRegister your toolchain 257*61c4878aSAndroid Build Coastguard Worker----------------------- 258*61c4878aSAndroid Build Coastguard WorkerOnce you've declared a complete toolchain to your liking, you'll need to 259*61c4878aSAndroid Build Coastguard Workerregister it in your project's ``WORKSPACE`` file so Bazel knows it can use the 260*61c4878aSAndroid Build Coastguard Workernew toolchain. An example for a ``toolchain`` with the name 261*61c4878aSAndroid Build Coastguard Worker``host_cc_toolchain_linux`` living in ``//toolchains/BUILD`` is illustrated 262*61c4878aSAndroid Build Coastguard Workerbelow. 263*61c4878aSAndroid Build Coastguard Worker 264*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 265*61c4878aSAndroid Build Coastguard Worker 266*61c4878aSAndroid Build Coastguard Worker register_toolchains( 267*61c4878aSAndroid Build Coastguard Worker "//toolchains:host_cc_toolchain_linux", 268*61c4878aSAndroid Build Coastguard Worker ) 269*61c4878aSAndroid Build Coastguard Worker 270*61c4878aSAndroid Build Coastguard WorkerAt this point, you should have a custom, working toolchain! For more extensive 271*61c4878aSAndroid Build Coastguard Workerexamples, consider taking a look at Pigweed's 272*61c4878aSAndroid Build Coastguard Worker`fully instantiated and supported toolchains <https://cs.opensource.google/pigweed/pigweed/+/main:pw_toolchain/host_clang/BUILD.bazel>`_ 273*61c4878aSAndroid Build Coastguard Worker 274*61c4878aSAndroid Build Coastguard Worker--------------------------------- 275*61c4878aSAndroid Build Coastguard WorkerCustomize behavior with flag sets 276*61c4878aSAndroid Build Coastguard Worker--------------------------------- 277*61c4878aSAndroid Build Coastguard WorkerNow that your toolchain is working, you can customize it by introducing new flag 278*61c4878aSAndroid Build Coastguard Workersets. 279*61c4878aSAndroid Build Coastguard Worker 280*61c4878aSAndroid Build Coastguard WorkerConfigure warnings 281*61c4878aSAndroid Build Coastguard Worker================== 282*61c4878aSAndroid Build Coastguard WorkerEnabling compiler warnings and setting them to be treated as errors is a great 283*61c4878aSAndroid Build Coastguard Workerway to prevent unintentional bugs that stem from dubious code. 284*61c4878aSAndroid Build Coastguard Worker 285*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 286*61c4878aSAndroid Build Coastguard Worker 287*61c4878aSAndroid Build Coastguard Worker load( 288*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//cc_toolchain:defs.bzl", 289*61c4878aSAndroid Build Coastguard Worker "pw_cc_flag_set", 290*61c4878aSAndroid Build Coastguard Worker ) 291*61c4878aSAndroid Build Coastguard Worker 292*61c4878aSAndroid Build Coastguard Worker pw_cc_flag_set( 293*61c4878aSAndroid Build Coastguard Worker name = "warnings", 294*61c4878aSAndroid Build Coastguard Worker actions = [ 295*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_c_compiler_actions", 296*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_cpp_compiler_actions", 297*61c4878aSAndroid Build Coastguard Worker ], 298*61c4878aSAndroid Build Coastguard Worker flags = [ 299*61c4878aSAndroid Build Coastguard Worker "-Wall", 300*61c4878aSAndroid Build Coastguard Worker "-Wextra", 301*61c4878aSAndroid Build Coastguard Worker "-Werror", # Make all warnings errors, except for the exemptions below. 302*61c4878aSAndroid Build Coastguard Worker "-Wno-error=cpp", # preprocessor #warning statement 303*61c4878aSAndroid Build Coastguard Worker "-Wno-error=deprecated-declarations", # [[deprecated]] attribute 304*61c4878aSAndroid Build Coastguard Worker ], 305*61c4878aSAndroid Build Coastguard Worker ) 306*61c4878aSAndroid Build Coastguard Worker 307*61c4878aSAndroid Build Coastguard WorkerOmit unreferenced symbols 308*61c4878aSAndroid Build Coastguard Worker========================= 309*61c4878aSAndroid Build Coastguard WorkerIf a function, variable, or data section isn't used anywhere in your binaries, 310*61c4878aSAndroid Build Coastguard Workerit can be omitted with the following flag sets. 311*61c4878aSAndroid Build Coastguard Worker 312*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 313*61c4878aSAndroid Build Coastguard Worker 314*61c4878aSAndroid Build Coastguard Worker load( 315*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//cc_toolchain:defs.bzl", 316*61c4878aSAndroid Build Coastguard Worker "pw_cc_flag_set", 317*61c4878aSAndroid Build Coastguard Worker ) 318*61c4878aSAndroid Build Coastguard Worker 319*61c4878aSAndroid Build Coastguard Worker # Treats symbols representing functions and data as individual sections. 320*61c4878aSAndroid Build Coastguard Worker # This is mostly relevant when using `:omit_unused_sections`. 321*61c4878aSAndroid Build Coastguard Worker pw_cc_flag_set( 322*61c4878aSAndroid Build Coastguard Worker name = "function_and_data_sections", 323*61c4878aSAndroid Build Coastguard Worker actions = [ 324*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_c_compiler_actions", 325*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_cpp_compiler_actions", 326*61c4878aSAndroid Build Coastguard Worker ], 327*61c4878aSAndroid Build Coastguard Worker flags = [ 328*61c4878aSAndroid Build Coastguard Worker "-ffunction-sections", 329*61c4878aSAndroid Build Coastguard Worker "-fdata-sections", 330*61c4878aSAndroid Build Coastguard Worker ], 331*61c4878aSAndroid Build Coastguard Worker ) 332*61c4878aSAndroid Build Coastguard Worker 333*61c4878aSAndroid Build Coastguard Worker pw_cc_flag_set( 334*61c4878aSAndroid Build Coastguard Worker name = "omit_unused_sections", 335*61c4878aSAndroid Build Coastguard Worker actions = ["@pw_toolchain//actions:all_link_actions"], 336*61c4878aSAndroid Build Coastguard Worker # This flag is parameterized by operating system. macOS and iOS require 337*61c4878aSAndroid Build Coastguard Worker # a different flag to express this concept. 338*61c4878aSAndroid Build Coastguard Worker flags = select({ 339*61c4878aSAndroid Build Coastguard Worker "@platforms//os:macos": ["-Wl,-dead_strip"], 340*61c4878aSAndroid Build Coastguard Worker "@platforms//os:ios": ["-Wl,-dead_strip"], 341*61c4878aSAndroid Build Coastguard Worker "//conditions:default": ["-Wl,--gc-sections"], 342*61c4878aSAndroid Build Coastguard Worker }), 343*61c4878aSAndroid Build Coastguard Worker ) 344*61c4878aSAndroid Build Coastguard Worker 345*61c4878aSAndroid Build Coastguard WorkerSet global defines 346*61c4878aSAndroid Build Coastguard Worker================== 347*61c4878aSAndroid Build Coastguard WorkerToolchains may declare preprocessor defines that are available for all compile 348*61c4878aSAndroid Build Coastguard Workeractions. 349*61c4878aSAndroid Build Coastguard Worker 350*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 351*61c4878aSAndroid Build Coastguard Worker 352*61c4878aSAndroid Build Coastguard Worker load( 353*61c4878aSAndroid Build Coastguard Worker "["@pw_toolchain//cc_toolchain:defs.bzl"]", 354*61c4878aSAndroid Build Coastguard Worker "pw_cc_flag_set", 355*61c4878aSAndroid Build Coastguard Worker ) 356*61c4878aSAndroid Build Coastguard Worker 357*61c4878aSAndroid Build Coastguard Worker # Specify global defines that should be available to all compile actions. 358*61c4878aSAndroid Build Coastguard Worker pw_cc_flag_set( 359*61c4878aSAndroid Build Coastguard Worker name = "global_defines", 360*61c4878aSAndroid Build Coastguard Worker actions = [ 361*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_asm_compiler_actions", 362*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_c_compiler_actions", 363*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//actions:all_cpp_compiler_actions", 364*61c4878aSAndroid Build Coastguard Worker ], 365*61c4878aSAndroid Build Coastguard Worker flags = [ 366*61c4878aSAndroid Build Coastguard Worker "-DPW_LOG_LEVEL=PW_LOG_LEVEL_INFO", # Omit all debug logs. 367*61c4878aSAndroid Build Coastguard Worker ], 368*61c4878aSAndroid Build Coastguard Worker ) 369*61c4878aSAndroid Build Coastguard Worker 370*61c4878aSAndroid Build Coastguard WorkerBind custom flags to a toolchain 371*61c4878aSAndroid Build Coastguard Worker================================ 372*61c4878aSAndroid Build Coastguard WorkerAfter you've assembled a selection of custom flag sets, you can bind them to 373*61c4878aSAndroid Build Coastguard Workeryour toolchain definition by listing them in 374*61c4878aSAndroid Build Coastguard Worker:py:attr:`pw_cc_toolchain.flag_sets`\: 375*61c4878aSAndroid Build Coastguard Worker 376*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 377*61c4878aSAndroid Build Coastguard Worker :emphasize-lines: 12,13,14,15 378*61c4878aSAndroid Build Coastguard Worker 379*61c4878aSAndroid Build Coastguard Worker pw_cc_toolchain( 380*61c4878aSAndroid Build Coastguard Worker name = "host_toolchain", 381*61c4878aSAndroid Build Coastguard Worker action_configs = [ 382*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:ar", 383*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:clang", 384*61c4878aSAndroid Build Coastguard Worker "@linux_clang_toolchain//:clang++", 385*61c4878aSAndroid Build Coastguard Worker ... 386*61c4878aSAndroid Build Coastguard Worker flag_sets = [ 387*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:c++17", 388*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:debugging", 389*61c4878aSAndroid Build Coastguard Worker "@pw_toolchain//flag_sets:no_canonical_prefixes", 390*61c4878aSAndroid Build Coastguard Worker ":warnings", # Newly added pw_cc_flag_set from above. 391*61c4878aSAndroid Build Coastguard Worker ":function_and_data_sections", # Newly added pw_cc_flag_set from above. 392*61c4878aSAndroid Build Coastguard Worker ":omit_unused_sections", # Newly added pw_cc_flag_set from above. 393*61c4878aSAndroid Build Coastguard Worker ":global_defines", # Newly added pw_cc_flag_set from above. 394*61c4878aSAndroid Build Coastguard Worker ], 395*61c4878aSAndroid Build Coastguard Worker ) 396*61c4878aSAndroid Build Coastguard Worker 397*61c4878aSAndroid Build Coastguard Worker.. admonition:: Note 398*61c4878aSAndroid Build Coastguard Worker 399*61c4878aSAndroid Build Coastguard Worker Flags appear in the tool invocations in the order as they are listed 400*61c4878aSAndroid Build Coastguard Worker in :py:attr:`pw_cc_toolchain.flag_sets`\, so if you need a flag 401*61c4878aSAndroid Build Coastguard Worker to appear earlier in the command-line invocation of the tool just move it to 402*61c4878aSAndroid Build Coastguard Worker towards the beginning of the list. 403*61c4878aSAndroid Build Coastguard Worker 404*61c4878aSAndroid Build Coastguard WorkerYou can use ``pw_cc_flag_set`` rules to add support for new architectures, 405*61c4878aSAndroid Build Coastguard Workerenable/disable warnings, add preprocessor defines, enable LTO, and more. 406