Skip to content

Commit 5de3cb1

Browse files
authored
feat: ecsact_binary provides CcInfo (#53)
1 parent 8b5cd9b commit 5de3cb1

File tree

3 files changed

+121
-41
lines changed

3 files changed

+121
-41
lines changed

MODULE.bazel.lock

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecsact/private/ecsact_binary.bzl

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ load("//ecsact/private:ecsact_build_recipe.bzl", "EcsactBuildRecipeInfo")
44

55
def _ecsact_binary_impl(ctx):
66
cc_toolchain = find_cc_toolchain(ctx)
7+
feature_configuration = cc_common.configure_features(
8+
ctx = ctx,
9+
cc_toolchain = cc_toolchain,
10+
requested_features = ctx.features,
11+
unsupported_features = ctx.disabled_features,
12+
)
713

814
temp_dir = ctx.actions.declare_directory("{}.ecsact_binary".format(ctx.attr.name))
915

@@ -27,11 +33,16 @@ def _ecsact_binary_impl(ctx):
2733

2834
ecsact_toolchain = ctx.toolchains["//ecsact:toolchain_type"].ecsact_info
2935

30-
preferred_output_extension = ctx.attr.lib_extension
36+
preferred_output_extension = ctx.attr.shared_library_extension
3137

3238
runtime_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, preferred_output_extension))
33-
outputs = [runtime_output_file]
39+
interface_output_file = None
40+
if ctx.attr.interface_library_extension:
41+
interface_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, ctx.attr.interface_library_extension))
3442
tools = [] + ecsact_toolchain.tool_files
43+
outputs = [runtime_output_file]
44+
if interface_output_file != None:
45+
outputs.append(interface_output_file)
3546

3647
args = ctx.actions.args()
3748
args.add("build")
@@ -89,7 +100,29 @@ def _ecsact_binary_impl(ctx):
89100
toolchain = Label("//ecsact:toolchain_type"),
90101
)
91102

103+
library_to_link = cc_common.create_library_to_link(
104+
actions = ctx.actions,
105+
feature_configuration = feature_configuration,
106+
cc_toolchain = cc_toolchain,
107+
static_library = None,
108+
pic_static_library = None,
109+
interface_library = interface_output_file,
110+
dynamic_library = runtime_output_file,
111+
alwayslink = False,
112+
)
113+
114+
linker_input = cc_common.create_linker_input(
115+
libraries = depset([library_to_link]),
116+
user_link_flags = depset(ctx.attr.linkopts),
117+
owner = ctx.label,
118+
)
119+
120+
linking_context = cc_common.create_linking_context(
121+
linker_inputs = depset([linker_input]),
122+
)
123+
92124
return [
125+
CcInfo(linking_context = linking_context),
93126
DefaultInfo(
94127
files = depset(outputs),
95128
),
@@ -111,22 +144,35 @@ _ecsact_binary = rule(
111144
"@rules_cc//cc:current_cc_toolchain",
112145
),
113146
),
114-
"lib_extension": attr.string(
147+
"shared_library_extension": attr.string(
115148
mandatory = True,
116149
),
150+
"interface_library_extension": attr.string(
151+
mandatory = True,
152+
),
153+
"linkopts": attr.string_list(
154+
mandatory = False,
155+
),
117156
},
118157
toolchains = ["//ecsact:toolchain_type"] + use_cc_toolchain(),
119158
fragments = ["cpp"],
120159
)
121160

122161
def ecsact_binary(**kwargs):
123162
_ecsact_binary(
124-
lib_extension = select({
163+
shared_library_extension = select({
125164
"@platforms//os:windows": ".dll",
126165
"@platforms//os:linux": ".so",
127166
"@platforms//os:macos": ".dylib",
128167
"@platforms//os:wasi": ".wasm",
129168
"@platforms//os:none": ".wasm", # for non-wasi
130169
}),
170+
interface_library_extension = select({
171+
"@platforms//os:windows": ".lib",
172+
"@platforms//os:linux": "",
173+
"@platforms//os:macos": "",
174+
"@platforms//os:wasi": "",
175+
"@platforms//os:none": "", # for non-wasi
176+
}),
131177
**kwargs
132178
)

ecsact/private/ecsact_build_recipe.bzl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ EcsactBuildRecipeInfo = provider(
88
},
99
)
1010

11+
CPP_HEADER_SUFFIXES = [
12+
".hh",
13+
".h",
14+
".hpp",
15+
".inl",
16+
]
17+
18+
def _strip_external(p):
19+
# type: (string) -> string
20+
EXTERNAL_PREFIX = "external/"
21+
if p.startswith(EXTERNAL_PREFIX):
22+
return p[p.index("/", len(EXTERNAL_PREFIX)) + 1:]
23+
return p
24+
25+
def _source_outdir(src):
26+
# type: (File) -> string
27+
src_path = src.path
28+
for cpp_header_suffix in CPP_HEADER_SUFFIXES:
29+
if src.path.endswith(cpp_header_suffix):
30+
return "include/" + _strip_external(src.dirname)
31+
return _strip_external(src.dirname)
32+
1133
def _ecsact_build_recipe(ctx):
1234
# type: (ctx) -> None
1335

@@ -19,11 +41,20 @@ def _ecsact_build_recipe(ctx):
1941
for src in ctx.files.srcs:
2042
sources.append({
2143
"path": src.path,
22-
"outdir": src.dirname,
44+
"outdir": _source_outdir(src),
2345
"relative_to_cwd": True,
2446
})
2547
recipe_data.append(src)
2648

49+
for fetch_src_outdir in ctx.attr.fetch_srcs:
50+
fetch_srcs = []
51+
for fetch_url in ctx.attr.fetch_srcs[fetch_src_outdir]:
52+
fetch_srcs.append({
53+
"fetch": fetch_url,
54+
"outdir": fetch_src_outdir,
55+
})
56+
sources.extend(fetch_srcs)
57+
2758
for codegen_plugin in ctx.attr.codegen_plugins:
2859
info = codegen_plugin[EcsactCodegenPluginInfo]
2960
sources.append({
@@ -57,6 +88,9 @@ ecsact_build_recipe = rule(
5788
"srcs": attr.label_list(
5889
allow_files = True,
5990
),
91+
"fetch_srcs": attr.string_list_dict(
92+
allow_empty = True,
93+
),
6094
"codegen_plugins": attr.label_keyed_string_dict(
6195
providers = [EcsactCodegenPluginInfo],
6296
),

0 commit comments

Comments
 (0)