Search in sources :

Example 1 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class AppleTestDescription method getXctool.

private Optional<SourcePath> getXctool(BuildRuleParams params, BuildRuleResolver resolver) {
    // can use that directly.
    if (appleConfig.getXctoolZipTarget().isPresent()) {
        final BuildRule xctoolZipBuildRule = resolver.getRule(appleConfig.getXctoolZipTarget().get());
        BuildTarget unzipXctoolTarget = BuildTarget.builder(xctoolZipBuildRule.getBuildTarget()).addFlavors(UNZIP_XCTOOL_FLAVOR).build();
        final Path outputDirectory = BuildTargets.getGenPath(params.getProjectFilesystem(), unzipXctoolTarget, "%s/unzipped");
        if (!resolver.getRuleOptional(unzipXctoolTarget).isPresent()) {
            BuildRuleParams unzipXctoolParams = params.withBuildTarget(unzipXctoolTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(xctoolZipBuildRule)), Suppliers.ofInstance(ImmutableSortedSet.of()));
            resolver.addToIndex(new AbstractBuildRule(unzipXctoolParams) {

                @Override
                public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
                    buildableContext.recordArtifact(outputDirectory);
                    return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), outputDirectory), new UnzipStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(Preconditions.checkNotNull(xctoolZipBuildRule.getSourcePathToOutput())), outputDirectory));
                }

                @Override
                public SourcePath getSourcePathToOutput() {
                    return new ExplicitBuildTargetSourcePath(getBuildTarget(), outputDirectory);
                }
            });
        }
        return Optional.of(new ExplicitBuildTargetSourcePath(unzipXctoolTarget, outputDirectory.resolve("bin/xctool")));
    } else if (appleConfig.getXctoolPath().isPresent()) {
        return Optional.of(new PathSourcePath(params.getProjectFilesystem(), appleConfig.getXctoolPath().get()));
    } else {
        return Optional.empty();
    }
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) UnzipStep(com.facebook.buck.zip.UnzipStep) ImmutableList(com.google.common.collect.ImmutableList) PathSourcePath(com.facebook.buck.rules.PathSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildContext(com.facebook.buck.rules.BuildContext) BuildTarget(com.facebook.buck.model.BuildTarget) BuildableContext(com.facebook.buck.rules.BuildableContext) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Example 2 with BuildContext

use of com.facebook.buck.rules.BuildContext 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 3 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class AndroidManifestTest method testBuildInternal.

@Test
public void testBuildInternal() throws IOException {
    AndroidManifest androidManifest = createSimpleAndroidManifestRule();
    ProjectFilesystem filesystem = androidManifest.getProjectFilesystem();
    Path skeletonPath = Paths.get("java/com/example/AndroidManifestSkeleton.xml");
    // Mock out a BuildContext whose DependencyGraph will be traversed.
    BuildContext buildContext = FakeBuildContext.NOOP_CONTEXT;
    SourcePathResolver pathResolver = buildContext.getSourcePathResolver();
    expect(pathResolver.getAbsolutePath(new PathSourcePath(filesystem, skeletonPath))).andStubReturn(filesystem.resolve(skeletonPath));
    expect(pathResolver.getAllAbsolutePaths(ImmutableSortedSet.of())).andStubReturn(ImmutableSortedSet.of());
    Path outPath = Paths.get("foo/bar");
    expect(pathResolver.getRelativePath(androidManifest.getSourcePathToOutput())).andStubReturn(outPath);
    replay(pathResolver);
    List<Step> steps = androidManifest.getBuildSteps(buildContext, new FakeBuildableContext());
    Step generateManifestStep = steps.get(2);
    assertEquals(new GenerateManifestStep(filesystem, filesystem.resolve(skeletonPath), /* libraryManifestPaths */
    ImmutableSet.of(), outPath), generateManifestStep);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Test(org.junit.Test)

Example 4 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class NdkLibraryTest method testSimpleNdkLibraryRule.

@Test
public void testSimpleNdkLibraryRule() throws Exception {
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildContext context = FakeBuildContext.NOOP_CONTEXT;
    String basePath = "java/src/com/facebook/base";
    BuildTarget target = BuildTargetFactory.newInstance(String.format("//%s:base", basePath));
    NdkLibrary ndkLibrary = new NdkLibraryBuilder(target).setFlags(ImmutableList.of("flag1", "flag2")).setIsAsset(true).build(ruleResolver, projectFilesystem);
    assertEquals("ndk_library", ndkLibrary.getType());
    assertTrue(ndkLibrary.getProperties().is(ANDROID));
    assertTrue(ndkLibrary.isAsset());
    assertEquals(projectFilesystem.getBuckPaths().getGenDir().resolve(basePath).resolve("__libbase"), ndkLibrary.getLibraryPath());
    List<Step> steps = ndkLibrary.getBuildSteps(context, new FakeBuildableContext());
    String libbase = projectFilesystem.getBuckPaths().getScratchDir().resolve(basePath).resolve("__libbase").toString();
    MoreAsserts.assertShellCommands("ndk_library() should invoke ndk-build on the given path with some -j value", ImmutableList.of(String.format("%s -j %d -C %s flag1 flag2 " + "APP_PROJECT_PATH=%s " + "APP_BUILD_SCRIPT=%s " + "NDK_OUT=%s " + "NDK_LIBS_OUT=%s " + "BUCK_PROJECT_DIR=../../../../.. " + "host-echo-build-step=%s " + "--silent", ndkBuildCommand, Runtime.getRuntime().availableProcessors(), Paths.get(basePath).toString(), /* APP_PROJECT_PATH */
    projectFilesystem.resolve(libbase) + File.separator, /* APP_BUILD_SCRIPT */
    projectFilesystem.resolve(NdkLibraryDescription.getGeneratedMakefilePath(target, projectFilesystem)), /* NDK_OUT */
    projectFilesystem.resolve(libbase) + File.separator, /* NDK_LIBS_OUT */
    projectFilesystem.resolve(Paths.get(libbase, "libs")), /* host-echo-build-step */
    Platform.detect() == Platform.WINDOWS ? "@REM" : "@#")), steps.subList(3, 4), executionContext);
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildTarget(com.facebook.buck.model.BuildTarget) Step(com.facebook.buck.step.Step) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 5 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class ArchiveTest method flagsArePropagated.

@Test
public void flagsArePropagated() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    Archive archive = Archive.from(target, params, ruleFinder, DEFAULT_ARCHIVER, ImmutableList.of("-foo"), DEFAULT_RANLIB, ImmutableList.of("-bar"), Archive.Contents.NORMAL, DEFAULT_OUTPUT, ImmutableList.of(new FakeSourcePath("simple.o")));
    BuildContext buildContext = BuildContext.builder().from(FakeBuildContext.NOOP_CONTEXT).setSourcePathResolver(pathResolver).build();
    ImmutableList<Step> steps = archive.getBuildSteps(buildContext, new FakeBuildableContext());
    Step archiveStep = FluentIterable.from(steps).filter(ArchiveStep.class).first().get();
    assertThat(archiveStep.getDescription(TestExecutionContext.newInstance()), containsString("-foo"));
    Step ranlibStep = FluentIterable.from(steps).filter(RanlibStep.class).first().get();
    assertThat(ranlibStep.getDescription(TestExecutionContext.newInstance()), containsString("-bar"));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

BuildContext (com.facebook.buck.rules.BuildContext)28 Step (com.facebook.buck.step.Step)22 FakeBuildContext (com.facebook.buck.rules.FakeBuildContext)19 FakeBuildableContext (com.facebook.buck.rules.FakeBuildableContext)18 Test (org.junit.Test)18 BuildTarget (com.facebook.buck.model.BuildTarget)17 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)15 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)13 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)13 Path (java.nio.file.Path)13 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)12 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)12 BuildRule (com.facebook.buck.rules.BuildRule)9 SourcePath (com.facebook.buck.rules.SourcePath)9 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)8 BuildableContext (com.facebook.buck.rules.BuildableContext)8 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)8 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)8 ExecutionContext (com.facebook.buck.step.ExecutionContext)8 ImmutableList (com.google.common.collect.ImmutableList)8