Example usage for com.google.common.collect ImmutableSortedSet.Builder addAll

List of usage examples for com.google.common.collect ImmutableSortedSet.Builder addAll

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedSet.Builder addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Adds all of the elements in the specified collection to this set if they're not already present (optional operation).

Usage

From source file:com.facebook.buck.apple.AppleLibraryDescriptionSwiftEnhancer.java

@SuppressWarnings("unused")
public static BuildRule createSwiftCompileRule(BuildTarget target, CellPathResolver cellRoots,
        ActionGraphBuilder graphBuilder, SourcePathRuleFinder ruleFinder, BuildRuleParams params,
        AppleNativeTargetDescriptionArg args, ProjectFilesystem filesystem, CxxPlatform platform,
        AppleCxxPlatform applePlatform, SwiftBuckConfig swiftBuckConfig,
        ImmutableSet<CxxPreprocessorInput> inputs) {

    SourcePathRuleFinder rulePathFinder = new SourcePathRuleFinder(graphBuilder);
    SwiftLibraryDescriptionArg.Builder delegateArgsBuilder = SwiftLibraryDescriptionArg.builder();
    SwiftDescriptions.populateSwiftLibraryDescriptionArg(swiftBuckConfig,
            DefaultSourcePathResolver.from(rulePathFinder), delegateArgsBuilder, args, target);
    SwiftLibraryDescriptionArg swiftArgs = delegateArgsBuilder.build();

    Preprocessor preprocessor = platform.getCpp().resolve(graphBuilder);

    ImmutableSet<BuildRule> inputDeps = RichStream.from(inputs)
            .flatMap(input -> RichStream.from(input.getDeps(graphBuilder, rulePathFinder))).toImmutableSet();

    ImmutableSortedSet.Builder<BuildRule> sortedDeps = ImmutableSortedSet.naturalOrder();
    sortedDeps.addAll(inputDeps);

    BuildRuleParams paramsWithDeps = params.withExtraDeps(sortedDeps.build());

    PreprocessorFlags.Builder flagsBuilder = PreprocessorFlags.builder();
    inputs.forEach(input -> flagsBuilder.addAllIncludes(input.getIncludes()));
    inputs.forEach(input -> flagsBuilder.addAllFrameworkPaths(input.getFrameworks()));
    PreprocessorFlags preprocessorFlags = flagsBuilder.build();

    Optional<CxxPreprocessorInput> underlyingModule = AppleLibraryDescription
            .underlyingModuleCxxPreprocessorInput(target, graphBuilder, platform);

    return SwiftLibraryDescription.createSwiftCompileRule(platform, applePlatform.getSwiftPlatform().get(),
            swiftBuckConfig, target, paramsWithDeps, graphBuilder, rulePathFinder, cellRoots, filesystem,
            swiftArgs, preprocessor, preprocessorFlags, underlyingModule.isPresent());
}

From source file:com.facebook.buck.apple.AppleDebuggableBinary.java

public static ImmutableSortedSet<BuildRule> getRequiredRuntimeDeps(AppleDebugFormat debugFormat,
        BuildRule strippedBinaryRule, ProvidesLinkedBinaryDeps unstrippedBinaryRule,
        Optional<AppleDsym> appleDsym) {
    if (debugFormat == AppleDebugFormat.NONE) {
        return ImmutableSortedSet.of(strippedBinaryRule);
    }/*from  w w  w .  j  av a  2s.c o m*/
    ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
    if (debugFormat == AppleDebugFormat.DWARF) {
        builder.add(unstrippedBinaryRule);
        builder.addAll(unstrippedBinaryRule.getCompileDeps());
        builder.addAll(unstrippedBinaryRule.getStaticLibraryDeps());
    } else if (debugFormat == AppleDebugFormat.DWARF_AND_DSYM) {
        Preconditions.checkArgument(appleDsym.isPresent(),
                "debugFormat %s expects AppleDsym rule to be present", AppleDebugFormat.DWARF_AND_DSYM);
        builder.add(strippedBinaryRule);
        builder.add(appleDsym.get());
    }
    return builder.build();
}

