Skip to content

Commit d724bab

Browse files
authored
[libc][bazel] Create a libc_header_library macro for hand-in-hand. (#133131)
Create a proper way to build header-only libraries for llvm-libc code sharing. Use it to group headers that can be shared with libcxx for std::from_chars() implementation. It mostly works, though the macro needs to be updated to enforce that no .cpp files are listed in dependencies (it's not the case now) - see PR #133126.
1 parent 2c7d40b commit d724bab

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
99
load(
1010
":libc_build_rules.bzl",
1111
"libc_function",
12+
"libc_header_library",
1213
"libc_math_function",
1314
"libc_support_library",
1415
)
@@ -1589,6 +1590,7 @@ libc_support_library(
15891590

15901591
########################## externally shared targets ###########################
15911592

1593+
# TODO: Remove this once downstream users are migrated to libcxx_shared_headers.
15921594
libc_support_library(
15931595
name = "libc_external_common",
15941596
hdrs = glob(
@@ -1603,6 +1605,21 @@ libc_support_library(
16031605
],
16041606
)
16051607

1608+
libc_header_library(
1609+
name = "libcxx_shared_headers",
1610+
hdrs = [
1611+
"shared/fp_bits.h",
1612+
"shared/str_to_float.h",
1613+
"shared/str_to_integer.h",
1614+
],
1615+
deps = [
1616+
":__support_common",
1617+
":__support_fputil_fp_bits",
1618+
":__support_str_to_float",
1619+
":__support_str_to_integer",
1620+
],
1621+
)
1622+
16061623
############################### errno ########################################
16071624

16081625
libc_support_library(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,42 @@ def libc_release_library(
165165
**kwargs
166166
)
167167

168+
def libc_header_library(name, hdrs, deps = [], **kwargs):
169+
"""Creates a header-only library to share libc functionality.
170+
171+
Args:
172+
name: Name of the cc_library target.
173+
hdrs: List of headers to be shared.
174+
deps: The list of libc_support_library dependencies if any.
175+
**kwargs: All other attributes relevant for the cc_library rule.
176+
"""
177+
178+
# Combine sources from dependencies to create a single cc_library target.
179+
native.filegroup(
180+
name = name + "_hdr_deps",
181+
srcs = [dep + "_srcs" for dep in deps],
182+
)
183+
native.cc_library(
184+
name = name + "_textual_hdr_library",
185+
textual_hdrs = [dep + "_textual_hdrs" for dep in deps],
186+
)
187+
native.cc_library(
188+
name = name,
189+
# Technically speaking, we should put _hdr_deps in srcs, as they are
190+
# not a part of this cc_library interface. However, we keep it here to
191+
# workaround the presence of .cpp files in _hdr_deps - we need to
192+
# fix that and enforce their absence, since libc_header_library
193+
# should be header-only and not produce any object files.
194+
# See PR #133126 which tracks it.
195+
hdrs = hdrs + [":" + name + "_hdr_deps"],
196+
deps = [":" + name + "_textual_hdr_library"],
197+
# copts don't really matter, since it's a header-only library, but we
198+
# need proper -I flags for header validation, which are specified in
199+
# libc_common_copts().
200+
copts = libc_common_copts(),
201+
**kwargs
202+
)
203+
168204
def libc_math_function(
169205
name,
170206
additional_deps = None):

0 commit comments

Comments
 (0)