Bazel build system integrations#

Pigweed provides a suite of Bazel build integrations to compliment existing Bazel toolchain constructs such as rules_cc toolchains to make it easier to design robust, feature-rich toolchains.

Upstream Pigweed toolchains#

Pigweed’s C/C++ toolchains are automatically registered when using Pigweed from a Bzlmod Bazel project. Legacy WORKSPACE-based projects can use Pigweed’s upstream toolchains by calling register_pigweed_cxx_toolchains():

load("@pigweed//pw_toolchain:register_toolchains.bzl", "register_pigweed_cxx_toolchains")

register_pigweed_cxx_toolchains()

Note

Pigweed’s upstream toolchains are subject to change without notice. If you would prefer more stability in toolchain configurations, consider declaring custom toolchains in your project.

Compiler-specific build logic#

Whenever possible, avoid introducing compiler-specific behaviors in Bazel BUILD files. Instead, prefer to design build logic against more intentional Bazel build compatibility patterns. For compiler-specific behavior, this means defining and/or using compiler capabilities like @rules_cc//cc/toolchains/capabilities:supports_interface_shared_libraries

If you need to expose a toolchain capability as a choice in a select, you can use pw_cc_toolchain_feature_is_enabled.

Example:

load(
    "@pigweed//pw_toolchain/cc/current_toolchain:pw_cc_toolchain_feature_is_enabled.bzl",
    "pw_cc_toolchain_feature_is_enabled",
)

pw_cc_toolchain_feature_is_enabled(
    name = "llvm_libc_enabled",
    feature_name = "llvm_libc",
)

cc_library(
    name = "libfoo",
    deps = select({
        ":llvm_libc_enabled": ["//foo:llvm_libc_extras"],
        "//conditions:default": [],
    }),
)

If you absolutely must introduce a select statement that checks the current compiler, use Pigweed’s helper macros.

Example:

load(
    "@pigweed//pw_toolchain/cc/current_toolchain:conditions.bzl",
    "if_compiler_is_clang",
    "if_linker_is_gcc",
)

cc_library(
    copts = if_compiler_is_clang(
        ["-fno-codegen"],
        default = [],
    ),
    linkopts = if_linker_is_gcc(
        ["-Wl,--delete-main"],
        default = [],
    ),
    srcs = ["lib.cc"],
)