Skip to content

Commit bb67de6

Browse files
authored
[libc][bazel] Enforce that libc hand-in-hand libs are headers-only. (llvm#136219)
Extend Bazel rule implementation to enforce that all transitive dependencies of libc_header_library targets (used to implement hand-in-hand code sharing via headers) indeed only contain header files. This fixes Bazel portion of PR llvm#133126.
1 parent f0621b3 commit bb67de6

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ _get_libc_info_aspect = aspect(
123123
)
124124

125125
def _libc_srcs_filegroup_impl(ctx):
126-
return DefaultInfo(
127-
files = depset(transitive = [
128-
fn[LibcLibraryInfo].srcs
129-
for fn in ctx.attr.libs
130-
]),
131-
)
126+
srcs = depset(transitive = [
127+
fn[LibcLibraryInfo].srcs
128+
for fn in ctx.attr.libs
129+
])
130+
if ctx.attr.enforce_headers_only:
131+
paths = [f.short_path for f in srcs.to_list() if f.extension != "h"]
132+
if paths:
133+
fail("Unexpected non-header files: {}".format(paths))
134+
return DefaultInfo(files = srcs)
132135

133136
_libc_srcs_filegroup = rule(
134137
doc = "Returns all sources for building the specified libraries.",
@@ -138,6 +141,7 @@ _libc_srcs_filegroup = rule(
138141
mandatory = True,
139142
aspects = [_get_libc_info_aspect],
140143
),
144+
"enforce_headers_only": attr.bool(default = False),
141145
},
142146
)
143147

@@ -218,6 +222,7 @@ def libc_header_library(name, hdrs, deps = [], **kwargs):
218222
_libc_srcs_filegroup(
219223
name = name + "_hdr_deps",
220224
libs = deps,
225+
enforce_headers_only = True,
221226
)
222227

223228
_libc_textual_hdrs_filegroup(
@@ -231,13 +236,10 @@ def libc_header_library(name, hdrs, deps = [], **kwargs):
231236

232237
native.cc_library(
233238
name = name,
234-
# Technically speaking, we should put _hdr_deps in srcs, as they are
235-
# not a part of this cc_library interface. However, we keep it here to
236-
# workaround the presence of .cpp files in _hdr_deps - we need to
237-
# fix that and enforce their absence, since libc_header_library
238-
# should be header-only and not produce any object files.
239-
# See PR #133126 which tracks it.
240-
hdrs = hdrs + [":" + name + "_hdr_deps"],
239+
hdrs = hdrs,
240+
# We put _hdr_deps in srcs, as they are not a part of this cc_library
241+
# interface, but instead are used to implement shared headers.
242+
srcs = [":" + name + "_hdr_deps"],
241243
deps = [":" + name + "_textual_hdr_library"],
242244
# copts don't really matter, since it's a header-only library, but we
243245
# need proper -I flags for header validation, which are specified in

0 commit comments

Comments
 (0)