Skip to content

Commit e5be6b8

Browse files
committed
feat: collect sources from cc_deps into build recipe sources
1 parent febb0e8 commit e5be6b8

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

ecsact/private/ecsact_build_recipe.bzl

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,59 @@ CPP_HEADER_SUFFIXES = [
1818
".ipp",
1919
]
2020

21+
CPP_SOURCE_SUFFIXES = [
22+
".c",
23+
".cc",
24+
".cpp",
25+
".cxx",
26+
".m",
27+
".mm",
28+
".S",
29+
".s",
30+
]
31+
32+
CppSourceCollectorInfo = provider(
33+
doc = "",
34+
fields = {
35+
"sources": "list of sources from cc libraries",
36+
},
37+
)
38+
2139
def _is_cc_header(p):
2240
for cpp_header_suffix in CPP_HEADER_SUFFIXES:
2341
if p.endswith(cpp_header_suffix):
2442
return True
2543
return False
2644

45+
def _is_cc_source(p):
46+
for suffix in CPP_SOURCE_SUFFIXES:
47+
if p.endswith(suffix):
48+
return True
49+
return False
50+
51+
def _cpp_source_collector_aspect_impl(target, ctx):
52+
sources = []
53+
if hasattr(ctx.rule.attr, "srcs"):
54+
for src in ctx.rule.files.srcs:
55+
if _is_cc_source(src.path):
56+
sources.append(src)
57+
58+
return [CppSourceCollectorInfo(sources = sources)]
59+
60+
_cpp_source_collector_aspect = aspect(
61+
implementation = _cpp_source_collector_aspect_impl,
62+
attr_aspects = ["deps"],
63+
)
64+
2765
def _strip_external(p):
2866
# type: (string) -> string
2967
EXTERNAL_PREFIX = "external/"
3068
if p.startswith(EXTERNAL_PREFIX):
31-
return p[p.index("/", len(EXTERNAL_PREFIX)) + 1:]
69+
slash_after_external = p.find("/", len(EXTERNAL_PREFIX))
70+
if slash_after_external != -1:
71+
return p[slash_after_external + 1:]
72+
else:
73+
return ""
3274
return p
3375

3476
def _source_outdir(src):
@@ -89,6 +131,15 @@ def _ecsact_build_recipe(ctx):
89131
cc_info = cc_dep[CcInfo]
90132
cc_dep_compilation_contexts.append(cc_info.compilation_context)
91133

134+
source_info = cc_dep[CppSourceCollectorInfo]
135+
for src in source_info.sources:
136+
source_paths.append({
137+
"path": src.path,
138+
"outdir": _source_outdir(src),
139+
"relative_to_cwd": True,
140+
})
141+
recipe_data.append(src)
142+
92143
if len(cc_dep_compilation_contexts) > 0:
93144
cc_dep_compilation_context = cc_common.merge_compilation_contexts(compilation_contexts = cc_dep_compilation_contexts)
94145

@@ -156,6 +207,7 @@ ecsact_build_recipe = rule(
156207
),
157208
"cc_deps": attr.label_list(
158209
providers = [CcInfo],
210+
aspects = [_cpp_source_collector_aspect],
159211
),
160212
"fetch_srcs": attr.string_list_dict(
161213
allow_empty = True,

0 commit comments

Comments
 (0)