Skip to content

Commit

Permalink
Use proto compiler from proto_toolchain rule
Browse files Browse the repository at this point in the history
Issue: bazelbuild/rules_proto#179
PiperOrigin-RevId: 567570286
Change-Id: Ia7e1ff08a0123a325f1df00f56b81212c01e2588
  • Loading branch information
comius authored and copybara-github committed Sep 22, 2023
1 parent 01a7c72 commit 42800a8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/** Constants used in Proto rules. */
public final class ProtoConstants {
/** Default label for proto compiler. */
static final String DEFAULT_PROTOC_LABEL = "@bazel_tools//tools/proto:protoc";
public static final String DEFAULT_PROTOC_LABEL = "@bazel_tools//tools/proto:protoc";

/** Default label for java proto toolchains. */
static final String DEFAULT_JAVA_PROTO_LABEL = "@bazel_tools//tools/proto:java_toolchain";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def _rule_impl(ctx):
if ctx.attr.plugin != None:
plugin = ctx.attr.plugin[DefaultInfo].files_to_run

if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.proto_compiler
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.protoc_opts
else:
proto_compiler = ctx.attr._proto_compiler.files_to_run
protoc_opts = ctx.fragments.proto.experimental_protoc_opts

return [
DefaultInfo(
files = depset(),
Expand All @@ -44,8 +51,8 @@ def _rule_impl(ctx):
plugin = plugin,
runtime = ctx.attr.runtime,
provided_proto_sources = provided_proto_sources,
proto_compiler = ctx.attr._proto_compiler.files_to_run,
protoc_opts = ctx.fragments.proto.experimental_protoc_opts,
proto_compiler = proto_compiler,
protoc_opts = protoc_opts,
progress_message = ctx.attr.progress_message,
mnemonic = ctx.attr.mnemonic,
allowlist_different_package = ctx.attr.allowlist_different_package,
Expand Down Expand Up @@ -74,13 +81,15 @@ proto_lang_toolchain = rule(
cfg = "exec",
providers = [PackageSpecificationInfo],
),
} | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
"_proto_compiler": attr.label(
cfg = "exec",
executable = True,
allow_files = True,
default = configuration_field("proto", "proto_compiler"),
),
},
}),
provides = [ProtoLangToolchainInfo],
fragments = ["proto"],
toolchains = semantics.PROTO_TOOLCHAIN, # Used to obtain protoc
)
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/packages/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/pkgcache",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function",
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/rules/proto",
"//src/main/java/com/google/devtools/build/lib/rules/python",
"//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value",
"//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.packages.util;

import com.google.devtools.build.lib.rules.proto.ProtoConstants;
import com.google.devtools.build.lib.testutil.TestConstants;
import java.io.IOException;

Expand Down Expand Up @@ -43,7 +44,9 @@ private static void registerProtoToolchain(MockToolsConfig config) throws IOExce
"tools/proto/toolchains/BUILD",
TestConstants.LOAD_PROTO_TOOLCHAIN,
"proto_toolchain(name = 'protoc_sources',"
+ "proto_compiler = '//net/proto2/compiler/public:protocol_compiler')");
+ "proto_compiler = '"
+ ProtoConstants.DEFAULT_PROTOC_LABEL
+ "')");
}

/** Create a dummy "net/proto2 compiler and proto APIs for all languages and versions. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ public void protoToolchain() throws Exception {
validateProtoCompiler(toolchain, ProtoConstants.DEFAULT_PROTOC_LABEL);
}

@Test
public void protoToolchainResolution_enabled() throws Exception {
setBuildLanguageOptions("--incompatible_enable_proto_toolchain_resolution");
scratch.file(
"third_party/x/BUILD",
"licenses(['unencumbered'])",
"cc_binary(name = 'plugin', srcs = ['plugin.cc'])",
"cc_library(name = 'runtime', srcs = ['runtime.cc'])",
"filegroup(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])",
"filegroup(name = 'any', srcs = ['any.proto'])",
"proto_library(name = 'denied', srcs = [':descriptors', ':any'])");
scratch.file(
"foo/BUILD",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"licenses(['unencumbered'])",
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line:$(OUT)',",
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" progress_message = 'Progress Message %{label}',",
" mnemonic = 'MyMnemonic',",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
ProtoLangToolchainProvider toolchain =
ProtoLangToolchainProvider.get(getConfiguredTarget("//foo:toolchain"));

validateProtoLangToolchain(toolchain);
validateProtoCompiler(toolchain, ProtoConstants.DEFAULT_PROTOC_LABEL);
}

@Test
public void protoToolchainBlacklistProtoLibraries() throws Exception {
scratch.file(
Expand Down

0 comments on commit 42800a8

Please sign in to comment.