Search in sources :

Example 61 with ImmutableSet

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

the class AuditFlavorsCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ImmutableSet<BuildTarget> targets = getArgumentsFormattedAsBuildTargets(params.getBuckConfig()).stream().map(input -> BuildTargetParser.INSTANCE.parse(input, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver())).collect(MoreCollectors.toImmutableSet());
    if (targets.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Please specify at least one build target."));
        return 1;
    }
    ImmutableList.Builder<TargetNode<?, ?>> builder = ImmutableList.builder();
    try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
        for (BuildTarget target : targets) {
            TargetNode<?, ?> targetNode = params.getParser().getTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), target);
            builder.add(targetNode);
        }
    } catch (BuildFileParseException | BuildTargetException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
        return 1;
    }
    ImmutableList<TargetNode<?, ?>> targetNodes = builder.build();
    if (shouldGenerateJsonOutput()) {
        printJsonFlavors(targetNodes, params);
    } else {
        printFlavors(targetNodes, params);
    }
    return 0;
}
Also used : MoreExceptions(com.facebook.buck.util.MoreExceptions) RichStream(com.facebook.buck.util.RichStream) Flavored(com.facebook.buck.model.Flavored) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) FlavorDomain(com.facebook.buck.model.FlavorDomain) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSet(com.google.common.collect.ImmutableSet) TargetNode(com.facebook.buck.rules.TargetNode) BuildTargetException(com.facebook.buck.model.BuildTargetException) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) BuildTarget(com.facebook.buck.model.BuildTarget) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator) List(java.util.List) TreeMap(java.util.TreeMap) UserFlavor(com.facebook.buck.model.UserFlavor) Optional(java.util.Optional) SortedMap(java.util.SortedMap) Description(com.facebook.buck.rules.Description) BuildTargetException(com.facebook.buck.model.BuildTargetException) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableList(com.google.common.collect.ImmutableList) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 62 with ImmutableSet

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

the class NativeLinkables method getNativeLinkables.

/**
     * Extract from the dependency graph all the libraries which must be considered for linking.
     *
     * Traversal proceeds depending on whether each dependency is to be statically or dynamically
     * linked.
     *
     * @param linkStyle how dependencies should be linked, if their preferred_linkage is
     *                  {@code NativeLinkable.Linkage.ANY}.
     */
public static ImmutableMap<BuildTarget, NativeLinkable> getNativeLinkables(final CxxPlatform cxxPlatform, Iterable<? extends NativeLinkable> inputs, final Linker.LinkableDepType linkStyle, final Predicate<? super NativeLinkable> traverse) {
    final Map<BuildTarget, NativeLinkable> nativeLinkables = Maps.newHashMap();
    for (NativeLinkable nativeLinkable : inputs) {
        nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
    }
    final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
    AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(nativeLinkables.keySet()) {

        @Override
        public ImmutableSet<BuildTarget> visit(BuildTarget target) {
            NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
            graph.addNode(target);
            // We always traverse a rule's exported native linkables.
            Iterable<? extends NativeLinkable> nativeLinkableDeps = nativeLinkable.getNativeLinkableExportedDepsForPlatform(cxxPlatform);
            boolean shouldTraverse = true;
            switch(nativeLinkable.getPreferredLinkage(cxxPlatform)) {
                case ANY:
                    shouldTraverse = linkStyle != Linker.LinkableDepType.SHARED;
                    break;
                case SHARED:
                    shouldTraverse = false;
                    break;
                case STATIC:
                    shouldTraverse = true;
                    break;
            }
            // If we're linking this dependency statically, we also need to traverse its deps.
            if (shouldTraverse) {
                nativeLinkableDeps = Iterables.concat(nativeLinkableDeps, nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform));
            }
            // Process all the traversable deps.
            ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
            for (NativeLinkable dep : nativeLinkableDeps) {
                if (traverse.apply(dep)) {
                    BuildTarget depTarget = dep.getBuildTarget();
                    graph.addEdge(target, depTarget);
                    deps.add(depTarget);
                    nativeLinkables.put(depTarget, dep);
                }
            }
            return deps.build();
        }
    };
    visitor.start();
    // Topologically sort the rules.
    Iterable<BuildTarget> ordered = TopologicalSort.sort(graph).reverse();
    // Return a map of of the results.
    ImmutableMap.Builder<BuildTarget, NativeLinkable> result = ImmutableMap.builder();
    for (BuildTarget target : ordered) {
        result.put(target, nativeLinkables.get(target));
    }
    return result.build();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) AbstractBreadthFirstTraversal(com.facebook.buck.graph.AbstractBreadthFirstTraversal) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Example 63 with ImmutableSet

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

