use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class Parser method resolveTargetSpecs.
private ImmutableList<ImmutableSet<BuildTarget>> resolveTargetSpecs(PerBuildState state, BuckEventBus eventBus, Cell rootCell, Iterable<? extends TargetNodeSpec> specs, final ParserConfig.ApplyDefaultFlavorsMode applyDefaultFlavorsMode) throws BuildFileParseException, BuildTargetException, InterruptedException, IOException {
ParserConfig parserConfig = rootCell.getBuckConfig().getView(ParserConfig.class);
ParserConfig.BuildFileSearchMethod buildFileSearchMethod;
if (parserConfig.getBuildFileSearchMethod().isPresent()) {
buildFileSearchMethod = parserConfig.getBuildFileSearchMethod().get();
} else if (parserConfig.getAllowSymlinks() == ParserConfig.AllowSymlinks.FORBID) {
// If unspecified, only use Watchman in repositories which enforce a "no symlinks" rule
// (Watchman doesn't follow symlinks).
buildFileSearchMethod = ParserConfig.BuildFileSearchMethod.WATCHMAN;
} else {
buildFileSearchMethod = ParserConfig.BuildFileSearchMethod.FILESYSTEM_CRAWL;
}
// Convert the input spec iterable into a list so we have a fixed ordering, which we'll rely on
// when returning results.
final ImmutableList<TargetNodeSpec> orderedSpecs = ImmutableList.copyOf(specs);
// Resolve all the build files from all the target specs. We store these into a multi-map which
// maps the path to the build file to the index of it's spec file in the ordered spec list.
Multimap<Path, Integer> perBuildFileSpecs = LinkedHashMultimap.create();
for (int index = 0; index < orderedSpecs.size(); index++) {
TargetNodeSpec spec = orderedSpecs.get(index);
Cell cell = rootCell.getCell(spec.getBuildFileSpec().getCellPath());
ImmutableSet<Path> buildFiles;
try (SimplePerfEvent.Scope perfEventScope = SimplePerfEvent.scope(eventBus, PerfEventId.of("FindBuildFiles"), "targetNodeSpec", spec)) {
// Iterate over the build files the given target node spec returns.
buildFiles = spec.getBuildFileSpec().findBuildFiles(cell, buildFileSearchMethod);
}
for (Path buildFile : buildFiles) {
perBuildFileSpecs.put(buildFile, index);
}
}
// Kick off parse futures for each build file.
ArrayList<ListenableFuture<ImmutableList<Map.Entry<Integer, ImmutableSet<BuildTarget>>>>> targetFutures = new ArrayList<>();
for (Path buildFile : perBuildFileSpecs.keySet()) {
final Collection<Integer> buildFileSpecs = perBuildFileSpecs.get(buildFile);
TargetNodeSpec firstSpec = orderedSpecs.get(Iterables.get(buildFileSpecs, 0));
Cell cell = rootCell.getCell(firstSpec.getBuildFileSpec().getCellPath());
// Format a proper error message for non-existent build files.
if (!cell.getFilesystem().isFile(buildFile)) {
throw new MissingBuildFileException(firstSpec, cell.getFilesystem().getRootPath().relativize(buildFile));
}
// Build up a list of all target nodes from the build file.
targetFutures.add(Futures.transform(state.getAllTargetNodesJob(cell, buildFile), new Function<ImmutableSet<TargetNode<?, ?>>, ImmutableList<Map.Entry<Integer, ImmutableSet<BuildTarget>>>>() {
@Override
public ImmutableList<Map.Entry<Integer, ImmutableSet<BuildTarget>>> apply(ImmutableSet<TargetNode<?, ?>> nodes) {
ImmutableList.Builder<Map.Entry<Integer, ImmutableSet<BuildTarget>>> targets = ImmutableList.builder();
for (int index : buildFileSpecs) {
// Call back into the target node spec to filter the relevant build targets.
// We return a pair of spec index and build target set, so that we can build a
// final result list that maintains the input spec ordering.
targets.add(new AbstractMap.SimpleEntry<>(index, applySpecFilter(orderedSpecs.get(index), nodes, applyDefaultFlavorsMode)));
}
return targets.build();
}
}));
}
// Now walk through and resolve all the futures, and place their results in a multimap that
// is indexed by the integer representing the input target spec order.
LinkedHashMultimap<Integer, BuildTarget> targetsMap = LinkedHashMultimap.create();
try {
for (ListenableFuture<ImmutableList<Map.Entry<Integer, ImmutableSet<BuildTarget>>>> targetFuture : targetFutures) {
ImmutableList<Map.Entry<Integer, ImmutableSet<BuildTarget>>> results = targetFuture.get();
for (Map.Entry<Integer, ImmutableSet<BuildTarget>> ent : results) {
targetsMap.putAll(ent.getKey(), ent.getValue());
}
}
} catch (ExecutionException e) {
Throwables.throwIfInstanceOf(e.getCause(), BuildFileParseException.class);
Throwables.throwIfInstanceOf(e.getCause(), BuildTargetException.class);
Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
Throwables.throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
// Finally, pull out the final build target results in input target spec order, and place them
// into a list of sets that exactly matches the ihput order.
ImmutableList.Builder<ImmutableSet<BuildTarget>> targets = ImmutableList.builder();
for (int index = 0; index < orderedSpecs.size(); index++) {
targets.add(ImmutableSet.copyOf(targetsMap.get(index)));
}
return targets.build();
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class Parser method buildTargetGraph.
@SuppressWarnings("PMD.PrematureDeclaration")
protected TargetGraph buildTargetGraph(final PerBuildState state, final BuckEventBus eventBus, final Iterable<BuildTarget> toExplore, final boolean ignoreBuckAutodepsFiles) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException {
if (Iterables.isEmpty(toExplore)) {
return TargetGraph.EMPTY;
}
final Map<BuildTarget, TargetGroup> groups = Maps.newHashMap();
for (TargetGroup group : state.getAllGroups()) {
groups.put(group.getBuildTarget(), group);
}
final MutableDirectedGraph<TargetNode<?, ?>> graph = new MutableDirectedGraph<>();
final Map<BuildTarget, TargetNode<?, ?>> index = new HashMap<>();
ParseEvent.Started parseStart = ParseEvent.started(toExplore);
eventBus.post(parseStart);
GraphTraversable<BuildTarget> traversable = target -> {
TargetNode<?, ?> node;
try {
node = state.getTargetNode(target);
} catch (BuildFileParseException | BuildTargetException e) {
throw new RuntimeException(e);
}
if (ignoreBuckAutodepsFiles) {
return Collections.emptyIterator();
}
for (BuildTarget dep : node.getDeps()) {
try {
state.getTargetNode(dep);
} catch (BuildFileParseException | BuildTargetException | HumanReadableException e) {
throw new HumanReadableException(e, "Couldn't get dependency '%s' of target '%s':\n%s", dep, target, e.getMessage());
}
}
return node.getDeps().iterator();
};
GraphTraversable<BuildTarget> groupExpander = target -> {
TargetGroup group = Preconditions.checkNotNull(groups.get(target), "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
return Iterators.filter(group.iterator(), groups::containsKey);
};
AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetGroupExpansion = new AcyclicDepthFirstPostOrderTraversal<>(groupExpander);
AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetNodeTraversal = new AcyclicDepthFirstPostOrderTraversal<>(traversable);
TargetGraph targetGraph = null;
try {
for (BuildTarget target : targetNodeTraversal.traverse(toExplore)) {
TargetNode<?, ?> targetNode = state.getTargetNode(target);
Preconditions.checkNotNull(targetNode, "No target node found for %s", target);
graph.addNode(targetNode);
MoreMaps.putCheckEquals(index, target, targetNode);
if (target.isFlavored()) {
BuildTarget unflavoredTarget = BuildTarget.of(target.getUnflavoredBuildTarget());
MoreMaps.putCheckEquals(index, unflavoredTarget, state.getTargetNode(unflavoredTarget));
}
for (BuildTarget dep : targetNode.getDeps()) {
graph.addEdge(targetNode, state.getTargetNode(dep));
}
}
for (BuildTarget groupTarget : targetGroupExpansion.traverse(groups.keySet())) {
ImmutableMap<BuildTarget, Iterable<BuildTarget>> replacements = Maps.toMap(groupExpander.findChildren(groupTarget), target -> {
TargetGroup group = groups.get(target);
return Preconditions.checkNotNull(group, "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
});
if (!replacements.isEmpty()) {
// TODO(tophyr): Stop duplicating target lists
groups.put(groupTarget, Preconditions.checkNotNull(groups.get(groupTarget)).withReplacedTargets(replacements));
}
}
targetGraph = new TargetGraph(graph, ImmutableMap.copyOf(index), ImmutableSet.copyOf(groups.values()));
state.ensureConcreteFilesExist(eventBus);
return targetGraph;
} catch (AcyclicDepthFirstPostOrderTraversal.CycleException e) {
throw new HumanReadableException(e.getMessage());
} catch (RuntimeException e) {
throw propagateRuntimeCause(e);
} finally {
eventBus.post(ParseEvent.finished(parseStart, Optional.ofNullable(targetGraph)));
}
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class DistBuildStateTest method canReconstructGraphAndTopLevelBuildTargets.
@Test
public void canReconstructGraphAndTopLevelBuildTargets() throws Exception {
ProjectWorkspace projectWorkspace = TestDataHelper.createProjectWorkspaceForScenario(this, "simple_java_target", temporaryFolder);
projectWorkspace.setUp();
Cell cell = projectWorkspace.asCell();
ProjectFilesystem projectFilesystem = cell.getFilesystem();
projectFilesystem.mkdirs(projectFilesystem.getBuckPaths().getBuckOut());
BuckConfig buckConfig = cell.getBuckConfig();
TypeCoercerFactory typeCoercerFactory = new DefaultTypeCoercerFactory(ObjectMappers.newDefaultInstance());
ConstructorArgMarshaller constructorArgMarshaller = new ConstructorArgMarshaller(typeCoercerFactory);
Parser parser = new Parser(new BroadcastEventListener(), buckConfig.getView(ParserConfig.class), typeCoercerFactory, constructorArgMarshaller);
TargetGraph targetGraph = parser.buildTargetGraph(BuckEventBusFactory.newInstance(), cell, /* enableProfiling */
false, MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()), ImmutableSet.of(BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:lib1"), BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:lib2"), BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:lib3")));
DistBuildTargetGraphCodec targetGraphCodec = createDefaultCodec(cell, Optional.of(parser));
BuildJobState dump = DistBuildState.dump(new DistBuildCellIndexer(cell), emptyActionGraph(), targetGraphCodec, targetGraph, ImmutableSet.of(BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:lib1"), BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:lib2")));
Cell rootCellWhenLoading = new TestCellBuilder().setFilesystem(createJavaOnlyFilesystem("/loading")).build();
DistBuildState distributedBuildState = DistBuildState.load(Optional.empty(), dump, rootCellWhenLoading, knownBuildRuleTypesFactory);
ProjectFilesystem reconstructedCellFilesystem = distributedBuildState.getCells().get(0).getFilesystem();
TargetGraph reconstructedGraph = distributedBuildState.createTargetGraph(targetGraphCodec).getTargetGraph();
assertEquals(reconstructedGraph.getNodes().stream().map(targetNode -> targetNode.castArg(JavaLibraryDescription.Arg.class).get()).sorted().map(targetNode -> targetNode.getConstructorArg().srcs).collect(Collectors.toList()), Lists.newArrayList("A.java", "B.java", "C.java").stream().map(f -> reconstructedCellFilesystem.getPath(f)).map(p -> new PathSourcePath(reconstructedCellFilesystem, p)).map(ImmutableSortedSet::of).collect(Collectors.toList()));
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class DistBuildStateTest method createDefaultCodec.
public static DistBuildTargetGraphCodec createDefaultCodec(final Cell cell, final Optional<Parser> parser) {
// NOPMD confused by lambda
ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
BuckEventBus eventBus = BuckEventBusFactory.newInstance();
Function<? super TargetNode<?, ?>, ? extends Map<String, Object>> nodeToRawNode;
if (parser.isPresent()) {
nodeToRawNode = (Function<TargetNode<?, ?>, Map<String, Object>>) input -> {
try {
return parser.get().getRawTargetNode(eventBus, cell.getCell(input.getBuildTarget()), false, MoreExecutors.listeningDecorator(MoreExecutors.newDirectExecutorService()), input);
} catch (BuildFileParseException e) {
throw new RuntimeException(e);
}
};
} else {
nodeToRawNode = Functions.constant(ImmutableMap.<String, Object>of());
}
DistBuildTypeCoercerFactory typeCoercerFactory = new DistBuildTypeCoercerFactory(objectMapper);
ParserTargetNodeFactory<TargetNode<?, ?>> parserTargetNodeFactory = DefaultParserTargetNodeFactory.createForDistributedBuild(new ConstructorArgMarshaller(typeCoercerFactory), new TargetNodeFactory(typeCoercerFactory));
return new DistBuildTargetGraphCodec(objectMapper, parserTargetNodeFactory, nodeToRawNode, ImmutableSet.of());
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class DistBuildSlaveExecutor method createGraphCodec.
private DistBuildTargetGraphCodec createGraphCodec() {
DistBuildTypeCoercerFactory typeCoercerFactory = new DistBuildTypeCoercerFactory(args.getObjectMapper());
ParserTargetNodeFactory<TargetNode<?, ?>> parserTargetNodeFactory = DefaultParserTargetNodeFactory.createForDistributedBuild(new ConstructorArgMarshaller(typeCoercerFactory), new TargetNodeFactory(typeCoercerFactory));
DistBuildTargetGraphCodec targetGraphCodec = new DistBuildTargetGraphCodec(args.getObjectMapper(), parserTargetNodeFactory, new Function<TargetNode<?, ?>, Map<String, Object>>() {
@Nullable
@Override
public Map<String, Object> apply(TargetNode<?, ?> input) {
try {
return args.getParser().getRawTargetNode(args.getBuckEventBus(), args.getRootCell().getCell(input.getBuildTarget()), /* enableProfiling */
false, args.getExecutorService(), input);
} catch (BuildFileParseException e) {
throw new RuntimeException(e);
}
}
}, new HashSet<>(args.getState().getRemoteState().getTopLevelTargets()));
return targetGraphCodec;
}
Aggregations