From source file:com.facebook.buck.d.DDescriptionUtils.java

/**
 * Ensures that a DCompileBuildRule exists for the given target, creating a DCompileBuildRule
 * if neccesary.//w ww .j  a v  a 2s  .  co  m
 * @param baseParams build parameters for the rule
 * @param buildRuleResolver BuildRuleResolver the rule should be in
 * @param sourcePathResolver used to resolve source paths
 * @param src the source file to be compiled
 * @param compilerFlags flags to pass to the compiler
 * @param compileTarget the target the rule should be for
 * @param dBuckConfig the Buck configuration for D
 * @return the build rule
 */
public static DCompileBuildRule requireBuildRule(BuildTarget compileTarget, BuildRuleParams baseParams,
        BuildRuleResolver buildRuleResolver, SourcePathResolver sourcePathResolver,
        SourcePathRuleFinder ruleFinder, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags,
        String name, SourcePath src, DIncludes includes) throws NoSuchBuildTargetException {
    Optional<BuildRule> existingRule = buildRuleResolver.getRuleOptional(compileTarget);
    if (existingRule.isPresent()) {
        return (DCompileBuildRule) existingRule.get();
    } else {
        Tool compiler = dBuckConfig.getDCompiler();

        Map<BuildTarget, DIncludes> transitiveIncludes = new TreeMap<>();
        transitiveIncludes.put(baseParams.getBuildTarget(), includes);
        for (Map.Entry<BuildTarget, DLibrary> library : getTransitiveDLibraryRules(baseParams.getDeps())
                .entrySet()) {
            transitiveIncludes.put(library.getKey(), library.getValue().getIncludes());
        }

        ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
        depsBuilder.addAll(compiler.getDeps(ruleFinder));
        depsBuilder.addAll(ruleFinder.filterBuildRuleInputs(src));
        for (DIncludes dIncludes : transitiveIncludes.values()) {
            depsBuilder.addAll(dIncludes.getDeps(ruleFinder));
        }
        ImmutableSortedSet<BuildRule> deps = depsBuilder.build();

        return buildRuleResolver
                .addToIndex(
                        new DCompileBuildRule(
                                baseParams.copyWithChanges(compileTarget, Suppliers.ofInstance(deps),
                                        Suppliers.ofInstance(ImmutableSortedSet.of())),
                                sourcePathResolver, compiler,
                                ImmutableList.<String>builder().addAll(dBuckConfig.getBaseCompilerFlags())
                                        .addAll(compilerFlags).build(),
                                name, ImmutableSortedSet.of(src),
                                ImmutableList.copyOf(transitiveIncludes.values())));
    }
}

From source file:com.facebook.buck.features.d.DDescriptionUtils.java

/**
 * Ensures that a DCompileBuildRule exists for the given target, creating a DCompileBuildRule if
 * neccesary./*from  w w w. j  a  v  a 2  s.  c  o  m*/
 *
 * @param baseParams build parameters for the rule
 * @param graphBuilder BuildRuleResolver the rule should be in
 * @param src the source file to be compiled
 * @param compilerFlags flags to pass to the compiler
 * @param compileTarget the target the rule should be for
 * @param dBuckConfig the Buck configuration for D
 * @return the build rule
 */
