1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain-bazel: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker=============================== 4*61c4878aSAndroid Build Coastguard WorkerBazel build system integrations 5*61c4878aSAndroid Build Coastguard Worker=============================== 6*61c4878aSAndroid Build Coastguard WorkerPigweed provides a suite of Bazel build integrations to compliment existing 7*61c4878aSAndroid Build Coastguard WorkerBazel toolchain constructs such as `rules_cc toolchains <https://github.com/bazelbuild/rules_cc/blob/main/cc/toolchains/README.md>`_ 8*61c4878aSAndroid Build Coastguard Workerto make it easier to design robust, feature-rich toolchains. 9*61c4878aSAndroid Build Coastguard Worker 10*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain-bazel-upstream-pigweed-toolchains: 11*61c4878aSAndroid Build Coastguard Worker 12*61c4878aSAndroid Build Coastguard Worker--------------------------- 13*61c4878aSAndroid Build Coastguard WorkerUpstream Pigweed toolchains 14*61c4878aSAndroid Build Coastguard Worker--------------------------- 15*61c4878aSAndroid Build Coastguard WorkerPigweed's C/C++ toolchains are automatically registered when using Pigweed from 16*61c4878aSAndroid Build Coastguard Workera Bzlmod Bazel project. Legacy WORKSPACE-based projects can use Pigweed's 17*61c4878aSAndroid Build Coastguard Workerupstream toolchains by calling ``register_pigweed_cxx_toolchains()``: 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 20*61c4878aSAndroid Build Coastguard Worker 21*61c4878aSAndroid Build Coastguard Worker load("@pigweed//pw_toolchain:register_toolchains.bzl", "register_pigweed_cxx_toolchains") 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Worker register_pigweed_cxx_toolchains() 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker 26*61c4878aSAndroid Build Coastguard Worker.. admonition:: Note 27*61c4878aSAndroid Build Coastguard Worker :class: warning 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker Pigweed's upstream toolchains are subject to change without notice. If you 30*61c4878aSAndroid Build Coastguard Worker would prefer more stability in toolchain configurations, consider declaring 31*61c4878aSAndroid Build Coastguard Worker custom toolchains in your project. 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain-bazel-compiler-specific-logic: 34*61c4878aSAndroid Build Coastguard Worker 35*61c4878aSAndroid Build Coastguard Worker----------------------------- 36*61c4878aSAndroid Build Coastguard WorkerCompiler-specific build logic 37*61c4878aSAndroid Build Coastguard Worker----------------------------- 38*61c4878aSAndroid Build Coastguard WorkerWhenever possible, avoid introducing compiler-specific behaviors in Bazel 39*61c4878aSAndroid Build Coastguard Worker``BUILD`` files. Instead, prefer to design build logic against 40*61c4878aSAndroid Build Coastguard Workermore intentional :ref:`docs-bazel-compatibility`. For compiler-specific 41*61c4878aSAndroid Build Coastguard Workerbehavior, this means defining and/or using compiler capabilities like 42*61c4878aSAndroid Build Coastguard Worker`@rules_cc//cc/toolchains/capabilities:supports_interface_shared_libraries <https://github.com/bazelbuild/rules_cc/blob/main/cc/toolchains/capabilities/BUILD>`__ 43*61c4878aSAndroid Build Coastguard Worker 44*61c4878aSAndroid Build Coastguard WorkerIf you need to expose a toolchain capability as a choice in a select, you 45*61c4878aSAndroid Build Coastguard Workercan use ``pw_cc_toolchain_feature_is_enabled``. 46*61c4878aSAndroid Build Coastguard Worker 47*61c4878aSAndroid Build Coastguard WorkerExample: 48*61c4878aSAndroid Build Coastguard Worker 49*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker load( 52*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_toolchain/cc/current_toolchain:pw_cc_toolchain_feature_is_enabled.bzl", 53*61c4878aSAndroid Build Coastguard Worker "pw_cc_toolchain_feature_is_enabled", 54*61c4878aSAndroid Build Coastguard Worker ) 55*61c4878aSAndroid Build Coastguard Worker 56*61c4878aSAndroid Build Coastguard Worker pw_cc_toolchain_feature_is_enabled( 57*61c4878aSAndroid Build Coastguard Worker name = "llvm_libc_enabled", 58*61c4878aSAndroid Build Coastguard Worker feature_name = "llvm_libc", 59*61c4878aSAndroid Build Coastguard Worker ) 60*61c4878aSAndroid Build Coastguard Worker 61*61c4878aSAndroid Build Coastguard Worker cc_library( 62*61c4878aSAndroid Build Coastguard Worker name = "libfoo", 63*61c4878aSAndroid Build Coastguard Worker deps = select({ 64*61c4878aSAndroid Build Coastguard Worker ":llvm_libc_enabled": ["//foo:llvm_libc_extras"], 65*61c4878aSAndroid Build Coastguard Worker "//conditions:default": [], 66*61c4878aSAndroid Build Coastguard Worker }), 67*61c4878aSAndroid Build Coastguard Worker ) 68*61c4878aSAndroid Build Coastguard Worker 69*61c4878aSAndroid Build Coastguard WorkerIf you absolutely must introduce a ``select`` statement that checks the current 70*61c4878aSAndroid Build Coastguard Workercompiler, use Pigweed's helper macros. 71*61c4878aSAndroid Build Coastguard Worker 72*61c4878aSAndroid Build Coastguard WorkerExample: 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard Worker load( 77*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_toolchain/cc/current_toolchain:conditions.bzl", 78*61c4878aSAndroid Build Coastguard Worker "if_compiler_is_clang", 79*61c4878aSAndroid Build Coastguard Worker "if_linker_is_gcc", 80*61c4878aSAndroid Build Coastguard Worker ) 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker cc_library( 83*61c4878aSAndroid Build Coastguard Worker copts = if_compiler_is_clang( 84*61c4878aSAndroid Build Coastguard Worker ["-fno-codegen"], 85*61c4878aSAndroid Build Coastguard Worker default = [], 86*61c4878aSAndroid Build Coastguard Worker ), 87*61c4878aSAndroid Build Coastguard Worker linkopts = if_linker_is_gcc( 88*61c4878aSAndroid Build Coastguard Worker ["-Wl,--delete-main"], 89*61c4878aSAndroid Build Coastguard Worker default = [], 90*61c4878aSAndroid Build Coastguard Worker ), 91*61c4878aSAndroid Build Coastguard Worker srcs = ["lib.cc"], 92*61c4878aSAndroid Build Coastguard Worker ) 93