From dd8d47fc4387a6f959e255cee1bbfda3445b8d84 Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Mon, 15 Jul 2024 19:31:11 -0600 Subject: [PATCH] Adapt rules with output_file_suffix --- rules/proto_compile.bzl | 22 +++++++++- rules/proto_compile_gencopy.bzl | 72 +++++++++++--------------------- rules/proto_compiled_sources.bzl | 4 +- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/rules/proto_compile.bzl b/rules/proto_compile.bzl index e5444389..346cb02f 100644 --- a/rules/proto_compile.bzl +++ b/rules/proto_compile.bzl @@ -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 @@ -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"], ) diff --git a/rules/proto_compile_gencopy.bzl b/rules/proto_compile_gencopy.bzl index 14e24db2..1578cfda 100644 --- a/rules/proto_compile_gencopy.bzl +++ b/rules/proto_compile_gencopy.bzl @@ -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 object - src: the source file - dst: the destination path of the file - Returns: - 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, ), ) @@ -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: @@ -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, ), ) @@ -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)) diff --git a/rules/proto_compiled_sources.bzl b/rules/proto_compiled_sources.bzl index c13c57b4..c2e3c315 100644 --- a/rules/proto_compiled_sources.bzl +++ b/rules/proto_compiled_sources.bzl @@ -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 )