Search in sources :

Example 26 with SourcePath

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

the class AbstractCxxSymlinkTreeHeaders method appendToRuleKey.

@Override
public void appendToRuleKey(RuleKeyObjectSink sink) {
    sink.setReflectively("type", getIncludeType());
    for (Path path : ImmutableSortedSet.copyOf(getNameToPathMap().keySet())) {
        SourcePath source = getNameToPathMap().get(path);
        sink.setReflectively("include(" + path.toString() + ")", source);
    }
}
Also used : ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath)

Example 27 with SourcePath

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

the class HaskellDescriptionUtils method createCompileRule.

/**
   * Create a Haskell compile rule that compiles all the given haskell sources in one step and
   * pulls interface files from all transitive haskell dependencies.
   */
private static HaskellCompileRule createCompileRule(BuildTarget target, final BuildRuleParams baseParams, final BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, ImmutableSet<BuildRule> deps, final CxxPlatform cxxPlatform, HaskellConfig haskellConfig, final Linker.LinkableDepType depType, Optional<String> main, Optional<HaskellPackageInfo> packageInfo, ImmutableList<String> flags, HaskellSources sources) throws NoSuchBuildTargetException {
    final Map<BuildTarget, ImmutableList<String>> depFlags = new TreeMap<>();
    final Map<BuildTarget, ImmutableList<SourcePath>> depIncludes = new TreeMap<>();
    final ImmutableSortedMap.Builder<String, HaskellPackage> exposedPackagesBuilder = ImmutableSortedMap.naturalOrder();
    final ImmutableSortedMap.Builder<String, HaskellPackage> packagesBuilder = ImmutableSortedMap.naturalOrder();
    new AbstractBreadthFirstThrowingTraversal<BuildRule, NoSuchBuildTargetException>(deps) {

        private final ImmutableSet<BuildRule> empty = ImmutableSet.of();

        @Override
        public Iterable<BuildRule> visit(BuildRule rule) throws NoSuchBuildTargetException {
            ImmutableSet<BuildRule> ruleDeps = empty;
            if (rule instanceof HaskellCompileDep) {
                ruleDeps = rule.getDeps();
                HaskellCompileInput compileInput = ((HaskellCompileDep) rule).getCompileInput(cxxPlatform, depType);
                depFlags.put(rule.getBuildTarget(), compileInput.getFlags());
                depIncludes.put(rule.getBuildTarget(), compileInput.getIncludes());
                // We add packages from first-order deps as expose modules, and transitively included
                // packages as hidden ones.
                boolean firstOrderDep = deps.contains(rule);
                for (HaskellPackage pkg : compileInput.getPackages()) {
                    if (firstOrderDep) {
                        exposedPackagesBuilder.put(pkg.getInfo().getIdentifier(), pkg);
                    } else {
                        packagesBuilder.put(pkg.getInfo().getIdentifier(), pkg);
                    }
                }
            }
            return ruleDeps;
        }
    }.start();
    Collection<CxxPreprocessorInput> cxxPreprocessorInputs = CxxPreprocessables.getTransitiveCxxPreprocessorInput(cxxPlatform, deps);
    ExplicitCxxToolFlags.Builder toolFlagsBuilder = CxxToolFlags.explicitBuilder();
    PreprocessorFlags.Builder ppFlagsBuilder = PreprocessorFlags.builder();
    toolFlagsBuilder.setPlatformFlags(CxxSourceTypes.getPlatformPreprocessFlags(cxxPlatform, CxxSource.Type.C));
    for (CxxPreprocessorInput input : cxxPreprocessorInputs) {
        ppFlagsBuilder.addAllIncludes(input.getIncludes());
        ppFlagsBuilder.addAllFrameworkPaths(input.getFrameworks());
        toolFlagsBuilder.addAllRuleFlags(input.getPreprocessorFlags().get(CxxSource.Type.C));
    }
    ppFlagsBuilder.setOtherFlags(toolFlagsBuilder.build());
    PreprocessorFlags ppFlags = ppFlagsBuilder.build();
    ImmutableList<String> compileFlags = ImmutableList.<String>builder().addAll(haskellConfig.getCompilerFlags()).addAll(flags).addAll(Iterables.concat(depFlags.values())).build();
    ImmutableList<SourcePath> includes = ImmutableList.copyOf(Iterables.concat(depIncludes.values()));
    ImmutableSortedMap<String, HaskellPackage> exposedPackages = exposedPackagesBuilder.build();
    ImmutableSortedMap<String, HaskellPackage> packages = packagesBuilder.build();
    return HaskellCompileRule.from(target, baseParams, ruleFinder, haskellConfig.getCompiler().resolve(resolver), haskellConfig.getHaskellVersion(), compileFlags, ppFlags, cxxPlatform, depType == Linker.LinkableDepType.STATIC ? CxxSourceRuleFactory.PicType.PDC : CxxSourceRuleFactory.PicType.PIC, main, packageInfo, includes, exposedPackages, packages, sources, CxxSourceTypes.getPreprocessor(cxxPlatform, CxxSource.Type.C).resolve(resolver));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) PreprocessorFlags(com.facebook.buck.cxx.PreprocessorFlags) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) TreeMap(java.util.TreeMap) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) ExplicitCxxToolFlags(com.facebook.buck.cxx.ExplicitCxxToolFlags)

