Skip to content

Commit

Permalink
Remove option to pass TransitiveApi to java_common.compile exports at…
Browse files Browse the repository at this point in the history
…tribute.

PiperOrigin-RevId: 373105495
  • Loading branch information
comius authored and copybara-github committed May 11, 2021
1 parent 7800ff9 commit caaf905
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ JavaInfo createJavaInfo(
JavaCompilationArgsProvider.class, javaCompilationArgsBuilder.build());

if (withExportsProvider) {
javaInfoBuilder.addProvider(
JavaExportsProvider.class,
createJavaExportsProvider(exports, /* labels = */ ImmutableList.of()));
javaInfoBuilder.addProvider(JavaExportsProvider.class, createJavaExportsProvider(exports));
}

javaInfoBuilder.javaPluginInfo(mergeExportedJavaPluginInfo(exports));
Expand Down Expand Up @@ -227,12 +225,9 @@ private Stream<NestedSet<Artifact>> fetchSourceJars(Iterable<JavaInfo> javaInfos
return concat(transitiveSourceJars, sourceJars);
}

private JavaExportsProvider createJavaExportsProvider(
Iterable<JavaInfo> exports, Iterable<Label> labels) {
ImmutableList.Builder<JavaExportsProvider> builder = new ImmutableList.Builder<>();
builder.addAll(JavaInfo.fetchProvidersFromList(exports, JavaExportsProvider.class));
builder.add(new JavaExportsProvider(NestedSetBuilder.wrap(Order.STABLE_ORDER, labels)));
return JavaExportsProvider.merge(builder.build());
private JavaExportsProvider createJavaExportsProvider(Iterable<JavaInfo> exports) {
return JavaExportsProvider.merge(
JavaInfo.fetchProvidersFromList(exports, JavaExportsProvider.class));
}

private JavaPluginInfo mergeExportedJavaPluginInfo(Iterable<JavaInfo> javaInfos) {
Expand All @@ -254,7 +249,6 @@ public JavaInfo createJavaCompileAction(
List<JavaInfo> runtimeDeps,
List<JavaInfo> experimentalLocalCompileTimeDeps,
List<JavaInfo> exports,
List<Label> exportLabels,
List<JavaInfo> plugins,
List<JavaInfo> exportedPlugins,
List<CcInfo> nativeLibraries,
Expand Down Expand Up @@ -351,7 +345,7 @@ public JavaInfo createJavaCompileAction(
createJavaSourceJarsProvider(outputSourceJars, concat(runtimeDeps, exports, deps)))
.addProvider(JavaRuleOutputJarsProvider.class, outputJarsBuilder.build())
.javaPluginInfo(mergeExportedJavaPluginInfo(concat(exportedPlugins, exports)))
.addProvider(JavaExportsProvider.class, createJavaExportsProvider(exports, exportLabels))
.addProvider(JavaExportsProvider.class, createJavaExportsProvider(exports))
.addProvider(JavaCcInfoProvider.class, JavaCcInfoProvider.merge(transitiveNativeLibraries))
.addTransitiveOnlyRuntimeJarsToJavaInfo(deps)
.addTransitiveOnlyRuntimeJarsToJavaInfo(exports)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,21 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.java;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions.INCOMPATIBLE_ENABLE_EXPORTS_PROVIDER;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.starlark.StarlarkActionFactory;
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.BazelModuleContext;
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaCommonApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaToolchainStarlarkApiProviderApi;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkList;
Expand All @@ -49,13 +44,6 @@ public class JavaStarlarkCommon
StarlarkActionFactory> {
private final JavaSemantics javaSemantics;

private static final ImmutableSet<String> PRIVATE_STARLARKIFICATION_ALLOWLIST =
ImmutableSet.of(
"@_builtins//:blaze/java/java_library.bzl",
"@_builtins//:blaze/java/java_common.bzl",
"//tools/build_defs/java:java_library.bzl",
"//tools/build_defs/java:java_common.bzl");

public JavaStarlarkCommon(JavaSemantics javaSemantics) {
this.javaSemantics = javaSemantics;
}
Expand Down Expand Up @@ -91,28 +79,6 @@ public JavaInfo createJavaCompileAction(
StarlarkThread thread)
throws EvalException, InterruptedException {

Sequence<JavaInfo> exportsJavaInfo;
Sequence<Label> exportsLabels;
if (exports.isEmpty() || exports.get(0) instanceof JavaInfo) {
exportsLabels = StarlarkList.empty();
exportsJavaInfo = Sequence.cast(exports, JavaInfo.class, "exports");
} else {
Label label =
((BazelModuleContext) Module.ofInnermostEnclosingStarlarkFunction(thread).getClientData())
.label();
if (!PRIVATE_STARLARKIFICATION_ALLOWLIST.contains(label.toString())) {
throw Starlark.errorf("Rule in '%s' cannot use private API", label.getPackageName());
}
Sequence<TransitiveInfoCollection> e =
Sequence.cast(exports, TransitiveInfoCollection.class, "exports");
exportsLabels =
StarlarkList.immutableCopyOf(
e.stream().map(TransitiveInfoCollection::getLabel).collect(toImmutableList()));
exportsJavaInfo =
StarlarkList.immutableCopyOf(
e.stream().map(JavaInfo::getJavaInfo).collect(toImmutableList()));
}

return JavaInfoBuildHelper.getInstance()
.createJavaCompileAction(
starlarkRuleContext,
Expand All @@ -127,8 +93,7 @@ public JavaInfo createJavaCompileAction(
experimentalLocalCompileTimeDeps,
JavaInfo.class,
"experimental_local_compile_time_deps"),
exportsJavaInfo,
exportsLabels,
Sequence.cast(exports, JavaInfo.class, "exports"),
Sequence.cast(plugins, JavaInfo.class, "plugins"),
Sequence.cast(exportedPlugins, JavaInfo.class, "exported_plugins"),
Sequence.cast(nativeLibraries, CcInfo.class, "native_libraries"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkActionFactoryApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleContextApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.TransitiveInfoCollectionApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.platform.ConstraintValueInfoApi;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -131,7 +130,6 @@ public interface JavaCommonApi<
named = true,
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = JavaInfoApi.class),
@ParamType(type = Sequence.class, generic1 = TransitiveInfoCollectionApi.class),
},
defaultValue = "[]",
doc = "A list of exports. Optional."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.Truth8.assertThat;
import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.prettyArtifactNames;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -2159,95 +2158,6 @@ public void javaInfo_nativeLibrariesPropagate() throws Exception {
.inOrder();
}

private void writeJavaCustomLibraryWithLabels(String path) throws Exception {
scratch.file(
path,
"def _impl(ctx):",
" output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
" compilation_provider = java_common.compile(",
" ctx,",
" source_files = ctx.files.srcs,",
" output = output_jar,",
" exports = ctx.attr.exports,",
" java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo],",
" )",
" return [",
" DefaultInfo(",
" files = depset([output_jar]),",
" ),",
" compilation_provider",
" ]",
"java_custom_library = rule(",
" implementation = _impl,",
" outputs = {",
" 'my_output': 'lib%{name}.jar'",
" },",
" attrs = {",
" 'srcs': attr.label_list(allow_files=['.java']),",
" 'exports': attr.label_list(),",
" '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
" },",
" fragments = ['java']",
")");
}

@Test
public void javaCompile_transitiveExportsWithLabels() throws Exception {
setBuildLanguageOptions("--incompatible_enable_exports_provider");
JavaToolchainTestUtil.writeBuildFileForJavaToolchain(scratch);
writeJavaCustomLibraryWithLabels("tools/build_defs/java/java_library.bzl");
scratch.file(
"foo/BUILD",
"load('//tools/build_defs/java:java_library.bzl', 'java_custom_library')",
"load(':extension.bzl', 'my_rule')",
"java_custom_library(name = 'lib',",
" srcs = ['Lib.java'],",
" exports = [ ':export' ])",
"java_custom_library(name = 'export',",
" srcs = ['Export.java'])",
"my_rule(name = 'my_starlark_rule', dep = ':lib')");
scratch.file("tools/build_defs/java/BUILD");
scratch.file(
"foo/extension.bzl",
"result = provider()",
"def _impl(ctx):",
" return [result(property = ctx.attr.dep[JavaInfo].transitive_exports)]",
"my_rule = rule(_impl, attrs = { 'dep' : attr.label() })");

assertNoEvents();
ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_starlark_rule");
StructImpl info =
(StructImpl)
myRuleTarget.get(
new StarlarkProvider.Key(
Label.parseAbsolute(
"//foo:extension.bzl", /* repositoryMapping = */ ImmutableMap.of()),
"result"));

Depset exports = (Depset) info.getValue("property");

assertThat(exports.getSet(Label.class).toList())
.containsExactly(Label.parseAbsolute("//foo:export", ImmutableMap.of()));
}

@Test
public void javaCompileTransitiveExportsWithLabels_limitedToBuiltins() throws Exception {
JavaToolchainTestUtil.writeBuildFileForJavaToolchain(scratch);
writeJavaCustomLibraryWithLabels("foo/java_library.bzl");
scratch.file(
"foo/BUILD",
"load('//foo:java_library.bzl', 'java_custom_library')",
"java_custom_library(name = 'lib',",
" srcs = ['Lib.java'],",
" exports = [ ':export' ])",
"java_custom_library(name = 'export',",
" srcs = ['Export.java'])");

AssertionError e = assertThrows(AssertionError.class, () -> getConfiguredTarget("//foo:lib"));

assertThat(e).hasMessageThat().contains("cannot use private API");
}

@Test
public void testJavaInfoGetGenJarsProvider() throws Exception {
scratch.file(
Expand Down

0 comments on commit caaf905

Please sign in to comment.