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

Create proto_toolchain rule #10937

Closed
wants to merge 17 commits 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
5 changes: 5 additions & 0 deletions scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,18 @@ EOF
link_file "${PWD}/tools/java/runfiles/Util.java" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/Util.java"
link_file "${PWD}/tools/java/runfiles/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/BUILD"

# Create @bazel_tools/tools/proto/BUILD
mkdir -p ${BAZEL_TOOLS_REPO}/tools/proto
link_file "${PWD}/tools/proto/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/proto/BUILD"

# Create @bazel_tools/tools/python/BUILD
mkdir -p ${BAZEL_TOOLS_REPO}/tools/python
link_file "${PWD}/tools/python/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/python/BUILD"

# Create the rest of @bazel_tools//tools/...
link_children "${PWD}" tools/cpp "${BAZEL_TOOLS_REPO}"
mv -f ${BAZEL_TOOLS_REPO}/tools/cpp/BUILD.tools ${BAZEL_TOOLS_REPO}/tools/cpp/BUILD
link_children "${PWD}" tools/proto "${BAZEL_TOOLS_REPO}"
link_children "${PWD}" tools/python "${BAZEL_TOOLS_REPO}"
link_children "${PWD}" tools "${BAZEL_TOOLS_REPO}"

Expand Down
4 changes: 2 additions & 2 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ rule_size_test(
# WARNING: Only adjust the number in `expect` if you are intentionally
# adding or removing embedded tools. Know that the more embedded tools there
# are in Bazel, the bigger the binary becomes and the slower Bazel starts.
expect = 470,
margin = 5, # percentage
expect = 500,
margin = 2, # percentage
)

filegroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
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.ProtoLangToolchainRule;
import com.google.devtools.build.lib.rules.proto.ProtoToolchainRule;
import com.google.devtools.build.lib.rules.python.PyInfo;
import com.google.devtools.build.lib.rules.python.PyRuleClasses.PySymlink;
import com.google.devtools.build.lib.rules.python.PyRuntimeInfo;
Expand Down Expand Up @@ -254,6 +255,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addConfigurationFragment(new ProtoConfiguration.Loader());
builder.addRuleDefinition(new BazelProtoLibraryRule());
builder.addRuleDefinition(new ProtoLangToolchainRule());
builder.addRuleDefinition(new ProtoToolchainRule());

ProtoBootstrap bootstrap =
new ProtoBootstrap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static class GraveyardOptions extends OptionsBase {
metadataTags = {OptionMetadataTag.DEPRECATED},
help = "Deprecated no-op.")
public String glibc;