Example 28 with SourcePath

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

the class HaskellLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
    final BuildTarget buildTarget = params.getBuildTarget();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    // See if we're building a particular "type" and "platform" of this library, and if so, extract
    // them from the flavors attached to the build target.
    Optional<Map.Entry<Flavor, Type>> type = LIBRARY_TYPE.getFlavorAndValue(buildTarget);
    Optional<CxxPlatform> cxxPlatform = cxxPlatforms.getValue(buildTarget);
    if (type.isPresent()) {
        Preconditions.checkState(cxxPlatform.isPresent());
        // Get the base build, without any flavors referring to the library type or platform.
        BuildTarget baseTarget = params.getBuildTarget().withoutFlavors(Sets.union(Type.FLAVOR_VALUES, cxxPlatforms.getFlavors()));
        switch(type.get().getValue()) {
            case PACKAGE_SHARED:
            case PACKAGE_STATIC:
            case PACKAGE_STATIC_PIC:
                Linker.LinkableDepType depType;
                if (type.get().getValue().equals(Type.PACKAGE_SHARED)) {
                    depType = Linker.LinkableDepType.SHARED;
                } else if (type.get().getValue().equals(Type.PACKAGE_STATIC)) {
                    depType = Linker.LinkableDepType.STATIC;
                } else {
                    depType = Linker.LinkableDepType.STATIC_PIC;
                }
                return requirePackage(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, depType);
            case SHARED:
                return requireSharedLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args);
            case STATIC_PIC:
            case STATIC:
                return requireStaticLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, type.get().getValue() == Type.STATIC ? Linker.LinkableDepType.STATIC : Linker.LinkableDepType.STATIC_PIC);
        }
        throw new IllegalStateException(String.format("%s: unexpected type `%s`", params.getBuildTarget(), type.get().getValue()));
    }
    return new HaskellLibrary(params) {

        @Override
        public HaskellCompileInput getCompileInput(CxxPlatform cxxPlatform, Linker.LinkableDepType depType) throws NoSuchBuildTargetException {
            HaskellPackageRule rule = requirePackage(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, depType);
            return HaskellCompileInput.builder().addPackages(rule.getPackage()).build();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableDeps() {
            return ImmutableList.of();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableExportedDeps() {
            return FluentIterable.from(getDeps()).filter(NativeLinkable.class);
        }

        @Override
        public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException {
            Iterable<com.facebook.buck.rules.args.Arg> linkArgs;
            switch(type) {
                case STATIC:
                case STATIC_PIC:
                    Archive archive = requireStaticLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, type);
                    linkArgs = args.linkWhole.orElse(false) ? cxxPlatform.getLd().resolve(resolver).linkWhole(archive.toArg()) : ImmutableList.of(archive.toArg());
                    break;
                case SHARED:
                    BuildRule rule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
                    linkArgs = ImmutableList.of(SourcePathArg.of(rule.getSourcePathToOutput()));
                    break;
                default:
                    throw new IllegalStateException();
            }
            return NativeLinkableInput.builder().addAllArgs(linkArgs).build();
        }

        @Override
        public Linkage getPreferredLinkage(CxxPlatform cxxPlatform) {
            return args.preferredLinkage.orElse(Linkage.ANY);
        }

        @Override
        public ImmutableMap<String, SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
            ImmutableMap.Builder<String, SourcePath> libs = ImmutableMap.builder();
            String sharedLibrarySoname = CxxDescriptionEnhancer.getSharedLibrarySoname(Optional.empty(), getBuildTarget(), cxxPlatform);
            BuildRule sharedLibraryBuildRule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
            libs.put(sharedLibrarySoname, sharedLibraryBuildRule.getSourcePathToOutput());
            return libs.build();
        }
    };
}
Also used : Archive(com.facebook.buck.cxx.Archive) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) Linker(com.facebook.buck.cxx.Linker) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) BuildRule(com.facebook.buck.rules.BuildRule)

Example 29 with SourcePath

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

the class GoDescriptors method makeSymlinkTree.

private static SymlinkTree makeSymlinkTree(BuildRuleParams params, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, ImmutableSet<GoLinkable> linkables) {
    ImmutableMap.Builder<Path, SourcePath> treeMapBuilder = ImmutableMap.builder();
    for (GoLinkable linkable : linkables) {
        for (Map.Entry<Path, SourcePath> linkInput : linkable.getGoLinkInput().entrySet()) {
            treeMapBuilder.put(getPathInSymlinkTree(pathResolver, linkInput.getKey(), linkInput.getValue()), linkInput.getValue());
        }
    }
    ImmutableMap<Path, SourcePath> treeMap;
    try {
        treeMap = treeMapBuilder.build();
    } catch (IllegalArgumentException ex) {
        throw new HumanReadableException(ex, "Multiple go targets have the same package name when compiling %s", params.getBuildTarget().getFullyQualifiedName());
    }
    Path root = BuildTargets.getScratchPath(params.getProjectFilesystem(), params.getBuildTarget(), "__%s__tree");
    return new SymlinkTree(params.getBuildTarget(), params.getProjectFilesystem(), root, treeMap, ruleFinder);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) HumanReadableException(com.facebook.buck.util.HumanReadableException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 30 with SourcePath

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

the class HalideLibraryDescription 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);
    BuildTarget target = params.getBuildTarget();
    ImmutableSet<Flavor> flavors = ImmutableSet.copyOf(target.getFlavors());
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(flavors).orElse(defaultCxxPlatform);
    if (flavors.contains(CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR)) {
        ImmutableMap.Builder<Path, SourcePath> headersBuilder = ImmutableMap.builder();
        BuildTarget compileTarget = resolver.requireRule(target.withFlavors(HALIDE_COMPILE_FLAVOR, cxxPlatform.getFlavor())).getBuildTarget();
        Path outputPath = HalideCompile.headerOutputPath(compileTarget, params.getProjectFilesystem(), args.functionName);
        headersBuilder.put(outputPath.getFileName(), new ExplicitBuildTargetSourcePath(compileTarget, outputPath));
        return CxxDescriptionEnhancer.createHeaderSymlinkTree(params, resolver, cxxPlatform, headersBuilder.build(), HeaderVisibility.PUBLIC, true);
    } else if (flavors.contains(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR)) {
        CxxPlatform hostCxxPlatform = cxxPlatforms.getValue(CxxPlatforms.getHostFlavor());
        return CxxDescriptionEnhancer.createSandboxTreeBuildRule(resolver, args, hostCxxPlatform, params);
    } else if (flavors.contains(HALIDE_COMPILER_FLAVOR)) {
        // We always want to build the halide "compiler" for the host platform, so
        // we use the host flavor here, regardless of the flavors on the build
        // target.
        CxxPlatform hostCxxPlatform = cxxPlatforms.getValue(CxxPlatforms.getHostFlavor());
        final ImmutableSortedSet<BuildTarget> compilerDeps = args.compilerDeps;
        return createHalideCompiler(params.withAppendedFlavor(HALIDE_COMPILER_FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(resolver.getAllRules(compilerDeps)), Suppliers.ofInstance(ImmutableSortedSet.of())), resolver, pathResolver, ruleFinder, hostCxxPlatform, args.srcs, args.compilerFlags, args.platformCompilerFlags, args.langCompilerFlags, args.linkerFlags, args.platformLinkerFlags, args.includeDirs);
    } else if (flavors.contains(CxxDescriptionEnhancer.STATIC_FLAVOR) || flavors.contains(CxxDescriptionEnhancer.STATIC_PIC_FLAVOR)) {
        // See: https://github.com/halide/Halide/blob/e3c301f3/src/LLVM_Output.cpp#L152
        return createHalideStaticLibrary(params, resolver, ruleFinder, cxxPlatform, args);
    } else if (flavors.contains(CxxDescriptionEnhancer.SHARED_FLAVOR)) {
        throw new HumanReadableException("halide_library '%s' does not support shared libraries as output", params.getBuildTarget());
    } else if (flavors.contains(HALIDE_COMPILE_FLAVOR)) {
        return createHalideCompile(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), resolver, cxxPlatform, Optional.of(args.compilerInvocationFlags), args.functionName);
    }
    return new HalideLibrary(params, resolver, args.supportedPlatformsRegex);
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Aggregations

SourcePath (com.facebook.buck.rules.SourcePath)285 Path (java.nio.file.Path)151 BuildTarget (com.facebook.buck.model.BuildTarget)111 Test (org.junit.Test)105 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)102 PathSourcePath (com.facebook.buck.rules.PathSourcePath)100 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)96 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)90 BuildRule (com.facebook.buck.rules.BuildRule)79 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)69 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)69 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)64 ImmutableList (com.google.common.collect.ImmutableList)64 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)53 ImmutableMap (com.google.common.collect.ImmutableMap)48 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)43 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)42 HumanReadableException (com.facebook.buck.util.HumanReadableException)39 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)37 ImmutableSet (com.google.common.collect.ImmutableSet)34