Search in sources :

Example 1 with ExportDependencies

use of com.facebook.buck.rules.ExportDependencies in project buck by facebook.

the class Project method walkRuleAndAdd.

/**
   * Walks the dependencies of a build rule and adds the appropriate DependentModules to the
   * specified dependencies collection. All library dependencies will be added before any module
   * dependencies. See {@code ProjectTest#testThatJarsAreListedBeforeModules()} for details on why
   * this behavior is important.
   */
@SuppressWarnings("PMD.LooseCoupling")
private void walkRuleAndAdd(final BuildRule rule, final boolean isForTests, final LinkedHashSet<SerializableDependentModule> dependencies, @Nullable final BuildRule srcTarget) {
    final Path basePathForRule = rule.getBuildTarget().getBasePath();
    Set<BuildRule> targetsToWalk;
    if (rule instanceof JavaTest) {
        targetsToWalk = ((JavaTest) rule).getCompiledTestsLibrary().getDeps();
    } else {
        targetsToWalk = rule.getDeps();
    }
    new AbstractBreadthFirstTraversal<BuildRule>(targetsToWalk) {

        private final LinkedHashSet<SerializableDependentModule> librariesToAdd = Sets.newLinkedHashSet();

        private final LinkedHashSet<SerializableDependentModule> modulesToAdd = Sets.newLinkedHashSet();

        @Override
        public ImmutableSet<BuildRule> visit(BuildRule dep) {
            // Hack: we don't want uber R.java to show up in the deps that IntelliJ sees.
            if (dep.getBuildTarget().toString().endsWith("_uber_r_dot_java")) {
                return ImmutableSet.of();
            }
            ImmutableSet<BuildRule> depsToVisit;
            if (rule.getProperties().is(PACKAGING) || dep instanceof AndroidResource || dep == rule) {
                depsToVisit = dep.getDeps();
            } else if (dep.getProperties().is(LIBRARY) && dep instanceof ExportDependencies) {
                depsToVisit = ((ExportDependencies) dep).getExportedDeps();
            } else {
                depsToVisit = ImmutableSet.of();
            }
            // should contain the union of :lib and :test's deps as dependent modules.
            if (isForTests && depsToVisit.isEmpty() && dep.getBuildTarget().getBasePath().equals(basePathForRule) && !dep.equals(srcTarget)) {
                depsToVisit = dep.getDeps();
            }
            SerializableDependentModule dependentModule;
            if (androidAars.contains(dep)) {
                AndroidPrebuiltAar aar = androidAars.getParentAar(dep);
                dependentModule = SerializableDependentModule.newLibrary(aar.getBuildTarget(), getIntellijNameForAar(aar));
            } else if (dep instanceof PrebuiltJar) {
                libraryJars.add(dep);
                String libraryName = getIntellijNameForRule(dep);
                dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
            } else if (dep instanceof AndroidPrebuiltAar) {
                androidAars.add((AndroidPrebuiltAar) dep);
                String libraryName = getIntellijNameForAar(dep);
                dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
            } else if ((dep instanceof CxxLibrary) || (dep instanceof NdkLibrary) || (dep instanceof JavaLibrary) || (dep instanceof AndroidResource)) {
                String moduleName = getIntellijNameForRule(dep);
                dependentModule = SerializableDependentModule.newModule(dep.getBuildTarget(), moduleName);
            } else {
                return depsToVisit;
            }
            if (librariesToAdd.contains(dependentModule) || modulesToAdd.contains(dependentModule)) {
                return depsToVisit;
            }
            if (isForTests) {
                dependentModule.scope = "TEST";
            } else {
                // If the dependentModule has already been added in the "TEST" scope, then it should be
                // removed and then re-added using the current (compile) scope.
                String currentScope = dependentModule.scope;
                dependentModule.scope = "TEST";
                if (dependencies.contains(dependentModule)) {
                    dependencies.remove(dependentModule);
                }
                dependentModule.scope = currentScope;
            }
            // dependencies collection once the traversal is complete in the onComplete() method.
            if (dependentModule.isLibrary()) {
                librariesToAdd.add(dependentModule);
            } else {
                modulesToAdd.add(dependentModule);
            }
            return depsToVisit;
        }

        @Override
        protected void onComplete() {
            dependencies.addAll(librariesToAdd);
            dependencies.addAll(modulesToAdd);
        }
    }.start();
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) CxxLibrary(com.facebook.buck.cxx.CxxLibrary) ExportDependencies(com.facebook.buck.rules.ExportDependencies) AndroidResource(com.facebook.buck.android.AndroidResource) PrebuiltJar(com.facebook.buck.jvm.java.PrebuiltJar) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) JavaTest(com.facebook.buck.jvm.java.JavaTest) AndroidPrebuiltAar(com.facebook.buck.android.AndroidPrebuiltAar) BuildRule(com.facebook.buck.rules.BuildRule) NdkLibrary(com.facebook.buck.android.NdkLibrary)

Aggregations

AndroidPrebuiltAar (com.facebook.buck.android.AndroidPrebuiltAar)1 AndroidResource (com.facebook.buck.android.AndroidResource)1 NdkLibrary (com.facebook.buck.android.NdkLibrary)1 CxxLibrary (com.facebook.buck.cxx.CxxLibrary)1 JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)1 JavaTest (com.facebook.buck.jvm.java.JavaTest)1 PrebuiltJar (com.facebook.buck.jvm.java.PrebuiltJar)1 BuildRule (com.facebook.buck.rules.BuildRule)1 ExportDependencies (com.facebook.buck.rules.ExportDependencies)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Path (java.nio.file.Path)1