the class ExecutableFinder method getPaths.

private ImmutableSet<Path> getPaths(ImmutableMap<String, String> env) {
    ImmutableSet.Builder<Path> paths = ImmutableSet.builder();
    // Add the empty path so that when we iterate over it, we can check for the suffixed version of
    // a given path, be it absolute or not.
    paths.add(Paths.get(""));
    String pathEnv = env.get("PATH");
    if (pathEnv != null) {
        pathEnv = pathEnv.trim();
        paths.addAll(StreamSupport.stream(Splitter.on(pathSeparator).omitEmptyStrings().split(pathEnv).spliterator(), false).map(Paths::get).iterator());
    }
    if (platform == Platform.MACOS) {
        Path osXPaths = Paths.get("/etc/paths");
        if (Files.exists(osXPaths)) {
            try {
                paths.addAll(Files.readAllLines(osXPaths, Charset.defaultCharset()).stream().map(Paths::get).iterator());
            } catch (IOException e) {
                LOG.warn("Unable to read mac-specific paths. Skipping");
            }
        }
    }
    return paths.build();
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) Paths(java.nio.file.Paths) IOException(java.io.IOException)

Example 64 with ImmutableSet

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

the class JavaFileParser method extractFeaturesFromJavaCode.

public JavaFileFeatures extractFeaturesFromJavaCode(String code) {
    // For now, we will harcode this. Ultimately, we probably want to make this configurable via
    // .buckconfig. For example, the Buck project itself is diligent about disallowing wildcard
    // imports, but the one exception is the Java code generated via Thrift in src-gen.
    final boolean shouldThrowForUnsupportedWildcardImport = false;
    final AtomicBoolean isPoisonedByUnsupportedWildcardImport = new AtomicBoolean(false);
    final CompilationUnit compilationUnit = makeCompilationUnitFromSource(code);
    final ImmutableSortedSet.Builder<String> providedSymbols = ImmutableSortedSet.naturalOrder();
    final ImmutableSortedSet.Builder<String> requiredSymbols = ImmutableSortedSet.naturalOrder();
    final ImmutableSortedSet.Builder<String> exportedSymbols = ImmutableSortedSet.naturalOrder();
    final ImmutableSortedSet.Builder<String> requiredSymbolsFromExplicitImports = ImmutableSortedSet.naturalOrder();
    compilationUnit.accept(new ASTVisitor() {

        @Nullable
        private String packageName;

        /** Maps simple name to fully-qualified name. */
        private Map<String, String> simpleImportedTypes = new HashMap<>();

        /**
       * Maps wildcard import prefixes, such as {@code "java.util"} to the types in the respective
       * package if a wildcard import such as {@code import java.util.*} is used.
       */
        private Map<String, ImmutableSet<String>> wildcardImports = new HashMap<>();

        @Override
        public boolean visit(PackageDeclaration node) {
            Preconditions.checkState(packageName == null, "There should be at most one package declaration");
            packageName = node.getName().getFullyQualifiedName();
            return false;
        }

        // providedSymbols
        @Override
        public boolean visit(TypeDeclaration node) {
            // Local classes can be declared inside of methods. Skip over these.
            if (node.getParent() instanceof TypeDeclarationStatement) {
                return true;
            }
            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }
            @SuppressWarnings("unchecked") List<Type> interfaceTypes = node.superInterfaceTypes();
            for (Type interfaceType : interfaceTypes) {
                tryAddType(interfaceType, DependencyType.EXPORTED);
            }
            Type superclassType = node.getSuperclassType();
            if (superclassType != null) {
                tryAddType(superclassType, DependencyType.EXPORTED);
            }
            return true;
        }

        @Override
        public boolean visit(EnumDeclaration node) {
            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }
            return true;
        }

        @Override
        public boolean visit(AnnotationTypeDeclaration node) {
            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }
            return true;
        }

        // requiredSymbols
        /**
       * Uses heuristics to try to figure out what type of QualifiedName this is. Returns a non-null
       * value if this is believed to be a reference that qualifies as a "required symbol"
       * relationship.
       */
        @Override
        public boolean visit(QualifiedName node) {
            QualifiedName ancestor = findMostQualifiedAncestor(node);
            ASTNode parent = ancestor.getParent();
            if (!(parent instanceof PackageDeclaration) && !(parent instanceof ImportDeclaration)) {
                String symbol = ancestor.getFullyQualifiedName();
                // lookup.
                if (CharMatcher.javaUpperCase().matches(symbol.charAt(0))) {
                    addTypeFromDotDelimitedSequence(symbol, DependencyType.REQUIRED);
                }
            }
            return false;
        }

        /**
       * @param expr could be "Example", "Example.field", "com.example.Example". Note it could also
       *     be a built-in type, such as "java.lang.Integer", in which case it will not be added to
       *     the set of required symbols.
       */
        private void addTypeFromDotDelimitedSequence(String expr, DependencyType dependencyType) {
            // check it against JAVA_LANG_TYPES.
            if (startsWithUppercaseChar(expr)) {
                int index = expr.indexOf('.');
                if (index >= 0) {
                    String leftmostComponent = expr.substring(0, index);
                    if (JAVA_LANG_TYPES.contains(leftmostComponent)) {
                        return;
                    }
                }
            }
            expr = qualifyWithPackageNameIfNecessary(expr);
            addSymbol(expr, dependencyType);
        }

        @Override
        public boolean visit(ImportDeclaration node) {
            String fullyQualifiedName = node.getName().getFullyQualifiedName();
            // third-party code. As such, we will tolerate these for some of the common cases.
            if (node.isOnDemand()) {
                ImmutableSet<String> value = SUPPORTED_WILDCARD_IMPORTS.get(fullyQualifiedName);
                if (value != null) {
                    wildcardImports.put(fullyQualifiedName, value);
                    return false;
                } else if (shouldThrowForUnsupportedWildcardImport) {
                    throw new RuntimeException(String.format("Use of wildcard 'import %s.*' makes it impossible to statically determine " + "required symbols in this file. Please enumerate explicit imports.", fullyQualifiedName));
                } else {
                    isPoisonedByUnsupportedWildcardImport.set(true);
                    return false;
                }
            }
            // Only worry about the dependency on the enclosing type.
            Optional<String> simpleName = getSimpleNameFromFullyQualifiedName(fullyQualifiedName);
            if (simpleName.isPresent()) {
                String name = simpleName.get();
                int index = fullyQualifiedName.indexOf("." + name);
                String enclosingType = fullyQualifiedName.substring(0, index + name.length() + 1);
                requiredSymbolsFromExplicitImports.add(enclosingType);
                simpleImportedTypes.put(name, enclosingType);
            } else {
                LOG.warn("Suspicious import lacks obvious enclosing type: %s", fullyQualifiedName);
                // The one example we have seen of this in the wild is
                // "org.whispersystems.curve25519.java.curve_sigs". In practice, we still need to add it
                // as a required symbol in this case.
                requiredSymbols.add(fullyQualifiedName);
            }
            return false;
        }

        @Override
        public boolean visit(MethodInvocation node) {
            if (node.getExpression() == null) {
                return true;
            }
            String receiver = node.getExpression().toString();
            if (looksLikeAType(receiver)) {
                addTypeFromDotDelimitedSequence(receiver, DependencyType.REQUIRED);
            }
            return true;
        }

        /** An annotation on a member with zero arguments. */
        @Override
        public boolean visit(MarkerAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        /** An annotation on a member with named arguments. */
        @Override
        public boolean visit(NormalAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        /** An annotation on a member with a single, unnamed argument. */
        @Override
        public boolean visit(SingleMemberAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        private DependencyType findDependencyTypeForAnnotation(Annotation annotation) {
            ASTNode parentNode = annotation.getParent();
            if (parentNode == null) {
                return DependencyType.REQUIRED;
            }
            if (parentNode instanceof BodyDeclaration) {
                // Note that BodyDeclaration is an abstract class. Its subclasses are things like
                // FieldDeclaration and MethodDeclaration. We want to be sure that an annotation on any
                // non-private declaration is considered an exported symbol.
                BodyDeclaration declaration = (BodyDeclaration) parentNode;
                int modifiers = declaration.getModifiers();
                if ((modifiers & Modifier.PRIVATE) == 0) {
                    return DependencyType.EXPORTED;
                }
            }
            return DependencyType.REQUIRED;
        }

        @Override
        public boolean visit(SimpleType node) {
            // This method is responsible for finding the overwhelming majority of the required symbols
            // in the AST.
            tryAddType(node, DependencyType.REQUIRED);
            return true;
        }

        // exportedSymbols
        @Override
        public boolean visit(MethodDeclaration node) {
            // Types from private method signatures need not be exported.
            if ((node.getModifiers() & Modifier.PRIVATE) != 0) {
                return true;
            }
            Type returnType = node.getReturnType2();
            if (returnType != null) {
                tryAddType(returnType, DependencyType.EXPORTED);
            }
            @SuppressWarnings("unchecked") List<SingleVariableDeclaration> params = node.parameters();
            for (SingleVariableDeclaration decl : params) {
                tryAddType(decl.getType(), DependencyType.EXPORTED);
            }
            @SuppressWarnings("unchecked") List<Type> exceptions = node.thrownExceptionTypes();
            for (Type exception : exceptions) {
                tryAddType(exception, DependencyType.EXPORTED);
            }
            return true;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            // Types from private fields need not be exported.
            if ((node.getModifiers() & Modifier.PRIVATE) == 0) {
                tryAddType(node.getType(), DependencyType.EXPORTED);
            }
            return true;
        }

        private void tryAddType(Type type, DependencyType dependencyType) {
            if (type.isSimpleType()) {
                SimpleType simpleType = (SimpleType) type;
                Name simpleTypeName = simpleType.getName();
                String simpleName = simpleTypeName.toString();
                // rather than simply required.
                if (!CharMatcher.javaUpperCase().matchesAllOf(simpleName) || (dependencyType == DependencyType.EXPORTED && simpleImportedTypes.containsKey(simpleName))) {
                    addSimpleTypeName(simpleTypeName, dependencyType);
                }
            } else if (type.isArrayType()) {
                ArrayType arrayType = (ArrayType) type;
                tryAddType(arrayType.getElementType(), dependencyType);
            } else if (type.isParameterizedType()) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                tryAddType(parameterizedType.getType(), dependencyType);
                @SuppressWarnings("unchecked") List<Type> argTypes = parameterizedType.typeArguments();
                for (Type argType : argTypes) {
                    tryAddType(argType, dependencyType);
                }
            }
        }

        private void addSimpleTypeName(Name simpleTypeName, DependencyType dependencyType) {
            String simpleName = simpleTypeName.toString();
            if (JAVA_LANG_TYPES.contains(simpleName)) {
                return;
            }
            String fullyQualifiedNameForSimpleName = simpleImportedTypes.get(simpleName);
            if (fullyQualifiedNameForSimpleName != null) {
                // May need to promote from required to exported in this case.
                if (dependencyType == DependencyType.EXPORTED) {
                    addSymbol(fullyQualifiedNameForSimpleName, DependencyType.EXPORTED);
                }
                return;
            }
            // the iterator most of the time.
            if (!wildcardImports.isEmpty()) {
                for (Map.Entry<String, ImmutableSet<String>> entry : wildcardImports.entrySet()) {
                    Set<String> types = entry.getValue();
                    if (types.contains(simpleName)) {
                        String packageName = entry.getKey();
                        addSymbol(packageName + "." + simpleName, dependencyType);
                        return;
                    }
                }
            }
            String symbol = simpleTypeName.getFullyQualifiedName();
            symbol = qualifyWithPackageNameIfNecessary(symbol);
            addSymbol(symbol, dependencyType);
        }

        private void addSymbol(String symbol, DependencyType dependencyType) {
            ((dependencyType == DependencyType.REQUIRED) ? requiredSymbols : exportedSymbols).add(symbol);
        }

        private String qualifyWithPackageNameIfNecessary(String symbol) {
            if (!startsWithUppercaseChar(symbol)) {
                return symbol;
            }
            // If the symbol starts with a capital letter, then we assume that it is a reference to
            // a type in the same package.
            int index = symbol.indexOf('.');
            if (index >= 0) {
                symbol = symbol.substring(0, index);
            }
            if (packageName != null) {
                symbol = packageName + "." + symbol;
            }
            return symbol;
        }
    });
    // TODO(bolinfest): Special treatment for exportedSymbols when poisoned by wildcard import.
    ImmutableSortedSet<String> totalExportedSymbols = exportedSymbols.build();
    // If we were poisoned by an unsupported wildcard import, then we should rely exclusively on
    // the explicit imports to determine the required symbols.
    Set<String> totalRequiredSymbols = new HashSet<>();
    if (isPoisonedByUnsupportedWildcardImport.get()) {
        totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build());
    } else {
        totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build());
        totalRequiredSymbols.addAll(requiredSymbols.build());
    }
    // Make sure that required and exported symbols are disjoint sets.
    totalRequiredSymbols.removeAll(totalExportedSymbols);
    return new JavaFileFeatures(providedSymbols.build(), ImmutableSortedSet.copyOf(totalRequiredSymbols), totalExportedSymbols);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) HashMap(java.util.HashMap) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) List(java.util.List) LinkedList(java.util.LinkedList) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) HashSet(java.util.HashSet) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Optional(java.util.Optional) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Nullable(javax.annotation.Nullable)

