Skip to content

Commit c388da6

Browse files
authored
[libc][bazel] Simplify libc_build_rules by grouping release copts (#121630)
Extract all compiler options used to build "release" versions of libc API functions into a separate helper function, instead of burying this logic inside libc_function() macro. With this change, we further split two "flavors" of cc_library() produced for each libc public function: * `<function>.__internal__` library used in unit tests is *not* built with release copts and is thus indistinguishable from regular libc_support_library(). Arguably, it's a good thing, because all sources in a unit test are built with the same set of compiler flags, instead of "franken-build" when a subset of sources is always built with -O3. If a user needs to run the tests in optimized mode, they should really be using Bazel invocation-level compile flags instead. * `<function>` library that libc users can use to construct their own static archive *is* built with the same release copts as before. There is a pre-existing problem that its libc_support_library() dependencies are not built with the same copts. We're not addressing it here now.
1 parent 7d53762 commit c388da6

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,39 @@ def libc_common_copts():
2222
"-DLIBC_NAMESPACE=" + LIBC_NAMESPACE,
2323
]
2424

25-
def _libc_library(name, hidden, copts = [], deps = [], local_defines = [], **kwargs):
25+
def libc_release_copts():
26+
copts = [
27+
"-DLIBC_COPT_PUBLIC_PACKAGING",
28+
# This is used to explicitly give public symbols "default" visibility.
29+
# See src/__support/common.h for more information.
30+
"-DLLVM_LIBC_FUNCTION_ATTR='[[gnu::visibility(\"default\")]]'",
31+
# All other libc sources need to be compiled with "hidden" visibility.
32+
"-fvisibility=hidden",
33+
"-O3",
34+
"-fno-builtin",
35+
"-fno-lax-vector-conversions",
36+
"-ftrivial-auto-var-init=pattern",
37+
"-fno-omit-frame-pointer",
38+
"-fstack-protector-strong",
39+
]
40+
41+
platform_copts = selects.with_or({
42+
PLATFORM_CPU_X86_64: ["-mno-omit-leaf-frame-pointer"],
43+
"//conditions:default": [],
44+
})
45+
return copts + platform_copts
46+
47+
def _libc_library(name, copts = [], deps = [], local_defines = [], **kwargs):
2648
"""Internal macro to serve as a base for all other libc library rules.
2749
2850
Args:
2951
name: Target name.
3052
copts: The special compiler options for the target.
3153
deps: The list of target dependencies if any.
3254
local_defines: The list of target local_defines if any.
33-
hidden: Whether the symbols should be explicitly hidden or not.
3455
**kwargs: All other attributes relevant for the cc_library rule.
3556
"""
3657

37-
# We want all libc sources to be compiled with "hidden" visibility.
38-
# The public symbols will be given "default" visibility explicitly.
39-
# See src/__support/common.h for more information.
40-
if hidden:
41-
copts = copts + ["-fvisibility=hidden"]
4258
native.cc_library(
4359
name = name,
4460
copts = copts + libc_common_copts(),
@@ -52,13 +68,13 @@ def _libc_library(name, hidden, copts = [], deps = [], local_defines = [], **kwa
5268
# Any library which does not define a public function should be listed with
5369
# libc_support_library.
5470
def libc_support_library(name, **kwargs):
55-
_libc_library(name = name, hidden = False, **kwargs)
71+
_libc_library(name = name, **kwargs)
5672

5773
def libc_function(
5874
name,
5975
srcs,
6076
weak = False,
61-
copts = None,
77+
copts = [],
6278
local_defines = [],
6379
**kwargs):
6480
"""Add target for a libc function.
@@ -81,25 +97,6 @@ def libc_function(
8197
**kwargs: Other attributes relevant for a cc_library. For example, deps.
8298
"""
8399

84-
# We use the explicit equals pattern here because append and += mutate the
85-
# original list, where this creates a new list and stores it in deps.
86-
copts = copts or []
87-
copts = copts + [
88-
"-O3",
89-
"-fno-builtin",
90-
"-fno-lax-vector-conversions",
91-
"-ftrivial-auto-var-init=pattern",
92-
"-fno-omit-frame-pointer",
93-
"-fstack-protector-strong",
94-
]
95-
96-
# x86 targets have -mno-omit-leaf-frame-pointer.
97-
platform_copts = selects.with_or({
98-
PLATFORM_CPU_X86_64: ["-mno-omit-leaf-frame-pointer"],
99-
"//conditions:default": [],
100-
})
101-
copts = copts + platform_copts
102-
103100
# We compile the code twice, the first target is suffixed with ".__internal__" and contains the
104101
# C++ functions in the "LIBC_NAMESPACE" namespace. This allows us to test the function in the
105102
# presence of another libc.
@@ -111,22 +108,16 @@ def libc_function(
111108
**kwargs
112109
)
113110

114-
# This second target is the llvm libc C function with either a default or hidden visibility.
115-
# All other functions are hidden.
111+
# This second target is the llvm libc C function with default visibility.
116112
func_attrs = [
117113
"LLVM_LIBC_FUNCTION_ATTR_" + name + "='LLVM_LIBC_EMPTY, [[gnu::weak]]'",
118114
] if weak else []
119115

120-
local_defines = (local_defines +
121-
["LIBC_COPT_PUBLIC_PACKAGING"] +
122-
["LLVM_LIBC_FUNCTION_ATTR='[[gnu::visibility(\"default\")]]'"] +
123-
func_attrs)
124116
_libc_library(
125117
name = name,
126-
hidden = True,
127118
srcs = srcs,
128-
copts = copts,
129-
local_defines = local_defines,
119+
copts = copts + libc_release_copts(),
120+
local_defines = local_defines + func_attrs,
130121
**kwargs
131122
)
132123

0 commit comments

Comments
 (0)