Add //cc/libc config settings for C standard libraries (#792)

Adds a new `//cc/libc` package with `config_setting`s for common C standard libraries, fed by a new `libc_flag` rule that exposes the current CC toolchain's `target_libc` value to `select()`. This mirrors the existing `//cc/compiler` package for compilers.

Covered libcs: `glibc`, `musl`, `uclibc`, `bionic`, `macosx` (Apple's libSystem, using the historical value set by toolchains for all Apple platforms), `freebsd`, `netbsd`, `openbsd`, `msvcrt`, `ucrt`, `msys`, `newlib`, `picolibc`, `llvm-libc`, and `wasi-libc`. Toolchains not shipped with Bazel are encouraged to use the same canonical names.

Since Bazel documents `target_libc` as "the libc version string (e.g. `glibc-2.2.2`)", `libc_flag` strips a version suffix (everything from the first `-` followed by a digit) before matching, so versioned values still match their family's setting while names like `wasi-libc` and `llvm-libc` are preserved.

The following changes make the new constraints usable:
* the auto-configured Unix toolchain detects the default libc via preprocessor macros and `-dumpmachine`
* the `target_libc` values emitted by the bundled Windows and BSD toolchain configs are changed to the correct values
* rule-based `cc_toolchain`s can declare their `target_libc` or fall back to a reasonable default

Closes #792

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/792 from fmeum:libc-settings 7215a9ced75b2d01816d25b87e76c8387b0eee34
PiperOrigin-RevId: 970542238
Change-Id: Id569a70de73fc51ca8c69ca74169b8ba44cdc1bf
16 files changed
tree: 2a9ee5c2392f6dfa8a723b36c876f10fe6f1bde9
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. cc/
  5. docs/
  6. examples/
  7. tests/
  8. .bazelignore
  9. .bazelrc
  10. .gitignore
  11. AUTHORS
  12. BUILD
  13. CODEOWNERS
  14. CONTRIBUTING.md
  15. googletest.patch
  16. ISSUE_TEMPLATE.md
  17. LICENSE
  18. local_bazel.bzl
  19. MODULE.bazel
  20. README.md
  21. WORKSPACE
  22. WORKSPACE.bzlmod
README.md

C++ rules for Bazel

  • Postsubmit Build status

This repository contains C, C++, and Objective-C language support for the Bazel build system.

For this module's main reference, see the Bazel documentation.

Get Started

Install Bazel

Follow the official instructions to Install Bazel.

Add rules_cc to your MODULE.bazel

Add the latest release to your MODULE.bazel project file.

Declare a build target

In a BUILD.bazel file, import and use the rules:

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")

cc_binary(
    name = "hello_world",
    srcs = ["hello_world.cc"],
)

Build and run your project

Build and run your C/C++ binary with one command:

$ bazel run hello_world

To build the project without running the binary, use Bazel's build subcommand:

$ bazel build hello_world

Toolchains

Default autoconfigured toolchain

rules_cc includes an auto-configured toolchain that uses the local compiler installed on the host machine.

You can disable the autoconfigured C/C++ toolchain by adding the following Bazel flag to your project's .bazelrc file:

--repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1

Hermetic toolchains

Configuring a hermetic toolchain makes your build more deterministic. rules_cc itself does not yet offer a hermetic toolchain distribution. Other community owned and maintained projects offer hermetic C/C++ toolchains:

Contributing

Bazel and rules_cc are the work of many contributors. We appreciate your help!

To contribute, please read the contribution guidelines: CONTRIBUTING.md.

Note that the rules_cc use the GitHub issue tracker for bug reports and feature requests only. For asking questions see: