Search in sources :

Example 61 with DefaultBuildTargetSourcePath

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

the class SourcePathTypeCoercerTest method coerceCrossRepoBuildTarget.

@Test
public void coerceCrossRepoBuildTarget() throws CoerceFailedException, IOException {
    Path helloRoot = Paths.get("/opt/src/hello");
    cellRoots = new FakeCellPathResolver(projectFilesystem, ImmutableMap.of("hello", helloRoot));
    SourcePath sourcePath = sourcePathTypeCoercer.coerce(cellRoots, projectFilesystem, pathRelativeToProjectRoot, "hello//:hello");
    // Note that the important thing is that the root of the target has been set to `helloRoot` so
    // the cell name should be absent (otherwise, we'd look for a cell named `@hello` from the
    // `@hello` cell. Yeah. My head hurts a little too.
    assertEquals(new DefaultBuildTargetSourcePath(BuildTarget.of(UnflavoredBuildTarget.of(helloRoot, Optional.of("hello"), "//", "hello"), ImmutableSortedSet.of())), sourcePath);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeCellPathResolver(com.facebook.buck.rules.FakeCellPathResolver) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) Test(org.junit.Test)

Example 62 with DefaultBuildTargetSourcePath

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

the class HaskellBinaryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(params.getBuildTarget()).orElse(defaultCxxPlatform);
    Linker.LinkableDepType depType = getLinkStyle(params.getBuildTarget(), args);
    // The target to use for the link rule.
    BuildTarget binaryTarget = params.getBuildTarget().withFlavors(InternalFlavor.of("binary"));
    // Maintain backwards compatibility to ease upgrade flows.
    if (haskellConfig.shouldUsedOldBinaryOutputLocation().orElse(true)) {
        binaryTarget = binaryTarget.withAppendedFlavors(cxxPlatform.getFlavor());
    }
    ImmutableSet.Builder<BuildRule> depsBuilder = ImmutableSet.builder();
    params.getDeclaredDeps().get().stream().filter(NativeLinkable.class::isInstance).forEach(depsBuilder::add);
    args.depsQuery.ifPresent(depsQuery -> QueryUtils.resolveDepQuery(params, depsQuery, resolver, targetGraph).filter(NativeLinkable.class::isInstance).forEach(depsBuilder::add));
    ImmutableSet<BuildRule> deps = depsBuilder.build();
    ImmutableList.Builder<String> linkFlagsBuilder = ImmutableList.builder();
    ImmutableList.Builder<com.facebook.buck.rules.args.Arg> linkArgsBuilder = ImmutableList.builder();
    CommandTool.Builder executableBuilder = new CommandTool.Builder();
    // Add the binary as the first argument.
    executableBuilder.addArg(SourcePathArg.of(new DefaultBuildTargetSourcePath(binaryTarget)));
    // Special handling for dynamically linked binaries.
    if (depType == Linker.LinkableDepType.SHARED) {
        // Create a symlink tree with for all shared libraries needed by this binary.
        SymlinkTree sharedLibraries = resolver.addToIndex(CxxDescriptionEnhancer.createSharedLibrarySymlinkTree(ruleFinder, params.getBuildTarget(), params.getProjectFilesystem(), cxxPlatform, deps, NativeLinkable.class::isInstance));
        // Embed a origin-relative library path into the binary so it can find the shared libraries.
        // The shared libraries root is absolute. Also need an absolute path to the linkOutput
        Path absBinaryDir = params.getBuildTarget().getCellPath().resolve(HaskellLinkRule.getOutputDir(binaryTarget, params.getProjectFilesystem()));
        linkFlagsBuilder.addAll(MoreIterables.zipAndConcat(Iterables.cycle("-optl"), Linkers.iXlinker("-rpath", String.format("%s/%s", cxxPlatform.getLd().resolve(resolver).origin(), absBinaryDir.relativize(sharedLibraries.getRoot()).toString()))));
        // Add all the shared libraries and the symlink tree as inputs to the tool that represents
        // this binary, so that users can attach the proper deps.
        executableBuilder.addDep(sharedLibraries);
        executableBuilder.addInputs(sharedLibraries.getLinks().values());
    }
    // Generate the compile rule and add its objects to the link.
    HaskellCompileRule compileRule = resolver.addToIndex(HaskellDescriptionUtils.requireCompileRule(params, resolver, ruleFinder, deps, cxxPlatform, haskellConfig, depType, args.main, Optional.empty(), args.compilerFlags, HaskellSources.from(params.getBuildTarget(), resolver, pathResolver, ruleFinder, cxxPlatform, "srcs", args.srcs)));
    linkArgsBuilder.addAll(SourcePathArg.from(compileRule.getObjects()));
    ImmutableList<String> linkFlags = linkFlagsBuilder.build();
    ImmutableList<com.facebook.buck.rules.args.Arg> linkArgs = linkArgsBuilder.build();
    final CommandTool executable = executableBuilder.build();
    final HaskellLinkRule linkRule = HaskellDescriptionUtils.createLinkRule(binaryTarget, params, resolver, ruleFinder, cxxPlatform, haskellConfig, Linker.LinkType.EXECUTABLE, linkFlags, linkArgs, RichStream.from(deps).filter(NativeLinkable.class).toImmutableList(), depType);
    return new HaskellBinary(params.copyAppendingExtraDeps(linkRule), ruleFinder, deps, executable, linkRule.getSourcePathToOutput());
}
Also used : NativeLinkable(com.facebook.buck.cxx.NativeLinkable) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) Linker(com.facebook.buck.cxx.Linker) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) CommandTool(com.facebook.buck.rules.CommandTool) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg)

