Search in sources :

Example 1 with CommandTool

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

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

the class RustCompileUtils method createBinaryBuildRule.

public static BinaryWrapperRule createBinaryBuildRule(BuildRuleParams params, BuildRuleResolver resolver, RustBuckConfig rustBuckConfig, FlavorDomain<CxxPlatform> cxxPlatforms, CxxPlatform defaultCxxPlatform, Optional<String> crateName, ImmutableSortedSet<String> features, Iterator<String> rustcFlags, Iterator<String> linkerFlags, Linker.LinkableDepType linkStyle, boolean rpath, ImmutableSortedSet<SourcePath> srcs, Optional<SourcePath> crateRoot, ImmutableSet<String> defaultRoots) throws NoSuchBuildTargetException {
    final BuildTarget buildTarget = params.getBuildTarget();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ImmutableList.Builder<String> rustcArgs = ImmutableList.builder();
    RustCompileUtils.addFeatures(buildTarget, features, rustcArgs);
    rustcArgs.addAll(rustcFlags);
    ImmutableList.Builder<String> linkerArgs = ImmutableList.builder();
    linkerArgs.addAll(linkerFlags);
    String crate = crateName.orElse(ruleToCrateName(buildTarget.getShortName()));
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(params.getBuildTarget()).orElse(defaultCxxPlatform);
    Pair<SourcePath, ImmutableSortedSet<SourcePath>> rootModuleAndSources = getRootModuleAndSources(params.getBuildTarget(), resolver, pathResolver, ruleFinder, cxxPlatform, crate, crateRoot, defaultRoots, srcs);
    // The target to use for the link rule.
    BuildTarget binaryTarget = params.getBuildTarget().withAppendedFlavors(cxxPlatform.getFlavor(), RustDescriptionEnhancer.RFBIN);
    CommandTool.Builder executableBuilder = new CommandTool.Builder();
    // Special handling for dynamically linked binaries.
    if (linkStyle == Linker.LinkableDepType.SHARED) {
        // Create a symlink tree with for all native shared (NativeLinkable) libraries
        // needed by this binary.
        SymlinkTree sharedLibraries = resolver.addToIndex(CxxDescriptionEnhancer.createSharedLibrarySymlinkTree(ruleFinder, params.getBuildTarget(), params.getProjectFilesystem(), cxxPlatform, params.getDeps(), RustLinkable.class::isInstance, RustLinkable.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(RustCompileRule.getOutputDir(binaryTarget, params.getProjectFilesystem()));
        linkerArgs.addAll(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());
        // Also add Rust shared libraries as runtime deps. We don't need these in the symlink tree
        // because rustc will include their dirs in rpath by default.
        Map<String, SourcePath> rustSharedLibraries = getTransitiveRustSharedLibraries(cxxPlatform, params.getDeps());
        executableBuilder.addInputs(rustSharedLibraries.values());
    }
    final RustCompileRule buildRule = RustCompileUtils.createBuild(binaryTarget, crate, params, resolver, pathResolver, ruleFinder, cxxPlatform, rustBuckConfig, rustcArgs.build(), linkerArgs.build(), /* linkerInputs */
    ImmutableList.of(), CrateType.BIN, linkStyle, rpath, rootModuleAndSources.getSecond(), rootModuleAndSources.getFirst());
    // Add the binary as the first argument.
    executableBuilder.addArg(SourcePathArg.of(buildRule.getSourcePathToOutput()));
    final CommandTool executable = executableBuilder.build();
    return new BinaryWrapperRule(params.copyAppendingExtraDeps(buildRule), ruleFinder) {

        @Override
        public Tool getExecutableCommand() {
            return executable;
        }

        @Override
        public SourcePath getSourcePathToOutput() {
            return new ForwardingBuildTargetSourcePath(getBuildTarget(), buildRule.getSourcePathToOutput());
        }
    };
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) HasSourcePath(com.facebook.buck.rules.args.HasSourcePath) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) ImmutableList(com.google.common.collect.ImmutableList) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BinaryWrapperRule(com.facebook.buck.rules.BinaryWrapperRule) SourcePath(com.facebook.buck.rules.SourcePath) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) HasSourcePath(com.facebook.buck.rules.args.HasSourcePath) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) CommandTool(com.facebook.buck.rules.CommandTool) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet)

Aggregations

CxxPlatform (com.facebook.buck.cxx.CxxPlatform)2 BuildTarget (com.facebook.buck.model.BuildTarget)2 CommandTool (com.facebook.buck.rules.CommandTool)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)2 SymlinkTree (com.facebook.buck.rules.SymlinkTree)2 ImmutableList (com.google.common.collect.ImmutableList)2 Path (java.nio.file.Path)2 Linker (com.facebook.buck.cxx.Linker)1 NativeLinkable (com.facebook.buck.cxx.NativeLinkable)1 BinaryWrapperRule (com.facebook.buck.rules.BinaryWrapperRule)1 BuildRule (com.facebook.buck.rules.BuildRule)1 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)1 ForwardingBuildTargetSourcePath (com.facebook.buck.rules.ForwardingBuildTargetSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 HasSourcePath (com.facebook.buck.rules.args.HasSourcePath)1 SourcePathArg (com.facebook.buck.rules.args.SourcePathArg)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1