Search in sources :

Example 16 with SourcePathRuleFinder

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

the class CxxDescriptionEnhancer method parseOnlyPlatformHeaders.

static ImmutableMap<String, SourcePath> parseOnlyPlatformHeaders(BuildTarget buildTarget, BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, SourcePathResolver sourcePathResolver, CxxPlatform cxxPlatform, String headersParameterName, SourceList headers, String platformHeadersParameterName, PatternMatchedCollection<SourceList> platformHeaders) throws NoSuchBuildTargetException {
    ImmutableMap.Builder<String, SourcePath> parsed = ImmutableMap.builder();
    java.util.function.Function<SourcePath, SourcePath> fixup = path -> {
        try {
            return CxxGenruleDescription.fixupSourcePath(resolver, ruleFinder, cxxPlatform, path);
        } catch (NoSuchBuildTargetException e) {
            throw new RuntimeException(e);
        }
    };
    // Include all normal exported headers that are generated by `cxx_genrule`.
    parsed.putAll(headers.toNameMap(buildTarget, sourcePathResolver, headersParameterName, path -> CxxGenruleDescription.wrapsCxxGenrule(ruleFinder, path), fixup));
    // Include all platform specific headers.
    for (SourceList sourceList : platformHeaders.getMatchingValues(cxxPlatform.getFlavor().toString())) {
        parsed.putAll(sourceList.toNameMap(buildTarget, sourcePathResolver, platformHeadersParameterName, path -> true, fixup));
    }
    return parsed.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) PatternMatchedCollection(com.facebook.buck.rules.coercer.PatternMatchedCollection) ImmutableCollection(com.google.common.collect.ImmutableCollection) RichStream(com.facebook.buck.util.RichStream) InternalFlavor(com.facebook.buck.model.InternalFlavor) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) SymlinkTree(com.facebook.buck.rules.SymlinkTree) FlavorDomain(com.facebook.buck.model.FlavorDomain) Matcher(java.util.regex.Matcher) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) StringArg(com.facebook.buck.rules.args.StringArg) Map(java.util.Map) SourceList(com.facebook.buck.rules.coercer.SourceList) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) Path(java.nio.file.Path) JsonConcatenate(com.facebook.buck.json.JsonConcatenate) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildTarget(com.facebook.buck.model.BuildTarget) RuleKeyAppendableFunction(com.facebook.buck.rules.args.RuleKeyAppendableFunction) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) Predicate(com.google.common.base.Predicate) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) Joiner(com.google.common.base.Joiner) CellPathResolver(com.facebook.buck.rules.CellPathResolver) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) HashMap(java.util.HashMap) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) Suppliers(com.google.common.base.Suppliers) StreamSupport(java.util.stream.StreamSupport) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) Functions(com.google.common.base.Functions) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) HumanReadableException(com.facebook.buck.util.HumanReadableException) QueryUtils(com.facebook.buck.rules.query.QueryUtils) Arg(com.facebook.buck.rules.args.Arg) Paths(java.nio.file.Paths) CommandTool(com.facebook.buck.rules.CommandTool) StringWithMacrosArg(com.facebook.buck.rules.args.StringWithMacrosArg) LocationMacroExpander(com.facebook.buck.rules.macros.LocationMacroExpander) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BuildTargets(com.facebook.buck.model.BuildTargets) SourceList(com.facebook.buck.rules.coercer.SourceList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 17 with SourcePathRuleFinder

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

the class CxxDescriptionEnhancer method createSandboxTreeBuildRule.

public static SymlinkTree createSandboxTreeBuildRule(BuildRuleResolver resolver, CxxConstructorArg args, CxxPlatform platform, BuildRuleParams params) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
    ImmutableCollection<SourcePath> privateHeaders = parseHeaders(params.getBuildTarget(), resolver, ruleFinder, sourcePathResolver, Optional.of(platform), args).values();
    ImmutableCollection<CxxSource> sources = parseCxxSources(params.getBuildTarget(), resolver, ruleFinder, sourcePathResolver, platform, args).values();
    HashMap<Path, SourcePath> links = new HashMap<>();
    for (SourcePath headerPath : privateHeaders) {
        links.put(Paths.get(sourcePathResolver.getSourcePathName(params.getBuildTarget(), headerPath)), headerPath);
    }
    if (args instanceof CxxLibraryDescription.Arg) {
        ImmutableCollection<SourcePath> publicHeaders = CxxDescriptionEnhancer.parseExportedHeaders(params.getBuildTarget(), resolver, ruleFinder, sourcePathResolver, Optional.of(platform), (CxxLibraryDescription.Arg) args).values();
        for (SourcePath headerPath : publicHeaders) {
            links.put(Paths.get(sourcePathResolver.getSourcePathName(params.getBuildTarget(), headerPath)), headerPath);
        }
    }
    for (CxxSource source : sources) {
        SourcePath sourcePath = source.getPath();
        links.put(Paths.get(sourcePathResolver.getSourcePathName(params.getBuildTarget(), sourcePath)), sourcePath);
    }
    return createSandboxSymlinkTree(params, platform, ImmutableMap.copyOf(links), ruleFinder);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) HashMap(java.util.HashMap) StringArg(com.facebook.buck.rules.args.StringArg) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) Arg(com.facebook.buck.rules.args.Arg) StringWithMacrosArg(com.facebook.buck.rules.args.StringWithMacrosArg) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 18 with SourcePathRuleFinder

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