Example 63 with DefaultBuildTargetSourcePath

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

the class DLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver buildRuleResolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    if (params.getBuildTarget().getFlavors().contains(DDescriptionUtils.SOURCE_LINK_TREE)) {
        return DDescriptionUtils.createSourceSymlinkTree(params.getBuildTarget(), params, ruleFinder, pathResolver, args.srcs);
    }
    BuildTarget sourceTreeTarget = params.getBuildTarget().withAppendedFlavors(DDescriptionUtils.SOURCE_LINK_TREE);
    DIncludes dIncludes = DIncludes.builder().setLinkTree(new DefaultBuildTargetSourcePath(sourceTreeTarget)).setSources(args.srcs.getPaths()).build();
    if (params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.STATIC_FLAVOR)) {
        buildRuleResolver.requireRule(sourceTreeTarget);
        return createStaticLibraryBuildRule(params, buildRuleResolver, pathResolver, ruleFinder, cxxPlatform, dBuckConfig, /* compilerFlags */
        ImmutableList.of(), args.srcs, dIncludes, CxxSourceRuleFactory.PicType.PDC);
    }
    return new DLibrary(params, buildRuleResolver, dIncludes);
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath)

Example 64 with DefaultBuildTargetSourcePath

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

the class CxxPrecompiledHeaderRuleTest method userRuleChangesDependencyPCHRuleFlags.

@Test
public void userRuleChangesDependencyPCHRuleFlags() throws Exception {
    assumeTrue(platformOkForPCHTests());
    BuildTarget pchTarget = newTarget("//test:pch");
    CxxPrecompiledHeaderTemplate pch = newPCH(pchTarget);
    ruleResolver.addToIndex(pch);
    BuildTarget libTarget = newTarget("//test:lib");
    BuildRuleParams libParams = newParams(libTarget);
    CxxSourceRuleFactory factory1 = newFactoryBuilder(libParams, "-flag-for-factory").setPrecompiledHeader(new DefaultBuildTargetSourcePath(pchTarget)).build();
    CxxPreprocessAndCompile lib = factory1.createPreprocessAndCompileBuildRule("lib.cpp", newCxxSourceBuilder().setPath(new FakeSourcePath("lib.cpp")).setFlags(ImmutableList.of("-flag-for-source")).build());
    ruleResolver.addToIndex(lib);
    ImmutableList<String> libCmd = lib.makeMainStep(pathResolver, Paths.get("/tmp/x"), false).getCommand();
    assertTrue(seek(libCmd, "-flag-for-source").size() > 0);
    assertTrue(seek(libCmd, "-flag-for-factory").size() > 0);
    CxxPrecompiledHeader pchInstance = null;
    for (BuildRule dep : lib.getDeps()) {
        if (dep instanceof CxxPrecompiledHeader) {
            pchInstance = (CxxPrecompiledHeader) dep;
        }
    }
    assertNotNull(pchInstance);
    ImmutableList<String> pchCmd = pchInstance.makeMainStep(pathResolver, Paths.get("/tmp/x")).getCommand();
    assertTrue(seek(pchCmd, "-flag-for-source").size() > 0);
    assertTrue(seek(pchCmd, "-flag-for-factory").size() > 0);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) Test(org.junit.Test)

Example 65 with DefaultBuildTargetSourcePath

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

the class CxxLibraryTest method cxxLibraryInterfaces.

