Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions cc/libc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright 2026 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Config settings for C standard libraries identified by Bazel.

Targets that require libc-specific flags can use the config_settings defined in
this package in their select() statements.

Toolchains not shipped with Bazel are encouraged to use the same names to
identify C standard libraries as used below, but this is not enforced. A
toolchain's `target_libc` value may carry a semantic version suffix in the form
(e.g. "glibc-2.31"), which is ignored when matching the config settings below.

Example:

cc_binary(
name = "foo",
srcs = ["foo.cc"],
copts = select({
"//cc/libc:glibc": [...],
"//cc/libc:musl": [...],
"//cc/libc:ucrt": [...],
# Fallback case for an undetected C standard library.
"//conditions:default": [...],
}),
)

If multiple targets use the same set of conditionally enabled flags, this can be
simplified by extracting the select expression into a Starlark constant.
"""

load("//cc/toolchains:libc_flag.bzl", "libc_flag")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

libc_flag(name = "libc")

### Linux

# The GNU C Library (https://www.gnu.org/software/libc/).
config_setting(
name = "glibc",
flag_values = {":libc": "glibc"},
)

# musl libc (https://musl.libc.org). Also used by Emscripten and, in the form
# of the derived wasi-libc, by WASI toolchains.
config_setting(
name = "musl",
flag_values = {":libc": "musl"},
)

# uClibc and its successor uClibc-ng (https://uclibc-ng.org).
config_setting(
name = "uclibc",
flag_values = {":libc": "uclibc"},
)

### Android

# Bionic, the C standard library of Android
# (https://android.googlesource.com/platform/bionic/).
config_setting(
name = "bionic",
flag_values = {":libc": "bionic"},
)

### Apple

# On Apple platforms, the C standard library is provided by libSystem. For
# historical reasons, toolchains identify it via the value "macosx" on all
# Apple platforms, not just macOS.
config_setting(
name = "macosx",
flag_values = {":libc": "macosx"},
)

### BSDs

# The C standard libraries developed as part of the respective BSD operating
# system.
config_setting(
name = "freebsd",
flag_values = {":libc": "freebsd"},
)

config_setting(
name = "netbsd",
flag_values = {":libc": "netbsd"},
)

config_setting(
name = "openbsd",
flag_values = {":libc": "openbsd"},
)

### Windows

# The legacy Microsoft Visual C++ Runtime (msvcrt.dll), which is also targeted
# by MinGW-w64 toolchains by default.
config_setting(
name = "msvcrt",
flag_values = {":libc": "msvcrt"},
)

# The Universal C Runtime (ucrtbase.dll), used by MSVC and clang-cl as well as
# the UCRT flavor of MinGW-w64 toolchains.
config_setting(
name = "ucrt",
flag_values = {":libc": "ucrt"},
)

# The MSYS2 runtime (msys-2.0.dll), a Cygwin-derived POSIX emulation layer
# targeted by MSYS2's gcc.
config_setting(
name = "msys",
flag_values = {":libc": "msys"},
)

### Embedded

# Newlib (https://sourceware.org/newlib/), commonly used by bare-metal
# toolchains such as arm-none-eabi-gcc.
config_setting(
name = "newlib",
flag_values = {":libc": "newlib"},
)

# Picolibc (https://github.com/picolibc/picolibc).
config_setting(
name = "picolibc",
flag_values = {":libc": "picolibc"},
)

# LLVM libc (https://libc.llvm.org).
config_setting(
name = "llvm-libc",
flag_values = {":libc": "llvm-libc"},
)

### WebAssembly

# wasi-libc (https://github.com/WebAssembly/wasi-libc), the musl-derived C
# standard library of WASI SDK toolchains.
config_setting(
name = "wasi-libc",
flag_values = {":libc": "wasi-libc"},
)

### Generic

config_setting(
name = "none",
flag_values = {":libc": "none"},
)
16 changes: 8 additions & 8 deletions cc/private/toolchain/BUILD.windows.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ cc_toolchain_config(
compiler = "mingw-gcc",
host_system_name = "local",
target_system_name = "local",
target_libc = "mingw",
target_libc = "msvcrt",
abi_version = "local",
abi_libc_version = "local",
tool_bin_path = "%{mingw_tool_bin_path}",
Expand Down Expand Up @@ -267,7 +267,7 @@ cc_toolchain_config(
compiler = "mingw-gcc",
host_system_name = "local",
target_system_name = "local",
target_libc = "mingw",
target_libc = "msvcrt",
abi_version = "local",
abi_libc_version = "local",
tool_bin_path = "%{mingw_tool_bin_path}",
Expand Down Expand Up @@ -318,7 +318,7 @@ cc_toolchain_config(
compiler = "msvc-cl",
host_system_name = "local",
target_system_name = "local",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "msvc_x64",
Expand Down Expand Up @@ -393,7 +393,7 @@ cc_toolchain_config(
compiler = "msvc-cl",
host_system_name = "local",
target_system_name = "local",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "msvc_x64_x86",
Expand Down Expand Up @@ -468,7 +468,7 @@ cc_toolchain_config(
compiler = "msvc-cl",
host_system_name = "local",
target_system_name = "local",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "msvc_x64_arm",
Expand Down Expand Up @@ -543,7 +543,7 @@ cc_toolchain_config(
compiler = "msvc-cl",
host_system_name = "local",
target_system_name = "local",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "msvc_arm64",
Expand Down Expand Up @@ -618,7 +618,7 @@ cc_toolchain_config(
compiler = "clang-cl",
host_system_name = "local",
target_system_name = "local",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "clang_cl_x64",
Expand Down Expand Up @@ -691,7 +691,7 @@ cc_toolchain_config(
compiler = "clang-cl",
host_system_name = "local",
target_system_name = "aarch64-pc-windows-msvc",
target_libc = "msvcrt",
target_libc = "ucrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "clang_cl_arm64",
Expand Down
2 changes: 1 addition & 1 deletion cc/private/toolchain/armeabi_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _impl(ctx):
host_system_name = "armeabi-v7a"
target_system_name = "armeabi-v7a"
target_cpu = "armeabi-v7a"
target_libc = "armeabi-v7a"
target_libc = "unknown"
compiler = "compiler"
abi_version = "armeabi-v7a"
abi_libc_version = "armeabi-v7a"
Expand Down
2 changes: 1 addition & 1 deletion cc/private/toolchain/bsd_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _impl(ctx):
toolchain_identifier = "local_{}".format(cpu) if is_bsd else "stub_armeabi-v7a"
host_system_name = "local" if is_bsd else "armeabi-v7a"
target_system_name = "local" if is_bsd else "armeabi-v7a"
target_libc = "local" if is_bsd else "armeabi-v7a"
target_libc = cpu if is_bsd else "unknown"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this--why would the libc be the cpu for bsd?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 59/60, cpu is set to the particular OS flavor, which is also the name of the libc. Yes, that also doesn't make much sense, but I didn't want to touch cpu as well :-)

abi_version = "local" if is_bsd else "armeabi-v7a"
abi_libc_version = "local" if is_bsd else "armeabi-v7a"

Expand Down
2 changes: 1 addition & 1 deletion cc/private/toolchain/empty_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _impl(ctx):
host_system_name = "local",
target_system_name = "local",
target_cpu = "local",
target_libc = "local",
target_libc = "unknown",
compiler = "compiler",
abi_version = "local",
abi_libc_version = "local",
Expand Down
59 changes: 53 additions & 6 deletions cc/private/toolchain/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,58 @@ def _prepare_include_path(repo_ctx, path):
return path[len(repo_root):]
return path

def _get_target_libc(repository_ctx, cc, darwin, compile_opts):
"""Detect the C standard library targeted by the compiler.

