Search in sources :

Example 36 with Optional

use of java.util.Optional in project buck by facebook.

the class MiniAapt method resourceUnion.

/**
   * Collect resource information from R.txt for each dep and perform a resource union.
   * @throws IOException
   */
public void resourceUnion() throws IOException {
    for (Path depRTxt : pathsToSymbolsOfDeps) {
        Iterable<String> lines = FluentIterable.from(filesystem.readLines(depRTxt)).filter(input -> !Strings.isNullOrEmpty(input)).toList();
        for (String line : lines) {
            Optional<RDotTxtEntry> entry = RDotTxtEntry.parse(line);
            Preconditions.checkState(entry.isPresent());
            resourceCollector.addResourceIfNotPresent(entry.get());
        }
    }
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BuckEventBus(com.facebook.buck.event.BuckEventBus) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Step(com.facebook.buck.step.Step) XPathConstants(javax.xml.xpath.XPathConstants) SourcePath(com.facebook.buck.rules.SourcePath) XmlDomParser(com.facebook.buck.util.XmlDomParser) IdType(com.facebook.buck.android.aapt.RDotTxtEntry.IdType) XPathExpression(javax.xml.xpath.XPathExpression) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ExecutionContext(com.facebook.buck.step.ExecutionContext) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PrintWriter(java.io.PrintWriter) ImmutableSet(com.google.common.collect.ImmutableSet) NodeList(org.w3c.dom.NodeList) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) Element(org.w3c.dom.Element) Ordering(com.google.common.collect.Ordering) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AaptStep(com.facebook.buck.android.AaptStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream)

Example 37 with Optional

use of java.util.Optional in project buck by facebook.

the class MiniAapt method verifyReferences.

@VisibleForTesting
ImmutableSet<RDotTxtEntry> verifyReferences(ProjectFilesystem filesystem, ImmutableSet<RDotTxtEntry> references) throws IOException {
    ImmutableSet.Builder<RDotTxtEntry> unresolved = ImmutableSet.builder();
    ImmutableSet.Builder<RDotTxtEntry> definitionsBuilder = ImmutableSet.builder();
    definitionsBuilder.addAll(resourceCollector.getResources());
    for (Path depRTxt : pathsToSymbolsOfDeps) {
        Iterable<String> lines = FluentIterable.from(filesystem.readLines(depRTxt)).filter(input -> !Strings.isNullOrEmpty(input)).toList();
        for (String line : lines) {
            Optional<RDotTxtEntry> entry = RDotTxtEntry.parse(line);
            Preconditions.checkState(entry.isPresent());
            definitionsBuilder.add(entry.get());
        }
    }
    Set<RDotTxtEntry> definitions = definitionsBuilder.build();
    for (RDotTxtEntry reference : references) {
        if (!definitions.contains(reference)) {
            unresolved.add(reference);
        }
    }
    return unresolved.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BuckEventBus(com.facebook.buck.event.BuckEventBus) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Step(com.facebook.buck.step.Step) XPathConstants(javax.xml.xpath.XPathConstants) SourcePath(com.facebook.buck.rules.SourcePath) XmlDomParser(com.facebook.buck.util.XmlDomParser) IdType(com.facebook.buck.android.aapt.RDotTxtEntry.IdType) XPathExpression(javax.xml.xpath.XPathExpression) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ExecutionContext(com.facebook.buck.step.ExecutionContext) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PrintWriter(java.io.PrintWriter) ImmutableSet(com.google.common.collect.ImmutableSet) NodeList(org.w3c.dom.NodeList) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) Element(org.w3c.dom.Element) Ordering(com.google.common.collect.Ordering) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AaptStep(com.facebook.buck.android.AaptStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) ImmutableSet(com.google.common.collect.ImmutableSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 38 with Optional

use of java.util.Optional in project buck by facebook.

the class CxxLibraryDescription method createMetadata.

@Override
public <A extends Arg, U> Optional<U> createMetadata(BuildTarget buildTarget, BuildRuleResolver resolver, A args, Optional<ImmutableMap<BuildTarget, Version>> selectedVersions, final Class<U> metadataClass) throws NoSuchBuildTargetException {
    Map.Entry<Flavor, MetadataType> type = METADATA_TYPE.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
    BuildTarget baseTarget = buildTarget.withoutFlavors(type.getKey());
    switch(type.getValue()) {
        case CXX_HEADERS:
            {
                Optional<CxxHeaders> symlinkTree = Optional.empty();
                if (!args.exportedHeaders.isEmpty()) {
                    CxxPreprocessables.HeaderMode mode = HEADER_MODE.getRequiredValue(buildTarget);
                    baseTarget = baseTarget.withoutFlavors(mode.getFlavor());
                    symlinkTree = Optional.of(CxxSymlinkTreeHeaders.from((HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(Type.EXPORTED_HEADERS.getFlavor(), mode.getFlavor())), CxxPreprocessables.IncludeType.LOCAL));
                }
                return symlinkTree.map(metadataClass::cast);
            }
        case CXX_PREPROCESSOR_INPUT:
            {
                Map.Entry<Flavor, CxxPlatform> platform = cxxPlatforms.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
                Map.Entry<Flavor, HeaderVisibility> visibility = HEADER_VISIBILITY.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
                baseTarget = baseTarget.withoutFlavors(platform.getKey(), visibility.getKey());
                CxxPreprocessorInput.Builder cxxPreprocessorInputBuilder = CxxPreprocessorInput.builder();
                // TODO(andrewjcg): We currently always add exported flags and frameworks to the
                // preprocessor input to mimic existing behavior, but this should likely be fixed.
                cxxPreprocessorInputBuilder.putAllPreprocessorFlags(CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags, args.exportedPlatformPreprocessorFlags, args.exportedLangPreprocessorFlags, platform.getValue()));
                cxxPreprocessorInputBuilder.addAllFrameworks(args.frameworks);
                if (visibility.getValue() == HeaderVisibility.PRIVATE && !args.headers.isEmpty()) {
                    HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.HEADERS.getFlavor()));
                    cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
                }
                if (visibility.getValue() == HeaderVisibility.PUBLIC) {
                    // Add platform-agnostic headers.
                    boolean shouldCreatePublicHeaderSymlinks = args.xcodePublicHeadersSymlinks.orElse(true);
                    CxxPreprocessables.HeaderMode mode = CxxDescriptionEnhancer.getHeaderModeForPlatform(resolver, platform.getValue(), shouldCreatePublicHeaderSymlinks);
                    Optional<CxxHeaders> exportedHeaders = resolver.requireMetadata(baseTarget.withAppendedFlavors(MetadataType.CXX_HEADERS.getFlavor(), mode.getFlavor()), CxxHeaders.class);
                    exportedHeaders.ifPresent(cxxPreprocessorInputBuilder::addIncludes);
                    // Add platform-specific headers.
                    if (!args.exportedPlatformHeaders.getMatchingValues(platform.getKey().toString()).isEmpty()) {
                        HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.EXPORTED_HEADERS.getFlavor()));
                        cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
                    }
                }
                CxxPreprocessorInput cxxPreprocessorInput = cxxPreprocessorInputBuilder.build();
                return Optional.of(cxxPreprocessorInput).map(metadataClass::cast);
            }
        case COMPILATION_DATABASE_DEPS:
            {
                return CxxDescriptionEnhancer.createCompilationDatabaseDependencies(buildTarget, cxxPlatforms, resolver, args).map(metadataClass::cast);
            }
    }
    throw new IllegalStateException(String.format("unhandled metadata type: %s", type.getValue()));
}
Also used : Optional(java.util.Optional) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) BuildTarget(com.facebook.buck.model.BuildTarget) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 39 with Optional

