Skip to content

Commit

Permalink
ProtoConfiguration: Expose some fields to Starlark
Browse files Browse the repository at this point in the history
This change exposes the values of `--protoc` and `--proto_options` to
Starlark, thus making it possible to implement `proto_toolchain` in
Starlark. See bazelbuild/rules_proto#82 for the
implementation of `proto_toolchain`.

Working towards #10005
  • Loading branch information
Yannic committed Jan 26, 2021
1 parent 30b5175 commit af11d09
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/google/devtools/build/lib/rules/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:rule_definition_environment",
"//src/main/java/com/google/devtools/build/lib/analysis:transitive_info_collection",
"//src/main/java/com/google/devtools/build/lib/analysis:transitive_info_provider",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/analysis/starlark/annotations",
"//src/main/java/com/google/devtools/build/lib/analysis/stringtemplate",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
Expand All @@ -54,6 +56,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//third_party:auto_value",
"//third_party:guava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.devtools.build.lib.analysis.config.Fragment;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
import com.google.devtools.build.lib.analysis.config.RequiresOptions;
import com.google.devtools.build.lib.analysis.starlark.annotations.StarlarkConfigurationField;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.starlarkbuildapi.ProtoConfigurationApi;
Expand Down Expand Up @@ -204,6 +205,7 @@ public ProtoConfiguration(BuildOptions buildOptions) {
this.options = options;
}

@Override
public ImmutableList<String> protocOpts() {
return protocOpts;
}
Expand All @@ -221,6 +223,11 @@ public boolean runExperimentalProtoExtraActions() {
return options.experimentalProtoExtraActions;
}

// Must match `@rules_proto//proto/private/rules:proto_toolchain.bzl`.
@StarlarkConfigurationField(
name = "protoc",
doc = "Exposes the value of `--proto_compiler`.",
documented = false)
public Label protoCompiler() {
return options.protoCompiler;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.rules.proto;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;

/** Information about the tools used by the {@code proto_*} and {@code LANG_proto_*} rules. */
@Immutable
@AutoValue
public abstract class ProtoToolchainInfo {
private static final String PROTOC_ATTR_NAME = "protoc";
private static final String PROTOC_OPTIONS_ATTR_NAME = "protoc_options";

/** The {@code protoc} binary to use for proto actions. */
public abstract FilesToRunProvider getProtoc();

/** Additional options to pass to {@code protoc}. */
public abstract ImmutableList<String> getProtocOptions();

/**
* Constructs a {@link ProtoToolchainInfo} from {@link ToolchainInfo} (i.e. as returned by {@code
* proto_toolchain} from {@code @rules_proto}).
*/
public static ProtoToolchainInfo fromToolchainInfo(ToolchainInfo toolchain) throws EvalException {
FilesToRunProvider protoc = getValue(toolchain, PROTOC_ATTR_NAME, FilesToRunProvider.class);
ImmutableList<String> protocOptions =
Sequence.cast(
getValue(toolchain, PROTOC_OPTIONS_ATTR_NAME, Object.class),
String.class,
PROTOC_OPTIONS_ATTR_NAME)
.getImmutableList();
return new AutoValue_ProtoToolchainInfo(protoc, protocOptions);
}

private static <T> T getValue(ToolchainInfo toolchain, String name, Class<T> clazz)
throws EvalException {
T value = toolchain.getValue(name, clazz);
if (value == null) {
throw Starlark.errorf("Proto toolchain does not have mandatory field '%s'", name);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@

package com.google.devtools.build.lib.starlarkbuildapi;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.docgen.annot.DocCategory;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.StarlarkValue;

/** A configuration fragment representing protocol buffers. */
@StarlarkBuiltin(
name = "proto",
category = DocCategory.CONFIGURATION_FRAGMENT,
doc = "A configuration fragment representing protocol buffers.")
public interface ProtoConfigurationApi extends StarlarkValue {}
public interface ProtoConfigurationApi extends StarlarkValue {
@StarlarkMethod(
name = "protoc_options",
doc = "Exposes the value of `--protocopt`.",
documented = false,
structField = true)
ImmutableList<String> protocOpts();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package com.google.devtools.build.lib.starlarkbuildapi.proto;

import com.google.common.collect.ImmutableList;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.StarlarkValue;

/**
Expand All @@ -31,4 +33,16 @@
+ "to load this symbol from <a href=\"https://github.com/bazelbuild/rules_proto\">"
+ "rules_proto</a>"
+ "</p>")
public interface ProtoCommonApi extends StarlarkValue {}
public interface ProtoCommonApi extends StarlarkValue {
@Deprecated
@StarlarkMethod(
name = "available_configuration_fields",
doc =
"Indicates that this version exposes fields on this configuration fragment. Do not use "
+ "this field, its only purpose is to help with migration.",
documented = false,
structField = true)
default ImmutableList<String> availableConfigurationFields() {
return ImmutableList.of("protoc", "protoc_options");
}
}

0 comments on commit af11d09

Please sign in to comment.