use of com.facebook.buck.rules.ActionGraphAndResolver in project buck by facebook.
the class TargetsCommand method computeShowRules.
/**
* Assumes at least one target is specified. Computes each of the
* specified targets, followed by the rule key, output path, and/or
* target hash, depending on what flags are passed in.
* @return An immutable map consisting of result of show options
* for to each target rule
*/
private ImmutableMap<BuildTarget, ShowOptions> computeShowRules(CommandRunnerParams params, ListeningExecutorService executor, TargetGraphAndTargetNodes targetGraphAndTargetNodes) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException, CycleException {
Map<BuildTarget, ShowOptions.Builder> showOptionBuilderMap = new HashMap<>();
if (isShowTargetHash()) {
computeShowTargetHash(params, executor, targetGraphAndTargetNodes, showOptionBuilderMap);
}
// We only need the action graph if we're showing the output or the keys, and the
// RuleKeyFactory if we're showing the keys.
Optional<ActionGraph> actionGraph = Optional.empty();
Optional<BuildRuleResolver> buildRuleResolver = Optional.empty();
Optional<DefaultRuleKeyFactory> ruleKeyFactory = Optional.empty();
if (isShowRuleKey() || isShowOutput() || isShowFullOutput()) {
ActionGraphAndResolver result = Preconditions.checkNotNull(ActionGraphCache.getFreshActionGraph(params.getBuckEventBus(), targetGraphAndTargetNodes.getTargetGraph()));
actionGraph = Optional.of(result.getActionGraph());
buildRuleResolver = Optional.of(result.getResolver());
if (isShowRuleKey()) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(result.getResolver());
ruleKeyFactory = Optional.of(new DefaultRuleKeyFactory(new RuleKeyFieldLoader(params.getBuckConfig().getKeySeed()), params.getFileHashCache(), new SourcePathResolver(ruleFinder), ruleFinder));
}
}
for (TargetNode<?, ?> targetNode : targetGraphAndTargetNodes.getTargetNodes()) {
ShowOptions.Builder showOptionsBuilder = getShowOptionBuilder(showOptionBuilderMap, targetNode.getBuildTarget());
Preconditions.checkNotNull(showOptionsBuilder);
if (actionGraph.isPresent()) {
BuildRule rule = buildRuleResolver.get().requireRule(targetNode.getBuildTarget());
if (isShowRuleKey()) {
showOptionsBuilder.setRuleKey(ruleKeyFactory.get().build(rule).toString());
}
if (isShowOutput() || isShowFullOutput()) {
Optional<Path> outputPath = getUserFacingOutputPath(new SourcePathResolver(new SourcePathRuleFinder(buildRuleResolver.get())), rule, isShowFullOutput(), params.getBuckConfig().getBuckOutCompatLink());
if (outputPath.isPresent()) {
showOptionsBuilder.setOutputPath(outputPath.get().toString());
}
}
}
}
ImmutableMap.Builder<BuildTarget, ShowOptions> builder = new ImmutableMap.Builder<>();
for (Entry<BuildTarget, ShowOptions.Builder> entry : showOptionBuilderMap.entrySet()) {
builder.put(entry.getKey(), entry.getValue().build());
}
return builder.build();
}
use of com.facebook.buck.rules.ActionGraphAndResolver in project buck by facebook.
the class BuildCommand method createActionGraphAndResolver.
private ActionGraphAndResolver createActionGraphAndResolver(CommandRunnerParams params, TargetGraphAndBuildTargets targetGraphAndBuildTargets) throws ActionGraphCreationException {
buildTargets = targetGraphAndBuildTargets.getBuildTargets();
buildTargetsHaveBeenCalculated = true;
ActionGraphAndResolver actionGraphAndResolver = Preconditions.checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(), params.getBuckConfig().isActionGraphCheckingEnabled(), params.getBuckConfig().isSkipActionGraphCache(), targetGraphAndBuildTargets.getTargetGraph(), params.getBuckConfig().getKeySeed()));
// If the user specified an explicit build target, use that.
if (justBuildTarget != null) {
BuildTarget explicitTarget = BuildTargetParser.INSTANCE.parse(justBuildTarget, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver());
Iterable<BuildRule> actionGraphRules = Preconditions.checkNotNull(actionGraphAndResolver.getActionGraph().getNodes());
ImmutableSet<BuildTarget> actionGraphTargets = ImmutableSet.copyOf(Iterables.transform(actionGraphRules, BuildRule::getBuildTarget));
if (!actionGraphTargets.contains(explicitTarget)) {
throw new ActionGraphCreationException("Targets specified via `--just-build` must be a subset of action graph.");
}
buildTargets = ImmutableSet.of(explicitTarget);
}
return actionGraphAndResolver;
}
use of com.facebook.buck.rules.ActionGraphAndResolver in project buck by facebook.
the class TestCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
LOG.debug("Running with arguments %s", getArguments());
try (CommandThreadManager pool = new CommandThreadManager("Test", getConcurrencyLimit(params.getBuckConfig()))) {
// Post the build started event, setting it to the Parser recorded start time if appropriate.
BuildEvent.Started started = BuildEvent.started(getArguments());
if (params.getParser().getParseStartTime().isPresent()) {
params.getBuckEventBus().post(started, params.getParser().getParseStartTime().get());
} else {
params.getBuckEventBus().post(started);
}
// The first step is to parse all of the build files. This will populate the parser and find
// all of the test rules.
TargetGraphAndBuildTargets targetGraphAndBuildTargets;
ParserConfig parserConfig = params.getBuckConfig().getView(ParserConfig.class);
try {
// If the user asked to run all of the tests, parse all of the build files looking for any
// test rules.
boolean ignoreBuckAutodepsFiles = false;
if (isRunAllTests()) {
targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), ImmutableList.of(TargetNodePredicateSpec.of(input -> Description.getBuildRuleType(input.getDescription()).isTestRule(), BuildFileSpec.fromRecursivePath(Paths.get(""), params.getCell().getRoot()))), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
targetGraphAndBuildTargets = targetGraphAndBuildTargets.withBuildTargets(ImmutableSet.of());
// Otherwise, the user specified specific test targets to build and run, so build a graph
// around these.
} else {
LOG.debug("Parsing graph for arguments %s", getArguments());
targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments()), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
LOG.debug("Got explicit build targets %s", targetGraphAndBuildTargets.getBuildTargets());
ImmutableSet.Builder<BuildTarget> testTargetsBuilder = ImmutableSet.builder();
for (TargetNode<?, ?> node : targetGraphAndBuildTargets.getTargetGraph().getAll(targetGraphAndBuildTargets.getBuildTargets())) {
ImmutableSortedSet<BuildTarget> nodeTests = TargetNodes.getTestTargetsForNode(node);
if (!nodeTests.isEmpty()) {
LOG.debug("Got tests for target %s: %s", node.getBuildTarget(), nodeTests);
testTargetsBuilder.addAll(nodeTests);
}
}
ImmutableSet<BuildTarget> testTargets = testTargetsBuilder.build();
if (!testTargets.isEmpty()) {
LOG.debug("Got related test targets %s, building new target graph...", testTargets);
TargetGraph targetGraph = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), Iterables.concat(targetGraphAndBuildTargets.getBuildTargets(), testTargets));
LOG.debug("Finished building new target graph with tests.");
targetGraphAndBuildTargets = targetGraphAndBuildTargets.withTargetGraph(targetGraph);
}
}
if (params.getBuckConfig().getBuildVersions()) {
targetGraphAndBuildTargets = toVersionedTargetGraph(params, targetGraphAndBuildTargets);
}
} catch (BuildTargetException | BuildFileParseException | VersionException e) {
params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
return 1;
}
ActionGraphAndResolver actionGraphAndResolver = Preconditions.checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(), params.getBuckConfig().isActionGraphCheckingEnabled(), params.getBuckConfig().isSkipActionGraphCache(), targetGraphAndBuildTargets.getTargetGraph(), params.getBuckConfig().getKeySeed()));
// Look up all of the test rules in the action graph.
Iterable<TestRule> testRules = Iterables.filter(actionGraphAndResolver.getActionGraph().getNodes(), TestRule.class);
// the build.
if (!isBuildFiltered(params.getBuckConfig())) {
testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
}
CachingBuildEngineBuckConfig cachingBuildEngineBuckConfig = params.getBuckConfig().getView(CachingBuildEngineBuckConfig.class);
try (CommandThreadManager artifactFetchService = getArtifactFetchService(params.getBuckConfig(), pool.getExecutor());
RuleKeyCacheScope<RuleKey> ruleKeyCacheScope = getDefaultRuleKeyCacheScope(params, new RuleKeyCacheRecycler.SettingsAffectingCache(params.getBuckConfig().getKeySeed(), actionGraphAndResolver.getActionGraph()))) {
LocalCachingBuildEngineDelegate localCachingBuildEngineDelegate = new LocalCachingBuildEngineDelegate(params.getFileHashCache());
CachingBuildEngine cachingBuildEngine = new CachingBuildEngine(new LocalCachingBuildEngineDelegate(params.getFileHashCache()), pool.getExecutor(), artifactFetchService == null ? pool.getExecutor() : artifactFetchService.getExecutor(), new DefaultStepRunner(), getBuildEngineMode().orElse(cachingBuildEngineBuckConfig.getBuildEngineMode()), cachingBuildEngineBuckConfig.getBuildDepFiles(), cachingBuildEngineBuckConfig.getBuildMaxDepFileCacheEntries(), cachingBuildEngineBuckConfig.getBuildArtifactCacheSizeLimit(), params.getObjectMapper(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getResourceAwareSchedulingInfo(), new RuleKeyFactoryManager(params.getBuckConfig().getKeySeed(), fs -> localCachingBuildEngineDelegate.getFileHashCache(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getBuildInputRuleKeyFileSizeLimit(), ruleKeyCacheScope.getCache()));
try (Build build = createBuild(params.getBuckConfig(), actionGraphAndResolver.getActionGraph(), actionGraphAndResolver.getResolver(), params.getCell(), params.getAndroidPlatformTargetSupplier(), cachingBuildEngine, params.getArtifactCacheFactory().newInstance(), params.getConsole(), params.getBuckEventBus(), getTargetDeviceOptional(), params.getPersistentWorkerPools(), params.getPlatform(), params.getEnvironment(), params.getObjectMapper(), params.getClock(), Optional.of(getAdbOptions(params.getBuckConfig())), Optional.of(getTargetDeviceOptions()), params.getExecutors())) {
// Build all of the test rules.
int exitCode = build.executeAndPrintFailuresToEventBus(RichStream.from(testRules).map(TestRule::getBuildTarget).collect(MoreCollectors.toImmutableList()), isKeepGoing(), params.getBuckEventBus(), params.getConsole(), getPathToBuildReport(params.getBuckConfig()));
params.getBuckEventBus().post(BuildEvent.finished(started, exitCode));
if (exitCode != 0) {
return exitCode;
}
// the filtering here, after we've done the build but before we run the tests.
if (isBuildFiltered(params.getBuckConfig())) {
testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
}
// Once all of the rules are built, then run the tests.
Optional<ImmutableList<String>> externalTestRunner = params.getBuckConfig().getExternalTestRunner();
if (externalTestRunner.isPresent()) {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(actionGraphAndResolver.getResolver()));
return runTestsExternal(params, build, externalTestRunner.get(), testRules, pathResolver);
}
return runTestsInternal(params, cachingBuildEngine, build, testRules);
}
}
}
}
use of com.facebook.buck.rules.ActionGraphAndResolver in project buck by facebook.
the class DuplicateResourcesTest method getAaptStepShellCommand.
private ImmutableList<String> getAaptStepShellCommand(TargetNode<AndroidBinaryDescription.Arg, AndroidBinaryDescription> binary) {
TargetGraph targetGraph = TargetGraphFactory.newInstance(binary, mainRes, directDepRes, transitiveDepRes, transitiveDepLib, bottomDepRes, library, keystore);
ActionGraphAndResolver actionGraphAndResolver = ActionGraphCache.getFreshActionGraph(BuckEventBusFactory.newInstance(new IncrementingFakeClock(TimeUnit.SECONDS.toNanos(1))), new DefaultTargetNodeToBuildRuleTransformer(), targetGraph);
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(actionGraphAndResolver.getResolver()));
ImmutableSet<ImmutableList<Step>> ruleSteps = RichStream.from(actionGraphAndResolver.getActionGraph().getNodes()).filter(AaptPackageResources.class).filter(r -> androidBinaryTarget.getUnflavoredBuildTarget().equals(r.getBuildTarget().getUnflavoredBuildTarget())).map(b -> b.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext())).map(steps -> steps.stream().filter(step -> step instanceof AaptStep).collect(MoreCollectors.toImmutableList())).filter(steps -> !steps.isEmpty()).collect(MoreCollectors.toImmutableSet());
assertEquals(1, ruleSteps.size());
assertEquals(1, Iterables.getOnlyElement(ruleSteps).size());
AaptStep step = (AaptStep) Iterables.getOnlyElement(Iterables.getOnlyElement(ruleSteps));
AndroidDirectoryResolver androidDirectoryResolver = new FakeAndroidDirectoryResolver(Optional.of(filesystem.getPath("/android-sdk")), Optional.of(filesystem.getPath("/android-build-tools")), Optional.empty(), Optional.empty());
AndroidPlatformTarget androidPlatformTarget = AndroidPlatformTarget.createFromDefaultDirectoryStructure("", androidDirectoryResolver, "", ImmutableSet.of(), Optional.empty());
ExecutionContext context = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
return step.getShellCommand(context);
}
Aggregations