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);
}
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;
}
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());
}
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();
}
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;
}
}
}
}
Aggregations