Search in sources :

Example 41 with TargetNode

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

the class AbstractProjectPredicates method forIde.

/**
   * Creates a {@link ProjectPredicates} value type configured for
   * the specified IDE.
   */
public static ProjectPredicates forIde(ProjectCommand.Ide targetIde) {
    Predicate<TargetNode<?, ?>> projectRootsPredicate;
    AssociatedTargetNodePredicate associatedProjectPredicate;
    // Prepare the predicates to create the project graph based on the IDE.
    switch(targetIde) {
        case INTELLIJ:
            projectRootsPredicate = input -> input.getDescription() instanceof ProjectConfigDescription;
            associatedProjectPredicate = (targetNode, targetGraph) -> {
                ProjectConfigDescription.Arg projectArg;
                if (targetNode.getDescription() instanceof ProjectConfigDescription) {
                    projectArg = (ProjectConfigDescription.Arg) targetNode.getConstructorArg();
                } else {
                    return false;
                }
                BuildTarget projectTarget = null;
                if (projectArg.srcTarget.isPresent()) {
                    projectTarget = projectArg.srcTarget.get();
                } else if (projectArg.testTarget.isPresent()) {
                    projectTarget = projectArg.testTarget.get();
                }
                return (projectTarget != null && targetGraph.getOptional(projectTarget).isPresent());
            };
            break;
        case XCODE:
            projectRootsPredicate = input -> input.getDescription() instanceof XcodeWorkspaceConfigDescription;
            associatedProjectPredicate = (targetNode, targetGraph) -> false;
            break;
        default:
            // unreachable
            throw new IllegalStateException("'ide' should always be of type 'INTELLIJ' or 'XCODE'");
    }
    return ProjectPredicates.of(projectRootsPredicate, associatedProjectPredicate);
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) XcodeWorkspaceConfigDescription(com.facebook.buck.apple.XcodeWorkspaceConfigDescription) BuildTarget(com.facebook.buck.model.BuildTarget) AssociatedTargetNodePredicate(com.facebook.buck.rules.AssociatedTargetNodePredicate) ProjectConfigDescription(com.facebook.buck.rules.ProjectConfigDescription)

Example 42 with TargetNode

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

the class AuditInputCommand method printJsonInputs.

