@Override public int runWithoutHelp(final CommandRunnerParams params) throws IOException, InterruptedException { // Create a TargetGraph that is composed of the transitive closure of all of the dependent // TargetNodes for the specified BuildTargets. final ImmutableSet<String> fullyQualifiedBuildTargets = ImmutableSet.copyOf(getArgumentsFormattedAsBuildTargets(params.getBuckConfig())); if (fullyQualifiedBuildTargets.isEmpty()) { params.getConsole().printBuildFailure("Please specify at least one build target."); return 1; } ImmutableSet<BuildTarget> targets = FluentIterable.from(getArgumentsFormattedAsBuildTargets(params.getBuckConfig())) .transform( new Function<String, BuildTarget>() { @Override public BuildTarget apply(String input) { return BuildTargetParser.INSTANCE.parse( input, BuildTargetPatternParser.fullyQualified()); } }) .toSet(); LOG.debug("Getting input for targets: %s", targets); TargetGraph graph; try { graph = params .getParser() .buildTargetGraphForBuildTargets( targets, new ParserConfig(params.getBuckConfig()), params.getBuckEventBus(), params.getConsole(), params.getEnvironment(), getEnableProfiling()); } catch (BuildTargetException | BuildFileParseException e) { params.getConsole().printBuildFailureWithoutStacktrace(e); return 1; } if (shouldGenerateJsonOutput()) { return printJsonInputs(params, graph); } return printInputs(params, graph); }
private void buildGraphForBuildTargets(Set<BuildTarget> targets) throws QueryException, InterruptedException { try { graph = params .getParser() .buildTargetGraphForBuildTargets( targets, parserConfig, params.getBuckEventBus(), params.getConsole(), params.getEnvironment(), enableProfiling); } catch (BuildTargetException | BuildFileParseException | IOException e) { throw new QueryException("Error in building depencency graph"); } }
static OwnersReport buildOwnersReport( CommandRunnerParams params, ParserConfig parserConfig, BuildFileTree buildFileTree, Iterable<String> arguments, boolean guessForDeletedEnabled) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException { final Path rootPath = params.getCell().getFilesystem().getRootPath(); Preconditions.checkState(rootPath.isAbsolute()); Map<Path, List<TargetNode<?>>> targetNodes = Maps.newHashMap(); OwnersReport report = OwnersReport.emptyReport(); for (Path filePath : getArgumentsAsPaths(rootPath, arguments)) { Optional<Path> basePath = buildFileTree.getBasePathOfAncestorTarget(filePath); if (!basePath.isPresent()) { report = report.updatedWith( new OwnersReport( ImmutableSetMultimap.<TargetNode<?>, Path>of(), /* inputWithNoOwners */ ImmutableSet.of(filePath), Sets.<String>newHashSet(), Sets.<String>newHashSet())); continue; } Path buckFile = basePath.get().resolve(parserConfig.getBuildFileName()); Preconditions.checkState(params.getCell().getFilesystem().exists(buckFile)); // Parse buck files and load target nodes. if (!targetNodes.containsKey(buckFile)) { try { targetNodes.put( buckFile, params .getParser() .getOrLoadTargetNodes( buckFile, parserConfig, params.getBuckEventBus(), params.getConsole(), params.getEnvironment())); } catch (BuildFileParseException | BuildTargetException e) { Path targetBasePath = MorePaths.relativize(rootPath, rootPath.resolve(basePath.get())); String targetBaseName = "//" + MorePaths.pathWithUnixSeparators(targetBasePath); params .getConsole() .getStdErr() .format("Could not parse build targets for %s", targetBaseName); throw e; } } for (TargetNode<?> targetNode : targetNodes.get(buckFile)) { report = report.updatedWith( generateOwnersReport( params, targetNode, ImmutableList.of(filePath.toString()), guessForDeletedEnabled)); } } return report; }
@Override public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException { LOG.debug("Running with arguments %s", getArguments()); try (CommandThreadManager pool = new CommandThreadManager( "Test", params.getBuckConfig().getWorkQueueExecutionOrder(), 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. TargetGraph targetGraph; ImmutableSet<BuildTarget> explicitBuildTargets; try { // If the user asked to run all of the tests, parse all of the build files looking for any // test rules. if (isRunAllTests()) { targetGraph = params .getParser() .buildTargetGraphForTargetNodeSpecs( params.getBuckEventBus(), params.getCell(), getEnableProfiling(), pool.getExecutor(), ImmutableList.of( TargetNodePredicateSpec.of( new Predicate<TargetNode<?>>() { @Override public boolean apply(TargetNode<?> input) { return input.getType().isTestRule(); } }, BuildFileSpec.fromRecursivePath(Paths.get(""))))) .getSecond(); explicitBuildTargets = 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()); Pair<ImmutableSet<BuildTarget>, TargetGraph> result = params .getParser() .buildTargetGraphForTargetNodeSpecs( params.getBuckEventBus(), params.getCell(), getEnableProfiling(), pool.getExecutor(), parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments())); targetGraph = result.getSecond(); explicitBuildTargets = result.getFirst(); LOG.debug("Got explicit build targets %s", explicitBuildTargets); ImmutableSet.Builder<BuildTarget> testTargetsBuilder = ImmutableSet.builder(); for (TargetNode<?> node : targetGraph.getAll(explicitBuildTargets)) { 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 = params .getParser() .buildTargetGraph( params.getBuckEventBus(), params.getCell(), getEnableProfiling(), pool.getExecutor(), Iterables.concat(explicitBuildTargets, testTargets)); LOG.debug("Finished building new target graph with tests."); } } } catch (BuildTargetException | BuildFileParseException e) { params .getBuckEventBus() .post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e))); return 1; } TargetGraphToActionGraph targetGraphToActionGraph = new TargetGraphToActionGraph( params.getBuckEventBus(), new BuildTargetNodeToBuildRuleTransformer()); Pair<ActionGraph, BuildRuleResolver> actionGraphAndResolver = Preconditions.checkNotNull(targetGraphToActionGraph.apply(targetGraph)); // Look up all of the test rules in the action graph. Iterable<TestRule> testRules = Iterables.filter(actionGraphAndResolver.getFirst().getNodes(), TestRule.class); // Unless the user requests that we build filtered tests, filter them out here, before // the build. if (!isBuildFiltered(params.getBuckConfig())) { testRules = filterTestRules(params.getBuckConfig(), explicitBuildTargets, testRules); } if (isDryRun()) { printMatchingTestRules(params.getConsole(), testRules); } CachingBuildEngine cachingBuildEngine = new CachingBuildEngine( pool.getExecutor(), params.getFileHashCache(), getBuildEngineMode().or(params.getBuckConfig().getBuildEngineMode()), params.getBuckConfig().getDependencySchedulingOrder(), params.getBuckConfig().getBuildDepFiles(), params.getBuckConfig().getBuildMaxDepFileCacheEntries(), actionGraphAndResolver.getSecond()); try (Build build = createBuild( params.getBuckConfig(), actionGraphAndResolver.getFirst(), actionGraphAndResolver.getSecond(), params.getAndroidPlatformTargetSupplier(), cachingBuildEngine, params.getArtifactCache(), params.getConsole(), params.getBuckEventBus(), getTargetDeviceOptional(), params.getPlatform(), params.getEnvironment(), params.getObjectMapper(), params.getClock(), Optional.of(getAdbOptions(params.getBuckConfig())), Optional.of(getTargetDeviceOptions()))) { // Build all of the test rules. int exitCode = build.executeAndPrintFailuresToEventBus( testRules, isKeepGoing(), params.getBuckEventBus(), params.getConsole(), getPathToBuildReport(params.getBuckConfig())); params.getBuckEventBus().post(BuildEvent.finished(started, exitCode)); if (exitCode != 0) { return exitCode; } // If the user requests that we build tests that we filter out, then we perform // the filtering here, after we've done the build but before we run the tests. if (isBuildFiltered(params.getBuckConfig())) { testRules = filterTestRules(params.getBuckConfig(), explicitBuildTargets, testRules); } // Once all of the rules are built, then run the tests. Optional<ImmutableList<String>> externalTestRunner = params.getBuckConfig().getExternalTestRunner(); if (externalTestRunner.isPresent()) { return runTestsExternal(params, build, externalTestRunner.get(), testRules); } return runTestsInternal(params, cachingBuildEngine, build, testRules); } } }
@Override public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException { if (getArguments().isEmpty()) { params.getConsole().printBuildFailure("Must specify at least one build target to fetch."); return 1; } // 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); } FetchTargetNodeToBuildRuleTransformer ruleGenerator = createFetchTransformer(params); TargetGraphToActionGraph transformer = new TargetGraphToActionGraph( params.getBuckEventBus(), ruleGenerator, params.getFileHashCache()); ActionGraph actionGraph; ImmutableSet<BuildTarget> buildTargets; try { Pair<ImmutableSet<BuildTarget>, TargetGraph> result = params .getParser() .buildTargetGraphForTargetNodeSpecs( parseArgumentsAsTargetNodeSpecs( params.getBuckConfig(), params.getRepository().getFilesystem().getIgnorePaths(), getArguments()), new ParserConfig(params.getBuckConfig()), params.getBuckEventBus(), params.getConsole(), params.getEnvironment(), getEnableProfiling()); actionGraph = transformer.apply(result.getSecond()); buildTargets = ruleGenerator.getDownloadableTargets(); } catch (BuildTargetException | BuildFileParseException e) { params.getConsole().printBuildFailureWithoutStacktrace(e); return 1; } int exitCode; SourcePathResolver pathResolver = new SourcePathResolver(transformer.getRuleResolver()); try (CommandThreadManager pool = new CommandThreadManager("Fetch", getConcurrencyLimit(params.getBuckConfig())); Build build = createBuild( params.getBuckConfig(), actionGraph, params.getAndroidPlatformTargetSupplier(), new CachingBuildEngine( pool.getExecutor(), params.getFileHashCache(), getBuildEngineMode().or(params.getBuckConfig().getBuildEngineMode()), params.getBuckConfig().getBuildDepFiles(), new InputBasedRuleKeyBuilderFactory(params.getFileHashCache(), pathResolver), new AbiRuleKeyBuilderFactory(params.getFileHashCache(), pathResolver), new DependencyFileRuleKeyBuilderFactory( params.getFileHashCache(), pathResolver)), getArtifactCache(params), params.getConsole(), params.getBuckEventBus(), Optional.<TargetDevice>absent(), params.getPlatform(), params.getEnvironment(), params.getObjectMapper(), params.getClock(), Optional.<AdbOptions>absent(), Optional.<TargetDeviceOptions>absent())) { exitCode = build.executeAndPrintFailuresToEventBus( buildTargets, isKeepGoing(), params.getBuckEventBus(), params.getConsole().getAnsi(), getPathToBuildReport(params.getBuckConfig())); } params.getBuckEventBus().post(BuildEvent.finished(started, exitCode)); return exitCode; }