Search in sources :

Example 1 with ActionGraph

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);
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) ActionGraph(com.facebook.buck.rules.ActionGraph) TargetNodePredicateSpec(com.facebook.buck.parser.TargetNodePredicateSpec) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) MoreExceptions(com.facebook.buck.util.MoreExceptions) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) LocalCachingBuildEngineDelegate(com.facebook.buck.rules.LocalCachingBuildEngineDelegate) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultStepRunner(com.facebook.buck.step.DefaultStepRunner) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) ConcurrencyLimit(com.facebook.buck.util.concurrent.ConcurrencyLimit) CachingBuildEngineBuckConfig(com.facebook.buck.rules.CachingBuildEngineBuckConfig) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Cell(com.facebook.buck.rules.Cell) WeightedListeningExecutorService(com.facebook.buck.util.concurrent.WeightedListeningExecutorService) BuildFileSpec(com.facebook.buck.parser.BuildFileSpec) DefaultRuleKeyCache(com.facebook.buck.rules.keys.DefaultRuleKeyCache) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildTargetException(com.facebook.buck.model.BuildTargetException) IOException(java.io.IOException) CachingBuildEngine(com.facebook.buck.rules.CachingBuildEngine) JavaDepsFinder(com.facebook.buck.jvm.java.autodeps.JavaDepsFinder) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) Paths(java.nio.file.Paths) ExecutorPool(com.facebook.buck.step.ExecutorPool) BuildEngine(com.facebook.buck.rules.BuildEngine) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) RuleKeyFactoryManager(com.facebook.buck.rules.keys.RuleKeyFactoryManager) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BuckEventBus(com.facebook.buck.event.BuckEventBus) JavaDepsFinder(com.facebook.buck.jvm.java.autodeps.JavaDepsFinder) DefaultRuleKeyCache(com.facebook.buck.rules.keys.DefaultRuleKeyCache) TargetGraph(com.facebook.buck.rules.TargetGraph) RuleKeyFactoryManager(com.facebook.buck.rules.keys.RuleKeyFactoryManager) WeightedListeningExecutorService(com.facebook.buck.util.concurrent.WeightedListeningExecutorService) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultStepRunner(com.facebook.buck.step.DefaultStepRunner) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Cell(com.facebook.buck.rules.Cell) BuildTargetException(com.facebook.buck.model.BuildTargetException) ConcurrencyLimit(com.facebook.buck.util.concurrent.ConcurrencyLimit) LocalCachingBuildEngineDelegate(com.facebook.buck.rules.LocalCachingBuildEngineDelegate) ActionGraph(com.facebook.buck.rules.ActionGraph) CachingBuildEngine(com.facebook.buck.rules.CachingBuildEngine) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) CachingBuildEngine(com.facebook.buck.rules.CachingBuildEngine) BuildEngine(com.facebook.buck.rules.BuildEngine) ExecutionContext(com.facebook.buck.step.ExecutionContext) ExecutorPool(com.facebook.buck.step.ExecutorPool) WeightedListeningExecutorService(com.facebook.buck.util.concurrent.WeightedListeningExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CachingBuildEngineBuckConfig(com.facebook.buck.rules.CachingBuildEngineBuckConfig)

Example 2 with ActionGraph

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);
}
Also used : ActionGraph(com.facebook.buck.rules.ActionGraph) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Cell(com.facebook.buck.rules.Cell) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) TestCellBuilder(com.facebook.buck.rules.TestCellBuilder)

Example 3 with ActionGraph

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);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) ActionGraph(com.facebook.buck.rules.ActionGraph) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) BuildContext(com.facebook.buck.rules.BuildContext) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 4 with ActionGraph

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();
}
Also used : RuleKeyFieldLoader(com.facebook.buck.rules.keys.RuleKeyFieldLoader) Path(java.nio.file.Path) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) ActionGraph(com.facebook.buck.rules.ActionGraph) HashMap(java.util.HashMap) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) BuildTarget(com.facebook.buck.model.BuildTarget) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) BuildRule(com.facebook.buck.rules.BuildRule)

Example 5 with ActionGraph

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();
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) ActionGraph(com.facebook.buck.rules.ActionGraph) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AndroidPlatformTarget(com.facebook.buck.android.AndroidPlatformTarget)

Aggregations

ActionGraph (com.facebook.buck.rules.ActionGraph)7 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)6 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)5 BuckEventBus (com.facebook.buck.event.BuckEventBus)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)3 BuildContext (com.facebook.buck.rules.BuildContext)3 Cell (com.facebook.buck.rules.Cell)3 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)3 Path (java.nio.file.Path)3 AndroidPlatformTarget (com.facebook.buck.android.AndroidPlatformTarget)2 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)2 JavaPackageFinder (com.facebook.buck.jvm.core.JavaPackageFinder)2 BuildId (com.facebook.buck.model.BuildId)2 BuildTarget (com.facebook.buck.model.BuildTarget)2 BuildEngine (com.facebook.buck.rules.BuildEngine)2 BuildEngineBuildContext (com.facebook.buck.rules.BuildEngineBuildContext)2 BuildRule (com.facebook.buck.rules.BuildRule)2 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)2 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)2