Skip to content

Commit

Permalink
Create proto_toolchain rule
Browse files Browse the repository at this point in the history
This change adds a new `proto_toolchain` rule which we will use later to resolve
tools and options required to compile Protocol Buffers, thus eventually replacing
some of the command-line options from `ProtoConfiguration`.

To preclude inconsistencies in the migration period, we will start with a
`proto_toolchain` rule that doesn't have any arguments and populate
its provider from the existing command-line options. After all consumers
have been migrated to resolve the tools from the toolchain instead of
reading the command-line, we will replace all proto-specific flags with
attributes on `proto_toolchain` (or similar, language-specific toolchains
for flags that only affect a specific language).

Updates bazelbuild#10005
  • Loading branch information
Yannic committed Nov 14, 2020
1 parent ca6209f commit 36aad53
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 1 deletion.
7 changes: 6 additions & 1 deletion scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 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,
# distributed under the License is link_file "${PWD}/tools/java/runfiles/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/BUILD"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.
Expand Down Expand Up @@ -286,6 +286,10 @@ 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"
Expand All @@ -298,6 +302,7 @@ EOF
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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 @@ -291,6 +292,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 @@ -476,6 +476,8 @@ public void initializeRuleClasses(ConfiguredRuleClassProvider.Builder builder) {
ResourceFileLoader.loadResource(BazelRulesModule.class, "xcode_configure.WORKSPACE"));
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(BazelShRuleClasses.class, "sh_configure.WORKSPACE"));
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(BazelRulesModule.class, "proto.WORKSPACE"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
register_toolchains("@bazel_tools//tools/proto:toolchain")
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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/stringtemplate",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 {
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,33 @@
// 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.starlarkbuildapi.proto.ProtoToolchainInfoApi;

/**
* 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());
}
}
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 @@ -25,6 +25,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/platform",
"//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//third_party:guava",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.starlarkbuildapi.proto;

import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.starlarkbuildapi.platform.ToolchainInfoApi;
import net.starlark.java.annot.StarlarkBuiltin;

/** Provides access to information about the Protobuf toolchain rule. */
@StarlarkBuiltin(
name = "ProtoToolchainInfo",
category = DocCategory.PROVIDER,
doc = "Provides access to information about the Protobuf toolchain rule.")
public interface ProtoToolchainInfoApi extends ToolchainInfoApi {}
2 changes: 2 additions & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ filegroup(
"//tools/test:srcs",
"//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:srcs",
"//tools/test/CoverageOutputGenerator/javatests/com/google/devtools/coverageoutputgenerator:srcs",
"//tools/proto:srcs",
"//tools/python:srcs",
"//tools/runfiles:srcs",
"//tools/sh:srcs",
Expand Down Expand Up @@ -56,6 +57,7 @@ filegroup(
"//tools/def_parser:srcs",
"//tools/platforms:srcs",
"//tools/objc:srcs",
"//tools/proto:srcs",
"//tools/python:embedded_tools",
"//tools/runfiles:embedded_tools",
"//tools/test:embedded_tools",
Expand Down
22 changes: 22 additions & 0 deletions tools/proto/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0

filegroup(
name = "srcs",
srcs = glob(["**"]),
)
34 changes: 34 additions & 0 deletions tools/proto/BUILD.tools
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(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0

# The toolchain type used to distinguish proto toolchains.
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

# buildifier: disable=native-proto
proto_toolchain(
name = "default_toolchain",
)

toolchain(
name = "toolchain",
toolchain = ":default_toolchain",
toolchain_type = ":toolchain_type",
)

0 comments on commit 36aad53

Please sign in to comment.