Returns one of the canonical names used by the config_settings in
@rules_cc//cc/libc if the C standard library is recognized and "unknown"
otherwise. Can be overridden via the BAZEL_TARGET_LIBC environment
variable.

Args:
repository_ctx: The repository context.
cc: Path of the compiler.
darwin: Whether the target platform is macOS.
compile_opts: User-configured C compile flags, which may affect the
targeted libc (e.g. via --sysroot or --target).
"""
if darwin:
return "macosx"
libc = get_env_var(repository_ctx, "BAZEL_TARGET_LIBC", "", False)
if libc:
return escape_string(libc)

# Preprocessing any standard library header defines macros that identify
# most implementations.
repository_ctx.file("tools/cpp/detect_libc.c", "#include <string.h>\n")
result = repository_ctx.execute([cc] + compile_opts + ["-E", "-dM", "tools/cpp/detect_libc.c"])
if result.return_code == 0:
for macro, libc in [
# uClibc also defines __GLIBC__ for compatibility, so check for it
# first.
("__UCLIBC__", "uclibc"),
("__GLIBC__", "glibc"),
("__BIONIC__", "bionic"),
("__LLVM_LIBC__", "llvm-libc"),
("__FreeBSD__", "freebsd"),
("__NetBSD__", "netbsd"),
("__OpenBSD__", "openbsd"),
]:
if ("#define %s " % macro) in result.stdout:
return libc

# musl intentionally doesn't define an identifying macro, but is usually
# mentioned in the compiler's target triple.
result = repository_ctx.execute([cc] + compile_opts + ["-dumpmachine"])
if result.return_code == 0:
triple = result.stdout.strip()
if "musl" in triple:
return "musl"
if "android" in triple:
return "bionic"

return "unknown"

def _find_tool(repository_ctx, tool, overridden_tools):
"""Find a tool for repository, taking overridden tools into account."""
if tool in overridden_tools:
Expand Down Expand Up @@ -770,12 +822,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overridden_tools):
cpu_value,
False,
)),
"%{target_libc}": "macosx" if darwin else escape_string(get_env_var(
repository_ctx,
"BAZEL_TARGET_LIBC",
"local",
False,
)),
"%{target_libc}": _get_target_libc(repository_ctx, cc, darwin, all_compile_opts + conly_opts),
"%{target_system_name}": escape_string(get_env_var(
repository_ctx,
"BAZEL_TARGET_SYSTEM",
Expand Down
34 changes: 34 additions & 0 deletions cc/toolchains/impl/libc_version.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2026 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helper function for parsing target_libc values."""

visibility([
"//cc/toolchains",
"//tests/libc_settings",
])

def libc_without_version(libc):
"""Strips a version suffix from a target_libc value.

Args:
libc: (str) The value of cc_toolchain.libc.

Returns:
The name of the C standard library without a version suffix.
"""
for i in range(1, len(libc) - 1):
if libc[i] == "-" and libc[i + 1].isdigit():
return libc[:i]
return libc
Loading