forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose ProtoToolchainInfo to Starlark
Progress on https://docs.google.com/document/d/1go3UMwm2nM8JHhI3MZrtyoFEy3BYg5omGqQmOwcjmNE/edit Closes bazelbuild#13732. PiperOrigin-RevId: 387087817
- Loading branch information
1 parent
3641e24
commit d5eeb04
Showing
7 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoToolchainInfoApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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.starlarkbuildapi.proto; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.docgen.annot.DocCategory; | ||
import com.google.devtools.build.docgen.annot.StarlarkConstructor; | ||
import com.google.devtools.build.lib.starlarkbuildapi.FileApi; | ||
import com.google.devtools.build.lib.starlarkbuildapi.FilesToRunProviderApi; | ||
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi; | ||
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi; | ||
import net.starlark.java.annot.Param; | ||
import net.starlark.java.annot.ParamType; | ||
import net.starlark.java.annot.StarlarkBuiltin; | ||
import net.starlark.java.annot.StarlarkMethod; | ||
import net.starlark.java.eval.EvalException; | ||
import net.starlark.java.eval.Sequence; | ||
|
||
/** Information about the {@code proto} toolchain. */ | ||
@StarlarkBuiltin( | ||
name = ProtoToolchainInfoApi.NAME, | ||
category = DocCategory.PROVIDER, | ||
doc = "Information about the `proto` toolchain.") | ||
public interface ProtoToolchainInfoApi< | ||
FilesToRunProviderApiT extends FilesToRunProviderApi<? extends FileApi>> | ||
extends StructApi { | ||
/** The name of the provider in Starlark. */ | ||
String NAME = "ProtoToolchainInfo"; | ||
|
||
@StarlarkMethod(name = "compiler", doc = "The proto compiler to use.", structField = true) | ||
FilesToRunProviderApiT getCompiler(); | ||
|
||
@StarlarkMethod( | ||
name = "compiler_options", | ||
doc = "Additional options to pass to `protoc`.", | ||
structField = true) | ||
ImmutableList<String> getCompilerOptions(); | ||
|
||
/** The provider implementing this can construct {@code ProtoToolchainInfo} objects. */ | ||
@StarlarkBuiltin( | ||
name = "Provider", | ||
doc = "", | ||
// This object is documented via the ProtoToolchainInfo documentation and the docuemntation of | ||
// its | ||
// callable function. | ||
documented = false) | ||
interface Provider<FilesToRunProviderApiT extends FilesToRunProviderApi<? extends FileApi>> | ||
extends ProviderApi { | ||
@StarlarkMethod( | ||
name = NAME, | ||
doc = "The `ProtoToolchainInfo` constructor.", | ||
parameters = { | ||
@Param( | ||
name = "compiler", | ||
doc = "The proto compiler.", | ||
positional = false, | ||
named = true, | ||
allowedTypes = {@ParamType(type = FilesToRunProviderApi.class)}), | ||
@Param( | ||
name = "compiler_options", | ||
doc = "The proto compiler.", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}) | ||
}, | ||
selfCall = true) | ||
@StarlarkConstructor | ||
ProtoToolchainInfoApi<FilesToRunProviderApiT> create( | ||
FilesToRunProviderApiT protoc, Sequence<?> protocOptions) throws EvalException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/com/google/devtools/build/lib/rules/proto/ProtoToolchainInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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 static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.analysis.FilesToRunProvider; | ||
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Unit tests for {@code ProtoToolchainInfo}. */ | ||
@RunWith(JUnit4.class) | ||
public class ProtoToolchainInfoTest extends BuildViewTestCase { | ||
@Before | ||
public void setUp() throws Exception { | ||
scratch.file( | ||
"proto/toolchain.bzl", | ||
"def _impl(ctx):", | ||
" return ProtoToolchainInfo(", | ||
" compiler = ctx.attr.compiler.files_to_run,", | ||
" compiler_options = ctx.attr.compiler_options,", | ||
" )", | ||
"proto_toolchain = rule(", | ||
" implementation = _impl,", | ||
" attrs = {", | ||
" 'compiler': attr.label(executable=True, cfg='exec'),", | ||
" 'compiler_options': attr.string_list(),", | ||
" },", | ||
")"); | ||
scratch.file( | ||
"proto/BUILD", | ||
"load(':toolchain.bzl', 'proto_toolchain')", | ||
"cc_binary(", | ||
" name = 'compiler',", | ||
")"); | ||
} | ||
|
||
@Test | ||
public void testStarlarkApi() throws Exception { | ||
scratch.file( | ||
"foo/BUILD", | ||
"load('//proto:toolchain.bzl', 'proto_toolchain')", | ||
"proto_toolchain(", | ||
" name = 'toolchain',", | ||
" compiler = '//proto:compiler',", | ||
")"); | ||
|
||
ConfiguredTarget target = getConfiguredTarget("//foo:toolchain"); | ||
ProtoToolchainInfo protoToolchain = target.get(ProtoToolchainInfo.PROVIDER); | ||
FilesToRunProvider compiler = protoToolchain.getCompiler(); | ||
assertThat(compiler.getExecutable().getOwner().toString()).isEqualTo("//proto:compiler"); | ||
assertThat(protoToolchain.getCompilerOptions()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testStarlarkApi_withCompilerOptions() throws Exception { | ||
scratch.file( | ||
"foo/BUILD", | ||
"load('//proto:toolchain.bzl', 'proto_toolchain')", | ||
"proto_toolchain(", | ||
" name = 'toolchain',", | ||
" compiler = '//proto:compiler',", | ||
" compiler_options = ['--foo', '--bar'],", | ||
")"); | ||
|
||
ConfiguredTarget target = getConfiguredTarget("//foo:toolchain"); | ||
ProtoToolchainInfo protoToolchain = target.get(ProtoToolchainInfo.PROVIDER); | ||
FilesToRunProvider compiler = protoToolchain.getCompiler(); | ||
assertThat(compiler.getExecutable().getOwner().toString()).isEqualTo("//proto:compiler"); | ||
assertThat(protoToolchain.getCompilerOptions()).containsExactly("--foo", "--bar").inOrder(); | ||
} | ||
} |