diff options
author | 2018-01-11 17:15:24 -0800 | |
---|---|---|
committer | 2018-01-11 17:17:19 -0800 | |
commit | 3864a45afa368473a4a6a90d69edb48cb67d367a (patch) | |
tree | 522a19950cd7a40ba80b7591ec0eefb7f620c8ca /src/main | |
parent | 3863b536bcab8de2000f342c85c31c7ea91cccbe (diff) |
Add support for native header outputs to {java,android}_library
PiperOrigin-RevId: 181684446
Diffstat (limited to 'src/main')
9 files changed, 111 insertions, 35 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java index 5624734808..506bab6e09 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java @@ -127,6 +127,7 @@ public class AndroidCommon { private JavaCompilationArgs recursiveJavaCompilationArgs = JavaCompilationArgs.EMPTY_ARGS; private NestedSet<Artifact> jarsProducedForRuntime; private Artifact classJar; + private Artifact nativeHeaderOutput; private Artifact iJar; private Artifact srcJar; private Artifact genClassJar; @@ -448,7 +449,8 @@ public class AndroidCommon { null /* manifestProtoOutput */, null /* genSourceJar */, outputDepsProto, - javaArtifactsBuilder); + javaArtifactsBuilder, + /* nativeHeaderOutput= */ null); } } else { // Otherwise, it should have been the AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR. @@ -635,9 +637,16 @@ public class AndroidCommon { .addAllTransitiveSourceJars(javaCommon.collectTransitiveSourceJars(srcJar)); helper.createSourceJarAction(srcJar, genSourceJar); + nativeHeaderOutput = helper.createNativeHeaderJar(classJar); + outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder); helper.createCompileActionWithInstrumentation( - classJar, manifestProtoOutput, genSourceJar, outputDepsProto, javaArtifactsBuilder); + classJar, + manifestProtoOutput, + genSourceJar, + outputDepsProto, + javaArtifactsBuilder, + nativeHeaderOutput); if (isBinary) { generatedExtensionRegistryProvider = @@ -707,6 +716,7 @@ public class AndroidCommon { javaRuleOutputJarsProviderBuilder .addOutputJar(classJar, iJar, ImmutableList.of(srcJar)) .setJdeps(outputDepsProto) + .setNativeHeaders(nativeHeaderOutput) .build(); JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build(); JavaCompilationArgsProvider compilationArgsProvider = diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java index 338f878963..bcdc919b76 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java @@ -235,7 +235,8 @@ public abstract class AndroidLocalTestBase implements RuleConfiguredTargetFactor manifestProtoOutput, genSourceJar, outputDepsProtoArtifact, - instrumentationMetadata); + instrumentationMetadata, + /* nativeHeaderOutput= */ null); helper.createSourceJarAction(srcJar, genSourceJar); setUpJavaCommon(javaCommon, helper, javaArtifactsBuilder.build()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java index 78c4dfed16..c7dfcc1264 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java @@ -231,7 +231,12 @@ public class JavaBinary implements RuleConfiguredTargetFactory { } helper.createCompileAction( - classJar, manifestProtoOutput, genSourceJar, outputDepsProto, instrumentationMetadata); + classJar, + manifestProtoOutput, + genSourceJar, + outputDepsProto, + instrumentationMetadata, + /* nativeHeaderOutput= */ null); helper.createSourceJarAction(srcJar, genSourceJar); common.setClassPathFragment( diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java index 97268a6520..ae91d43c8f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java @@ -39,6 +39,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.packages.AttributeMap; +import com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts.Builder; import com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaClasspathMode; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.util.FileType; @@ -165,19 +166,20 @@ public final class JavaCompilationHelper { * * @param outputJar the class jar Artifact to create with the Action * @param manifestProtoOutput the output artifact for the manifest proto emitted from JavaBuilder - * @param gensrcOutputJar the generated sources jar Artifact to create with the Action - * (null if no sources will be generated). - * @param outputDepsProto the compiler-generated jdeps file to create with the Action - * (null if not requested) + * @param gensrcOutputJar the generated sources jar Artifact to create with the Action (null if no + * sources will be generated). + * @param outputDepsProto the compiler-generated jdeps file to create with the Action (null if not + * requested) * @param instrumentationMetadataJar metadata file (null if no instrumentation is needed or if - * --experimental_java_coverage is true). + * @param nativeHeaderOutput an archive of generated native header files. */ public void createCompileAction( Artifact outputJar, Artifact manifestProtoOutput, @Nullable Artifact gensrcOutputJar, @Nullable Artifact outputDepsProto, - @Nullable Artifact instrumentationMetadataJar) { + @Nullable Artifact instrumentationMetadataJar, + @Nullable Artifact nativeHeaderOutput) { JavaTargetAttributes attributes = getAttributes(); @@ -207,6 +209,7 @@ public final class JavaCompilationHelper { builder.setToolsJars(javaToolchain.getTools()); builder.setJavaBuilder(javaToolchain.getJavaBuilder()); builder.setOutputJar(classJar); + builder.setNativeHeaderOutput(nativeHeaderOutput); builder.setManifestProtoOutput(manifestProtoOutput); builder.setGensrcOutputJar(gensrcOutputJar); builder.setOutputDepsProto(outputDepsProto); @@ -298,19 +301,22 @@ public final class JavaCompilationHelper { * @param gensrcJar the generated sources jar Artifact to create with the Action * @param outputDepsProto the compiler-generated jdeps file to create with the Action * @param javaArtifactsBuilder the build to store the instrumentation metadata in + * @param nativeHeaderOutput an archive of generated native header files. */ public void createCompileActionWithInstrumentation( Artifact outputJar, Artifact manifestProtoOutput, @Nullable Artifact gensrcJar, @Nullable Artifact outputDepsProto, - JavaCompilationArtifacts.Builder javaArtifactsBuilder) { + Builder javaArtifactsBuilder, + @Nullable Artifact nativeHeaderOutput) { createCompileAction( outputJar, manifestProtoOutput, gensrcJar, outputDepsProto, - createInstrumentationMetadata(outputJar, javaArtifactsBuilder)); + createInstrumentationMetadata(outputJar, javaArtifactsBuilder), + nativeHeaderOutput); } /** @@ -428,6 +434,15 @@ public final class JavaCompilationHelper { outputJar.getRoot()); } + /** Returns the artifact for a jar file containing native header files. */ + public Artifact createNativeHeaderJar(Artifact outputJar) { + return getRuleContext() + .getDerivedArtifact( + FileSystemUtils.appendWithoutExtension( + outputJar.getRootRelativePath(), "-native-header"), + outputJar.getRoot()); + } + /** * Returns the artifact for a jar file containing source files that were generated by * annotation processors. diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java index dd4a5caa07..62bce3865f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java @@ -22,8 +22,6 @@ import static java.nio.charset.StandardCharsets.ISO_8859_1; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; -import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -65,11 +63,11 @@ import com.google.devtools.build.lib.util.LazyString; import com.google.devtools.build.lib.util.StringCanonicalizer; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; /** Action that represents a Java compilation. */ @ThreadCompatible @@ -505,6 +503,7 @@ public final class JavaCompileAction extends SpawnAction { private PathFragment javaExecutable; private List<Artifact> javabaseInputs = ImmutableList.of(); private Artifact outputJar; + private Artifact nativeHeaderOutput; private Artifact gensrcOutputJar; private Artifact manifestProtoOutput; private Artifact outputDepsProto; @@ -599,14 +598,16 @@ public final class JavaCompileAction extends SpawnAction { Preconditions.checkState(javaExecutable != null, owner); - ImmutableList.Builder<Artifact> outputsBuilder = ImmutableList.<Artifact>builder() - .addAll( - new ArrayList<>(Collections2.filter(Arrays.asList( - outputJar, - metadata, - gensrcOutputJar, - manifestProtoOutput, - outputDepsProto), Predicates.notNull()))); + ImmutableList.Builder<Artifact> outputsBuilder = ImmutableList.<Artifact>builder(); + Stream.of( + outputJar, + metadata, + gensrcOutputJar, + manifestProtoOutput, + outputDepsProto, + nativeHeaderOutput) + .filter(x -> x != null) + .forEachOrdered(outputsBuilder::add); if (additionalOutputs != null) { outputsBuilder.addAll(additionalOutputs); } @@ -701,6 +702,9 @@ public final class JavaCompileAction extends SpawnAction { if (outputJar != null) { result.addExecPath("--output", outputJar); } + if (nativeHeaderOutput != null) { + result.addExecPath("--native_header_output", nativeHeaderOutput); + } if (sourceGenDirectory != null) { result.add("--sourcegendir").addPath(sourceGenDirectory); } @@ -875,6 +879,11 @@ public final class JavaCompileAction extends SpawnAction { return this; } + public Builder setNativeHeaderOutput(Artifact nativeHeaderOutput) { + this.nativeHeaderOutput = nativeHeaderOutput; + return this; + } + public Builder setGensrcOutputJar(Artifact gensrcOutputJar) { this.gensrcOutputJar = gensrcOutputJar; return this; diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java index 80b2f905f1..a7f60329c4 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java @@ -126,8 +126,15 @@ public class JavaLibrary implements RuleConfiguredTargetFactory { Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder); - helper.createCompileActionWithInstrumentation(classJar, manifestProtoOutput, genSourceJar, - outputDepsProto, javaArtifactsBuilder); + Artifact nativeHeaderOutput = helper.createNativeHeaderJar(classJar); + + helper.createCompileActionWithInstrumentation( + classJar, + manifestProtoOutput, + genSourceJar, + outputDepsProto, + javaArtifactsBuilder, + nativeHeaderOutput); helper.createSourceJarAction(srcJar, genSourceJar); Artifact iJar = null; @@ -137,7 +144,8 @@ public class JavaLibrary implements RuleConfiguredTargetFactory { JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = JavaRuleOutputJarsProvider.builder() .addOutputJar(classJar, iJar, ImmutableList.of(srcJar)) - .setJdeps(outputDepsProto); + .setJdeps(outputDepsProto) + .setNativeHeaders(nativeHeaderOutput); GeneratedExtensionRegistryProvider generatedExtensionRegistryProvider = null; if (includeGeneratedExtensionRegistry) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java index 727d4ff97d..4d84125bc6 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java @@ -209,10 +209,11 @@ public final class JavaLibraryHelper { Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(output, artifactsBuilder); helper.createCompileAction( output, - null /* manifestProtoOutput */, - null /* gensrcOutputJar */, + /* manifestProtoOutput= */ null, + /* gensrcOutputJar= */ null, outputDepsProto, - null /* outputMetadata */); + /* instrumentationMetadataJar= */ null, + /* nativeHeaderOutput= */ null); artifactsBuilder.addRuntimeJar(output); Artifact iJar = helper.createCompileTimeJarAction(output, artifactsBuilder); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuleOutputJarsProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuleOutputJarsProvider.java index 5bc041d0e8..2f08800478 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuleOutputJarsProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuleOutputJarsProvider.java @@ -14,6 +14,8 @@ package com.google.devtools.build.lib.rules.java; +import static java.util.Objects.requireNonNull; + import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -37,7 +39,8 @@ import javax.annotation.Nullable; public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider { public static final JavaRuleOutputJarsProvider EMPTY = - new JavaRuleOutputJarsProvider(ImmutableList.<OutputJar>of(), null); + new JavaRuleOutputJarsProvider( + ImmutableList.<OutputJar>of(), /* jdeps= */ null, /* nativeHeaders= */ null); /** A collection of artifacts associated with a jar output. */ @SkylarkModule( @@ -113,11 +116,16 @@ public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider final ImmutableList<OutputJar> outputJars; @Nullable final Artifact jdeps; + /** An archive of native header files. */ + @Nullable final Artifact nativeHeaders; - private JavaRuleOutputJarsProvider(ImmutableList<OutputJar> outputJars, - @Nullable Artifact jdeps) { + private JavaRuleOutputJarsProvider( + ImmutableList<OutputJar> outputJars, + @Nullable Artifact jdeps, + @Nullable Artifact nativeHeaders) { this.outputJars = outputJars; this.jdeps = jdeps; + this.nativeHeaders = nativeHeaders; } @SkylarkCallable(name = "jars", doc = "A list of jars the rule outputs.", structField = true) @@ -153,6 +161,17 @@ public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider return jdeps; } + @Nullable + @SkylarkCallable( + name = "native_headers", + doc = "An archive of native header files.", + structField = true, + allowReturnNones = true + ) + public Artifact getNativeHeaders() { + return nativeHeaders; + } + public static Builder builder() { return new Builder(); } @@ -161,8 +180,9 @@ public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider * Builder for {@link JavaRuleOutputJarsProvider}. */ public static class Builder { - ImmutableList.Builder<OutputJar> outputJars = ImmutableList.builder(); - Artifact jdeps; + private final ImmutableList.Builder<OutputJar> outputJars = ImmutableList.builder(); + private Artifact jdeps; + private Artifact nativeHeaders; public Builder addOutputJar( @Nullable Artifact classJar, @@ -188,8 +208,13 @@ public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider return this; } + public Builder setNativeHeaders(Artifact nativeHeaders) { + this.nativeHeaders = requireNonNull(nativeHeaders); + return this; + } + public JavaRuleOutputJarsProvider build() { - return new JavaRuleOutputJarsProvider(outputJars.build(), jdeps); + return new JavaRuleOutputJarsProvider(outputJars.build(), jdeps, nativeHeaders); } } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java index 3e273a03b3..02d1cbe367 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java @@ -62,6 +62,8 @@ public interface JavaSemantics { SafeImplicitOutputsFunction JAVA_LIBRARY_CLASS_JAR = fromTemplates("lib%{name}.jar"); + SafeImplicitOutputsFunction JAVA_LIBRARY_NATIVE_HEADER_JAR = + fromTemplates("lib%{name}-native-header.jar"); SafeImplicitOutputsFunction JAVA_LIBRARY_SOURCE_JAR = fromTemplates("lib%{name}-src.jar"); |