use of com.facebook.buck.rules.ActionGraph in project buck by facebook.
the class JavaBuildGraphProcessor method run.
/**
* Creates the appropriate target graph and other resources needed for the {@link Processor} and
* runs it. This method will take responsibility for cleaning up the executor service after it
* runs.
*/
static void run(final CommandRunnerParams params, final AbstractCommand command, final Processor processor) throws ExitCodeException, InterruptedException, IOException {
final ConcurrencyLimit concurrencyLimit = command.getConcurrencyLimit(params.getBuckConfig());
try (CommandThreadManager pool = new CommandThreadManager(command.getClass().getName(), concurrencyLimit)) {
Cell cell = params.getCell();
WeightedListeningExecutorService executorService = pool.getExecutor();
// Ideally, we should be able to construct the TargetGraph quickly assuming most of it is
// already in memory courtesy of buckd. Though we could make a performance optimization where
// we pass an option to buck.py that tells it to ignore reading the BUCK.autodeps files when
// parsing the BUCK files because we never need to consider the existing auto-generated deps
// when creating the new auto-generated deps. If we did so, we would have to make sure to keep
// the nodes for that version of the graph separate from the ones that are actually used for
// building.
TargetGraph graph;
try {
graph = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), cell, command.getEnableParserProfiling(), executorService, ImmutableList.of(TargetNodePredicateSpec.of(x -> true, BuildFileSpec.fromRecursivePath(Paths.get(""), cell.getRoot()))), /* ignoreBuckAutodepsFiles */
true).getTargetGraph();
} catch (BuildTargetException | BuildFileParseException e) {
params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
throw new ExitCodeException(1);
}
BuildRuleResolver buildRuleResolver = new BuildRuleResolver(graph, new DefaultTargetNodeToBuildRuleTransformer());
CachingBuildEngineBuckConfig cachingBuildEngineBuckConfig = params.getBuckConfig().getView(CachingBuildEngineBuckConfig.class);
LocalCachingBuildEngineDelegate cachingBuildEngineDelegate = new LocalCachingBuildEngineDelegate(params.getFileHashCache());
BuildEngine buildEngine = new CachingBuildEngine(cachingBuildEngineDelegate, executorService, executorService, new DefaultStepRunner(), CachingBuildEngine.BuildMode.SHALLOW, cachingBuildEngineBuckConfig.getBuildDepFiles(), cachingBuildEngineBuckConfig.getBuildMaxDepFileCacheEntries(), cachingBuildEngineBuckConfig.getBuildArtifactCacheSizeLimit(), params.getObjectMapper(), buildRuleResolver, cachingBuildEngineBuckConfig.getResourceAwareSchedulingInfo(), new RuleKeyFactoryManager(params.getBuckConfig().getKeySeed(), fs -> cachingBuildEngineDelegate.getFileHashCache(), buildRuleResolver, cachingBuildEngineBuckConfig.getBuildInputRuleKeyFileSizeLimit(), new DefaultRuleKeyCache<>()));
// Create a BuildEngine because we store symbol information as build artifacts.
BuckEventBus eventBus = params.getBuckEventBus();
ExecutionContext executionContext = ExecutionContext.builder().setConsole(params.getConsole()).setConcurrencyLimit(concurrencyLimit).setBuckEventBus(eventBus).setEnvironment(/* environment */
ImmutableMap.of()).setExecutors(ImmutableMap.<ExecutorPool, ListeningExecutorService>of(ExecutorPool.CPU, executorService)).setJavaPackageFinder(params.getJavaPackageFinder()).setObjectMapper(params.getObjectMapper()).setPlatform(params.getPlatform()).setCellPathResolver(params.getCell().getCellPathResolver()).build();
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(buildRuleResolver));
BuildEngineBuildContext buildContext = BuildEngineBuildContext.builder().setBuildContext(BuildContext.builder().setActionGraph(new ActionGraph(ImmutableList.of())).setSourcePathResolver(pathResolver).setJavaPackageFinder(executionContext.getJavaPackageFinder()).setEventBus(eventBus).build()).setClock(params.getClock()).setArtifactCache(params.getArtifactCacheFactory().newInstance()).setBuildId(eventBus.getBuildId()).setObjectMapper(params.getObjectMapper()).setEnvironment(executionContext.getEnvironment()).setKeepGoing(false).build();
// Traverse the TargetGraph to find all of the auto-generated dependencies.
JavaDepsFinder javaDepsFinder = JavaDepsFinder.createJavaDepsFinder(params.getBuckConfig(), params.getCell().getCellPathResolver(), params.getObjectMapper(), buildContext, executionContext, buildEngine);
processor.process(graph, javaDepsFinder, executorService);
}
}
use of com.facebook.buck.rules.ActionGraph in project buck by facebook.
the class DistBuildStateTest method emptyActionGraph.
private DistBuildFileHashes emptyActionGraph() throws IOException, InterruptedException {
ActionGraph actionGraph = new ActionGraph(ImmutableList.of());
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
ProjectFilesystem projectFilesystem = createJavaOnlyFilesystem("/opt/buck");
Cell rootCell = new TestCellBuilder().setFilesystem(projectFilesystem).setBuckConfig(FakeBuckConfig.builder().build()).build();
return new DistBuildFileHashes(actionGraph, sourcePathResolver, ruleFinder, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(projectFilesystem))), Functions.constant(0), MoreExecutors.newDirectExecutorService(), /* keySeed */
0, rootCell);
}
use of com.facebook.buck.rules.ActionGraph in project buck by facebook.
the class JavaSourceJarTest method shouldOnlyIncludePathBasedSources.
@Test
public void shouldOnlyIncludePathBasedSources() {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
SourcePath fileBased = new FakeSourcePath("some/path/File.java");
SourcePath ruleBased = new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//cheese:cake"));
JavaPackageFinder finderStub = createNiceMock(JavaPackageFinder.class);
expect(finderStub.findJavaPackageFolder((Path) anyObject())).andStubReturn(Paths.get("cheese"));
expect(finderStub.findJavaPackage((Path) anyObject())).andStubReturn("cheese");
// No need to verify. It's a stub. I don't care about the interactions.
EasyMock.replay(finderStub);
JavaSourceJar rule = new JavaSourceJar(new FakeBuildRuleParamsBuilder("//example:target").build(), ImmutableSortedSet.of(fileBased, ruleBased), Optional.empty());
BuildContext buildContext = BuildContext.builder().setActionGraph(new ActionGraph(ImmutableList.of())).setSourcePathResolver(pathResolver).setJavaPackageFinder(finderStub).setEventBus(BuckEventBusFactory.newInstance()).build();
ImmutableList<Step> steps = rule.getBuildSteps(buildContext, new FakeBuildableContext());
// There should be a CopyStep per file being copied. Count 'em.
int copyStepsCount = FluentIterable.from(steps).filter(CopyStep.class::isInstance).size();
assertEquals(1, copyStepsCount);
}
use of com.facebook.buck.rules.ActionGraph 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.ActionGraph in project buck by facebook.
the class DefaultJavaLibraryTest method createBuildContext.
// test.
private BuildContext createBuildContext(BuildRule javaLibrary, @Nullable String bootclasspath) {
AndroidPlatformTarget platformTarget = EasyMock.createMock(AndroidPlatformTarget.class);
ImmutableList<Path> bootclasspathEntries = (bootclasspath == null) ? ImmutableList.of(Paths.get("I am not used")) : ImmutableList.of(Paths.get(bootclasspath));
expect(platformTarget.getBootclasspathEntries()).andReturn(bootclasspathEntries).anyTimes();
replay(platformTarget);
// TODO(bolinfest): Create a utility that populates a BuildContext.Builder with fakes.
return BuildContext.builder().setActionGraph(new ActionGraph(ImmutableList.of(javaLibrary))).setSourcePathResolver(new SourcePathResolver(new SourcePathRuleFinder(ruleResolver))).setJavaPackageFinder(EasyMock.createMock(JavaPackageFinder.class)).setAndroidPlatformTargetSupplier(Suppliers.ofInstance(platformTarget)).setEventBus(BuckEventBusFactory.newInstance()).build();
}
Aggregations