Skip to content

Commit c51fc49

Browse files
authored
feat: Allow explicit file name outputs vs just the extension (#216)
1 parent 0c53919 commit c51fc49

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module(
66

77
bazel_dep(name = "rules_cc", version = "0.0.9")
88
bazel_dep(name = "bazel_skylib", version = "1.6.1")
9-
bazel_dep(name = "ecsact_runtime", version = "0.6.8")
10-
bazel_dep(name = "rules_ecsact", version = "0.5.6")
9+
bazel_dep(name = "ecsact_runtime", version = "0.6.9")
10+
bazel_dep(name = "rules_ecsact", version = "0.5.7")
1111
bazel_dep(name = "ecsact_codegen", version = "0.4.1")
1212

1313
bazel_dep(name = "ecsact_cli", version = "0.3.16", dev_dependency = True)

codegen_plugin.bzl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ load("@rules_ecsact//ecsact/private:ecsact_codegen_plugin.bzl", "EcsactCodegenPl
55

66
def _cc_ecsact_codegen_plugin_impl(ctx):
77
# type: (ctx) -> list
8-
98
plugin = None # type: File | None
109
files = ctx.attr.cc_binary[DefaultInfo].files.to_list() # type: list[File]
1110

@@ -30,6 +29,7 @@ def _cc_ecsact_codegen_plugin_impl(ctx):
3029
),
3130
EcsactCodegenPluginInfo(
3231
output_extension = ctx.attr.output_extension,
32+
outputs = ctx.attr.outputs,
3333
plugin = plugin,
3434
data = [plugin],
3535
),
@@ -39,7 +39,8 @@ _cc_ecsact_codegen_plugin = rule(
3939
implementation = _cc_ecsact_codegen_plugin_impl,
4040
attrs = {
4141
"cc_binary": attr.label(mandatory = True),
42-
"output_extension": attr.string(mandatory = True),
42+
"output_extension": attr.string(mandatory = False),
43+
"outputs": attr.string_list(mandatory = False)
4344
},
4445
)
4546

@@ -53,10 +54,16 @@ const char* ecsact_codegen_plugin_name() {{
5354

5455
def _cc_ecsact_codegen_plugin_src_impl(ctx):
5556
output_cc_src = ctx.actions.declare_file("{}.plugin_name.cc".format(ctx.attr.name))
56-
ctx.actions.write(
57-
output = output_cc_src,
58-
content = _generated_src.format(output_extension = ctx.attr.output_extension),
59-
)
57+
if ctx.attr.output_extension!= None:
58+
ctx.actions.write(
59+
output = output_cc_src,
60+
content = _generated_src.format(output_extension = ctx.attr.output_extension),
61+
)
62+
else:
63+
ctx.actions.write(
64+
output = output_cc_src,
65+
content = _generated_src.format(output_extension = ctx.attr.name),
66+
)
6067

6168
return [
6269
DefaultInfo(files = depset([output_cc_src])),
@@ -65,11 +72,11 @@ def _cc_ecsact_codegen_plugin_src_impl(ctx):
6572
_cc_ecsact_codegen_plugin_src = rule(
6673
implementation = _cc_ecsact_codegen_plugin_src_impl,
6774
attrs = {
68-
"output_extension": attr.string(mandatory = True),
75+
"output_extension": attr.string(mandatory = False),
6976
},
7077
)
7178

72-
def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no_validate_test = False, output_extension = None, **kwargs):
79+
def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no_validate_test = False, output_extension = None, outputs = [], **kwargs):
7380
"""Create ecsact codegen plugin with C++
7481
7582
NOTE: ecsact_codegen_plugin_name() is automatically generated for you based
@@ -80,10 +87,14 @@ def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no
8087
srcs: Passed to underling cc_binary
8188
deps: Passed to underling cc_binary
8289
defines: Passed to underling cc_binary
83-
output_extension: File extension the plugin writes to
90+
output_extension: File extension the plugin writes to. Cannot be used with outputs
91+
outputs: A list of well known filenames to output. Cannot be used with output_extension
8492
no_validate_test: Don't create plugin validation test (not recommended)
8593
**kwargs: Passed to underling cc_binary
8694
"""
95+
if output_extension and len(outputs) != 0:
96+
fail("You cannot use both output extension and outputs")
97+
8798
name_hash = hash(name)
8899
cc_binary(
89100
name = "{}__bin".format(name_hash),
@@ -101,13 +112,19 @@ def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no
101112
**kwargs
102113
)
103114

104-
_cc_ecsact_codegen_plugin_src(
105-
name = "{}__src".format(name_hash),
106-
output_extension = output_extension,
107-
)
115+
if(output_extension != None):
116+
_cc_ecsact_codegen_plugin_src(
117+
name = "{}__src".format(name_hash),
118+
output_extension = output_extension,
119+
)
120+
else:
121+
_cc_ecsact_codegen_plugin_src(
122+
name = "{}__src".format(name_hash),
123+
)
108124

109125
_cc_ecsact_codegen_plugin(
110126
name = name,
111127
cc_binary = ":{}__bin".format(name_hash),
112128
output_extension = output_extension,
129+
outputs = outputs
113130
)

0 commit comments

Comments
 (0)