use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.
the class JavaProtoLibrary method create.
@Override
public ConfiguredTarget create(final RuleContext ruleContext) throws InterruptedException, RuleErrorException {
JavaCompilationArgsProvider dependencyArgsProviders = JavaCompilationArgsProvider.merge(Iterables.<JavaCompilationArgsAspectProvider, JavaCompilationArgsProvider>transform(this.<JavaCompilationArgsAspectProvider>getDeps(ruleContext, JavaCompilationArgsAspectProvider.class), JavaCompilationArgsAspectProvider.GET_PROVIDER));
if (!StrictDepsUtils.isStrictDepsJavaProtoLibrary(ruleContext)) {
dependencyArgsProviders = StrictDepsUtils.makeNonStrict(dependencyArgsProviders);
}
Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()).addArtifacts(dependencyArgsProviders.getRecursiveJavaCompilationArgs().getRuntimeJars()).build();
JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.merge(Iterables.<JavaSourceJarsAspectProvider, JavaSourceJarsProvider>transform(this.<JavaSourceJarsAspectProvider>getDeps(ruleContext, JavaSourceJarsAspectProvider.class), JavaSourceJarsAspectProvider.GET_PROVIDER));
NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
filesToBuild.addAll(sourceJarsProvider.getSourceJars());
for (JavaProtoLibraryTransitiveFilesToBuildProvider provider : ruleContext.getPrerequisites("deps", Mode.TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class)) {
filesToBuild.addTransitive(provider.getJars());
}
JavaRuleOutputJarsProvider ruleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().build();
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(dependencyArgsProviders);
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild.build()).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)).addOutputGroup(OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)).add(JavaCompilationArgsProvider.class, dependencyArgsProviders).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).build();
}
use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.
the class ActionReuser method reuseExistingActions.
/**
* If the underlying proto_library rule already registers the compile actions we need, just reuse
* them. This will preserve memory.
*
* <p>TODO(b/36191931): Delete when it's time.
*/
public static boolean reuseExistingActions(ConfiguredTarget base, RuleContext ruleContext, ConfiguredAspect.Builder aspect) {
if (!ruleContext.getConfiguration().getFragment(ProtoConfiguration.class).reuseJavaCompileActionsFromProtoLibrary()) {
return false;
}
ProtoJavaApiInfoProvider javaApi = base.getProvider(ProtoJavaApiInfoProvider.class);
if (javaApi == null) {
return false;
}
JavaCompilationArgs directJars = javaApi.getJavaCompilationArgsImmutable();
if (isEmpty(directJars.getCompileTimeJars()) || javaApi.sourceJarImmutable() == null) {
return false;
}
JavaCompilationArgs transitiveJars = JavaCompilationArgs.builder().addTransitiveArgs(javaApi.getTransitiveJavaCompilationArgsImmutable(), BOTH).addTransitiveDependencies(javaApi.getProtoRuntimeImmutable(), true).addTransitiveArgs(directJars, BOTH).build();
Artifact outputJar = getOnlyElement(directJars.getRuntimeJars());
Artifact compileTimeJar = getOnlyElement(directJars.getCompileTimeJars());
Artifact sourceJar = checkNotNull(javaApi.sourceJarImmutable());
JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(directJars, transitiveJars, NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER), NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER));
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(createOutputJarProvider(outputJar, compileTimeJar, sourceJar)).setSourceJarsProvider(createSrcJarProvider(sourceJar)).setCompilationArgsProvider(compilationArgsProvider);
NestedSet<Artifact> transitiveOutputJars = NestedSetBuilder.fromNestedSets(transform(ruleContext.getPrerequisites("deps", TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class), GET_JARS)).add(outputJar).build();
aspect.addSkylarkTransitiveInfo(JavaSkylarkApiProvider.PROTO_NAME.getLegacyId(), skylarkApiProvider.build()).addProviders(new JavaProtoLibraryTransitiveFilesToBuildProvider(transitiveOutputJars), new JavaCompilationArgsAspectProvider(compilationArgsProvider));
return true;
}
use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.
the class JavaLiteProtoLibrary method create.
@Override
public ConfiguredTarget create(final RuleContext ruleContext) throws InterruptedException, RuleErrorException {
JavaCompilationArgsProvider dependencyArgsProviders = JavaCompilationArgsProvider.merge(Iterables.<JavaCompilationArgsAspectProvider, JavaCompilationArgsProvider>transform(getDeps(ruleContext, JavaCompilationArgsAspectProvider.class), JavaCompilationArgsAspectProvider.GET_PROVIDER));
if (!StrictDepsUtils.isStrictDepsJavaProtoLibrary(ruleContext)) {
dependencyArgsProviders = StrictDepsUtils.makeNonStrict(dependencyArgsProviders);
}
Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()).addArtifacts(dependencyArgsProviders.getRecursiveJavaCompilationArgs().getRuntimeJars()).build();
JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.merge(Iterables.<JavaSourceJarsAspectProvider, JavaSourceJarsProvider>transform(getDeps(ruleContext, JavaSourceJarsAspectProvider.class), JavaSourceJarsAspectProvider.GET_PROVIDER));
NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
filesToBuild.addAll(sourceJarsProvider.getSourceJars());
for (JavaProtoLibraryTransitiveFilesToBuildProvider provider : ruleContext.getPrerequisites("deps", Mode.TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class)) {
filesToBuild.addTransitive(provider.getJars());
}
JavaRuleOutputJarsProvider javaRuleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().build();
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(javaRuleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(dependencyArgsProviders);
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild.build()).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)).addOutputGroup(OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)).add(JavaCompilationArgsProvider.class, dependencyArgsProviders).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)).add(ProguardSpecProvider.class, getJavaLiteRuntimeSpec(ruleContext)).add(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider).build();
}
use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.
the class SkylarkJavaLiteProtoLibraryTest method testEmptySrcsForJavaApi.
@Test
public void testEmptySrcsForJavaApi() throws Exception {
ConfiguredTarget target = scratchConfiguredTarget("notbad", "lite_pb2", "load('//tools/build_rules/java_lite_proto_library:java_lite_proto_library.bzl',", " 'java_lite_proto_library')", "java_lite_proto_library(name = 'lite_pb2', deps = [':null_lib'])", "proto_library(name = 'null_lib')");
JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class, target);
assertThat(compilationArgsProvider).isNotNull();
assertThat(compilationArgsProvider.getJavaCompilationArgs()).isNotNull();
JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class, target);
assertThat(sourceJarsProvider).isNotNull();
assertThat(sourceJarsProvider.getSourceJars()).isNotNull();
}
use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.
the class SkylarkJavaLiteProtoLibraryTest method jplCorrectlyDefinesDirectJars_strictDepsEnabled_aliasProto.
/**
* Tests that a java_proto_library only provides direct jars corresponding on the proto_library
* rules it directly depends on, excluding anything that the proto_library rules depends on
* themselves. This does not concern strict-deps in the compilation of the generated Java code
* itself, only compilation of regular code in java_library/java_binary and similar rules.
*
* <p>Here, a java_lite_proto_library dependes on an alias proto. We make sure that the system
* behaves as if we depend directly on the aliased proto_library.
*/
@Test
public void jplCorrectlyDefinesDirectJars_strictDepsEnabled_aliasProto() throws Exception {
scratch.file("x/BUILD", "load('//tools/build_rules/java_lite_proto_library:java_lite_proto_library.bzl',", " 'java_lite_proto_library')", "java_lite_proto_library(name = 'foo_java_proto_lite', deps = [':foo_proto'], ", " strict_deps = 1)", "proto_library(", " name = 'foo_proto',", " deps = [ ':bar_proto' ],", ")", "proto_library(", " name = 'bar_proto',", " srcs = [ 'bar.proto' ],", ")");
JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class, getConfiguredTarget("//x:foo_java_proto_lite"));
Iterable<String> directJars = prettyJarNames(compilationArgsProvider.getJavaCompilationArgs().getCompileTimeJars());
assertThat(directJars).containsExactly("x/libbar_proto-lite-hjar.jar");
}
Aggregations