Search in sources :

Example 91 with ImmutableList

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList 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 92 with ImmutableList

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList in project buck by facebook.

the class Omnibus method createOmnibus.

// Create a build rule to link the giant merged omnibus library described by the given spec.
protected static OmnibusLibrary createOmnibus(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, OmnibusSpec spec) throws NoSuchBuildTargetException {
    ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
    // Add extra ldflags to the beginning of the link.
    argsBuilder.addAll(extraLdflags);
    // For roots that aren't dependencies of nodes in the body, we extract their undefined symbols
    // to add to the link so that required symbols get pulled into the merged library.
    List<SourcePath> undefinedSymbolsOnlyRoots = new ArrayList<>();
    for (BuildTarget target : Sets.difference(spec.getRoots().keySet(), spec.getGraph().getNodes())) {
        NativeLinkTarget linkTarget = Preconditions.checkNotNull(spec.getRoots().get(target));
        undefinedSymbolsOnlyRoots.add(ruleResolver.requireRule(getRootTarget(params.getBuildTarget(), shouldCreateDummyRoot(linkTarget, cxxPlatform) ? getDummyRootTarget(target) : target)).getSourcePathToOutput());
    }
    argsBuilder.addAll(createUndefinedSymbolsArgs(params, ruleResolver, ruleFinder, cxxPlatform, undefinedSymbolsOnlyRoots));
    // Walk the graph in topological order, appending each nodes contributions to the link.
    ImmutableList<BuildTarget> targets = TopologicalSort.sort(spec.getGraph()).reverse();
    for (BuildTarget target : targets) {
        // If this is a root, just place the shared library we've linked above onto the link line.
        // We need this so that the linker can grab any undefined symbols from it, and therefore
        // know which symbols to pull in from the body nodes.
        NativeLinkTarget root = spec.getRoots().get(target);
        if (root != null) {
            argsBuilder.add(SourcePathArg.of(((CxxLink) ruleResolver.requireRule(getRootTarget(params.getBuildTarget(), root.getBuildTarget()))).getSourcePathToOutput()));
            continue;
        }
        // Otherwise, this is a body node, and we need to add its static library to the link line,
        // so that the linker can discard unused object files from it.
        NativeLinkable nativeLinkable = Preconditions.checkNotNull(spec.getBody().get(target));
        NativeLinkableInput input = NativeLinkables.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.STATIC_PIC, nativeLinkable);
        argsBuilder.addAll(input.getArgs());
    }
    // We process all excluded omnibus deps last, and just add their components as if this were a
    // normal shared link.
    ImmutableMap<BuildTarget, NativeLinkable> deps = NativeLinkables.getNativeLinkables(cxxPlatform, spec.getDeps().values(), Linker.LinkableDepType.SHARED);
    for (NativeLinkable nativeLinkable : deps.values()) {
        NativeLinkableInput input = NativeLinkables.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.SHARED, nativeLinkable);
        argsBuilder.addAll(input.getArgs());
    }
    // Create the merged omnibus library using the arguments assembled above.
    BuildTarget omnibusTarget = params.getBuildTarget().withAppendedFlavors(OMNIBUS_FLAVOR);
    String omnibusSoname = getOmnibusSoname(cxxPlatform);
    CxxLink omnibusRule = ruleResolver.addToIndex(CxxLinkableEnhancer.createCxxLinkableSharedBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, omnibusTarget, BuildTargets.getGenPath(params.getProjectFilesystem(), omnibusTarget, "%s").resolve(omnibusSoname), Optional.of(omnibusSoname), argsBuilder.build()));
    return OmnibusLibrary.of(omnibusSoname, omnibusRule.getSourcePathToOutput());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SourcePath(com.facebook.buck.rules.SourcePath) 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)

Example 93 with ImmutableList

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList in project buck by facebook.

the class DDescriptionUtils method sourcePathsForCompiledSources.