public static DCompileBuildRule requireBuildRule(BuildTarget compileTarget, BuildTarget baseBuildTarget,
        ProjectFilesystem projectFilesystem, BuildRuleParams baseParams, ActionGraphBuilder graphBuilder,
        SourcePathRuleFinder ruleFinder, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags,
        String name, SourcePath src, DIncludes includes) {
    return (DCompileBuildRule) graphBuilder.computeIfAbsent(compileTarget, ignored -> {
        Tool compiler = dBuckConfig.getDCompiler();

        Map<BuildTarget, DIncludes> transitiveIncludes = new TreeMap<>();
        transitiveIncludes.put(baseBuildTarget, includes);
        for (Map.Entry<BuildTarget, DLibrary> library : getTransitiveDLibraryRules(baseParams.getBuildDeps())
                .entrySet()) {
            transitiveIncludes.put(library.getKey(), library.getValue().getIncludes());
        }

        ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
        depsBuilder.addAll(BuildableSupport.getDepsCollection(compiler, ruleFinder));
        depsBuilder.addAll(ruleFinder.filterBuildRuleInputs(src));
        for (DIncludes dIncludes : transitiveIncludes.values()) {
            depsBuilder.addAll(dIncludes.getDeps(ruleFinder));
        }
        ImmutableSortedSet<BuildRule> deps = depsBuilder.build();

        return new DCompileBuildRule(compileTarget, projectFilesystem,
                baseParams.withDeclaredDeps(deps).withoutExtraDeps(), compiler,
                ImmutableList.<String>builder().addAll(dBuckConfig.getBaseCompilerFlags()).addAll(compilerFlags)
                        .build(),
                name, ImmutableSortedSet.of(src), ImmutableList.copyOf(transitiveIncludes.values()));
    });
}

From source file:com.facebook.buck.apple.AppleBuildRules.java

static void addDirectAndExportedDeps(TargetGraph targetGraph, TargetNode<?, ?> targetNode,
        ImmutableSortedSet.Builder<TargetNode<?, ?>> directDepsBuilder,
        ImmutableSortedSet.Builder<TargetNode<?, ?>> exportedDepsBuilder) {
    directDepsBuilder.addAll(targetGraph.getAll(targetNode.getDepsStream()::iterator));
    if (targetNode.getDescription() instanceof AppleLibraryDescription
            || targetNode.getDescription() instanceof CxxLibraryDescription) {
        CxxLibraryDescription.Arg arg = (CxxLibraryDescription.Arg) targetNode.getConstructorArg();
        LOG.verbose("Exported deps of node %s: %s", targetNode, arg.exportedDeps);
        Iterable<TargetNode<?, ?>> exportedNodes = targetGraph.getAll(arg.exportedDeps);
        directDepsBuilder.addAll(exportedNodes);
        exportedDepsBuilder.addAll(exportedNodes);
    }//  w w  w .  j  a  v  a 2s .  c  o m
}

From source file:com.facebook.buck.cxx.CxxCompilationDatabase.java

public static CxxCompilationDatabase createCompilationDatabase(BuildRuleParams params,
        SourcePathResolver pathResolver, Iterable<CxxPreprocessAndCompile> compileAndPreprocessRules) {
    ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
    ImmutableSortedSet.Builder<CxxPreprocessAndCompile> compileRules = ImmutableSortedSet.naturalOrder();
    for (CxxPreprocessAndCompile compileRule : compileAndPreprocessRules) {
        compileRules.add(compileRule);/*  ww w . j  a va2s .com*/
        deps.addAll(compileRule.getDeps());
    }

    return new CxxCompilationDatabase(
            params.copyWithDeps(Suppliers.ofInstance(ImmutableSortedSet.of()),
                    Suppliers.ofInstance(ImmutableSortedSet.of())),
            pathResolver, compileRules.build(), deps.build());
}

From source file:com.facebook.buck.rules.Buildables.java

/**
 * Helper function for {@link Buildable}s to create their lists of files for caching.
 *//*w w  w  .  j  a  v a 2  s  . c om*/
public static void addInputsToSortedSet(@Nullable Path pathToDirectory,
        ImmutableSortedSet.Builder<Path> inputsToConsiderForCachingPurposes, DirectoryTraverser traverser) {
    if (pathToDirectory == null) {
        return;
    }

    Set<Path> files;
    try {
        files = DirectoryTraversers.getInstance().findFiles(pathToDirectory.toString(), traverser);
    } catch (IOException e) {
        throw new RuntimeException("Exception while traversing " + pathToDirectory + ".", e);
    }

    inputsToConsiderForCachingPurposes.addAll(files);
}

From source file:com.google.devtools.build.lib.rules.android.ProguardHelper.java