use of java.util.Optional in project buck by facebook.

the class Omnibus method createRoot.

// Create a build rule which links the given root node against the merged omnibus library
// described by the given spec file.
protected static OmnibusRoot createRoot(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, OmnibusSpec spec, SourcePath omnibus, NativeLinkTarget root, BuildTarget rootTargetBase, Optional<Path> output) throws NoSuchBuildTargetException {
    ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
    // Add any extra flags to the link.
    argsBuilder.addAll(extraLdflags);
    // Since the dummy omnibus library doesn't actually contain any symbols, make sure the linker
    // won't drop its runtime reference to it.
    argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getNoAsNeededSharedLibsFlags()));
    // Since we're linking against a dummy libomnibus, ignore undefined symbols.
    argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getIgnoreUndefinedSymbolsFlags()));
    // Add the args for the root link target first.
    NativeLinkableInput input = root.getNativeLinkTargetInput(cxxPlatform);
    argsBuilder.addAll(input.getArgs());
    // Grab a topologically sorted mapping of all the root's deps.
    ImmutableMap<BuildTarget, NativeLinkable> deps = NativeLinkables.getNativeLinkables(cxxPlatform, root.getNativeLinkTargetDeps(cxxPlatform), Linker.LinkableDepType.SHARED);
    // Now process the dependencies in topological order, to assemble the link line.
    boolean alreadyAddedOmnibusToArgs = false;
    for (Map.Entry<BuildTarget, NativeLinkable> entry : deps.entrySet()) {
        BuildTarget target = entry.getKey();
        NativeLinkable nativeLinkable = entry.getValue();
        Linker.LinkableDepType linkStyle = NativeLinkables.getLinkStyle(nativeLinkable.getPreferredLinkage(cxxPlatform), Linker.LinkableDepType.SHARED);
        // If this dep needs to be linked statically, then we always link it directly.
        if (linkStyle != Linker.LinkableDepType.SHARED) {
            Preconditions.checkState(linkStyle == Linker.LinkableDepType.STATIC_PIC);
            argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
            continue;
        }
        // If this dep is another root node, substitute in the custom linked library we built for it.
        if (spec.getRoots().containsKey(target)) {
            argsBuilder.add(SourcePathArg.of(new DefaultBuildTargetSourcePath(getRootTarget(params.getBuildTarget(), target))));
            continue;
        }
        // libomnibus instead.
        if (spec.getBody().containsKey(target)) {
            // && linkStyle == Linker.LinkableDepType.SHARED) {
            if (!alreadyAddedOmnibusToArgs) {
                argsBuilder.add(SourcePathArg.of(omnibus));
                alreadyAddedOmnibusToArgs = true;
            }
            continue;
        }
        // Otherwise, this is either an explicitly statically linked or excluded node, so link it
        // normally.
        Preconditions.checkState(spec.getExcluded().containsKey(target));
        argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
    }
    // Create the root library rule using the arguments assembled above.
    BuildTarget rootTarget = getRootTarget(params.getBuildTarget(), rootTargetBase);
    NativeLinkTargetMode rootTargetMode = root.getNativeLinkTargetMode(cxxPlatform);
    CxxLink rootLinkRule;
    switch(rootTargetMode.getType()) {
        // Link the root as a shared library.
        case SHARED:
            {
                Optional<String> rootSoname = rootTargetMode.getLibraryName();
                rootLinkRule = CxxLinkableEnhancer.createCxxLinkableSharedBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootSoname.orElse(String.format("%s.%s", rootTarget.getShortName(), cxxPlatform.getSharedLibraryExtension())))), rootSoname, argsBuilder.build());
                break;
            }
        // Link the root as an executable.
        case EXECUTABLE:
            {
                rootLinkRule = CxxLinkableEnhancer.createCxxLinkableBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootTarget.getShortName())), argsBuilder.build(), Linker.LinkableDepType.SHARED, Optional.empty());
                break;
            }
        // $CASES-OMITTED$
        default:
            throw new IllegalStateException(String.format("%s: unexpected omnibus root type: %s %s", params.getBuildTarget(), root.getBuildTarget(), rootTargetMode.getType()));
    }
    CxxLink rootRule = ruleResolver.addToIndex(rootLinkRule);
    return OmnibusRoot.of(rootRule.getSourcePathToOutput());
}
Also used : Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) 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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 40 with Optional

