Skip to content

Commit

Permalink
Implement and expose proto_common.compile call.
Browse files Browse the repository at this point in the history
  • Loading branch information
comius authored and copybara-github committed Apr 7, 2022
1 parent 536f8d9 commit 982bbce
Show file tree
Hide file tree
Showing 6 changed files with 571 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import com.google.devtools.build.lib.rules.proto.BazelProtoLibraryRule;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
import com.google.devtools.build.lib.rules.proto.ProtoInfo;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainRule;
import com.google.devtools.build.lib.rules.python.PyInfo;
import com.google.devtools.build.lib.rules.python.PyRuleClasses.PySymlink;
Expand Down Expand Up @@ -291,6 +292,8 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
new StarlarkAspectStub(),
new ProviderStub());
builder.addStarlarkBootstrap(bootstrap);
builder.addStarlarkBuiltinsInternal(
"ProtoLangToolchainInfo", ProtoLangToolchainProvider.PROVIDER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ProtoBootstrap implements Bootstrap {
/** The name of the proto namespace in Starlark. */
public static final String PROTO_COMMON_NAME = "proto_common";

public static final String PROTO_COMMON_SECOND_NAME = "proto_common_do_not_use";

private final ProtoInfoProviderApi protoInfoApiProvider;
private final Object protoCommon;
private final StarlarkAspectApi protoRegistryAspect;
Expand All @@ -54,6 +56,7 @@ public ProtoBootstrap(
public void addBindingsToBuilder(ImmutableMap.Builder<String, Object> builder) {
builder.put(PROTO_INFO_STARLARK_NAME, protoInfoApiProvider);
builder.put(PROTO_COMMON_NAME, protoCommon);
builder.put(PROTO_COMMON_SECOND_NAME, protoCommon);
builder.put(
"ProtoRegistryAspect",
FlagGuardedValue.onlyWhenExperimentalFlagIsTrue(
Expand Down
3 changes: 2 additions & 1 deletion src/main/starlark/builtins_bzl/common/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load("@_builtins//:common/objc/objc_library.bzl", "objc_library")
load("@_builtins//:common/objc/apple_static_library.bzl", "apple_static_library")
load("@_builtins//:common/objc/compilation_support.bzl", "compilation_support")
load("@_builtins//:common/objc/linking_support.bzl", "linking_support")
load("@_builtins//:common/proto/proto_common.bzl", "proto_common")
load("@_builtins//:common/proto/proto_common.bzl", "proto_common", "proto_common_do_not_use")
load("@_builtins//:common/proto/proto_library.bzl", "proto_library")
load("@_builtins//:common/java/proto/java_lite_proto_library.bzl", "java_lite_proto_library")
load("@_builtins//:common/cc/cc_library.bzl", "cc_library")
Expand All @@ -33,6 +33,7 @@ exported_toplevels = {
# that builtins injection is working properly. Its built-in value is
# "original value".
"_builtins_dummy": "overridden value",
"proto_common_do_not_use": proto_common_do_not_use,
}

# A list of Starlarkified native rules.
Expand Down
81 changes: 81 additions & 0 deletions src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,87 @@ def _proto_path_flag(path):
def _Iimport_path_equals_fullpath(proto_source):
return "-I%s=%s" % (proto_source.import_path(), proto_source.source_file().path)

def _compile(
actions,
proto_library_target,
proto_lang_toolchain_info,
generated_files,
plugin_output = None,
additional_args = None,
additional_tools = [],
additional_inputs = depset(),
resource_set = None):
"""Creates proto compile action for compiling *.proto files to language specific sources.
Args:
actions: (ActionFactory) Obtained by ctx.actions, used to register the actions.
proto_library_target: (Target) The proto_library to generate the sources for.
Obtained as the `target` parameter from an aspect's implementation.
proto_lang_toolchain_info: (ProtoLangToolchainInfo) The proto lang toolchain info.
Obtained from a `proto_lang_toolchain` target or constructed ad-hoc..
generated_files: (list[File]) The output files generated by the proto compiler.
Callee needs to declare files using `ctx.actions.declare_file`.
See also: `proto_common.declare_generated_files`.
plugin_output: (File|str) The file or directory passed to the plugin.
Formatted with `proto_lang_toolchain.out_replacement_format_flag`
additional_args: (Args) Additional arguments to add to the action.
Accepts an ctx.actions.args() object that is added at the beginning
of the command line.
additional_tools: (list[File]) Additional tools to add to the action.
additional_inputs: (Depset[File]) Additional input files to add to the action.
resource_set:
(func) A callback function that is passed to the created action.
See `ctx.actions.run`, `resource_set` parameter for full definition of
the callback.
"""
proto_info = proto_library_target[_builtins.toplevel.ProtoInfo]

args = actions.args()
args.use_param_file(param_file_arg = "@%s")
args.set_param_file_format("multiline")
tools = list(additional_tools)

if plugin_output:
args.add(plugin_output, format = proto_lang_toolchain_info.out_replacement_format_flag)
if proto_lang_toolchain_info.plugin:
tools.append(proto_lang_toolchain_info.plugin)
args.add(proto_lang_toolchain_info.plugin.executable, format = proto_lang_toolchain_info.plugin_format_flag)

args.add_all(proto_info.transitive_proto_path, map_each = _proto_path_flag)
# Example: `--proto_path=--proto_path=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg`

args.add_all(proto_lang_toolchain_info.protoc_opts)

# Include maps
# For each import, include both the import as well as the import relativized against its
# protoSourceRoot. This ensures that protos can reference either the full path or the short
# path when including other protos.
args.add_all(proto_info.transitive_proto_sources(), map_each = _Iimport_path_equals_fullpath)
# Example: `-Ia.proto=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg/a.proto`

args.add_all(proto_info.direct_sources)

if additional_args:
additional_args.use_param_file(param_file_arg = "@%s")
additional_args.set_param_file_format("multiline")

actions.run(
mnemonic = proto_lang_toolchain_info.mnemonic,
progress_message = proto_lang_toolchain_info.progress_message,
executable = proto_lang_toolchain_info.proto_compiler,
arguments = [additional_args, args] if additional_args else [args],
inputs = depset(transitive = [proto_info.transitive_sources, additional_inputs]),
outputs = generated_files,
tools = tools,
use_default_shell_env = True,
resource_set = resource_set,
)

proto_common = struct(
create_proto_compile_action = _create_proto_compile_action,
)

proto_common_do_not_use = struct(
compile = _compile,
ProtoLangToolchainInfo = _builtins.internal.ProtoLangToolchainInfo,
)
18 changes: 18 additions & 0 deletions src/test/java/com/google/devtools/build/lib/rules/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ filegroup(
visibility = ["//src:__subpackages__"],
)

java_test(
name = "BazelProtoCommonTest",
srcs = ["BazelProtoCommonTest.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/actions:localhost_capacity",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/test/java/com/google/devtools/build/lib/actions/util",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//src/test/java/com/google/devtools/build/lib/packages:testutil",
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
"//third_party:junit4",
"//third_party:truth",
"@com_google_testparameterinjector//:testparameterinjector",
],
)

java_test(
name = "ProtoCompileActionBuilderTest",
srcs = ["ProtoCompileActionBuilderTest.java"],
Expand Down
Loading

0 comments on commit 982bbce

Please sign in to comment.