Search in sources :

Example 46 with BuildRuleParams

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

the class ApplePackageDescriptionTest method descriptionCreatesExternallyBuiltPackageRuleIfConfigExists.

@Test
public void descriptionCreatesExternallyBuiltPackageRuleIfConfigExists() throws Exception {
    ApplePackageDescription description = descriptionWithCommand("echo");
    BuildTarget binaryBuildTarget = BuildTargetFactory.newInstance("//foo:binary");
    BuildTarget bundleBuildTarget = BuildTargetFactory.newInstance("//foo:bundle");
    TargetGraph graph = TargetGraphFactory.newInstance(AppleBinaryBuilder.createBuilder(binaryBuildTarget).build(), AppleBundleBuilder.createBuilder(bundleBuildTarget).setBinary(binaryBuildTarget).setExtension(Either.ofLeft(AppleBundleExtension.APP)).build());
    ApplePackageDescription.Arg arg = description.createUnpopulatedConstructorArg();
    arg.bundle = bundleBuildTarget;
    BuildTarget packageBuildTarget = BuildTargetFactory.newInstance("//foo:package#macosx-x86_64");
    BuildRuleResolver resolver = new BuildRuleResolver(graph, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(packageBuildTarget).build();
    resolver.requireAllRules(description.findDepsForTargetFromConstructorArgs(packageBuildTarget, params.getCellRoots(), arg));
    BuildRule rule = description.createBuildRule(graph, new FakeBuildRuleParamsBuilder(packageBuildTarget).build(), resolver, arg);
    assertThat(rule, instanceOf(ExternallyBuiltApplePackage.class));
    assertThat(rule.getDeps(), hasItem(resolver.getRule(bundleBuildTarget.withFlavors(InternalFlavor.of("macosx-x86_64")))));
}
Also used : BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) TargetGraph(com.facebook.buck.rules.TargetGraph) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 47 with BuildRuleParams

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

the class CxxCollectAndLogInferDependenciesStepTest method testStepWritesOneCellTokenInFileWhenOneCellIsAbsent.

@Test
public void testStepWritesOneCellTokenInFileWhenOneCellIsAbsent() throws Exception {
    assumeThat(Platform.detect(), is(not(WINDOWS)));
    // filesystem, buildTarget and buildRuleParams for first, unnamed cell (analysis)
    ProjectFilesystem filesystem1 = createFakeFilesystem("/Users/user/default_cell");
    BuildTarget buildTarget1 = BuildTarget.builder().setUnflavoredBuildTarget(UnflavoredBuildTarget.of(filesystem1.getRootPath(), Optional.empty(), "//target/in_default_cell", "short")).addFlavors(CxxInferEnhancer.InferFlavors.INFER.get()).build();
    BuildRuleParams buildRuleParams1 = new FakeBuildRuleParamsBuilder(buildTarget1).setProjectFilesystem(filesystem1).build();
    // filesystem, buildTarget and buildRuleParams for second cell (capture)
    ProjectFilesystem filesystem2 = createFakeFilesystem("/Users/user/cell_two");
    BuildTarget buildTarget2 = BuildTarget.builder().setUnflavoredBuildTarget(UnflavoredBuildTarget.of(filesystem2.getRootPath(), Optional.of("cell2"), "//target/in_cell_two", "short2")).addFlavors(CxxInferEnhancer.InferFlavors.INFER_CAPTURE.get()).build();
    BuildRuleParams buildRuleParams2 = new FakeBuildRuleParamsBuilder(buildTarget2).setProjectFilesystem(filesystem2).build();
    BuildRuleResolver testBuildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver testSourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(testBuildRuleResolver));
    InferBuckConfig inferBuckConfig = new InferBuckConfig(FakeBuckConfig.builder().build());
    CxxInferCapture captureRule = createCaptureRule(buildRuleParams2, testSourcePathResolver, filesystem2, inferBuckConfig);
    CxxInferCaptureAndAggregatingRules<CxxInferAnalyze> captureAndAggregatingRules = new CxxInferCaptureAndAggregatingRules<>(ImmutableSet.of(captureRule), ImmutableSet.<CxxInferAnalyze>of());
    CxxInferAnalyze analyzeRule = new CxxInferAnalyze(buildRuleParams1, inferBuckConfig, captureAndAggregatingRules);
    Path outputFile = Paths.get("infer-deps.txt");
    CxxCollectAndLogInferDependenciesStep step = CxxCollectAndLogInferDependenciesStep.fromAnalyzeRule(analyzeRule, filesystem1, outputFile);
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    int exitCode = step.execute(executionContext).getExitCode();
    assertThat(exitCode, is(StepExecutionResult.SUCCESS.getExitCode()));
    String expectedOutput = InferLogLine.fromBuildTarget(buildTarget1, analyzeRule.getAbsolutePathToResultsDir()).toString() + "\n" + InferLogLine.fromBuildTarget(buildTarget2, captureRule.getAbsolutePathToOutput()).toString();
    assertEquals(expectedOutput + "\n", filesystem1.readFileIfItExists(outputFile).get());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 48 with BuildRuleParams

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

