Search in sources :

Example 41 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class AbstractCxxSourceRuleFactory method getPreprocessDeps.

private ImmutableSortedSet<BuildRule> getPreprocessDeps() {
    ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
    for (CxxPreprocessorInput input : getCxxPreprocessorInput()) {
        builder.addAll(input.getDeps(getResolver(), getRuleFinder()));
    }
    if (getPrefixHeader().isPresent()) {
        builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrefixHeader().get()));
    }
    if (getPrecompiledHeader().isPresent()) {
        builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrecompiledHeader().get()));
    }
    if (getSandboxTree().isPresent()) {
        SymlinkTree tree = getSandboxTree().get();
        builder.add(tree);
        builder.addAll(getRuleFinder().filterBuildRuleInputs(tree.getLinks().values()));
    }
    return builder.build();
}
Also used : SymlinkTree(com.facebook.buck.rules.SymlinkTree) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 42 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class CxxDescriptionEnhancer method createBuildRulesForCxxBinaryDescriptionArg.

public static CxxLinkAndCompileRules createBuildRulesForCxxBinaryDescriptionArg(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, CxxBinaryDescription.Arg args, Optional<StripStyle> stripStyle, Optional<LinkerMapMode> flavoredLinkerMapMode) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ImmutableMap<String, CxxSource> srcs = parseCxxSources(params.getBuildTarget(), resolver, ruleFinder, pathResolver, cxxPlatform, args);
    ImmutableMap<Path, SourcePath> headers = parseHeaders(params.getBuildTarget(), resolver, ruleFinder, pathResolver, Optional.of(cxxPlatform), args);
    // Build the binary deps.
    ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
    // Add original declared and extra deps.
    depsBuilder.addAll(params.getDeps());
    // Add in deps found via deps query.
    args.depsQuery.ifPresent(depsQuery -> QueryUtils.resolveDepQuery(params, depsQuery, resolver, targetGraph).forEach(depsBuilder::add));
    ImmutableSortedSet<BuildRule> deps = depsBuilder.build();
    return createBuildRulesForCxxBinary(params, resolver, cxxBuckConfig, cxxPlatform, srcs, headers, deps, stripStyle, flavoredLinkerMapMode, args.linkStyle.orElse(Linker.LinkableDepType.STATIC), args.preprocessorFlags, args.platformPreprocessorFlags, args.langPreprocessorFlags, args.frameworks, args.libraries, args.compilerFlags, args.langCompilerFlags, args.platformCompilerFlags, args.prefixHeader, args.precompiledHeader, args.linkerFlags, args.platformLinkerFlags, args.cxxRuntimeType, args.includeDirs, Optional.empty());
}
Also used : Path(java.nio.file.Path) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 43 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class JavaSymbolFinder method getCandidatePaths.

/**
   * Guessing file names from fully-qualified Java symbols is ambiguous, because we don't know ahead
   * of time exactly what part of the symbol is the package, and what part is class names or static
   * members. This returns the set of all possible Java files for a given symbol, given different
   * possibilities for the package name and resolving against all the available source roots.
   * Returns only those candidates that actually exist.
   */
private ImmutableSortedSet<Path> getCandidatePaths(String symbol, Collection<Path> srcRoots) {
    ImmutableSortedSet.Builder<Path> candidatePaths = ImmutableSortedSet.naturalOrder();
    List<String> symbolParts = Lists.newArrayList(symbol.split("\\."));
    for (int symbolIndex = 0; symbolIndex < symbolParts.size(); symbolIndex++) {
        List<String> pathPartsList = symbolParts.subList(0, symbolIndex);
        String[] pathParts = pathPartsList.toArray(new String[pathPartsList.size()]);
        String candidateFileName = symbolParts.get(symbolIndex) + ".java";
        for (Path srcRoot : srcRoots) {
            Path candidatePath = srcRoot.resolve(Paths.get("", pathParts)).resolve(candidateFileName);
            if (projectFilesystem.exists(candidatePath)) {
                candidatePaths.add(candidatePath);
            }
        }
    }
    return candidatePaths.build();
}
Also used : Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet)

