Search in sources :

Example 21 with BuildContext

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

the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreNoClassesToDex.

@Test
public void testGetBuildStepsWhenThereAreNoClassesToDex() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    DefaultJavaLibrary javaLibrary = JavaLibraryBuilder.createBuilder("//foo:bar").build(resolver);
    javaLibrary.getBuildOutputInitializer().setBuildOutput(new JavaLibrary.Data(ImmutableSortedMap.of()));
    BuildContext context = FakeBuildContext.NOOP_CONTEXT;
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
    DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibrary);
    List<Step> steps = preDex.getBuildSteps(context, buildableContext);
    Path dexOutput = BuildTargets.getGenPath(projectFilesystem, buildTarget, "%s.dex.jar");
    ExecutionContext executionContext = TestExecutionContext.newBuilder().build();
    MoreAsserts.assertSteps("Do not generate a .dex.jar file.", ImmutableList.of(String.format("rm -f %s", projectFilesystem.resolve(dexOutput)), String.format("mkdir -p %s", projectFilesystem.resolve(dexOutput.getParent())), "record_empty_dx"), steps, executionContext);
    Step recordArtifactAndMetadataStep = steps.get(2);
    assertThat(recordArtifactAndMetadataStep.getShortName(), startsWith("record_"));
    int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
    assertEquals(0, exitCode);
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) Step(com.facebook.buck.step.Step) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) 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 22 with BuildContext

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

the class ApkGenruleTest method testCreateAndRunApkGenrule.

@Test
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public void testCreateAndRunApkGenrule() throws IOException, NoSuchBuildTargetException {
    ProjectFilesystem projectFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    FileSystem fileSystem = projectFilesystem.getRootPath().getFileSystem();
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    createSampleAndroidBinaryRule(ruleResolver, projectFilesystem);
    // From the Python object, create a ApkGenruleBuildRuleFactory to create a ApkGenrule.Builder
    // that builds a ApkGenrule from the Python object.
    BuildTargetParser parser = EasyMock.createNiceMock(BuildTargetParser.class);
    final BuildTarget apkTarget = BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//:fb4a");
    EasyMock.expect(parser.parse(EasyMock.eq(":fb4a"), EasyMock.anyObject(BuildTargetPatternParser.class), EasyMock.anyObject())).andStubReturn(apkTarget);
    EasyMock.replay(parser);
    BuildTarget buildTarget = BuildTargetFactory.newInstance(projectFilesystem.getRootPath(), "//src/com/facebook:sign_fb4a");
    ApkGenruleDescription description = new ApkGenruleDescription();
    ApkGenruleDescription.Arg arg = description.createUnpopulatedConstructorArg();
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
    arg.apk = new FakeInstallable(apkTarget, pathResolver).getBuildTarget();
    arg.bash = Optional.of("");
    arg.cmd = Optional.of("python signer.py $APK key.properties > $OUT");
    arg.cmdExe = Optional.of("");
    arg.type = Optional.empty();
    arg.out = "signed_fb4a.apk";
    arg.srcs = ImmutableList.of(new PathSourcePath(projectFilesystem, fileSystem.getPath("src/com/facebook/signer.py")), new PathSourcePath(projectFilesystem, fileSystem.getPath("src/com/facebook/key.properties")));
    arg.tests = ImmutableSortedSet.of();
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
    ApkGenrule apkGenrule = (ApkGenrule) description.createBuildRule(TargetGraph.EMPTY, params, ruleResolver, arg);
    ruleResolver.addToIndex(apkGenrule);
    // Verify all of the observers of the Genrule.
    String expectedApkOutput = projectFilesystem.resolve(projectFilesystem.getBuckPaths().getGenDir().toString() + "/src/com/facebook/sign_fb4a/sign_fb4a.apk").toString();
    assertEquals(expectedApkOutput, apkGenrule.getAbsoluteOutputFilePath(pathResolver));
    assertEquals("The apk that this rule is modifying must have the apk in its deps.", ImmutableSet.of(apkTarget.toString()), apkGenrule.getDeps().stream().map(Object::toString).collect(MoreCollectors.toImmutableSet()));
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
    Iterable<Path> expectedInputsToCompareToOutputs = ImmutableList.of(fileSystem.getPath("src/com/facebook/signer.py"), fileSystem.getPath("src/com/facebook/key.properties"));
    MoreAsserts.assertIterablesEquals(expectedInputsToCompareToOutputs, pathResolver.filterInputsToCompareToOutput(apkGenrule.getSrcs()));
    // Verify that the shell commands that the genrule produces are correct.
    List<Step> steps = apkGenrule.getBuildSteps(buildContext, new FakeBuildableContext());
    assertEquals(6, steps.size());
    ExecutionContext executionContext = newEmptyExecutionContext();
    Step firstStep = steps.get(0);
    assertTrue(firstStep instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep mkdirCommand = (MakeCleanDirectoryStep) firstStep;
    Path mkdirDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a");
    assertEquals("First command should make sure the output directory exists and is empty.", mkdirDir, mkdirCommand.getPath());
    Step secondStep = steps.get(1);
    assertTrue(secondStep instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep secondMkdirCommand = (MakeCleanDirectoryStep) secondStep;
    Path relativePathToTmpDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a__tmp");
    assertEquals("Second command should make sure the temp directory exists.", relativePathToTmpDir, secondMkdirCommand.getPath());
    Step thirdStep = steps.get(2);
    assertTrue(thirdStep instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep thirdMkdirCommand = (MakeCleanDirectoryStep) thirdStep;
    Path relativePathToSrcDir = projectFilesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/sign_fb4a__srcs");
    assertEquals("Third command should make sure the temp directory exists.", relativePathToSrcDir, thirdMkdirCommand.getPath());
    MkdirAndSymlinkFileStep linkSource1 = (MkdirAndSymlinkFileStep) steps.get(3);
    assertEquals(fileSystem.getPath("src/com/facebook/signer.py"), linkSource1.getSource());
    assertEquals(fileSystem.getPath(relativePathToSrcDir + "/signer.py"), linkSource1.getTarget());
    MkdirAndSymlinkFileStep linkSource2 = (MkdirAndSymlinkFileStep) steps.get(4);
    assertEquals(fileSystem.getPath("src/com/facebook/key.properties"), linkSource2.getSource());
    assertEquals(fileSystem.getPath(relativePathToSrcDir + "/key.properties"), linkSource2.getTarget());
    Step sixthStep = steps.get(5);
    assertTrue(sixthStep instanceof AbstractGenruleStep);
    AbstractGenruleStep genruleCommand = (AbstractGenruleStep) sixthStep;
    assertEquals("genrule", genruleCommand.getShortName());
    ImmutableMap<String, String> environmentVariables = genruleCommand.getEnvironmentVariables(executionContext);
    assertEquals(new ImmutableMap.Builder<String, String>().put("APK", projectFilesystem.resolve(BuildTargets.getGenPath(projectFilesystem, apkTarget, "%s.apk")).toString()).put("OUT", expectedApkOutput).build(), environmentVariables);
    Path scriptFilePath = genruleCommand.getScriptFilePath(executionContext);
    String scriptFileContents = genruleCommand.getScriptFileContents(executionContext);
    assertEquals(ImmutableList.of("/bin/bash", "-e", scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
    assertEquals("python signer.py $APK key.properties > $OUT", scriptFileContents);
    EasyMock.verify(parser);
}
Also used : KeystoreBuilder(com.facebook.buck.jvm.java.KeystoreBuilder) JavaLibraryBuilder(com.facebook.buck.jvm.java.JavaLibraryBuilder) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) AbstractGenruleStep(com.facebook.buck.shell.AbstractGenruleStep) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) FileSystem(java.nio.file.FileSystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AbstractGenruleStep(com.facebook.buck.shell.AbstractGenruleStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) Test(org.junit.Test)

Example 23 with BuildContext

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

the class CxxPrecompiledHeaderTest method generatesPchStepShouldUseCorrectLang.

@Test
public void generatesPchStepShouldUseCorrectLang() throws Exception {
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    Preprocessor preprocessorSupportingPch = new GccPreprocessor(CxxPlatformUtils.DEFAULT_PLATFORM.getCpp().resolve(resolver)) {

        @Override
        public boolean supportsPrecompiledHeaders() {
            return true;
        }
    };
    Compiler compiler = CxxPlatformUtils.DEFAULT_PLATFORM.getCxx().resolve(resolver);
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    CxxPrecompiledHeader precompiledHeader = new CxxPrecompiledHeader(params, Paths.get("foo.hash1.hash2.gch"), new PreprocessorDelegate(sourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), Paths.get("./"), preprocessorSupportingPch, PreprocessorFlags.builder().build(), CxxDescriptionEnhancer.frameworkPathToSearchPath(CxxPlatformUtils.DEFAULT_PLATFORM, sourcePathResolver), Optional.empty(), /* leadingIncludePaths */
    Optional.empty()), new CompilerDelegate(sourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, compiler, CxxToolFlags.of()), CxxToolFlags.of(), new FakeSourcePath("foo.h"), CxxSource.Type.C, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER);
    resolver.addToIndex(precompiledHeader);
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(sourcePathResolver);
    ImmutableList<Step> postBuildSteps = precompiledHeader.getBuildSteps(buildContext, new FakeBuildableContext());
    CxxPreprocessAndCompileStep step = Iterables.getOnlyElement(Iterables.filter(postBuildSteps, CxxPreprocessAndCompileStep.class));
    assertThat("step that generates pch should have correct flags", step.getCommand(), hasItem(CxxSource.Type.C.getPrecompiledHeaderLanguage().get()));
}
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) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) 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)