/**
   * Generates BuildTargets and BuildRules to compile D sources to object files, and
   * returns a list of SourcePaths referring to the generated object files.
   * @param sources source files to compile
   * @param compilerFlags flags to pass to the compiler
   * @param baseParams build parameters for the compilation
   * @param buildRuleResolver resolver for build rules
   * @param sourcePathResolver resolver for source paths
   * @param cxxPlatform the C++ platform to compile for
   * @param dBuckConfig the Buck configuration for D
   * @return SourcePaths of the generated object files
   */
public static ImmutableList<SourcePath> sourcePathsForCompiledSources(BuildRuleParams baseParams, BuildRuleResolver buildRuleResolver, SourcePathResolver sourcePathResolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags, SourceList sources, DIncludes includes) throws NoSuchBuildTargetException {
    ImmutableList.Builder<SourcePath> sourcePaths = ImmutableList.builder();
    for (Map.Entry<String, SourcePath> source : sources.toNameMap(baseParams.getBuildTarget(), sourcePathResolver, "srcs").entrySet()) {
        BuildTarget compileTarget = createDCompileBuildTarget(baseParams.getBuildTarget(), source.getKey(), cxxPlatform);
        BuildRule rule = requireBuildRule(compileTarget, baseParams, buildRuleResolver, ruleFinder, dBuckConfig, compilerFlags, source.getKey(), source.getValue(), includes);
        sourcePaths.add(rule.getSourcePathToOutput());
    }
    return sourcePaths.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ImmutableList(com.google.common.collect.ImmutableList) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap)

Example 94 with ImmutableList

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList in project buck by facebook.

the class Depfiles method parseAndOutputBuckCompatibleDepfile.

/**
   * Reads and processes {@code .dep} file produced by a cxx compiler.
   *
   * @param eventBus Used for outputting perf events and messages.
   * @param filesystem Used to access the filesystem and handle String to Path conversion.
   * @param headerPathNormalizer Used to convert raw paths into absolutized paths that can be
   *                             resolved to SourcePaths.
   * @param headerVerification Setting for how to respond to untracked header errors.
   * @param sourceDepFile Path to the raw dep file
   * @param inputPath Path to source file input, used to skip any leading entries from
   *                  {@code -fsanitize-blacklist}.
   * @param outputPath Path to object file output, used for stat tracking.
   * @return Normalized path objects suitable for use as arguments to
   *   {@link HeaderPathNormalizer#getSourcePathForAbsolutePath(Path)}.
   * @throws IOException if an IO error occurs.
   * @throws HeaderVerificationException
   *  if HeaderVerification error occurs and {@code headerVerification == ERROR}.
   */
