use of com.facebook.buck.rules.TargetGroup in project buck by facebook.
the class DefaultParserTargetGroupFactory method createTargetNode.
@Override
public TargetGroup createTargetNode(Cell cell, Path buildFile, BuildTarget target, Map<String, Object> rawNode, Function<PerfEventId, SimplePerfEvent.Scope> perfEventScope) {
Preconditions.checkArgument(!target.isFlavored());
UnflavoredBuildTarget unflavoredBuildTarget = target.withoutCell().getUnflavoredBuildTarget();
UnflavoredBuildTarget unflavoredBuildTargetFromRawData = RawNodeParsePipeline.parseBuildTargetFromRawRule(cell.getRoot(), rawNode, buildFile);
if (!unflavoredBuildTarget.equals(unflavoredBuildTargetFromRawData)) {
throw new IllegalStateException(String.format("Inconsistent internal state, target from data: %s, expected: %s, raw data: %s", unflavoredBuildTargetFromRawData, unflavoredBuildTarget, Joiner.on(',').withKeyValueSeparator("->").join(rawNode)));
}
BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);
// Because of the way that the parser works, we know this can never return null.
Description<?> description = cell.getDescription(buildRuleType);
Cell targetCell = cell.getCell(target);
TargetGroupDescription.Arg constructorArg = (TargetGroupDescription.Arg) description.createUnpopulatedConstructorArg();
try {
ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
ImmutableSet.Builder<VisibilityPattern> visibilityPatterns = ImmutableSet.builder();
try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("MarshalledConstructorArg"))) {
marshaller.populate(targetCell.getCellPathResolver(), targetCell.getFilesystem(), target, constructorArg, declaredDeps, visibilityPatterns, rawNode);
}
try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("CreatedTargetNode"))) {
Hasher hasher = Hashing.sha1().newHasher();
hasher.putString(BuckVersion.getVersion(), UTF_8);
JsonObjectHashing.hashJsonObject(hasher, rawNode);
TargetGroup node = new TargetGroup(constructorArg.targets, constructorArg.restrictOutboundVisibility, target);
return node;
}
} catch (ParamInfoException e) {
throw new HumanReadableException("%s: %s", target, e.getMessage());
}
}
use of com.facebook.buck.rules.TargetGroup 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.TargetGroup in project buck by facebook.
the class ParsePipelineTest method fetchGroup.
@Test
public void fetchGroup() throws Exception {
try (Fixture fixture = createSynchronousExecutionFixture("groups")) {
final Cell cell = fixture.getCell();
TargetGroup group = fixture.getTargetGroupParsePipeline().getNode(cell, BuildTargetFactory.newInstance(cell.getFilesystem(), "//:group_one"));
TargetNode<?, ?> node = fixture.getTargetNodeParsePipeline().getNode(cell, BuildTargetFactory.newInstance(cell.getFilesystem(), "//:foo"));
assertThat(group.containsTarget(node.getBuildTarget()), is(true));
}
}
Aggregations