Example 24 with BuildContext

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

the class DirectHeaderMapTest method testBuildSteps.

@Test
public void testBuildSteps() throws IOException {
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    ImmutableList<Step> expectedBuildSteps = ImmutableList.of(new MkdirStep(projectFilesystem, headerMapPath.getParent()), new RmStep(projectFilesystem, headerMapPath), new HeaderMapStep(projectFilesystem, headerMapPath, ImmutableMap.of(Paths.get("file"), projectFilesystem.resolve(projectFilesystem.getBuckPaths().getBuckOut()).relativize(file1), Paths.get("directory/then/file"), projectFilesystem.resolve(projectFilesystem.getBuckPaths().getBuckOut()).relativize(file2))));
    ImmutableList<Step> actualBuildSteps = buildRule.getBuildSteps(buildContext, buildableContext);
    assertEquals(expectedBuildSteps, actualBuildSteps.subList(1, actualBuildSteps.size()));
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) RmStep(com.facebook.buck.step.fs.RmStep) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) MkdirStep(com.facebook.buck.step.fs.MkdirStep) RmStep(com.facebook.buck.step.fs.RmStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Test(org.junit.Test)

Example 25 with BuildContext

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

the class KeystoreTest method testBuildInternal.

@Test
public void testBuildInternal() throws Exception {
    BuildContext buildContext = FakeBuildContext.NOOP_CONTEXT;
    BuildRule keystore = createKeystoreRuleForTest();
    List<Step> buildSteps = keystore.getBuildSteps(buildContext, new FakeBuildableContext());
    assertEquals(ImmutableList.<Step>of(), buildSteps);
}
Also used : FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRule(com.facebook.buck.rules.BuildRule) Step(com.facebook.buck.step.Step) 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