the class CxxCompilationDatabaseTest method testCompilationDatabase.

@Test
public void testCompilationDatabase() throws Exception {
    BuildTarget testBuildTarget = BuildTarget.builder(BuildTargetFactory.newInstance("//foo:baz")).addAllFlavors(ImmutableSet.of(CxxCompilationDatabase.COMPILATION_DATABASE)).build();
    final String root = "/Users/user/src";
    final Path fakeRoot = Paths.get(root);
    ProjectFilesystem filesystem = new FakeProjectFilesystem(fakeRoot);
    BuildRuleParams testBuildRuleParams = new FakeBuildRuleParamsBuilder(testBuildTarget).setProjectFilesystem(filesystem).build();
    BuildRuleResolver testBuildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver testSourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(testBuildRuleResolver));
    HeaderSymlinkTree privateSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PRIVATE, true);
    testBuildRuleResolver.addToIndex(privateSymlinkTree);
    HeaderSymlinkTree exportedSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PUBLIC, true);
    testBuildRuleResolver.addToIndex(exportedSymlinkTree);
    BuildTarget compileTarget = BuildTarget.builder(testBuildRuleParams.getBuildTarget().getUnflavoredBuildTarget()).addFlavors(InternalFlavor.of("compile-test.cpp")).build();
    PreprocessorFlags preprocessorFlags = PreprocessorFlags.builder().addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/foo/bar")), CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/test"))).build();
    ImmutableSortedSet.Builder<CxxPreprocessAndCompile> rules = ImmutableSortedSet.naturalOrder();
    BuildRuleParams compileBuildRuleParams = new FakeBuildRuleParamsBuilder(compileTarget).setProjectFilesystem(filesystem).setDeclaredDeps(ImmutableSortedSet.of(privateSymlinkTree, exportedSymlinkTree)).build();
    rules.add(testBuildRuleResolver.addToIndex(CxxPreprocessAndCompile.preprocessAndCompile(compileBuildRuleParams, new PreprocessorDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), filesystem.getRootPath(), new GccPreprocessor(new HashedFileTool(Paths.get("preprocessor"))), preprocessorFlags, new RuleKeyAppendableFunction<FrameworkPath, Path>() {

        @Override
        public void appendToRuleKey(RuleKeyObjectSink sink) {
        // Do nothing.
        }

        @Override
        public Path apply(FrameworkPath input) {
            throw new UnsupportedOperationException("should not be called");
        }
    }, Optional.empty(), /* leadingIncludePaths */
    Optional.empty()), new CompilerDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, new GccCompiler(new HashedFileTool(Paths.get("compiler"))), CxxToolFlags.of()), Paths.get("test.o"), new FakeSourcePath(filesystem, "test.cpp"), CxxSource.Type.CXX, Optional.empty(), CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty())));
    CxxCompilationDatabase compilationDatabase = CxxCompilationDatabase.createCompilationDatabase(testBuildRuleParams, rules.build());
    testBuildRuleResolver.addToIndex(compilationDatabase);
    assertThat(compilationDatabase.getRuntimeDeps().collect(MoreCollectors.toImmutableSet()), Matchers.contains(exportedSymlinkTree.getBuildTarget(), privateSymlinkTree.getBuildTarget()));
    assertEquals("getPathToOutput() should be a function of the build target.", BuildTargets.getGenPath(filesystem, testBuildTarget, "__%s.json"), testSourcePathResolver.getRelativePath(compilationDatabase.getSourcePathToOutput()));
    List<Step> buildSteps = compilationDatabase.getBuildSteps(FakeBuildContext.withSourcePathResolver(testSourcePathResolver), new FakeBuildableContext());
    assertEquals(2, buildSteps.size());
    assertTrue(buildSteps.get(0) instanceof MkdirStep);
    assertTrue(buildSteps.get(1) instanceof CxxCompilationDatabase.GenerateCompilationCommandsJson);
    CxxCompilationDatabase.GenerateCompilationCommandsJson step = (CxxCompilationDatabase.GenerateCompilationCommandsJson) buildSteps.get(1);
    Iterable<CxxCompilationDatabaseEntry> observedEntries = step.createEntries();
    Iterable<CxxCompilationDatabaseEntry> expectedEntries = ImmutableList.of(CxxCompilationDatabaseEntry.of(root, root + "/test.cpp", ImmutableList.of("compiler", "-isystem", "/foo/bar", "-isystem", "/test", "-x", "c++", "-c", "-MD", "-MF", "test.o.dep", "test.cpp", "-o", "test.o")));
    MoreAsserts.assertIterablesEquals(expectedEntries, observedEntries);
}
Also used : MkdirStep(com.facebook.buck.step.fs.MkdirStep) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) HashedFileTool(com.facebook.buck.rules.HashedFileTool) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) RuleKeyAppendableFunction(com.facebook.buck.rules.args.RuleKeyAppendableFunction) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Test(org.junit.Test)