/**
 * Retrieves the full set of proguard specs that should be applied to this binary, including the
 * specs passed in, if Proguard should run on the given rule.  {@link #createProguardAction}
 * relies on this method returning an empty list if the given rule doesn't declare specs in
 * --java_optimization_mode=legacy.//from  w w w .  j  ava  2s  .  c  om
 *
 * <p>If Proguard shouldn't be applied, or the legacy link mode is used and there are no
 * proguard_specs on this rule, an empty list will be returned, regardless of any given specs or
 * specs from dependencies.  {@link AndroidBinary#createAndroidBinary} relies on that behavior.
 */
public static ImmutableList<Artifact> collectTransitiveProguardSpecs(RuleContext ruleContext,
        Artifact... specsToInclude) {
    JavaOptimizationMode optMode = getJavaOptimizationMode(ruleContext);
    if (optMode == JavaOptimizationMode.NOOP) {
        return ImmutableList.of();
    }

    ImmutableList<Artifact> proguardSpecs = ruleContext.attributes().has(PROGUARD_SPECS, BuildType.LABEL_LIST)
            ? ruleContext.getPrerequisiteArtifacts(PROGUARD_SPECS, Mode.TARGET).list()
            : ImmutableList.<Artifact>of();
    if (optMode == JavaOptimizationMode.LEGACY && proguardSpecs.isEmpty()) {
        return ImmutableList.of();
    }

    // TODO(bazel-team): In modes except LEGACY verify that proguard specs don't include -dont...
    // flags since those flags would override the desired optMode
    ImmutableSortedSet.Builder<Artifact> builder = ImmutableSortedSet.orderedBy(Artifact.EXEC_PATH_COMPARATOR)
            .addAll(proguardSpecs).add(specsToInclude)
            .addAll(ruleContext.getPrerequisiteArtifacts(":extra_proguard_specs", Mode.TARGET).list());
    for (ProguardSpecProvider dep : ruleContext.getPrerequisites("deps", Mode.TARGET,
            ProguardSpecProvider.class)) {
        builder.addAll(dep.getTransitiveProguardSpecs());
    }

    // Generate and include implicit Proguard spec for requested mode.
    if (!optMode.getImplicitProguardDirectives().isEmpty()) {
        Artifact implicitDirectives = getProguardConfigArtifact(ruleContext, optMode.name().toLowerCase());
        ruleContext.registerAction(new FileWriteAction(ruleContext.getActionOwner(), implicitDirectives,
                optMode.getImplicitProguardDirectives(), /*executable*/ false));
        builder.add(implicitDirectives);
    }

    return builder.build().asList();
}

From source file:com.facebook.buck.cli.Config.java

private static ImmutableSortedSet<Path> listFiles(Path root) throws IOException {
    if (!Files.isDirectory(root)) {
        return ImmutableSortedSet.of();
    }// ww  w .java2s.c o  m

    ImmutableSortedSet.Builder<Path> toReturn = ImmutableSortedSet.naturalOrder();

    try (DirectoryStream<Path> directory = Files.newDirectoryStream(root)) {
        toReturn.addAll(directory.iterator());
    }

    return toReturn.build();
}

From source file:org.dishevelled.collect.Sets.java

/**
  * Create and return an immutable sorted set containing the unique elements in the specified iterator
  * sorted according to the specified comparator. The returned immutable sorted set is a high-performance,
  * immutable <code>SortedSet</code> that stores its elements in a sorted array.
  *//w  ww .j ava  2  s . co  m
  * @param <T> element type
  * @param iterator iterator, must not be null
  * @param comparator comparator to be used to sort the returned immutable sorted set
  * @return an immutable set containing the unique elements returned by the specified iterator
  *    sorted according to the specified comparator
  */
public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(
        final Iterator<? extends T> iterator, final Comparator<? super T> comparator) {
    if (iterator == null) {
        throw new IllegalArgumentException("iterator must not be null");
    }
    if (comparator == null) {
        return asImmutableSortedSet(iterator);
    }
    ImmutableSortedSet.Builder<T> builder = new ImmutableSortedSet.Builder<T>(comparator);
    builder.addAll(iterator);
    return builder.build();
}