@Test
public void cxxLibraryInterfaces() {
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    CxxPlatform cxxPlatform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Setup some dummy values for the header info.
    final BuildTarget publicHeaderTarget = BuildTargetFactory.newInstance("//:header");
    final BuildTarget publicHeaderSymlinkTreeTarget = BuildTargetFactory.newInstance("//:symlink");
    final BuildTarget privateHeaderTarget = BuildTargetFactory.newInstance("//:privateheader");
    final BuildTarget privateHeaderSymlinkTreeTarget = BuildTargetFactory.newInstance("//:privatesymlink");
    // Setup some dummy values for the library archive info.
    final BuildRule archive = new FakeBuildRule("//:archive", pathResolver).setOutputFile("libarchive.a");
    // Setup some dummy values for the library archive info.
    final BuildRule sharedLibrary = new FakeBuildRule("//:shared", pathResolver).setOutputFile("libshared.so");
    final Path sharedLibraryOutput = Paths.get("output/path/lib.so");
    final String sharedLibrarySoname = "lib.so";
    // Construct a CxxLibrary object to test.
    FakeCxxLibrary cxxLibrary = new FakeCxxLibrary(params, publicHeaderTarget, publicHeaderSymlinkTreeTarget, privateHeaderTarget, privateHeaderSymlinkTreeTarget, archive, sharedLibrary, sharedLibraryOutput, sharedLibrarySoname, ImmutableSortedSet.of());
    // Verify that we get the header/symlink targets and root via the CxxPreprocessorDep
    // interface.
    CxxPreprocessorInput expectedPublicCxxPreprocessorInput = CxxPreprocessorInput.builder().addIncludes(CxxSymlinkTreeHeaders.builder().setIncludeType(CxxPreprocessables.IncludeType.LOCAL).putNameToPathMap(Paths.get("header.h"), new DefaultBuildTargetSourcePath(publicHeaderTarget)).setRoot(new DefaultBuildTargetSourcePath(publicHeaderSymlinkTreeTarget)).build()).build();
    assertEquals(expectedPublicCxxPreprocessorInput, cxxLibrary.getCxxPreprocessorInput(cxxPlatform, HeaderVisibility.PUBLIC));
    CxxPreprocessorInput expectedPrivateCxxPreprocessorInput = CxxPreprocessorInput.builder().addIncludes(CxxSymlinkTreeHeaders.builder().setIncludeType(CxxPreprocessables.IncludeType.LOCAL).setRoot(new DefaultBuildTargetSourcePath(privateHeaderSymlinkTreeTarget)).putNameToPathMap(Paths.get("header.h"), new DefaultBuildTargetSourcePath(privateHeaderTarget)).build()).build();
    assertEquals(expectedPrivateCxxPreprocessorInput, cxxLibrary.getCxxPreprocessorInput(cxxPlatform, HeaderVisibility.PRIVATE));
    // Verify that we get the static archive and its build target via the NativeLinkable
    // interface.
    NativeLinkableInput expectedStaticNativeLinkableInput = NativeLinkableInput.of(ImmutableList.of(SourcePathArg.of(archive.getSourcePathToOutput())), ImmutableSet.of(), ImmutableSet.of());
    assertEquals(expectedStaticNativeLinkableInput, cxxLibrary.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.STATIC));
    // Verify that we get the static archive and its build target via the NativeLinkable
    // interface.
    NativeLinkableInput expectedSharedNativeLinkableInput = NativeLinkableInput.of(ImmutableList.of(SourcePathArg.of(sharedLibrary.getSourcePathToOutput())), ImmutableSet.of(), ImmutableSet.of());
    assertEquals(expectedSharedNativeLinkableInput, cxxLibrary.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.SHARED));
    // Verify that the implemented BuildRule methods are effectively unused.
    assertEquals(ImmutableList.<Step>of(), cxxLibrary.getBuildSteps(null, null));
    assertNull(cxxLibrary.getSourcePathToOutput());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)71 Test (org.junit.Test)64 BuildTarget (com.facebook.buck.model.BuildTarget)53 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)26 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)25 SourcePath (com.facebook.buck.rules.SourcePath)25 PathSourcePath (com.facebook.buck.rules.PathSourcePath)22 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)21 TargetGraph (com.facebook.buck.rules.TargetGraph)19 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)18 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)18 Path (java.nio.file.Path)16 BuildRule (com.facebook.buck.rules.BuildRule)11 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)10 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)9 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)9 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)8 FrameworkPath (com.facebook.buck.rules.coercer.FrameworkPath)7 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)6 SourceTreePath (com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath)5