Example 65 with ImmutableSet

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

the class Jsr199Javac method buildWithClasspath.

@Override
public int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, Optional<Path> workingDirectory, JavacOptions.AbiGenerationMode abiGenerationMode) {
    JavaCompiler compiler = createCompiler(context);
    CustomZipOutputStream jarOutputStream = null;
    StandardJavaFileManager fileManager = null;
    JavaInMemoryFileManager inMemoryFileManager = null;
    try {
        fileManager = compiler.getStandardFileManager(null, null, null);
        Supplier<ImmutableSet<String>> alreadyAddedFilesAvailableAfterCompilation = Suppliers.ofInstance(ImmutableSet.of());
        if (context.getDirectToJarOutputSettings().isPresent()) {
            Path path = context.getProjectFilesystem().getPathForRelativePath(context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath());
            jarOutputStream = ZipOutputStreams.newOutputStream(path, ZipOutputStreams.HandleDuplicates.APPEND_TO_ZIP);
            inMemoryFileManager = new JavaInMemoryFileManager(fileManager, path, jarOutputStream, context.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar());
            alreadyAddedFilesAvailableAfterCompilation = inMemoryFileManager::getEntries;
            fileManager = inMemoryFileManager;
        }
        Iterable<? extends JavaFileObject> compilationUnits;
        try {
            compilationUnits = createCompilationUnits(fileManager, context.getProjectFilesystem()::resolve, javaSourceFilePaths);
        } catch (IOException e) {
            LOG.warn(e, "Error building compilation units");
            return 1;
        }
        try {
            int result = buildWithClasspath(context, invokingRule, options, annotationProcessors, javaSourceFilePaths, pathToSrcsList, compiler, fileManager, compilationUnits, abiGenerationMode);
            if (result != 0 || !context.getDirectToJarOutputSettings().isPresent()) {
                return result;
            }
            return JarDirectoryStepHelper.createJarFile(context.getProjectFilesystem(), context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath(), Preconditions.checkNotNull(jarOutputStream), context.getDirectToJarOutputSettings().get().getEntriesToJar(), alreadyAddedFilesAvailableAfterCompilation.get(), context.getDirectToJarOutputSettings().get().getMainClass(), context.getDirectToJarOutputSettings().get().getManifestFile(), /* mergeManifests */
            true, /* blacklist */
            ImmutableSet.of(), context.getEventSink(), context.getStdErr());
        } finally {
            close(compilationUnits);
        }
    } catch (IOException e) {
        LOG.warn(e, "Unable to create jarOutputStream");
    } finally {
        closeResources(fileManager, inMemoryFileManager, jarOutputStream);
    }
    return 1;
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) IOException(java.io.IOException)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)212 Path (java.nio.file.Path)66 BuildTarget (com.facebook.buck.model.BuildTarget)58 ImmutableList (com.google.common.collect.ImmutableList)48 ImmutableMap (com.google.common.collect.ImmutableMap)48 IOException (java.io.IOException)43 Optional (java.util.Optional)39 Test (org.junit.Test)35 SourcePath (com.facebook.buck.rules.SourcePath)31 Set (java.util.Set)31 Map (java.util.Map)30 TargetNode (com.facebook.buck.rules.TargetNode)28 List (java.util.List)28 BuildRule (com.facebook.buck.rules.BuildRule)26 HumanReadableException (com.facebook.buck.util.HumanReadableException)23 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)22 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)21 HashMap (java.util.HashMap)21 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)20 VisibleForTesting (com.google.common.annotations.VisibleForTesting)20