Debian Codegraph @

node/cc_headers_files.bzl

raw
1 """Expose a cc_library's compilation headers as raw files.
2
3 SCOPE: This is used ONLY by the Windows-Zig cross-compile genrule
4 (//node:opencc_node_windows_zig), which shells out to `zig c++` and therefore
5 needs raw Node header *files* it can pass via $(location) / -I. It is NOT part
6 of the main native-addon build path: //node:opencc is built by rules_node_addon,
7 which supplies the Node headers itself, and never goes through these rules.
8
9 rules_nodejs only exposes the active Node toolchain's headers as a cc_library
10 (CcInfo), not as a filegroup of raw files, so these rules unwrap the
11 CcInfo.compilation_context.headers depset into DefaultInfo files.
12 """
13
14 load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
15
16 def _cc_headers_files_impl(ctx):
17 # All (transitive) headers the library exposes to its consumers.
18 headers = ctx.attr.lib[CcInfo].compilation_context.headers
19 return [DefaultInfo(files = headers)]
20
21 cc_headers_files = rule(
22 implementation = _cc_headers_files_impl,
23 doc = "Collect all of a cc_library's compilation headers as raw files.",
24 attrs = {
25 "lib": attr.label(providers = [CcInfo]),
26 },
27 )
28
29 def _cc_header_pick_impl(ctx):
30 # $(location) requires a single-file output, so pick one header by basename.
31 wanted = ctx.attr.basename
32 picked = [
33 h
34 for h in ctx.attr.lib[CcInfo].compilation_context.headers.to_list()
35 if h.basename == wanted
36 ]
37 if len(picked) != 1:
38 fail("expected exactly one header named %s, found %d" % (wanted, len(picked)))
39 return [DefaultInfo(files = depset(picked))]
40
41 cc_header_pick = rule(
42 implementation = _cc_header_pick_impl,
43 doc = "Pick a single header file from a cc_library by basename (for $(location)).",
44 attrs = {
45 "lib": attr.label(providers = [CcInfo]),
46 "basename": attr.string(mandatory = True),
47 },
48 )