Skip to content

Commit

Permalink
Adapt rules with output_file_suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj committed Jul 16, 2024
1 parent da0ddad commit dd8d47f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
22 changes: 20 additions & 2 deletions rules/proto_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,30 @@ def _proto_compile_impl(ctx):
# output_file_map = {f.short_path[len(ctx.label.package):].lstrip("/"): f for f in outputs}
output_file_map = {f.short_path: f for f in outputs}

if ctx.attr.output_file_suffix:
for [rel, original_file] in output_file_map.items():
dst_name = original_file.basename + ctx.attr.output_file_suffix
copy_file = ctx.actions.declare_file(dst_name, sibling = original_file)
ctx.actions.run_shell(
mnemonic = "ProtoCompileCopyFile",
inputs = [original_file],
outputs = [copy_file],
command = "cp '{}' '{}'".format(original_file.path, copy_file.path),
progress_message = "copying {} to {}".format(original_file.basename, copy_file.basename),
)
output_file_map[rel] = copy_file

output_files = output_file_map.values()

providers = [
ProtoCompileInfo(
label = ctx.label,
outputs = outputs,
outputs = output_files,
output_file_map = output_file_map,
),
]
if ctx.attr.default_info:
providers.append(DefaultInfo(files = depset(outputs)))
providers.append(DefaultInfo(files = depset(output_files)))

return providers

Expand Down Expand Up @@ -409,6 +424,9 @@ proto_compile = rule(
doc = "If false, do not return the DefaultInfo provider",
default = True,
),
"output_file_suffix": attr.string(
doc = "If set, copy the output files to a new set having this suffix",
),
},
toolchains = ["@build_stack_rules_proto//toolchain:protoc"],
)
72 changes: 25 additions & 47 deletions rules/proto_compile_gencopy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,32 @@
load("//cmd/gencopy:gencopy.bzl", "gencopy_action", "gencopy_attrs", "gencopy_config")
load(":providers.bzl", "ProtoCompileInfo")

def _copy_file(actions, src, dst):
"""Copy a file to a new path destination
Args:
actions: the <ctx.actions> object
src: the source file <File>
dst: the destination path of the file
Returns:
<Generated File> for the copied file
"""
actions.run_shell(
mnemonic = "CopyFile",
inputs = [src],
outputs = [dst],
command = "cp '{}' '{}'".format(src.path, dst.path),
progress_message = "copying {} to {}".format(src.path, dst.path),
)

def _proto_compile_gencopy_run_impl(ctx):
config = gencopy_config(ctx)

runfiles = []
for info in [dep[ProtoCompileInfo] for dep in ctx.attr.deps]:
srcs = []
dsts = []
# List[String]: names of files that represent the source files. In an
# update, these are the target filenames of a file copy operation.
source_files = []

# List[String]: names of files that represent the generated files. In
# an update, these are the source filenames of a file copy operation
# (although the file itself was generated by proto_compile).
generated_files = []

for [rel, generated_file] in info.output_file_map.items():
runfiles.append(generated_file)
srcs.append(generated_file.short_path)
dsts.append(rel)
source_files.append(rel)
generated_files.append(generated_file.short_path)

config.packageConfigs.append(
struct(
targetLabel = str(info.label),
targetPackage = info.label.package,
targetWorkspaceRoot = info.label.workspace_root,
generatedFiles = dsts,
sourceFiles = srcs,
generatedFiles = generated_files,
sourceFiles = source_files,
),
)

Expand Down Expand Up @@ -73,8 +62,14 @@ def _proto_compile_gencopy_test_impl(ctx):
source_file_map = {f.short_path: f for f in ctx.files.srcs}

for info in [dep[ProtoCompileInfo] for dep in ctx.attr.deps]:
srcs = []
dsts = []
# List[String]: names of files that represent the source files. In a
# test, these are the file paths of actual source files that are in the
# workspace (and checked into source control).
source_files = []

# List[String]: names of files that represent the generated files. In a
# test, these are the outputs files from the proto_compile rule.
generated_files = []
for [rel, generated_file] in info.output_file_map.items():
source_file = source_file_map.get(rel)
if not source_file:
Expand All @@ -85,16 +80,16 @@ def _proto_compile_gencopy_test_impl(ctx):

runfiles.append(source_file)
runfiles.append(generated_file)
srcs.append(source_file.short_path)
dsts.append(generated_file.short_path)
source_files.append(source_file.short_path)
generated_files.append(generated_file.short_path)

config.packageConfigs.append(
struct(
targetLabel = str(info.label),
targetPackage = info.label.package,
targetWorkspaceRoot = info.label.workspace_root,
generatedFiles = dsts,
sourceFiles = srcs,
generatedFiles = generated_files,
sourceFiles = source_files,
),
)

Expand Down Expand Up @@ -122,20 +117,3 @@ proto_compile_gencopy_test = rule(
executable = True,
test = True,
)

# found = False
# for srcfilename, srcfile in srcfiles.items():
# if srcfilename == f.basename:
# replica = ctx.actions.declare_file(f.basename + ".actual", sibling = f)
# _copy_file(ctx.actions, srcfile, replica)
# runfiles.append(replica)
# srcs.append(replica.short_path)
# found = True
# break
# elif srcfilename == f.basename + ctx.attr.extension:
# runfiles.append(srcfile)
# srcs.append(srcfile.short_path)
# found = True
# break
# if not found:
# fail("could not find matching source file for generated file %s in %r" % (f.basename, srcfiles))
4 changes: 2 additions & 2 deletions rules/proto_compiled_sources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def proto_compiled_sources(**kwargs):
name = name,
srcs = srcs,
protoc = protoc,
# output_suffix = "2",
# default_info = False,
output_file_suffix = ".gen",
default_info = False,
**kwargs
)

Expand Down

0 comments on commit dd8d47f

Please sign in to comment.