Search in sources :

Example 11 with ImmutableSet

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

the class TargetsCommand method getDependentNodes.

/**
   * @param graph A graph used to resolve dependencies between targets.
   * @param nodes A set of nodes.
   * @param detectTestChanges If true, tests are considered to be dependencies of the targets they
   *                          are testing.
   * @return A set of all nodes that transitively depend on {@code nodes}
   * (a superset of {@code nodes}).
   */
private static ImmutableSet<TargetNode<?, ?>> getDependentNodes(final TargetGraph graph, ImmutableSet<TargetNode<?, ?>> nodes, boolean detectTestChanges) {
    ImmutableMultimap.Builder<TargetNode<?, ?>, TargetNode<?, ?>> extraEdgesBuilder = ImmutableMultimap.builder();
    if (detectTestChanges) {
        for (TargetNode<?, ?> node : graph.getNodes()) {
            if (node.getConstructorArg() instanceof HasTests) {
                ImmutableSortedSet<BuildTarget> tests = ((HasTests) node.getConstructorArg()).getTests();
                for (BuildTarget testTarget : tests) {
                    Optional<TargetNode<?, ?>> testNode = graph.getOptional(testTarget);
                    if (!testNode.isPresent()) {
                        throw new HumanReadableException("'%s' (test of '%s') is not in the target graph.", testTarget, node);
                    }
                    extraEdgesBuilder.put(testNode.get(), node);
                }
            }
        }
    }
    final ImmutableMultimap<TargetNode<?, ?>, TargetNode<?, ?>> extraEdges = extraEdgesBuilder.build();
    final ImmutableSet.Builder<TargetNode<?, ?>> builder = ImmutableSet.builder();
    AbstractBreadthFirstTraversal<TargetNode<?, ?>> traversal = new AbstractBreadthFirstTraversal<TargetNode<?, ?>>(nodes) {

        @Override
        public ImmutableSet<TargetNode<?, ?>> visit(TargetNode<?, ?> targetNode) {
            builder.add(targetNode);
            return FluentIterable.from(graph.getIncomingNodesFor(targetNode)).append(extraEdges.get(targetNode)).toSet();
        }
    };
    traversal.start();
    return builder.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) HasTests(com.facebook.buck.model.HasTests) AbstractBreadthFirstTraversal(com.facebook.buck.graph.AbstractBreadthFirstTraversal) ImmutableMultimap(com.google.common.collect.ImmutableMultimap)

Example 12 with ImmutableSet

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

the class PathHashing method hashPath.

