Skip to content

feat: Allow explicit file name outputs vs just the extension #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module(

bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "ecsact_runtime", version = "0.6.8")
bazel_dep(name = "rules_ecsact", version = "0.5.6")
bazel_dep(name = "ecsact_runtime", version = "0.6.9")
bazel_dep(name = "rules_ecsact", version = "0.5.7")
bazel_dep(name = "ecsact_codegen", version = "0.4.1")

bazel_dep(name = "ecsact_cli", version = "0.3.16", dev_dependency = True)
Expand Down
43 changes: 30 additions & 13 deletions codegen_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@rules_ecsact//ecsact/private:ecsact_codegen_plugin.bzl", "EcsactCodegenPl

def _cc_ecsact_codegen_plugin_impl(ctx):
# type: (ctx) -> list

plugin = None # type: File | None
files = ctx.attr.cc_binary[DefaultInfo].files.to_list() # type: list[File]

Expand All @@ -30,6 +29,7 @@ def _cc_ecsact_codegen_plugin_impl(ctx):
),
EcsactCodegenPluginInfo(
output_extension = ctx.attr.output_extension,
outputs = ctx.attr.outputs,
plugin = plugin,
data = [plugin],
),
Expand All @@ -39,7 +39,8 @@ _cc_ecsact_codegen_plugin = rule(
implementation = _cc_ecsact_codegen_plugin_impl,
attrs = {
"cc_binary": attr.label(mandatory = True),
"output_extension": attr.string(mandatory = True),
"output_extension": attr.string(mandatory = False),
"outputs": attr.string_list(mandatory = False)
},
)

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

def _cc_ecsact_codegen_plugin_src_impl(ctx):
output_cc_src = ctx.actions.declare_file("{}.plugin_name.cc".format(ctx.attr.name))
ctx.actions.write(
output = output_cc_src,
content = _generated_src.format(output_extension = ctx.attr.output_extension),
)
if ctx.attr.output_extension!= None:
ctx.actions.write(
output = output_cc_src,
content = _generated_src.format(output_extension = ctx.attr.output_extension),
)
else:
ctx.actions.write(
output = output_cc_src,
content = _generated_src.format(output_extension = ctx.attr.name),
)

return [
DefaultInfo(files = depset([output_cc_src])),
Expand All @@ -65,11 +72,11 @@ def _cc_ecsact_codegen_plugin_src_impl(ctx):
_cc_ecsact_codegen_plugin_src = rule(
implementation = _cc_ecsact_codegen_plugin_src_impl,
attrs = {
"output_extension": attr.string(mandatory = True),
"output_extension": attr.string(mandatory = False),
},
)

def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no_validate_test = False, output_extension = None, **kwargs):
def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no_validate_test = False, output_extension = None, outputs = [], **kwargs):
"""Create ecsact codegen plugin with C++

NOTE: ecsact_codegen_plugin_name() is automatically generated for you based
Expand All @@ -80,10 +87,14 @@ def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no
srcs: Passed to underling cc_binary
deps: Passed to underling cc_binary
defines: Passed to underling cc_binary
output_extension: File extension the plugin writes to
output_extension: File extension the plugin writes to. Cannot be used with outputs
outputs: A list of well known filenames to output. Cannot be used with output_extension
no_validate_test: Don't create plugin validation test (not recommended)
**kwargs: Passed to underling cc_binary
"""
if output_extension and len(outputs) != 0:
fail("You cannot use both output extension and outputs")

name_hash = hash(name)
cc_binary(
name = "{}__bin".format(name_hash),
Expand All @@ -101,13 +112,19 @@ def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no
**kwargs
)

_cc_ecsact_codegen_plugin_src(
name = "{}__src".format(name_hash),
output_extension = output_extension,
)
if(output_extension != None):
_cc_ecsact_codegen_plugin_src(
name = "{}__src".format(name_hash),
output_extension = output_extension,
)
else:
_cc_ecsact_codegen_plugin_src(
name = "{}__src".format(name_hash),
)

_cc_ecsact_codegen_plugin(
name = name,
cc_binary = ":{}__bin".format(name_hash),
output_extension = output_extension,
outputs = outputs
)