Skip to content

Commit 982bbce

Browse files
comiuscopybara-github
authored andcommitted
1 parent 536f8d9 commit 982bbce

6 files changed

Lines changed: 571 additions & 1 deletion

File tree

src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import com.google.devtools.build.lib.rules.proto.BazelProtoLibraryRule;
113113
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
114114
import com.google.devtools.build.lib.rules.proto.ProtoInfo;
115+
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider;
115116
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainRule;
116117
import com.google.devtools.build.lib.rules.python.PyInfo;
117118
import com.google.devtools.build.lib.rules.python.PyRuleClasses.PySymlink;
@@ -291,6 +292,8 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
291292
new StarlarkAspectStub(),
292293
new ProviderStub());
293294
builder.addStarlarkBootstrap(bootstrap);
295+
builder.addStarlarkBuiltinsInternal(
296+
"ProtoLangToolchainInfo", ProtoLangToolchainProvider.PROVIDER);
294297
}
295298

296299
@Override

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoBootstrap.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class ProtoBootstrap implements Bootstrap {
3434
/** The name of the proto namespace in Starlark. */
3535
public static final String PROTO_COMMON_NAME = "proto_common";
3636

37+
public static final String PROTO_COMMON_SECOND_NAME = "proto_common_do_not_use";
38+
3739
private final ProtoInfoProviderApi protoInfoApiProvider;
3840
private final Object protoCommon;
3941
private final StarlarkAspectApi protoRegistryAspect;
@@ -54,6 +56,7 @@ public ProtoBootstrap(
5456
public void addBindingsToBuilder(ImmutableMap.Builder<String, Object> builder) {
5557
builder.put(PROTO_INFO_STARLARK_NAME, protoInfoApiProvider);
5658
builder.put(PROTO_COMMON_NAME, protoCommon);
59+
builder.put(PROTO_COMMON_SECOND_NAME, protoCommon);
5760
builder.put(
5861
"ProtoRegistryAspect",
5962
FlagGuardedValue.onlyWhenExperimentalFlagIsTrue(

src/main/starlark/builtins_bzl/common/exports.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ load("@_builtins//:common/objc/objc_library.bzl", "objc_library")
2323
load("@_builtins//:common/objc/apple_static_library.bzl", "apple_static_library")
2424
load("@_builtins//:common/objc/compilation_support.bzl", "compilation_support")
2525
load("@_builtins//:common/objc/linking_support.bzl", "linking_support")
26-
load("@_builtins//:common/proto/proto_common.bzl", "proto_common")
26+
load("@_builtins//:common/proto/proto_common.bzl", "proto_common", "proto_common_do_not_use")
2727
load("@_builtins//:common/proto/proto_library.bzl", "proto_library")
2828
load("@_builtins//:common/java/proto/java_lite_proto_library.bzl", "java_lite_proto_library")
2929
load("@_builtins//:common/cc/cc_library.bzl", "cc_library")
@@ -33,6 +33,7 @@ exported_toplevels = {
3333
# that builtins injection is working properly. Its built-in value is
3434
# "original value".
3535
"_builtins_dummy": "overridden value",
36+
"proto_common_do_not_use": proto_common_do_not_use,
3637
}
3738

3839
# A list of Starlarkified native rules.

src/main/starlark/builtins_bzl/common/proto/proto_common.bzl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,87 @@ def _proto_path_flag(path):
8888
def _Iimport_path_equals_fullpath(proto_source):
8989
return "-I%s=%s" % (proto_source.import_path(), proto_source.source_file().path)
9090

91+
def _compile(
92+
actions,
93+
proto_library_target,
94+
proto_lang_toolchain_info,
95+
generated_files,
96+
plugin_output = None,
97+
additional_args = None,
98+
additional_tools = [],
99+
additional_inputs = depset(),
100+
resource_set = None):
101+
"""Creates proto compile action for compiling *.proto files to language specific sources.
102+
103+
Args:
104+
actions: (ActionFactory) Obtained by ctx.actions, used to register the actions.
105+
proto_library_target: (Target) The proto_library to generate the sources for.
106+
Obtained as the `target` parameter from an aspect's implementation.
107+
proto_lang_toolchain_info: (ProtoLangToolchainInfo) The proto lang toolchain info.
108+
Obtained from a `proto_lang_toolchain` target or constructed ad-hoc..
109+
generated_files: (list[File]) The output files generated by the proto compiler.
110+
Callee needs to declare files using `ctx.actions.declare_file`.
111+
See also: `proto_common.declare_generated_files`.
112+
plugin_output: (File|str) The file or directory passed to the plugin.
113+
Formatted with `proto_lang_toolchain.out_replacement_format_flag`
114+
additional_args: (Args) Additional arguments to add to the action.
115+
Accepts an ctx.actions.args() object that is added at the beginning
116+
of the command line.
117+
additional_tools: (list[File]) Additional tools to add to the action.
118+
additional_inputs: (Depset[File]) Additional input files to add to the action.
119+
resource_set:
120+
(func) A callback function that is passed to the created action.
121+
See `ctx.actions.run`, `resource_set` parameter for full definition of
122+
the callback.
123+
"""
124+
proto_info = proto_library_target[_builtins.toplevel.ProtoInfo]
125+
126+
args = actions.args()
127+
args.use_param_file(param_file_arg = "@%s")
128+
args.set_param_file_format("multiline")
129+
tools = list(additional_tools)
130+
131+
if plugin_output:
132+
args.add(plugin_output, format = proto_lang_toolchain_info.out_replacement_format_flag)
133+
if proto_lang_toolchain_info.plugin:
134+
tools.append(proto_lang_toolchain_info.plugin)
135+
args.add(proto_lang_toolchain_info.plugin.executable, format = proto_lang_toolchain_info.plugin_format_flag)
136+
137+
args.add_all(proto_info.transitive_proto_path, map_each = _proto_path_flag)
138+
# Example: `--proto_path=--proto_path=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg`
139+
140+
args.add_all(proto_lang_toolchain_info.protoc_opts)
141+
142+
# Include maps
143+
# For each import, include both the import as well as the import relativized against its
144+
# protoSourceRoot. This ensures that protos can reference either the full path or the short
145+
# path when including other protos.
146+
args.add_all(proto_info.transitive_proto_sources(), map_each = _Iimport_path_equals_fullpath)
147+
# Example: `-Ia.proto=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg/a.proto`
148+
149+
args.add_all(proto_info.direct_sources)
150+
151+
if additional_args:
152+
additional_args.use_param_file(param_file_arg = "@%s")
153+
additional_args.set_param_file_format("multiline")
154+
155+
actions.run(
156+
mnemonic = proto_lang_toolchain_info.mnemonic,
157+
progress_message = proto_lang_toolchain_info.progress_message,
158+
executable = proto_lang_toolchain_info.proto_compiler,
159+
arguments = [additional_args, args] if additional_args else [args],
160+
inputs = depset(transitive = [proto_info.transitive_sources, additional_inputs]),
161+
outputs = generated_files,
162+
tools = tools,
163+
use_default_shell_env = True,
164+
resource_set = resource_set,
165+
)
166+
91167
proto_common = struct(
92168
create_proto_compile_action = _create_proto_compile_action,
93169
)
170+
171+
proto_common_do_not_use = struct(
172+
compile = _compile,
173+
ProtoLangToolchainInfo = _builtins.internal.ProtoLangToolchainInfo,
174+
)

src/test/java/com/google/devtools/build/lib/rules/proto/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ filegroup(
1212
visibility = ["//src:__subpackages__"],
1313
)
1414

15+
java_test(
16+
name = "BazelProtoCommonTest",
17+
srcs = ["BazelProtoCommonTest.java"],
18+
deps = [
19+
"//src/main/java/com/google/devtools/build/lib/actions:localhost_capacity",
20+
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
21+
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
22+
"//src/main/java/com/google/devtools/build/lib/util:os",
23+
"//src/test/java/com/google/devtools/build/lib/actions/util",
24+
"//src/test/java/com/google/devtools/build/lib/analysis/util",
25+
"//src/test/java/com/google/devtools/build/lib/packages:testutil",
26+
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
27+
"//third_party:junit4",
28+
"//third_party:truth",
29+
"@com_google_testparameterinjector//:testparameterinjector",
30+
],
31+
)
32+
1533
java_test(
1634
name = "ProtoCompileActionBuilderTest",
1735
srcs = ["ProtoCompileActionBuilderTest.java"],

0 commit comments

Comments
 (0)