Skip to content

Commit

Permalink
java_library: provide a way to specify non transitive dependencies
Browse files Browse the repository at this point in the history
Add compile_deps parameter to java_library function to specify the non
transitive depencies. Because of the way how dependency graph is
implemented the deps parameter must be still list all dependencies, also
the non transitive:

  java_binary(
    name = 'plugin',
    manifest_file = 'resources/MANIFEST.MF',
    deps = ['//:plugin-library'],
  )

  java_library(
    name = 'plugin-library',
    srcs = glob(...),
    deps = ['//lib:plugin-api'],
    compile_deps = ['//lib:plugin-api'],
  )

JAR file plugin.jar doesn't include the content of plugin-api.
  • Loading branch information
davido committed Nov 10, 2013
1 parent 834a86e commit 2d27e26
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/com/facebook/buck/android/AndroidLibraryRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.BuildTargetPattern;
import com.facebook.buck.rules.AbstractBuildRuleBuilderParams;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.BuildRuleType;
Expand All @@ -35,6 +36,7 @@
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;

import java.io.IOException;
import java.util.List;
Expand All @@ -59,6 +61,7 @@ public AndroidLibraryRule(BuildRuleParams buildRuleParams,
Optional<String> manifestFile) {
super(buildRuleParams,
srcs,
ImmutableSortedSet.<BuildRule>of(),
resources,
proguardConfig,
/* exportDeps */ false,
Expand Down
81 changes: 77 additions & 4 deletions src/com/facebook/buck/java/DefaultJavaLibraryRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class DefaultJavaLibraryRule extends DoNotUseAbstractBuildable

private final ImmutableSortedSet<String> srcs;

private final ImmutableSortedSet<BuildRule> compileDeps;

private final ImmutableSortedSet<SourcePath> resources;

private final Optional<String> outputJar;
Expand All @@ -115,7 +117,6 @@ public class DefaultJavaLibraryRule extends DoNotUseAbstractBuildable

private final Optional<String> proguardConfig;


private final boolean exportDeps;

private final Supplier<ImmutableSetMultimap<JavaLibraryRule, String>> outputClasspathEntriesSupplier;
Expand All @@ -126,6 +127,9 @@ public class DefaultJavaLibraryRule extends DoNotUseAbstractBuildable
private final Supplier<ImmutableSetMultimap<JavaLibraryRule, String>>
declaredClasspathEntriesSupplier;

private final Supplier<ImmutableSetMultimap<JavaLibraryRule, String>>
compileClasspathEntriesSupplier;

private final JavacOptions javacOptions;

/**
Expand Down Expand Up @@ -182,12 +186,14 @@ public ImmutableSet<String> apply(String classPath) {

protected DefaultJavaLibraryRule(BuildRuleParams buildRuleParams,
Set<String> srcs,
Set<BuildRule> compileDeps,
Set<? extends SourcePath> resources,
Optional<String> proguardConfig,
boolean exportDeps,
JavacOptions javacOptions) {
super(buildRuleParams);
this.srcs = ImmutableSortedSet.copyOf(srcs);
this.compileDeps = ImmutableSortedSet.copyOf(compileDeps);
this.resources = ImmutableSortedSet.copyOf(resources);
this.proguardConfig = Preconditions.checkNotNull(proguardConfig);
this.exportDeps = exportDeps;
Expand Down Expand Up @@ -234,8 +240,15 @@ public ImmutableSetMultimap<JavaLibraryRule, String> get() {
public ImmutableSetMultimap<JavaLibraryRule, String> get() {
final ImmutableSetMultimap.Builder<JavaLibraryRule, String> classpathEntries =
ImmutableSetMultimap.builder();

ImmutableSortedSet.Builder<BuildRule> transitive = ImmutableSortedSet.naturalOrder();
for (BuildRule r: getDeps()) {
if (!DefaultJavaLibraryRule.this.compileDeps.contains(r)) {
transitive.add(r);
}
}
ImmutableSetMultimap<JavaLibraryRule, String> classpathEntriesForDeps =
Classpaths.getClasspathEntries(getDeps());
Classpaths.getClasspathEntries(transitive.build());

classpathEntries.putAll(classpathEntriesForDeps);

Expand All @@ -253,6 +266,31 @@ public ImmutableSetMultimap<JavaLibraryRule, String> get() {
}
});

compileClasspathEntriesSupplier =
Suppliers.memoize(new Supplier<ImmutableSetMultimap<JavaLibraryRule, String>>() {
@Override
public ImmutableSetMultimap<JavaLibraryRule, String> get() {
final ImmutableSetMultimap.Builder<JavaLibraryRule, String> classpathEntries =
ImmutableSetMultimap.builder();
ImmutableSetMultimap<JavaLibraryRule, String> classpathEntriesForCompileDeps =
Classpaths.getClasspathEntries(getCompileDeps());

classpathEntries.putAll(classpathEntriesForCompileDeps);

if (DefaultJavaLibraryRule.this.exportDeps) {
classpathEntries.putAll(DefaultJavaLibraryRule.this,
classpathEntriesForCompileDeps.values());
}

// Only add ourselves to the classpath if there's a jar to be built.
if (outputJar.isPresent()) {
classpathEntries.putAll(DefaultJavaLibraryRule.this, getPathToOutputFile());
}

return classpathEntries.build();
}
});

declaredClasspathEntriesSupplier =
Suppliers.memoize(new Supplier<ImmutableSetMultimap<JavaLibraryRule, String>>() {
@Override
Expand Down Expand Up @@ -282,6 +320,7 @@ public ImmutableSetMultimap<JavaLibraryRule, String> get() {
private void createCommandsForJavac(
String outputDirectory,
ImmutableSet<String> transitiveClasspathEntries,
ImmutableSet<String> compileClasspathEntries,
ImmutableSet<String> declaredClasspathEntries,
JavacOptions javacOptions,
BuildDependencies buildDependencies,
Expand All @@ -293,11 +332,15 @@ private void createCommandsForJavac(

// Only run javac if there are .java files to compile.
if (!getJavaSrcs().isEmpty()) {
ImmutableSet<String> declaredAndCompileEntries = new ImmutableSet.Builder<String>()
.addAll(compileClasspathEntries)
.addAll(declaredClasspathEntries)
.build();
final JavacInMemoryStep javac = new JavacInMemoryStep(
outputDirectory,
getJavaSrcs(),
transitiveClasspathEntries,
declaredClasspathEntries,
declaredAndCompileEntries,
javacOptions,
Optional.of(getPathToAbiOutputFile()),
Optional.of(getFullyQualifiedName()),
Expand Down Expand Up @@ -391,7 +434,8 @@ public RuleKey.Builder appendToRuleKey(RuleKey.Builder builder) throws IOExcepti
.set("srcs", srcs)
.setSourcePaths("resources", resources)
.set("proguard", proguardConfig)
.set("exportDeps", exportDeps);
.set("exportDeps", exportDeps)
.set("compileDeps", compileDeps);
javacOptions.appendToRuleKey(builder);
return builder;
}
Expand All @@ -411,11 +455,19 @@ public ImmutableSortedSet<String> getJavaSrcs() {
return srcs;
}

public ImmutableSortedSet<BuildRule> getCompileDeps() {
return compileDeps;
}

@Override
public ImmutableSetMultimap<JavaLibraryRule, String> getTransitiveClasspathEntries() {
return transitiveClasspathEntriesSupplier.get();
}

public ImmutableSetMultimap<JavaLibraryRule, String> getCompileClasspathEntries() {
return compileClasspathEntriesSupplier.get();
}

@Override
public ImmutableSetMultimap<JavaLibraryRule, String> getDeclaredClasspathEntries() {
return declaredClasspathEntriesSupplier.get();
Expand Down Expand Up @@ -475,6 +527,8 @@ public final List<Step> getBuildSteps(BuildContext context, BuildableContext bui

ImmutableSetMultimap<JavaLibraryRule, String> transitiveClasspathEntries =
getTransitiveClasspathEntries();
ImmutableSetMultimap<JavaLibraryRule, String> compileClasspathEntries =
getCompileClasspathEntries();
ImmutableSetMultimap<JavaLibraryRule, String> declaredClasspathEntries =
getDeclaredClasspathEntries();

Expand Down Expand Up @@ -523,6 +577,7 @@ public final List<Step> getBuildSteps(BuildContext context, BuildableContext bui
createCommandsForJavac(
outputDirectory,
ImmutableSet.copyOf(transitiveClasspathEntries.values()),
ImmutableSet.copyOf(compileClasspathEntries.values()),
ImmutableSet.copyOf(declaredClasspathEntries.values()),
javacOptions,
context.getBuildDependencies(),
Expand Down Expand Up @@ -762,6 +817,7 @@ public static class Builder extends AbstractBuildRuleBuilder<DefaultJavaLibraryR
SrcsAttributeBuilder, ResourcesAttributeBuilder {

protected Set<String> srcs = Sets.newHashSet();
protected Set<BuildTarget> compileDeps = Sets.newHashSet();
protected Set<SourcePath> resources = Sets.newHashSet();
protected final AnnotationProcessingParams.Builder annotationProcessingBuilder =
new AnnotationProcessingParams.Builder();
Expand All @@ -776,13 +832,16 @@ protected Builder(AbstractBuildRuleBuilderParams params) {
@Override
public DefaultJavaLibraryRule build(BuildRuleResolver ruleResolver) {
BuildRuleParams buildRuleParams = createBuildRuleParams(ruleResolver);
ImmutableSortedSet<BuildRule> compileDepsAsBuildRules =
getCompileDepsAsBuildRules(ruleResolver);
AnnotationProcessingParams processingParams =
annotationProcessingBuilder.build(ruleResolver);
javacOptions.setAnnotationProcessingData(processingParams);

return new DefaultJavaLibraryRule(
buildRuleParams,
srcs,
compileDepsAsBuildRules,
resources,
proguardConfig,
exportDeps,
Expand All @@ -806,6 +865,11 @@ public Builder addDep(BuildTarget dep) {
return this;
}

public Builder addCompileDep(BuildTarget compileDep) {
compileDeps.add(compileDep);
return this;
}

@Override
public Builder addSrc(String src) {
srcs.add(src);
Expand Down Expand Up @@ -844,5 +908,14 @@ public Builder setExportDeps(boolean exportDeps) {
this.exportDeps = exportDeps;
return this;
}

protected ImmutableSortedSet<BuildRule> getCompileDepsAsBuildRules(
final BuildRuleResolver ruleResolver) {
if (compileDeps.isEmpty()) {
return ImmutableSortedSet.of();
}
return getBuildTargetsAsBuildRules(ruleResolver,
compileDeps, false /* allowNonExistentRule */);
}
}
}
6 changes: 5 additions & 1 deletion src/com/facebook/buck/java/JavaLibraryBuildRuleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected void amendBuilder(DefaultJavaLibraryRule.Builder builder,
builder.setProguardConfig(
proguardConfig.transform(params.getResolveFilePathRelativeToBuildFileDirectoryTransform()));


boolean exportDeps = params.getBooleanAttribute("export_deps");
builder.setExportDeps(exportDeps);

Expand All @@ -53,6 +52,11 @@ protected void amendBuilder(DefaultJavaLibraryRule.Builder builder,
builder.setSourceLevel(sourceLevel.get());
}

for (String dep : params.getOptionalListAttribute("compile_deps")) {
BuildTarget buildTarget = params.resolveBuildTarget(dep);
builder.addCompileDep(buildTarget);
}

Optional<String> targetLevel = params.getOptionalStringAttribute("target");
if (targetLevel.isPresent()) {
builder.setTargetLevel(targetLevel.get());
Expand Down
1 change: 1 addition & 0 deletions src/com/facebook/buck/java/JavaTestRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected JavaTestRule(BuildRuleParams buildRuleParams,
ImmutableSet<JavaLibraryRule> sourceUnderTest) {
super(buildRuleParams,
srcs,
ImmutableSortedSet.<BuildRule>of(),
resources,
proguardConfig,
/* exportDeps */ false,
Expand Down
2 changes: 2 additions & 0 deletions src/com/facebook/buck/parser/buck.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def java_library(
target='6',
proguard_config=None,
deps=[],
compile_deps=[],
visibility=[],
build_env=None):
add_rule({
Expand All @@ -224,6 +225,7 @@ def java_library(
'target' : target,
'proguard_config' : proguard_config,
'deps' : deps,
'compile_deps' : compile_deps,
'visibility' : visibility,
}, build_env)

Expand Down

0 comments on commit 2d27e26

Please sign in to comment.