Skip to content

Commit

Permalink
Add neverlink support to java_common.compile.
Browse files Browse the repository at this point in the history
Fixes bazelbuild#3735.

RELNOTES: java_common.compile supports neverlink
PiperOrigin-RevId: 184017410
  • Loading branch information
iirina authored and Copybara-Service committed Jan 31, 2018
1 parent aaab11f commit 223ed93
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ java_library(
"rules/java/JavaBuildInfoFactory.java",
"rules/java/JavaCommon.java",
"rules/java/JavaCompilationArgs.java",
"rules/java/JavaCompilationArgsHelper.java",
"rules/java/JavaCompilationArgsProvider.java",
"rules/java/JavaCompilationArtifacts.java",
"rules/java/JavaCompilationHelper.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,21 @@ public static String javaLibraryPath(
*
* @param recursive Whether to scan dependencies recursively.
* @param isNeverLink Whether the target has the 'neverlink' attr.
* @param srcLessDepsExport If srcs is omitted, deps are exported
* (deprecated behaviour for android_library only)
* @param srcLessDepsExport If srcs is omitted, deps are exported (deprecated behaviour for
* android_library only)
*/
public JavaCompilationArgs collectJavaCompilationArgs(boolean recursive, boolean isNeverLink,
boolean srcLessDepsExport) {
ClasspathType type = isNeverLink ? ClasspathType.COMPILE_ONLY : ClasspathType.BOTH;
JavaCompilationArgs.Builder builder = JavaCompilationArgs.builder()
.merge(getJavaCompilationArtifacts(), isNeverLink)
.addTransitiveTargets(getExports(ruleContext), recursive, type);
// TODO(bazel-team): remove srcs-less behaviour after android_library users are refactored
if (recursive || srcLessDepsExport) {
builder
.addTransitiveTargets(targetsTreatedAsDeps(ClasspathType.COMPILE_ONLY), recursive, type)
.addTransitiveTargets(getRuntimeDeps(ruleContext), recursive, ClasspathType.RUNTIME_ONLY);
}
return builder.build();
public JavaCompilationArgs collectJavaCompilationArgs(
boolean recursive, boolean isNeverLink, boolean srcLessDepsExport) {
return JavaLibraryHelper.getJavaCompilationArgs(
JavaCompilationArgsHelper.builder()
.setRecursive(recursive)
.setIsNeverLink(isNeverLink)
.setSrcLessDepsExport(srcLessDepsExport)
.setCompilationArtifacts(getJavaCompilationArtifacts())
.setDeps(targetsTreatedAsDeps(ClasspathType.COMPILE_ONLY))
.setRuntimeDeps(getRuntimeDeps(ruleContext))
.setExports(getExports(ruleContext))
.build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ public Builder addTransitiveCompilationArgs(
return this;
}

public Builder addTransitiveCompilationArgs(
Iterable<JavaCompilationArgsProvider> args, boolean recursive, ClasspathType type) {
for (JavaCompilationArgsProvider provider : args) {
addTransitiveCompilationArgs(provider, recursive, type);
}
return this;
}

/**
* Merges the artifacts of another target.
*/
Expand Down Expand Up @@ -237,6 +245,13 @@ public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection>
return addTransitiveTargets(deps, recursive, ClasspathType.BOTH);
}

/**
* Merges the artifacts of a collection of targets.
*/
public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection> deps) {
return addTransitiveTargets(deps, /*recursive=*/true, ClasspathType.BOTH);
}

/**
* Merges the artifacts of a collection of targets.
*/
Expand All @@ -248,6 +263,15 @@ public Builder addTransitiveDependencies(Iterable<JavaCompilationArgsProvider> d
return this;
}

/** Merges the artifacts of a collection of targets. */
public Builder addTransitiveDependencies(
Iterable<JavaCompilationArgsProvider> deps, boolean recursive, ClasspathType type) {
for (JavaCompilationArgsProvider dep : deps) {
addTransitiveDependency(dep, recursive, type);
}
return this;
}

/**
* Merges the artifacts of another target.
*/
Expand All @@ -260,13 +284,6 @@ private Builder addTransitiveDependency(JavaCompilationArgsProvider dep, boolean
return this;
}

/**
* Merges the artifacts of a collection of targets.
*/
public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection> deps) {
return addTransitiveTargets(deps, /*recursive=*/true, ClasspathType.BOTH);
}

/**
* Includes the contents of another instance of {@link JavaCompilationArgs}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2018 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.java;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import java.util.List;

@AutoValue
abstract class JavaCompilationArgsHelper {
abstract boolean recursive();

abstract boolean isNeverLink();

abstract boolean srcLessDepsExport();

abstract JavaCompilationArtifacts compilationArtifacts();

abstract List<JavaCompilationArgsProvider> depsCompilationArgs();

abstract Iterable<? extends TransitiveInfoCollection> deps();

abstract List<JavaCompilationArgsProvider> runtimeDepsCompilationArgs();

abstract Iterable<? extends TransitiveInfoCollection> runtimeDeps();

abstract List<JavaCompilationArgsProvider> exportsCompilationArgs();

abstract Iterable<? extends TransitiveInfoCollection> exports();

static Builder builder() {
return new AutoValue_JavaCompilationArgsHelper.Builder()
.setDepsCompilationArgs(ImmutableList.of())
.setDeps(ImmutableList.of())
.setRuntimeDepsCompilationArgs(ImmutableList.of())
.setRuntimeDeps(ImmutableList.of())
.setExportsCompilationArgs(ImmutableList.of())
.setExports(ImmutableList.of());
}

public abstract Builder toBuilder();

@AutoValue.Builder
abstract static class Builder {
abstract Builder setRecursive(boolean value);

abstract Builder setIsNeverLink(boolean value);

abstract Builder setSrcLessDepsExport(boolean value);

abstract Builder setCompilationArtifacts(JavaCompilationArtifacts value);

abstract Builder setDepsCompilationArgs(List<JavaCompilationArgsProvider> value);

abstract Builder setDeps(Iterable<? extends TransitiveInfoCollection> value);

abstract Builder setRuntimeDepsCompilationArgs(List<JavaCompilationArgsProvider> value);

abstract Builder setRuntimeDeps(Iterable<? extends TransitiveInfoCollection> value);

abstract Builder setExportsCompilationArgs(List<JavaCompilationArgsProvider> value);

abstract Builder setExports(Iterable<? extends TransitiveInfoCollection> value);

abstract JavaCompilationArgsHelper build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public JavaInfo createJavaInfo(
return javaInfoBuilder.build();
}

/** Creates a {@link JavaSourceJarsProvider} from the given lists of source jars. */
private static JavaSourceJarsProvider createJavaSourceJarsProvider(
List<Artifact> sourceJars, NestedSet<Artifact> transitiveSourceJars) {
NestedSet<Artifact> javaSourceJars = NestedSetBuilder.wrap(Order.STABLE_ORDER, sourceJars);
return JavaSourceJarsProvider.create(transitiveSourceJars, javaSourceJars);
}

private JavaSourceJarsProvider createJavaSourceJarsProvider(
Iterable<Artifact> sourceJars, Iterable<JavaInfo> transitiveDeps) {
NestedSetBuilder<Artifact> transitiveSourceJars = NestedSetBuilder.stableOrder();
Expand Down Expand Up @@ -279,6 +286,7 @@ public JavaInfo createJavaCompileAction(
ConfiguredTarget hostJavabase,
SkylarkList<Artifact> sourcepathEntries,
SkylarkList<Artifact> resources,
Boolean neverlink,
JavaSemantics javaSemantics)
throws EvalException, InterruptedException {
if (sourceJars.isEmpty() && sourceFiles.isEmpty() && exports.isEmpty()) {
Expand Down Expand Up @@ -331,7 +339,7 @@ public JavaInfo createJavaCompileAction(
outputSourceJar);

JavaCompilationArgsProvider javaCompilationArgsProvider =
helper.buildCompilationArgsProvider(artifacts, true);
helper.buildCompilationArgsProvider(artifacts, true, neverlink);
Runfiles runfiles =
new Runfiles.Builder(skylarkRuleContext.getWorkspaceName())
.addTransitiveArtifactsWrappedInStableOrder(
Expand Down Expand Up @@ -363,6 +371,7 @@ public JavaInfo createJavaCompileAction(
.addProvider(JavaRuleOutputJarsProvider.class, outputJarsBuilder.build())
.addProvider(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles))
.addProvider(JavaPluginInfoProvider.class, transitivePluginsProvider)
.setNeverlink(neverlink)
.build();
}

Expand Down Expand Up @@ -444,12 +453,4 @@ private static Artifact getSourceJar(SkylarkRuleContext skylarkRuleContext, Arti
return JavaCompilationHelper.derivedArtifact(
skylarkRuleContext.getRuleContext(), outputJar, "", "-src.jar");
}

/** Creates a {@link JavaSourceJarsProvider} from the given lists of source jars. */
private static JavaSourceJarsProvider createJavaSourceJarsProvider(
List<Artifact> sourceJars, NestedSet<Artifact> transitiveSourceJars) {
NestedSet<Artifact> javaSourceJars =
NestedSetBuilder.<Artifact>stableOrder().addAll(sourceJars).build();
return JavaSourceJarsProvider.create(transitiveSourceJars, javaSourceJars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode.OFF;
import static com.google.devtools.build.lib.rules.java.JavaCompilationArgs.ClasspathType.BOTH;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand All @@ -27,6 +26,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.rules.java.JavaCompilationArgs.ClasspathType;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaClasspathMode;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import java.util.ArrayList;
Expand Down Expand Up @@ -242,16 +242,21 @@ public JavaCompilationArtifacts build(
* compilation. Contrast this with {@link #setCompilationStrictDepsMode}.
*/
public JavaCompilationArgsProvider buildCompilationArgsProvider(
JavaCompilationArtifacts artifacts, boolean isReportedAsStrict) {
JavaCompilationArgs directArgs = JavaCompilationArgs.builder()
.merge(artifacts)
.addTransitiveDependencies(exports, true /* recursive */)
.build();
JavaCompilationArgs transitiveArgs =
JavaCompilationArgs.builder()
.addTransitiveArgs(directArgs, BOTH)
.addTransitiveDependencies(deps, true /* recursive */)
JavaCompilationArtifacts artifacts, boolean isReportedAsStrict, boolean isNeverlink) {
JavaCompilationArgsHelper compilationArgsHelper =
JavaCompilationArgsHelper.builder()
.setRecursive(false)
.setIsNeverLink(isNeverlink)
.setSrcLessDepsExport(false)
.setCompilationArtifacts(artifacts)
.setDepsCompilationArgs(deps)
.setExportsCompilationArgs(exports)
.build();

JavaCompilationArgs directArgs = getJavaCompilationArgs(compilationArgsHelper);
JavaCompilationArgs transitiveArgs =
getJavaCompilationArgs(compilationArgsHelper.toBuilder().setRecursive(true).build());

Artifact compileTimeDepArtifact = artifacts.getCompileTimeDependencyArtifact();
NestedSet<Artifact> compileTimeJavaDepArtifacts = compileTimeDepArtifact != null
? NestedSetBuilder.create(Order.STABLE_ORDER, compileTimeDepArtifact)
Expand Down Expand Up @@ -288,6 +293,27 @@ private NestedSet<Artifact> getNonRecursiveCompileTimeJarsFromDeps() {
.getCompileTimeJars();
}

static JavaCompilationArgs getJavaCompilationArgs(JavaCompilationArgsHelper helper) {
ClasspathType type = helper.isNeverLink() ? ClasspathType.COMPILE_ONLY : ClasspathType.BOTH;
JavaCompilationArgs.Builder builder =
JavaCompilationArgs.builder()
.merge(helper.compilationArtifacts(), helper.isNeverLink())
.addTransitiveTargets(helper.exports(), helper.recursive(), type)
.addTransitiveCompilationArgs(
helper.exportsCompilationArgs(), helper.recursive(), type);
// TODO(bazel-team): remove srcs-less behaviour after android_library users are refactored
if (helper.recursive() || helper.srcLessDepsExport()) {
builder
.addTransitiveCompilationArgs(helper.depsCompilationArgs(), helper.recursive(), type)
.addTransitiveTargets(helper.deps(), helper.recursive(), type)
.addTransitiveCompilationArgs(
helper.runtimeDepsCompilationArgs(), helper.recursive(), ClasspathType.RUNTIME_ONLY)
.addTransitiveTargets(
helper.runtimeDeps(), helper.recursive(), ClasspathType.RUNTIME_ONLY);
}
return builder.build();
}

private boolean isStrict() {
return strictDepsMode != OFF;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ public Provider getJavaProvider() {
type = SkylarkList.class,
generic1 = Artifact.class,
defaultValue = "[]"
),
@Param(
name = "neverlink",
positional = false,
named = true,
type = Boolean.class,
defaultValue = "False"
)
}
)
Expand All @@ -328,7 +335,8 @@ public JavaInfo createJavaCompileAction(
ConfiguredTarget javaToolchain,
ConfiguredTarget hostJavabase,
SkylarkList<Artifact> sourcepathEntries,
SkylarkList<Artifact> resources) throws EvalException, InterruptedException {
SkylarkList<Artifact> resources,
Boolean neverlink) throws EvalException, InterruptedException {

return JavaInfoBuildHelper.getInstance()
.createJavaCompileAction(
Expand All @@ -346,6 +354,7 @@ public JavaInfo createJavaCompileAction(
hostJavabase,
sourcepathEntries,
resources,
neverlink,
javaSemantics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ private JavaCompilationArgsProvider createJavaCompileAction(
JavaRuleOutputJarsProvider.builder(),
/*createOutputSourceJar*/ false,
/*outputSourceJar=*/ null);
return helper.buildCompilationArgsProvider(artifacts, true /* isReportedAsStrict */);
return helper.buildCompilationArgsProvider(
artifacts, /*isReportedAsStrict=*/ true, /*isNeverlink=*/ false);
}

private ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ private JavaCompilationArgsProvider createJavaCompileAction(
JavaRuleOutputJarsProvider.builder(),
/*createOutputSourceJar*/ false,
/*outputSourceJar=*/ null),
true /* isReportedAsStrict */);
/*isReportedAsStrict=*/ true,
/*isNeverlink=*/ false);
}

private ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib:bazel-main",
"//src/main/java/com/google/devtools/build/lib:bazel-rules",
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:filetype",
"//src/main/java/com/google/devtools/build/lib:java-compilation",
"//src/main/java/com/google/devtools/build/lib:java-rules",
"//src/main/java/com/google/devtools/build/lib:packages-internal",
Expand Down
Loading

0 comments on commit 223ed93

Please sign in to comment.