public static ImmutableList<Path> parseAndOutputBuckCompatibleDepfile(BuckEventBus eventBus, ProjectFilesystem filesystem, HeaderPathNormalizer headerPathNormalizer, HeaderVerification headerVerification, Path sourceDepFile, Path inputPath, Path outputPath) throws IOException, HeaderVerificationException {
    // Process the dependency file, fixing up the paths, and write it out to it's final location.
    // The paths of the headers written out to the depfile are the paths to the symlinks from the
    // root of the repo if the compilation included them from the header search paths pointing to
    // the symlink trees, or paths to headers relative to the source file if the compilation
    // included them using source relative include paths. To handle both cases we check for the
    // prerequisites both in the values and the keys of the replacement map.
    Logger.get(Depfiles.class).debug("Processing dependency file %s as Makefile", sourceDepFile);
    ImmutableList.Builder<Path> resultBuilder = ImmutableList.builder();
    try (InputStream input = filesystem.newFileInputStream(sourceDepFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        SimplePerfEvent.Scope perfEvent = SimplePerfEvent.scope(eventBus, PerfEventId.of("depfile-parse"), ImmutableMap.of("input", inputPath, "output", outputPath))) {
        ImmutableList<String> prereqs = Depfiles.parseDepfile(reader).getPrereqs();
        // Additional files passed in via command-line flags (e.g. `-fsanitize-blacklist=<file>`)
        // appear first in the dep file, followed by the input source file.  So, just skip over
        // everything until just after the input source which should position us at the headers.
        //
        // TODO(#11303454): This means we're not including the content of these special files into the
        // rule key.  The correct way to handle this is likely to support macros in preprocessor/
        // compiler flags at which point we can use the entries for these files in the depfile to
        // verify that the user properly references these files via the macros.
        int inputIndex = prereqs.indexOf(inputPath.toString());
        Preconditions.checkState(inputIndex != -1, "Could not find input source (%s) in dep file prereqs (%s)", inputPath, prereqs);
        Iterable<String> headers = Iterables.skip(prereqs, inputIndex + 1);
        for (String rawHeader : headers) {
            Path header = filesystem.resolve(rawHeader).normalize();
            Optional<Path> absolutePath = headerPathNormalizer.getAbsolutePathForUnnormalizedPath(header);
            Optional<Path> repoRelativePath = filesystem.getPathRelativeToProjectRoot(header);
            if (absolutePath.isPresent()) {
                Preconditions.checkState(absolutePath.get().isAbsolute());
                resultBuilder.add(absolutePath.get());
            } else if (headerVerification.getMode() != HeaderVerification.Mode.IGNORE && !(headerVerification.isWhitelisted(header.toString()) || repoRelativePath.map(path -> headerVerification.isWhitelisted(path.toString())).orElse(false))) {
                String errorMessage = String.format("%s: included an untracked header \"%s\"", inputPath, repoRelativePath.orElse(header));
                eventBus.post(ConsoleEvent.create(headerVerification.getMode() == HeaderVerification.Mode.ERROR ? Level.SEVERE : Level.WARNING, errorMessage));
                if (headerVerification.getMode() == HeaderVerification.Mode.ERROR) {
                    throw new HeaderVerificationException(errorMessage);
                }
            }
        }
    }
    return resultBuilder.build();
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent)

Example 95 with ImmutableList

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList in project buck by facebook.

the class HeaderSymlinkTreeWithHeaderMap method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    LOG.debug("Generating post-build steps to write header map to %s", headerMapPath);
    Path buckOut = getProjectFilesystem().resolve(getProjectFilesystem().getBuckPaths().getBuckOut());
    ImmutableMap.Builder<Path, Path> headerMapEntries = ImmutableMap.builder();
    for (Path key : getLinks().keySet()) {
        // The key is the path that will be referred to in headers. It can be anything. However, the
        // value given in the headerMapEntries is the path of that entry in the generated symlink
        // tree. Because "reasons", we don't want to cache that value, so we need to relativize the
        // path to the output directory of this current rule. We then rely on magic and the stars
        // aligning in order to get this to work. May we find peace in another life.
        headerMapEntries.put(key, buckOut.relativize(getRoot().resolve(key)));
    }
    ImmutableList.Builder<Step> builder = ImmutableList.<Step>builder().addAll(super.getBuildSteps(context, buildableContext)).add(new HeaderMapStep(getProjectFilesystem(), headerMapPath, headerMapEntries.build()));
    if (shouldCreateModule) {
        Optional<String> umbrellaHeader = getUmbrellaHeader(getBuildTarget().getShortName());
        String moduleName = normalizeModuleName(getBuildTarget().getShortName());
        builder.add(new MkdirStep(getProjectFilesystem(), getRoot().resolve(moduleName)));
        builder.add(createCreateModuleStep(moduleName, umbrellaHeader));
    }
    return builder.build();
}
Also used : ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1079 List (java.util.List)293 ArrayList (java.util.ArrayList)169 Test (org.junit.Test)161 Map (java.util.Map)152 Path (java.nio.file.Path)149 ImmutableMap (com.google.common.collect.ImmutableMap)127 IOException (java.io.IOException)112 Set (java.util.Set)99 ImmutableSet (com.google.common.collect.ImmutableSet)98 SourcePath (com.facebook.buck.rules.SourcePath)91 Optional (java.util.Optional)89 HashMap (java.util.HashMap)85 Step (com.facebook.buck.step.Step)76 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)71 Collectors (java.util.stream.Collectors)62 File (java.io.File)58 HashSet (java.util.HashSet)58 Nullable (javax.annotation.Nullable)54 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)52