[llvm] [bazel][libc] Refactor to allow copts under full-build mode (PR #226851)
Jackson Stogel via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 27 17:13:55 PDT 2026
https://github.com/jtstogel created https://github.com/llvm/llvm-project/pull/226851
This PR moves away from having sets of predefined groups of copts and allows just directly using them. The idea behind `copt_sets` in `_libc_library` was to restrict the different ways `copts` could be defined, since `libc_release_library` doesn't respect `copts`. This PR still leaves `copts` restricted to only be configurable if the target is full-build only, so at least `libc_release_library` will be usable with all overlay functions.
>From 842e152d652efee801d399461320eec56799b20f Mon Sep 17 00:00:00 2001
From: jtstogel <jtstogel at gmail.com>
Date: Sun, 27 Sep 2026 13:53:29 -0700
Subject: [PATCH] [bazel][libc] Refactor to allow copts under full-build mode
This PR moves away from having sets of predefined groups of copts and allows just directly using them. This lets the Bazel rules more directly parallel CMake.
---
.../llvm-project-overlay/libc/BUILD.bazel | 65 +++++++++----------
.../libc/libc_build_rules.bzl | 53 +++++++--------
2 files changed, 57 insertions(+), 61 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 33fc35455fef3..53953e041f76d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1106,10 +1106,7 @@ libc_support_library(
name = "types_atexithandler_t",
hdrs = ["hdr/types/atexithandler_t.h"],
# Only compatible with full build mode. atexit is not available in overlay mode.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
)
libc_support_library(
@@ -1450,8 +1447,9 @@ libc_support_library(
libc_support_library(
name = "types_struct___pthread_cleanup_frame",
hdrs = ["hdr/types/struct___pthread_cleanup_frame.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [":llvm_libc_types_struct___pthread_cleanup_frame"],
@@ -4246,8 +4244,9 @@ libc_support_library(
libc_support_library(
name = "__support_threads_thread_attributes",
hdrs = ["src/__support/threads/thread_attributes.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -4402,8 +4401,9 @@ libc_support_library(
libc_support_library(
name = "__support_threads_tcb",
hdrs = ["src/__support/threads/tcb.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
textual_hdrs = select({
@@ -4429,8 +4429,9 @@ libc_support_library(
libc_support_library(
name = "__support_threads_thread_headers",
hdrs = ["src/__support/threads/thread.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -4452,8 +4453,9 @@ libc_support_library(
libc_support_library(
name = "__support_threads_thread",
srcs = ["src/__support/threads/thread.cpp"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -4474,9 +4476,13 @@ libc_support_library(
libc_support_library(
name = "__support_threads_linux_thread",
srcs = ["src/__support/threads/linux/thread.cpp"],
- copt_sets = ["threading"],
+ copts = [
+ "-fno-omit-frame-pointer",
+ "-Wno-frame-address",
+ ],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -15258,10 +15264,7 @@ libc_support_library(
name = "stdlib_exit_handler",
hdrs = ["src/stdlib/exit_handler.h"],
# Full-build only, as overlay mode must use the host libc's cleanup.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
deps = [
":__support_blockstore",
":__support_common",
@@ -15278,10 +15281,7 @@ libc_function(
srcs = ["src/stdlib/_Exit.cpp"],
hdrs = ["src/stdlib/_Exit.h"],
# Full-build only, as overlay mode must use the host libc's cleanup.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
deps = [
":__support_common",
":__support_macros_config",
@@ -15294,10 +15294,7 @@ libc_function(
srcs = ["src/stdlib/atexit.cpp"],
hdrs = ["src/stdlib/atexit.h"],
# Full-build only, as overlay mode must use the host libc's cleanup.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
deps = [
":__support_common",
":__support_macros_attributes",
@@ -15312,10 +15309,7 @@ libc_function(
srcs = ["src/stdlib/exit.cpp"],
hdrs = ["src/stdlib/exit.h"],
# Full-build only, as overlay mode must use the host libc's cleanup.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
deps = [
":__support_common",
":__support_macros_config",
@@ -15472,9 +15466,10 @@ libc_support_library(
libc_support_library(
name = "stdlib_linux_abort_utils",
hdrs = ["src/stdlib/linux/abort_utils.h"],
+ full_build_only = True,
# Full-build only, as overlay mode must use the host libc's cleanup.
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -15493,10 +15488,7 @@ libc_function(
srcs = ["src/stdlib/abort.cpp"],
hdrs = ["src/stdlib/abort.h"],
# Full-build only, as overlay mode must use the host libc's abort.
- target_compatible_with = select({
- ":full_build": [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
deps = [
":__support_macros_config",
":stdlib_abort_utils",
@@ -16722,8 +16714,9 @@ libc_function(
name = "fork",
srcs = ["src/unistd/linux/fork.cpp"],
hdrs = ["src/unistd/fork.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -16773,10 +16766,11 @@ libc_support_library(
libc_support_library(
name = "__signal_utils",
hdrs = ["src/signal/linux/signal_utils.h"],
+ full_build_only = True,
# Only compatible with full build mode on Linux. Overlay mode has header
# conflicts with system headers (e.g. sa_handler macro in glibc).
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
@@ -17174,8 +17168,9 @@ libc_function(
name = "sysconf",
srcs = ["src/unistd/linux/sysconf.cpp"],
hdrs = ["src/unistd/sysconf.h"],
+ full_build_only = True,
target_compatible_with = select({
- ":full_build_linux": [],
+ "@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index 4412ccc81dfed..07cb2b04a5dfd 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -75,42 +75,40 @@ def libc_release_copts():
})
return copts + platform_copts
-# Allowlisted sets of copts that may be used for a single libc library target.
-# Adding copts here is discouraged, as it complicates the build.
-_LIBC_LIBRARY_COPT_SETS = {
- "startup_object": [
- "-ffreestanding",
- "-fno-builtin",
- "-fno-omit-frame-pointer",
- "-fno-stack-protector",
- ],
- "threading": [
- "-fno-omit-frame-pointer",
- "-Wno-frame-address",
- ],
-}
-
def _libc_library(
name,
deps = [],
- copt_sets = [],
+ copts = [],
+ target_compatible_with = [],
+ full_build_only = False,
**kwargs):
"""Internal macro to serve as a base for all other libc library rules.
Args:
name: Target name.
deps: cc_library deps.
- copt_sets: Which sets of allow-listed copts to include.
+ copts: copts for cc_library, only allowed if full_build_only is True.
+ target_compatible_with: target_compatible_with for the cc_library rule.
+ full_build_only: Whether this target is only used in full-build mode.
**kwargs: All other attributes relevant for the cc_library rule.
"""
- for attr in ["copts", "local_defines"]:
+ for attr in ["local_defines"]:
if attr in kwargs:
fail("disallowed attribute: '{}' in rule: '{}'".format(attr, name))
- copts = []
- for feature in copt_sets:
- copts.extend(_LIBC_LIBRARY_COPT_SETS[feature])
+ # Allow copts if the target is only for full-builds. Startup or threading
+ # code necessitate copts, but LLVM-libc's build rules should prefer to
+ # avoid copts when possible as per-file-copts are not compatible with
+ # libc_release_library's method of aggregating sources into one cc_library.
+ if copts and not full_build_only:
+ fail("copts disallowed in overlay-compatible rule: '{}'".format(name))
+
+ if full_build_only:
+ target_compatible_with = target_compatible_with + select({
+ Label(":full_build"): [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ })
cc_library(
name = name,
@@ -118,6 +116,7 @@ def _libc_library(
local_defines = LIBC_CONFIGURE_OPTIONS,
deps = deps + libc_common_deps(),
linkstatic = 1,
+ target_compatible_with = target_compatible_with,
**kwargs
)
@@ -137,11 +136,13 @@ def libc_startup_library(name, **kwargs):
_libc_library(
name = name,
- copt_sets = ["startup_object"],
- target_compatible_with = select({
- Label("//libc:full_build_linux"): [],
- "//conditions:default": ["@platforms//:incompatible"],
- }),
+ full_build_only = True,
+ copts = [
+ "-ffreestanding",
+ "-fno-builtin",
+ "-fno-omit-frame-pointer",
+ "-fno-stack-protector",
+ ],
**kwargs
)
More information about the llvm-commits
mailing list