Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProtoConfiguration: Expose some fields to Starlark #12484

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Comment on lines +36 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments in bazelbuild/rules_proto#82