public static ImmutableSet<Path> hashPath(Hasher hasher, ProjectFileHashLoader fileHashLoader, ProjectFilesystem projectFilesystem, Path root) throws IOException {
    Preconditions.checkArgument(!root.equals(EMPTY_PATH), "Path to hash (%s) must not be empty", root);
    ImmutableSet.Builder<Path> children = ImmutableSet.builder();
    for (Path path : ImmutableSortedSet.copyOf(projectFilesystem.getFilesUnderPath(root))) {
        StringHashing.hashStringAndLength(hasher, MorePaths.pathWithUnixSeparators(path));
        if (!root.equals(path)) {
            children.add(root.relativize(path));
        }
        hasher.putBytes(fileHashLoader.get(path).asBytes());
    }
    return children.build();
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 13 with ImmutableSet

use of com.google.common.collect.ImmutableSet 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 14 with ImmutableSet

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

the class DefaultSuggestBuildRules method isMissingBuildRule.

/**
   *  @param transitiveNotDeclaredRule A {@link BuildRule} that is contained in the transitive
   *      dependency list but is not declared as a dependency.
   *  @param failedImports A Set of remaining failed imports.  This function will mutate this set
   *      and remove any imports satisfied by {@code transitiveNotDeclaredDep}.
   *  @return whether or not adding {@code transitiveNotDeclaredDep} as a dependency to this build
   *      rule would have satisfied one of the {@code failedImports}.
   */
private boolean isMissingBuildRule(BuildRule transitiveNotDeclaredRule, Set<String> failedImports, JarResolver jarResolver) {
    if (!(transitiveNotDeclaredRule instanceof JavaLibrary)) {
        return false;
    }
    ImmutableSet<Path> classPaths = ((JavaLibrary) transitiveNotDeclaredRule).getOutputClasspaths().stream().map(c -> pathResolver.getAbsolutePath(c)).collect(MoreCollectors.toImmutableSet());
    boolean containsMissingBuildRule = false;
    // classpath.
    for (Path classPath : classPaths) {
        ImmutableSet<String> topLevelSymbols = jarResolver.resolve(classPath);
        for (String symbolName : topLevelSymbols) {
            if (failedImports.contains(symbolName)) {
                failedImports.remove(symbolName);
                containsMissingBuildRule = true;
                // If we've found all of the missing imports, bail out early.
                if (failedImports.isEmpty()) {
                    return true;
                }
            }
        }
    }
    return containsMissingBuildRule;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Supplier(com.google.common.base.Supplier) Set(java.util.Set) BuildRuleDependencyVisitors(com.facebook.buck.rules.BuildRuleDependencyVisitors) Sets(com.google.common.collect.Sets) BuildRule(com.facebook.buck.rules.BuildRule) DirectedAcyclicGraph(com.facebook.buck.graph.DirectedAcyclicGraph) SuggestBuildRules(com.facebook.buck.jvm.core.SuggestBuildRules) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Suppliers(com.google.common.base.Suppliers) TopologicalSort(com.facebook.buck.graph.TopologicalSort) Path(java.nio.file.Path) MoreCollectors(com.facebook.buck.util.MoreCollectors) Path(java.nio.file.Path)

Example 15 with ImmutableSet

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

the class JUnitStep method getTimeoutHandler.

@Override
protected Optional<Consumer<Process>> getTimeoutHandler(final ExecutionContext context) {
    return Optional.of(process -> {
        Optional<Long> pid = Optional.empty();
        Platform platform = context.getPlatform();
        try {
            switch(platform) {
                case LINUX:
                case FREEBSD:
                case MACOS:
                    {
                        Field field = process.getClass().getDeclaredField("pid");
                        field.setAccessible(true);
                        try {
                            pid = Optional.of((long) field.getInt(process));
                        } catch (IllegalAccessException e) {
                            LOG.error(e, "Failed to access `pid`.");
                        }
                        break;
                    }
                case WINDOWS:
                    {
                        Field field = process.getClass().getDeclaredField("handle");
                        field.setAccessible(true);
                        try {
                            pid = Optional.of(field.getLong(process));
                        } catch (IllegalAccessException e) {
                            LOG.error(e, "Failed to access `handle`.");
                        }
                        break;
                    }
                case UNKNOWN:
                    LOG.info("Unknown platform; unable to obtain the process id!");
                    break;
            }
        } catch (NoSuchFieldException e) {
            LOG.error(e);
        }
        Optional<Path> jstack = new ExecutableFinder(context.getPlatform()).getOptionalExecutable(Paths.get("jstack"), context.getEnvironment());
        if (!pid.isPresent() || !jstack.isPresent()) {
            LOG.info("Unable to print a stack trace for timed out test!");
            return;
        }
        context.getStdErr().println("Test has timed out!  Here is a trace of what it is currently doing:");
        try {
            context.getProcessExecutor().launchAndExecute(ProcessExecutorParams.builder().addCommand(jstack.get().toString(), "-l", pid.get().toString()).setEnvironment(context.getEnvironment()).build(), ImmutableSet.<ProcessExecutor.Option>builder().add(ProcessExecutor.Option.PRINT_STD_OUT).add(ProcessExecutor.Option.PRINT_STD_ERR).build(), Optional.empty(), Optional.of(TimeUnit.SECONDS.toMillis(30)), Optional.of(input -> {
                context.getStdErr().print("Printing the stack took longer than 30 seconds. No longer trying.");
            }));
        } catch (Exception e) {
            LOG.error(e);
        }
    });
}
Also used : Path(java.nio.file.Path) Logger(com.facebook.buck.log.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Platform(com.facebook.buck.util.environment.Platform) ExecutableFinder(com.facebook.buck.io.ExecutableFinder) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) Field(java.lang.reflect.Field) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ExecutionContext(com.facebook.buck.step.ExecutionContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ImmutableList(com.google.common.collect.ImmutableList) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Paths(java.nio.file.Paths) Map(java.util.Map) Optional(java.util.Optional) ShellStep(com.facebook.buck.shell.ShellStep) Path(java.nio.file.Path) Field(java.lang.reflect.Field) ExecutableFinder(com.facebook.buck.io.ExecutableFinder) Platform(com.facebook.buck.util.environment.Platform)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)196 Path (java.nio.file.Path)65 BuildTarget (com.facebook.buck.model.BuildTarget)58 ImmutableMap (com.google.common.collect.ImmutableMap)44 ImmutableList (com.google.common.collect.ImmutableList)43 IOException (java.io.IOException)42 Optional (java.util.Optional)37 Test (org.junit.Test)32 SourcePath (com.facebook.buck.rules.SourcePath)31 TargetNode (com.facebook.buck.rules.TargetNode)28 BuildRule (com.facebook.buck.rules.BuildRule)26 Map (java.util.Map)26 List (java.util.List)25 Set (java.util.Set)25 HumanReadableException (com.facebook.buck.util.HumanReadableException)23 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)22 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)21 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)20 TargetGraph (com.facebook.buck.rules.TargetGraph)19 VisibleForTesting (com.google.common.annotations.VisibleForTesting)19