@VisibleForTesting
int printJsonInputs(final CommandRunnerParams params, TargetGraph graph) throws IOException {
    final SortedMap<String, ImmutableSortedSet<Path>> targetToInputs = new TreeMap<>();
    new AbstractBottomUpTraversal<TargetNode<?, ?>, RuntimeException>(graph) {

        @Override
        public void visit(TargetNode<?, ?> node) {
            Optional<Cell> cellRoot = params.getCell().getCellIfKnown(node.getBuildTarget());
            Cell cell = cellRoot.isPresent() ? cellRoot.get() : params.getCell();
            LOG.debug("Looking at inputs for %s", node.getBuildTarget().getFullyQualifiedName());
            SortedSet<Path> targetInputs = new TreeSet<>();
            for (Path input : node.getInputs()) {
                LOG.debug("Walking input %s", input);
                try {
                    if (!cell.getFilesystem().exists(input)) {
                        throw new HumanReadableException("Target %s refers to non-existent input file: %s", node, params.getCell().getRoot().relativize(cell.getRoot().resolve(input)));
                    }
                    targetInputs.addAll(cell.getFilesystem().getFilesUnderPath(input));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            targetToInputs.put(node.getBuildTarget().getFullyQualifiedName(), ImmutableSortedSet.copyOf(targetInputs));
        }
    }.traverse();
    params.getObjectMapper().writeValue(params.getConsole().getStdOut(), targetToInputs);
    return 0;
}
Also used : Path(java.nio.file.Path) TargetNode(com.facebook.buck.rules.TargetNode) Optional(java.util.Optional) IOException(java.io.IOException) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Cell(com.facebook.buck.rules.Cell) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 43 with TargetNode

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

the class IjModuleGraph method from.

/**
   * @param projectConfig the project config used
   * @param targetGraph input graph.
   * @param libraryFactory library factory.
   * @param moduleFactory module factory.
   * @param aggregationMode module aggregation mode.
   * @return module graph corresponding to the supplied {@link TargetGraph}. Multiple targets from
   * the same base path are mapped to a single module, therefore an IjModuleGraph edge
   * exists between two modules (Ma, Mb) if a TargetGraph edge existed between a pair of
   * nodes (Ta, Tb) and Ma contains Ta and Mb contains Tb.
   */
public static IjModuleGraph from(final IjProjectConfig projectConfig, final TargetGraph targetGraph, final IjLibraryFactory libraryFactory, final IjModuleFactory moduleFactory, AggregationMode aggregationMode) {
    final ImmutableMap<BuildTarget, IjModule> rulesToModules = createModules(projectConfig, targetGraph, moduleFactory, aggregationMode.getGraphMinimumDepth(targetGraph.getNodes().size()));
    final ExportedDepsClosureResolver exportedDepsClosureResolver = new ExportedDepsClosureResolver(targetGraph);
    ImmutableMap.Builder<IjProjectElement, ImmutableMap<IjProjectElement, DependencyType>> depsBuilder = ImmutableMap.builder();
    final Set<IjLibrary> referencedLibraries = new HashSet<>();
    for (final IjModule module : ImmutableSet.copyOf(rulesToModules.values())) {
        Map<IjProjectElement, DependencyType> moduleDeps = new HashMap<>();
        for (Map.Entry<BuildTarget, DependencyType> entry : module.getDependencies().entrySet()) {
            BuildTarget depBuildTarget = entry.getKey();
            DependencyType depType = entry.getValue();
            ImmutableSet<IjProjectElement> depElements;
            if (depType.equals(DependencyType.COMPILED_SHADOW)) {
                TargetNode<?, ?> targetNode = targetGraph.get(depBuildTarget);
                Optional<IjLibrary> library = libraryFactory.getLibrary(targetNode);
                if (library.isPresent()) {
                    depElements = ImmutableSet.of(library.get());
                } else {
                    depElements = ImmutableSet.of();
                }
            } else {
                depElements = Stream.concat(exportedDepsClosureResolver.getExportedDepsClosure(depBuildTarget).stream(), Stream.of(depBuildTarget)).filter(input -> {
                    TargetNode<?, ?> targetNode = targetGraph.get(input);
                    return !module.getTargets().contains(targetNode);
                }).map(depTarget -> {
                    IjModule depModule = rulesToModules.get(depTarget);
                    if (depModule != null) {
                        return depModule;
                    }
                    TargetNode<?, ?> targetNode = targetGraph.get(depTarget);
                    return libraryFactory.getLibrary(targetNode).orElse(null);
                }).filter(Objects::nonNull).collect(MoreCollectors.toImmutableSet());
            }
            for (IjProjectElement depElement : depElements) {
                Preconditions.checkState(!depElement.equals(module));
                DependencyType.putWithMerge(moduleDeps, depElement, depType);
            }
        }
        if (!module.getExtraClassPathDependencies().isEmpty()) {
            IjLibrary extraClassPathLibrary = IjLibrary.builder().setClassPaths(module.getExtraClassPathDependencies()).setTargets(ImmutableSet.of()).setName("library_" + module.getName() + "_extra_classpath").build();
            moduleDeps.put(extraClassPathLibrary, DependencyType.PROD);
        }
        moduleDeps.keySet().stream().filter(dep -> dep instanceof IjLibrary).map(library -> (IjLibrary) library).forEach(referencedLibraries::add);
        depsBuilder.put(module, ImmutableMap.copyOf(moduleDeps));
    }
    referencedLibraries.forEach(library -> depsBuilder.put(library, ImmutableMap.of()));
    return new IjModuleGraph(depsBuilder.build());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) Set(java.util.Set) HashMap(java.util.HashMap) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) BuildTarget(com.facebook.buck.model.BuildTarget) AndroidResourceDescription(com.facebook.buck.android.AndroidResourceDescription) HashSet(java.util.HashSet) Objects(java.util.Objects) Stream(java.util.stream.Stream) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) TargetNode(com.facebook.buck.rules.TargetNode) HashMap(java.util.HashMap) ImmutableMap(com.google.common.collect.ImmutableMap) BuildTarget(com.facebook.buck.model.BuildTarget) Objects(java.util.Objects) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 44 with TargetNode

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