the class CxxDescriptionEnhancer method createUberCompilationDatabase.

public static BuildRule createUberCompilationDatabase(BuildRuleParams params, BuildRuleResolver ruleResolver) throws NoSuchBuildTargetException {
    Optional<CxxCompilationDatabaseDependencies> compilationDatabases = ruleResolver.requireMetadata(params.withoutFlavor(CxxCompilationDatabase.UBER_COMPILATION_DATABASE).withAppendedFlavor(CxxCompilationDatabase.COMPILATION_DATABASE).getBuildTarget(), CxxCompilationDatabaseDependencies.class);
    Preconditions.checkState(compilationDatabases.isPresent());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    return new JsonConcatenate(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(ruleFinder.filterBuildRuleInputs(compilationDatabases.get().getSourcePaths()))), Suppliers.ofInstance(ImmutableSortedSet.of())), pathResolver.getAllAbsolutePaths(compilationDatabases.get().getSourcePaths()), "compilation-database-concatenate", "Concatenate compilation databases", "uber-compilation-database", "compile_commands.json");
}
Also used : SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) JsonConcatenate(com.facebook.buck.json.JsonConcatenate)

Example 19 with SourcePathRuleFinder

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

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

the class GoTestDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    GoPlatform platform = goBuckConfig.getPlatformFlavorDomain().getValue(params.getBuildTarget()).orElse(goBuckConfig.getDefaultPlatform());
    if (params.getBuildTarget().getFlavors().contains(TEST_LIBRARY_FLAVOR)) {
        return createTestLibrary(params, resolver, args, platform);
    }
    GoBinary testMain = createTestMainRule(params, resolver, args, platform);
    resolver.addToIndex(testMain);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    return new GoTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testMain)), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleFinder, testMain, args.labels, args.contacts, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.runTestSeparately.orElse(false), args.resources);
}
Also used : Optional(java.util.Optional) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Aggregations

SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)517 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)466 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)405 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)376 Test (org.junit.Test)352 BuildTarget (com.facebook.buck.model.BuildTarget)250 BuildRule (com.facebook.buck.rules.BuildRule)188 SourcePath (com.facebook.buck.rules.SourcePath)151 Path (java.nio.file.Path)151 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)149 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)139 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)131 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)94 PathSourcePath (com.facebook.buck.rules.PathSourcePath)92 TargetGraph (com.facebook.buck.rules.TargetGraph)91 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)87 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)73 RuleKey (com.facebook.buck.rules.RuleKey)72 ImmutableList (com.google.common.collect.ImmutableList)52 ExecutionContext (com.facebook.buck.step.ExecutionContext)50