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