the class IjProject method write.

/**
   * Write the project to disk.
   *
   * @param runPostGenerationCleaner Whether or not the post-generation cleaner should be run.
   * @return set of {@link BuildTarget}s which should be built in order for the project to index
   *   correctly.
   * @throws IOException
   */
public ImmutableSet<BuildTarget> write(boolean runPostGenerationCleaner, boolean removeUnusedLibraries, boolean excludeArtifacts) throws IOException {
    final ImmutableSet.Builder<BuildTarget> requiredBuildTargets = ImmutableSet.builder();
    IjLibraryFactory libraryFactory = new DefaultIjLibraryFactory(new IjLibraryFactoryResolver() {

        @Override
        public Path getPath(SourcePath path) {
            Optional<BuildRule> rule = ruleFinder.getRule(path);
            if (rule.isPresent()) {
                requiredBuildTargets.add(rule.get().getBuildTarget());
            }
            return projectFilesystem.getRootPath().relativize(sourcePathResolver.getAbsolutePath(path));
        }

        @Override
        public Optional<SourcePath> getPathIfJavaLibrary(TargetNode<?, ?> targetNode) {
            BuildRule rule = buildRuleResolver.getRule(targetNode.getBuildTarget());
            if (!(rule instanceof JavaLibrary)) {
                return Optional.empty();
            }
            if (rule instanceof AndroidPrebuiltAar) {
                AndroidPrebuiltAar aarRule = (AndroidPrebuiltAar) rule;
                return Optional.ofNullable(aarRule.getBinaryJar());
            }
            requiredBuildTargets.add(rule.getBuildTarget());
            return Optional.ofNullable(rule.getSourcePathToOutput());
        }
    });
    IjModuleFactoryResolver moduleFactoryResolver = new IjModuleFactoryResolver() {

        @Override
        public Optional<Path> getDummyRDotJavaPath(TargetNode<?, ?> targetNode) {
            BuildTarget dummyRDotJavaTarget = AndroidLibraryGraphEnhancer.getDummyRDotJavaTarget(targetNode.getBuildTarget());
            Optional<BuildRule> dummyRDotJavaRule = buildRuleResolver.getRuleOptional(dummyRDotJavaTarget);
            if (dummyRDotJavaRule.isPresent()) {
                requiredBuildTargets.add(dummyRDotJavaTarget);
                return Optional.of(DummyRDotJava.getRDotJavaBinFolder(dummyRDotJavaTarget, projectFilesystem));
            }
            return Optional.empty();
        }

        @Override
        public Path getAndroidManifestPath(TargetNode<AndroidBinaryDescription.Arg, ?> targetNode) {
            return sourcePathResolver.getAbsolutePath(targetNode.getConstructorArg().manifest);
        }

        @Override
        public Optional<Path> getLibraryAndroidManifestPath(TargetNode<AndroidLibraryDescription.Arg, ?> targetNode) {
            Optional<SourcePath> manifestPath = targetNode.getConstructorArg().manifest;
            Optional<Path> defaultAndroidManifestPath = intellijConfig.getAndroidManifest().map(Path::toAbsolutePath);
            return manifestPath.map(sourcePathResolver::getAbsolutePath).map(Optional::of).orElse(defaultAndroidManifestPath);
        }

        @Override
        public Optional<Path> getProguardConfigPath(TargetNode<AndroidBinaryDescription.Arg, ?> targetNode) {
            return targetNode.getConstructorArg().proguardConfig.map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAndroidResourcePath(TargetNode<AndroidResourceDescription.Arg, ?> targetNode) {
            return AndroidResourceDescription.getResDirectoryForProject(buildRuleResolver, targetNode).map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAssetsPath(TargetNode<AndroidResourceDescription.Arg, ?> targetNode) {
            return AndroidResourceDescription.getAssetsDirectoryForProject(buildRuleResolver, targetNode).map(this::getRelativePathAndRecordRule);
        }

        @Override
        public Optional<Path> getAnnotationOutputPath(TargetNode<? extends JvmLibraryArg, ?> targetNode) {
            AnnotationProcessingParams annotationProcessingParams = targetNode.getConstructorArg().buildAnnotationProcessingParams(targetNode.getBuildTarget(), projectFilesystem, buildRuleResolver, ImmutableSet.of());
            if (annotationProcessingParams == null || annotationProcessingParams.isEmpty()) {
                return Optional.empty();
            }
            return Optional.ofNullable(annotationProcessingParams.getGeneratedSourceFolderName());
        }

        private Path getRelativePathAndRecordRule(SourcePath sourcePath) {
            requiredBuildTargets.addAll(OptionalCompat.asSet(ruleFinder.getRule(sourcePath).map(BuildRule::getBuildTarget)));
            return sourcePathResolver.getRelativePath(sourcePath);
        }
    };
    IjModuleGraph moduleGraph = IjModuleGraph.from(projectConfig, targetGraphAndTargets.getTargetGraph(), libraryFactory, new IjModuleFactory(projectFilesystem, moduleFactoryResolver, projectConfig, excludeArtifacts), aggregationMode);
    JavaPackageFinder parsingJavaPackageFinder = ParsingJavaPackageFinder.preparse(javaFileParser, projectFilesystem, IjProjectTemplateDataPreparer.createPackageLookupPathSet(moduleGraph), javaPackageFinder);
    IjProjectWriter writer = new IjProjectWriter(new IjProjectTemplateDataPreparer(parsingJavaPackageFinder, moduleGraph, projectFilesystem), projectConfig, projectFilesystem, moduleGraph);
    writer.write(runPostGenerationCleaner, removeUnusedLibraries);
    return requiredBuildTargets.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) JvmLibraryArg(com.facebook.buck.jvm.java.JvmLibraryArg) SourcePath(com.facebook.buck.rules.SourcePath) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) AndroidResourceDescription(com.facebook.buck.android.AndroidResourceDescription) ImmutableSet(com.google.common.collect.ImmutableSet) AndroidBinaryDescription(com.facebook.buck.android.AndroidBinaryDescription) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Optional(java.util.Optional) AndroidLibraryDescription(com.facebook.buck.android.AndroidLibraryDescription) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) AndroidPrebuiltAar(com.facebook.buck.android.AndroidPrebuiltAar) AnnotationProcessingParams(com.facebook.buck.jvm.java.AnnotationProcessingParams)