use of java.util.Optional in project buck by facebook.

the class DTestDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver buildRuleResolver, A args) throws NoSuchBuildTargetException {
    BuildTarget target = params.getBuildTarget();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    SymlinkTree sourceTree = buildRuleResolver.addToIndex(DDescriptionUtils.createSourceSymlinkTree(DDescriptionUtils.getSymlinkTreeTarget(params.getBuildTarget()), params, ruleFinder, pathResolver, args.srcs));
    // Create a helper rule to build the test binary.
    // The rule needs its own target so that we can depend on it without creating cycles.
    BuildTarget binaryTarget = DDescriptionUtils.createBuildTargetForFile(target, "build-", target.getFullyQualifiedName(), cxxPlatform);
    BuildRule binaryRule = DDescriptionUtils.createNativeLinkable(params.withBuildTarget(binaryTarget), buildRuleResolver, cxxPlatform, dBuckConfig, cxxBuckConfig, ImmutableList.of("-unittest"), args.srcs, args.linkerFlags, DIncludes.builder().setLinkTree(sourceTree.getSourcePathToOutput()).addAllSources(args.srcs.getPaths()).build());
    buildRuleResolver.addToIndex(binaryRule);
    return new DTest(params.copyAppendingExtraDeps(ImmutableList.of(binaryRule)), binaryRule, args.contacts, args.labels, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs));
}
Also used : SymlinkTree(com.facebook.buck.rules.SymlinkTree) Optional(java.util.Optional) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Aggregations

Optional (java.util.Optional)3042 List (java.util.List)1831 Map (java.util.Map)1086 ArrayList (java.util.ArrayList)1032 Collectors (java.util.stream.Collectors)971 Set (java.util.Set)768 IOException (java.io.IOException)742 HashMap (java.util.HashMap)644 Test (org.junit.Test)526 Collections (java.util.Collections)506 Arrays (java.util.Arrays)454 Collection (java.util.Collection)442 Logger (org.slf4j.Logger)432 LoggerFactory (org.slf4j.LoggerFactory)425 HashSet (java.util.HashSet)386 Objects (java.util.Objects)324 ImmutableList (com.google.common.collect.ImmutableList)290 Stream (java.util.stream.Stream)282 ImmutableMap (com.google.common.collect.ImmutableMap)243 Function (java.util.function.Function)226