Example 44 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class JavaLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    BuildTarget target = params.getBuildTarget();
    // We know that the flavour we're being asked to create is valid, since the check is done when
    // creating the action graph from the target graph.
    ImmutableSortedSet<Flavor> flavors = target.getFlavors();
    if (flavors.contains(Javadoc.DOC_JAR)) {
        BuildTarget unflavored = BuildTarget.of(target.getUnflavoredBuildTarget());
        BuildRule baseLibrary = resolver.requireRule(unflavored);
        JarShape shape = params.getBuildTarget().getFlavors().contains(JavaLibrary.MAVEN_JAR) ? JarShape.MAVEN : JarShape.SINGLE;
        JarShape.Summary summary = shape.gatherDeps(baseLibrary);
        ImmutableSet<SourcePath> sources = summary.getPackagedRules().stream().filter(HasSources.class::isInstance).map(rule -> ((HasSources) rule).getSources()).flatMap(Collection::stream).collect(MoreCollectors.toImmutableSet());
        // In theory, the only deps we need are the ones that contribute to the sourcepaths. However,
        // javadoc wants to have classes being documented have all their deps be available somewhere.
        // Ideally, we'd not build everything, but then we're not able to document any classes that
        // rely on auto-generated classes, such as those created by the Immutables library. Oh well.
        // Might as well add them as deps. *sigh*
        ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
        // Sourcepath deps
        deps.addAll(ruleFinder.filterBuildRuleInputs(sources));
        // Classpath deps
        deps.add(baseLibrary);
        deps.addAll(summary.getClasspath().stream().filter(rule -> HasClasspathEntries.class.isAssignableFrom(rule.getClass())).flatMap(rule -> rule.getTransitiveClasspathDeps().stream()).iterator());
        BuildRuleParams emptyParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(deps.build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
        return new Javadoc(emptyParams, args.mavenCoords, args.mavenPomTemplate, summary.getMavenDeps(), sources);
    }
    if (CalculateAbi.isAbiTarget(target)) {
        BuildTarget libraryTarget = CalculateAbi.getLibraryTarget(target);
        BuildRule libraryRule = resolver.requireRule(libraryTarget);
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(libraryRule.getSourcePathToOutput()));
    }
    BuildRuleParams paramsWithMavenFlavor = null;
    if (flavors.contains(JavaLibrary.MAVEN_JAR)) {
        paramsWithMavenFlavor = params;
        // Maven rules will depend upon their vanilla versions, so the latter have to be constructed
        // without the maven flavor to prevent output-path conflict
        params = params.withoutFlavor(JavaLibrary.MAVEN_JAR);
    }
    if (flavors.contains(JavaLibrary.SRC_JAR)) {
        args.mavenCoords = args.mavenCoords.map(input -> AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES));
        if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
            return new JavaSourceJar(params, args.srcs, args.mavenCoords);
        } else {
            return MavenUberJar.SourceJar.create(Preconditions.checkNotNull(paramsWithMavenFlavor), args.srcs, args.mavenCoords, args.mavenPomTemplate);
        }
    }
    JavacOptions javacOptions = JavacOptionsFactory.create(defaultOptions, params, resolver, ruleFinder, args);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps);
    BuildRuleParams javaLibraryParams = params.copyAppendingExtraDeps(Iterables.concat(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), exportedDeps, resolver.getAllRules(args.providedDeps))), ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))));
    DefaultJavaLibrary defaultJavaLibrary = new DefaultJavaLibrary(javaLibraryParams, pathResolver, ruleFinder, args.srcs, validateResources(pathResolver, params.getProjectFilesystem(), args.resources), javacOptions.getGeneratedSourceFolderName(), args.proguardConfig, args.postprocessClassesCommands, exportedDeps, resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, javaLibraryParams.getDeps()), javacOptions.trackClassUsage(), /* additionalClasspathEntries */
    ImmutableSet.of(), new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), args.resourcesRoot, args.manifestFile, args.mavenCoords, args.tests, javacOptions.getClassesToRemoveFromJar());
    if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
        return defaultJavaLibrary;
    } else {
        resolver.addToIndex(defaultJavaLibrary);
        return MavenUberJar.create(defaultJavaLibrary, Preconditions.checkNotNull(paramsWithMavenFlavor), args.mavenCoords, args.mavenPomTemplate);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) VersionPropagator(com.facebook.buck.versions.VersionPropagator) Flavored(com.facebook.buck.model.Flavored) HasTests(com.facebook.buck.model.HasTests) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Suppliers(com.google.common.base.Suppliers) ResourceValidator.validateResources(com.facebook.buck.jvm.common.ResourceValidator.validateResources) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) BuildRules(com.facebook.buck.rules.BuildRules) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) AetherUtil(com.facebook.buck.maven.AetherUtil) TargetGraph(com.facebook.buck.rules.TargetGraph) Collection(java.util.Collection) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Hint(com.facebook.buck.rules.Hint) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Description(com.facebook.buck.rules.Description) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) Flavor(com.facebook.buck.model.Flavor) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePath(com.facebook.buck.rules.SourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 45 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class HaskellDescriptionUtils method createLinkRule.

/**
   * Create a Haskell link rule that links the given inputs to a executable or shared library and
   * pulls in transitive native linkable deps from the given dep roots.
   */
public static HaskellLinkRule createLinkRule(BuildTarget target, BuildRuleParams baseParams, BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, HaskellConfig haskellConfig, Linker.LinkType linkType, ImmutableList<String> extraFlags, Iterable<Arg> linkerInputs, Iterable<? extends NativeLinkable> deps, Linker.LinkableDepType depType) throws NoSuchBuildTargetException {
    Tool linker = haskellConfig.getLinker().resolve(resolver);
    String name = target.getShortName();
    ImmutableList.Builder<Arg> linkerArgsBuilder = ImmutableList.builder();
    ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
    // Add the base flags from the `.buckconfig` first.
    argsBuilder.addAll(StringArg.from(haskellConfig.getLinkerFlags()));
    // Pass in the appropriate flags to link a shared library.
    if (linkType.equals(Linker.LinkType.SHARED)) {
        name = CxxDescriptionEnhancer.getSharedLibrarySoname(Optional.empty(), target.withFlavors(), cxxPlatform);
        argsBuilder.addAll(StringArg.from("-shared", "-dynamic"));
        argsBuilder.addAll(StringArg.from(MoreIterables.zipAndConcat(Iterables.cycle("-optl"), cxxPlatform.getLd().resolve(resolver).soname(name))));
    }
    // Add in extra flags passed into this function.
    argsBuilder.addAll(StringArg.from(extraFlags));
    // We pass in the linker inputs and all native linkable deps by prefixing with `-optl` so that
    // the args go straight to the linker, and preserve their order.
    linkerArgsBuilder.addAll(linkerInputs);
    for (NativeLinkable nativeLinkable : NativeLinkables.getNativeLinkables(cxxPlatform, deps, depType).values()) {
        linkerArgsBuilder.addAll(NativeLinkables.getNativeLinkableInput(cxxPlatform, depType, nativeLinkable).getArgs());
    }
    // Since we use `-optl` to pass all linker inputs directly to the linker, the haskell linker
    // will complain about not having any input files.  So, create a dummy archive with an empty
    // module and pass that in normally to work around this.
    BuildTarget emptyModuleTarget = target.withAppendedFlavors(InternalFlavor.of("empty-module"));
    WriteFile emptyModule = resolver.addToIndex(new WriteFile(baseParams.withBuildTarget(emptyModuleTarget), "module Unused where", BuildTargets.getGenPath(baseParams.getProjectFilesystem(), emptyModuleTarget, "%s/Unused.hs"), /* executable */
    false));
    HaskellCompileRule emptyCompiledModule = resolver.addToIndex(createCompileRule(target.withAppendedFlavors(InternalFlavor.of("empty-compiled-module")), baseParams, resolver, ruleFinder, // Buck dependency.
    baseParams.getDeps(), cxxPlatform, haskellConfig, depType, Optional.empty(), Optional.empty(), ImmutableList.of(), HaskellSources.builder().putModuleMap("Unused", emptyModule.getSourcePathToOutput()).build()));
    BuildTarget emptyArchiveTarget = target.withAppendedFlavors(InternalFlavor.of("empty-archive"));
    Archive emptyArchive = resolver.addToIndex(Archive.from(emptyArchiveTarget, baseParams, ruleFinder, cxxPlatform, Archive.Contents.NORMAL, BuildTargets.getGenPath(baseParams.getProjectFilesystem(), emptyArchiveTarget, "%s/libempty.a"), emptyCompiledModule.getObjects()));
    argsBuilder.add(SourcePathArg.of(emptyArchive.getSourcePathToOutput()));
    ImmutableList<Arg> args = argsBuilder.build();
    ImmutableList<Arg> linkerArgs = linkerArgsBuilder.build();
    return resolver.addToIndex(new HaskellLinkRule(baseParams.withBuildTarget(target).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(linker.getDeps(ruleFinder)).addAll(Stream.of(args, linkerArgs).flatMap(Collection::stream).flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator()).build()), Suppliers.ofInstance(ImmutableSortedSet.of())), linker, name, args, linkerArgs, haskellConfig.shouldCacheLinks()));
}
Also used : NativeLinkables(com.facebook.buck.cxx.NativeLinkables) Iterables(com.google.common.collect.Iterables) Linker(com.facebook.buck.cxx.Linker) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) Archive(com.facebook.buck.cxx.Archive) SourcePath(com.facebook.buck.rules.SourcePath) InternalFlavor(com.facebook.buck.model.InternalFlavor) ExplicitCxxToolFlags(com.facebook.buck.cxx.ExplicitCxxToolFlags) BuildRule(com.facebook.buck.rules.BuildRule) CxxSourceTypes(com.facebook.buck.cxx.CxxSourceTypes) AbstractBreadthFirstThrowingTraversal(com.facebook.buck.graph.AbstractBreadthFirstThrowingTraversal) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) CxxPlatforms(com.facebook.buck.cxx.CxxPlatforms) CxxPreprocessables(com.facebook.buck.cxx.CxxPreprocessables) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) StringArg(com.facebook.buck.rules.args.StringArg) Map(java.util.Map) CxxToolFlags(com.facebook.buck.cxx.CxxToolFlags) WriteFile(com.facebook.buck.file.WriteFile) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) Suppliers(com.google.common.base.Suppliers) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CxxSourceRuleFactory(com.facebook.buck.cxx.CxxSourceRuleFactory) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) CxxSource(com.facebook.buck.cxx.CxxSource) BuildTarget(com.facebook.buck.model.BuildTarget) Arg(com.facebook.buck.rules.args.Arg) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) PreprocessorFlags(com.facebook.buck.cxx.PreprocessorFlags) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargets(com.facebook.buck.model.BuildTargets) MoreIterables(com.facebook.buck.util.MoreIterables) WriteFile(com.facebook.buck.file.WriteFile) Archive(com.facebook.buck.cxx.Archive) ImmutableList(com.google.common.collect.ImmutableList) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) StringArg(com.facebook.buck.rules.args.StringArg) Arg(com.facebook.buck.rules.args.Arg) Collection(java.util.Collection) Tool(com.facebook.buck.rules.Tool)

Aggregations

ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)53 BuildTarget (com.facebook.buck.model.BuildTarget)26 BuildRule (com.facebook.buck.rules.BuildRule)26 Path (java.nio.file.Path)25 SourcePath (com.facebook.buck.rules.SourcePath)22 ImmutableList (com.google.common.collect.ImmutableList)22 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)21 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)19 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 Optional (java.util.Optional)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)15 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)14 Map (java.util.Map)13 Flavor (com.facebook.buck.model.Flavor)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 Preconditions (com.google.common.base.Preconditions)11 Suppliers (com.google.common.base.Suppliers)10 TargetGraph (com.facebook.buck.rules.TargetGraph)9