Example 45 with TargetNode

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

the class APKModuleTest method testAPKModuleGraphComplexDependencyTree.

/*
                   +----------------------------+
                   |                            |
                   |                            |
    +--------------+---------------+            |
    |              |               v            v
  +--------+     +---------+     +------+     +--------+     +--------+
  | Binary | --> | Android | --> | Java | --> | Shared | --> |        |
  +--------+     +---------+     +------+     +--------+     |        |
    |              |               |                         |        |
    +--------------+---------------+-----------------------> | Common |
                   |               |                         |        |
                   |               |                         |        |
                   +---------------+-----------------------> |        |
                                   |                         +--------+
                                   |                           ^
                                   +---------------------------+
  */
@Test
public void testAPKModuleGraphComplexDependencyTree() throws Exception {
    ImmutableSet.Builder<TargetNode<?, ?>> nodeBuilder = ImmutableSet.builder();
    BuildTarget commonLibraryTarget = BuildTargetFactory.newInstance("//:test-common-library");
    nodeBuilder.add(AndroidLibraryBuilder.createBuilder(commonLibraryTarget).addSrc(Paths.get("src/com/facebook/TestCommonLibrary.java")).build());
    BuildTarget sharedLibraryTarget = BuildTargetFactory.newInstance("//:test-shared-library");
    nodeBuilder.add(AndroidLibraryBuilder.createBuilder(sharedLibraryTarget).addSrc(Paths.get("src/com/facebook/TestSharedLibrary.java")).addDep(commonLibraryTarget).build());
    BuildTarget javaDepLibraryTarget = BuildTargetFactory.newInstance("//:test-java-dep-library");
    nodeBuilder.add(AndroidLibraryBuilder.createBuilder(javaDepLibraryTarget).addSrc(Paths.get("src/com/facebook/TestJavaDepLibrary.java")).build());
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//:test-java-library");
    nodeBuilder.add(JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(Paths.get("src/com/facebook/TestJavaLibrary.java")).addDep(commonLibraryTarget).addDep(javaDepLibraryTarget).addDep(sharedLibraryTarget).build());
    BuildTarget androidLibraryTarget = BuildTargetFactory.newInstance("//:test-android-library");
    nodeBuilder.add(AndroidLibraryBuilder.createBuilder(androidLibraryTarget).addSrc(Paths.get("src/com/facebook/TestAndroidLibrary.java")).addDep(javaLibraryTarget).addDep(commonLibraryTarget).addDep(sharedLibraryTarget).build());
    BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//:keystore");
    nodeBuilder.add(KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath("debug.keystore")).setProperties(new FakeSourcePath("keystore.properties")).build());
    BuildTarget androidBinaryTarget = BuildTargetFactory.newInstance("//:test-android-binary");
    nodeBuilder.add(AndroidBinaryBuilder.createBuilder(androidBinaryTarget).setManifest(new FakeSourcePath("AndroidManifest.xml")).setKeystore(keystoreTarget).setOriginalDeps(ImmutableSortedSet.of(androidLibraryTarget, javaLibraryTarget, commonLibraryTarget)).build());
    TargetGraph graph = TargetGraphFactory.newInstance(nodeBuilder.build());
    Set<BuildTarget> seedTargets = new HashSet<>();
    seedTargets.add(androidLibraryTarget);
    seedTargets.add(javaLibraryTarget);
    APKModuleGraph dag = new APKModuleGraph(graph, androidBinaryTarget, Optional.of(seedTargets));
    ImmutableSet<APKModule> topLevelNodes = dag.getGraph().getNodesWithNoIncomingEdges();
    assertThat(topLevelNodes.size(), is(2));
    for (APKModule apkModule : topLevelNodes) {
        assertThat(apkModule.getName(), oneOf("test.android.library", "test.java.library"));
        ImmutableSet<APKModule> dependencies = dag.getGraph().getOutgoingNodesFor(apkModule);
        for (APKModule depModule : dependencies) {
            assertThat(depModule.getName(), oneOf("test.java.library", "test.shared.library", APKModuleGraph.ROOT_APKMODULE_NAME));
            switch(depModule.getName()) {
                case APKModuleGraph.ROOT_APKMODULE_NAME:
                    assertThat(dag.getGraph().getOutgoingNodesFor(depModule).size(), is(0));
                    break;
                case "test.java.library":
                    verifyDependencies(dag, depModule, ImmutableSet.of("test.shared.library", APKModuleGraph.ROOT_APKMODULE_NAME));
                    break;
                case "test.shared.library":
                    verifyDependencies(dag, depModule, ImmutableSet.of(APKModuleGraph.ROOT_APKMODULE_NAME));
                    break;
            }
        }
    }
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TargetNode (com.facebook.buck.rules.TargetNode)88 BuildTarget (com.facebook.buck.model.BuildTarget)73 TargetGraph (com.facebook.buck.rules.TargetGraph)43 Test (org.junit.Test)40 Path (java.nio.file.Path)36 ImmutableSet (com.google.common.collect.ImmutableSet)33 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)23 PathSourcePath (com.facebook.buck.rules.PathSourcePath)22 SourcePath (com.facebook.buck.rules.SourcePath)22 ImmutableMap (com.google.common.collect.ImmutableMap)22 Optional (java.util.Optional)20 Map (java.util.Map)18 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 ImmutableList (com.google.common.collect.ImmutableList)17 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)13 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)13 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)13 Cell (com.facebook.buck.rules.Cell)13 IOException (java.io.IOException)13 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)12