@Deprecated
@Option(
name = "experimental_shortened_obj_file_path",
Expand Down Expand Up @@ -444,6 +444,8 @@ public void initializeRuleClasses(ConfiguredRuleClassProvider.Builder builder) {
try {
// Load auto-configuration files, it is made outside of the rule class provider so that it
// will not be loaded for our Java tests.
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(BazelRulesModule.class, "proto.WORKSPACE"));
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(BazelRulesModule.class, "xcode_configure.WORKSPACE"));
builder.addWorkspaceFileSuffix(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@bazel_tools//tools/proto:toolchain.bzl", "maybe_register_proto_toolchain")

maybe_register_proto_toolchain()
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,20 @@ public class StarlarkSemanticsOptions extends OptionsBase implements Serializabl
+ " modest cost in memory. The stack is visible in some forms of query output.")
public boolean recordRuleInstantiationCallstack;

@Option(
name = "incompatible_provide_proto_toolchain_in_tools_workspace",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help =
"If set to true, the default proto_toolchain is no longer provided by Bazel and should "
+ "be loaded from https://github.com/bazelbuild/rules_proto instead.")
public boolean incompatibleProvideProtoToolchainInToolsWorkspace;

/**
* An interner to reduce the number of StarlarkSemantics instances. A single Blaze instance should
* never accumulate a large number of these and being able to shortcut on object identity makes a
Expand Down Expand Up @@ -688,6 +702,8 @@ public StarlarkSemantics toSkylarkSemantics() {
.incompatibleLinkoptsToLinkLibs(incompatibleLinkoptsToLinkLibs)
.maxComputationSteps(maxComputationSteps)
.recordRuleInstantiationCallstack(recordRuleInstantiationCallstack)
.incompatibleProvideProtoToolchainInToolsWorkspace(
incompatibleProvideProtoToolchainInToolsWorkspace)
.build();
return INTERNER.intern(semantics);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ public abstract class CcProtoAspect extends NativeAspectClass implements Configu
private final CppSemantics cppSemantics;
private final LabelLateBoundDefault<?> ccToolchainAttrValue;
private final Label ccToolchainType;
private final Label protoToolchainType;

protected CcProtoAspect(AspectLegalCppSemantics cppSemantics, RuleDefinitionEnvironment env) {
this.cppSemantics = cppSemantics;
this.ccToolchainAttrValue = CppRuleClasses.ccToolchainAttribute(env);
this.ccToolchainType = CppRuleClasses.ccToolchainTypeAttribute(env);
this.protoToolchainType = ProtoCommon.protoToolchainTypeAttribute(env);
}

@Override
Expand Down Expand Up @@ -124,7 +126,7 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
.propagateAlongAttribute("deps")
.requiresConfigurationFragments(CppConfiguration.class, ProtoConfiguration.class)
.requireSkylarkProviders(ProtoInfo.PROVIDER.id())
.addRequiredToolchains(ccToolchainType)
.addRequiredToolchains(ccToolchainType, protoToolchainType)
.add(
attr(PROTO_TOOLCHAIN_ATTR, LABEL)
.mandatoryNativeProviders(ImmutableList.of(ProtoLangToolchainProvider.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.devtools.build.lib.rules.java.JavaSemantics;
import com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider;
import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider;
import com.google.devtools.build.lib.rules.proto.ProtoCommon;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.Exports;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.Services;
Expand All @@ -71,6 +72,7 @@ public static LabelLateBoundDefault<?> getProtoToolchainLabel(String defaultValu
private final String defaultProtoToolchainLabel;
private final Label hostJdkAttribute;
private final Label javaToolchainAttribute;
private final Label protoToolchainType;

public JavaLiteProtoAspect(
JavaSemantics javaSemantics,
Expand All @@ -80,6 +82,7 @@ public JavaLiteProtoAspect(
this.defaultProtoToolchainLabel = defaultProtoToolchainLabel;
this.hostJdkAttribute = JavaSemantics.hostJdkAttribute(env);
this.javaToolchainAttribute = JavaSemantics.javaToolchainAttribute(env);
this.protoToolchainType = ProtoCommon.protoToolchainTypeAttribute(env);
}

@Override
Expand Down Expand Up @@ -110,6 +113,7 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
.requiresConfigurationFragments(
JavaConfiguration.class, ProtoConfiguration.class, PlatformConfiguration.class)
.requireSkylarkProviders(ProtoInfo.PROVIDER.id())
.addRequiredToolchains(protoToolchainType)
.advertiseProvider(JavaProtoLibraryAspectProvider.class)
.advertiseProvider(
ImmutableList.of(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.devtools.build.lib.rules.java.JavaSemantics;
import com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider;
import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider;
import com.google.devtools.build.lib.rules.proto.ProtoCommon;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.Exports;
import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.Services;
Expand All @@ -62,6 +63,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe

private final Label hostJdkAttribute;
private final Label javaToolchainAttribute;
private final Label protoToolchainType;

private static LabelLateBoundDefault<?> getSpeedProtoToolchainLabel(String defaultValue) {
return LabelLateBoundDefault.fromTargetConfiguration(
Expand All @@ -86,6 +88,7 @@ protected JavaProtoAspect(
Preconditions.checkNotNull(defaultSpeedProtoToolchainLabel);
this.hostJdkAttribute = JavaSemantics.hostJdkAttribute(env);
this.javaToolchainAttribute = JavaSemantics.javaToolchainAttribute(env);
this.protoToolchainType = ProtoCommon.protoToolchainTypeAttribute(env);
}

@Override
Expand Down Expand Up @@ -119,6 +122,7 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
.requiresConfigurationFragments(
JavaConfiguration.class, ProtoConfiguration.class, PlatformConfiguration.class)
.requireSkylarkProviders(ProtoInfo.PROVIDER.id())
.addRequiredToolchains(protoToolchainType)
.advertiseProvider(JavaProtoLibraryAspectProvider.class)
.advertiseProvider(
ImmutableList.of(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/actions:commandline_item",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
"//src/main/java/com/google/devtools/build/lib/concurrent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironmen

return builder
.requiresConfigurationFragments(ProtoConfiguration.class)
.addRequiredToolchains(ProtoCommon.protoToolchainTypeAttribute(env))
.setOutputToGenfiles()
.add(
attr(":proto_compiler", LABEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ArtifactRoot;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.actions.SymlinkAction;
import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode;
Expand Down Expand Up @@ -53,6 +54,12 @@ private ProtoCommon() {
public static final String PROTO_RULES_MIGRATION_LABEL =
"__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__";

private static final String TOOLCHAIN_TYPE_LABEL = "//tools/proto:toolchain_type";

public static Label protoToolchainTypeAttribute(RuleDefinitionEnvironment env) {
return env.getToolsLabel(TOOLCHAIN_TYPE_LABEL);
}

/**
* Gets the direct sources of a proto library. If protoSources is not empty, the value is just
* protoSources. Otherwise, it's the combined sources of all direct dependencies of the given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 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.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;

/** The implementation of the <code>proto_toolchain</code> rule. */
public class ProtoToolchain implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws ActionConflictException, RuleErrorException, InterruptedException {
ProtoCommon.checkRuleHasValidMigrationTag(ruleContext);

ProtoToolchainInfo toolchain = new ProtoToolchainInfo();
RuleConfiguredTargetBuilder builder =
new RuleConfiguredTargetBuilder(ruleContext)
.addNativeDeclaredProvider(toolchain)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2020 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.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skylarkbuildapi.proto.ProtoToolchainInfoApi;
import com.google.devtools.build.lib.syntax.Location;

/**
* Information about the tools used by the <code>proto_*</code> and <code>LANG_proto_*</code> rules.
*/
@Immutable
@AutoCodec
public class ProtoToolchainInfo extends ToolchainInfo implements ProtoToolchainInfoApi {
@AutoCodec.Instantiator
public ProtoToolchainInfo() {
super(ImmutableMap.of(), Location.BUILTIN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 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.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.packages.RuleClass;

/** Rule definition of the <code>proto_toolchain</code> rule. */
public class ProtoToolchainRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) {
return builder
.requiresConfigurationFragments(ProtoConfiguration.class)
.advertiseProvider(ProtoToolchainInfo.class)
.build();
}

@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("proto_toolchain")
.ancestors(BaseRuleClasses.BaseRule.class)
.factoryClass(ProtoToolchain.class)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib:syntax",
"//src/main/java/com/google/devtools/build/lib/skylarkbuildapi",
"//src/main/java/com/google/devtools/build/lib/skylarkbuildapi/core",
"//src/main/java/com/google/devtools/build/lib/skylarkbuildapi/platform",
"//src/main/java/com/google/devtools/build/lib/skylarkinterface",
"//third_party:guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

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

import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.syntax.StarlarkSemantics.FlagIdentifier;
import com.google.devtools.build.lib.syntax.StarlarkValue;

/**
Expand All @@ -31,4 +33,15 @@
+ "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
@SkylarkCallable(
name =
"provide_proto_toolchain_in_tools_workspace"
+ "_do_not_use_or_we_will_break_you_without_mercy",
doc = "Indicates whether `@bazel_tools//tools/proto:toolchain` exists.",
documented = false,
structField = true,
disableWithFlag = FlagIdentifier.INCOMPATIBLE_PROVIDE_PROTO_TOOLCHAIN_IN_TOOLS_WORKSPACE)
default void provideProtoToolchainInToolsWorkspace() {};
}
Loading