Example 49 with BuildRuleParams

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

the class CxxCollectAndLogInferDependenciesStepTest method testStepWritesNoCellTokenInFileWhenCellIsAbsent.

@Test
public void testStepWritesNoCellTokenInFileWhenCellIsAbsent() throws IOException, InterruptedException {
    assumeThat(Platform.detect(), is(not(WINDOWS)));
    ProjectFilesystem filesystem = createFakeFilesystem("/Users/user/src");
    BuildTarget testBuildTarget = BuildTarget.builder().setUnflavoredBuildTarget(UnflavoredBuildTarget.of(filesystem.getRootPath(), Optional.empty(), "//target", "short")).addFlavors(CxxInferEnhancer.InferFlavors.INFER.get()).build();
    BuildRuleParams testBuildRuleParams = new FakeBuildRuleParamsBuilder(testBuildTarget).setProjectFilesystem(filesystem).build();
    InferBuckConfig inferBuckConfig = new InferBuckConfig(FakeBuckConfig.builder().build());
    CxxInferCaptureAndAggregatingRules<CxxInferAnalyze> captureAndAggregatingRules = new CxxInferCaptureAndAggregatingRules<>(ImmutableSet.of(), ImmutableSet.<CxxInferAnalyze>of());
    CxxInferAnalyze analyzeRule = new CxxInferAnalyze(testBuildRuleParams, inferBuckConfig, captureAndAggregatingRules);
    Path outputFile = Paths.get("infer-deps.txt");
    CxxCollectAndLogInferDependenciesStep step = CxxCollectAndLogInferDependenciesStep.fromAnalyzeRule(analyzeRule, filesystem, outputFile);
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    int exitCode = step.execute(executionContext).getExitCode();
    assertThat(exitCode, is(StepExecutionResult.SUCCESS.getExitCode()));
    String expectedOutput = InferLogLine.fromBuildTarget(testBuildTarget, analyzeRule.getAbsolutePathToResultsDir()).toString();
    assertEquals(expectedOutput + "\n", filesystem.readFileIfItExists(outputFile).get());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 50 with BuildRuleParams

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

the class ArchiveTest method testThatBuildTargetSourcePathDepsAndPathsArePropagated.

@Test
public void testThatBuildTargetSourcePathDepsAndPathsArePropagated() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    // Create a couple of genrules to generate inputs for an archive rule.
    Genrule genrule1 = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:genrule")).setOut("foo/bar.o").build(resolver);
    Genrule genrule2 = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:genrule2")).setOut("foo/test.o").build(resolver);
    // Build the archive using a normal input the outputs of the genrules above.
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    Archive archive = Archive.from(target, params, ruleFinder, DEFAULT_ARCHIVER, ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, DEFAULT_OUTPUT, ImmutableList.of(new FakeSourcePath("simple.o"), genrule1.getSourcePathToOutput(), genrule2.getSourcePathToOutput()));
    // Verify that the archive dependencies include the genrules providing the
    // SourcePath inputs.
    assertEquals(ImmutableSortedSet.<BuildRule>of(genrule1, genrule2), archive.getDeps());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Genrule(com.facebook.buck.shell.Genrule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Aggregations

BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)145 BuildTarget (com.facebook.buck.model.BuildTarget)113 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)99 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)90 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)80 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)74 Test (org.junit.Test)73 BuildRule (com.facebook.buck.rules.BuildRule)72 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)69 SourcePath (com.facebook.buck.rules.SourcePath)58 Path (java.nio.file.Path)47 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)40 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)31 ImmutableList (com.google.common.collect.ImmutableList)27 PathSourcePath (com.facebook.buck.rules.PathSourcePath)26 Optional (java.util.Optional)26 Flavor (com.facebook.buck.model.Flavor)23 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)23 TargetGraph (com.facebook.buck.rules.TargetGraph)22 HumanReadableException (com.facebook.buck.util.HumanReadableException)22