Search in sources :

Example 66 with ImmutableMap

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

the class PythonUtil method getAllComponents.

static PythonPackageComponents getAllComponents(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, final PythonPackageComponents packageComponents, final PythonPlatform pythonPlatform, CxxBuckConfig cxxBuckConfig, final CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, final NativeLinkStrategy nativeLinkStrategy, final ImmutableSet<BuildTarget> preloadDeps) throws NoSuchBuildTargetException {
    final PythonPackageComponents.Builder allComponents = new PythonPackageComponents.Builder(params.getBuildTarget());
    final Map<BuildTarget, CxxPythonExtension> extensions = new LinkedHashMap<>();
    final Map<BuildTarget, NativeLinkable> nativeLinkableRoots = new LinkedHashMap<>();
    final OmnibusRoots.Builder omnibusRoots = OmnibusRoots.builder(cxxPlatform, preloadDeps);
    // Add the top-level components.
    allComponents.addComponent(packageComponents, params.getBuildTarget());
    // Walk all our transitive deps to build our complete package that we'll
    // turn into an executable.
    new AbstractBreadthFirstThrowingTraversal<BuildRule, NoSuchBuildTargetException>(params.getDeps()) {

        private final ImmutableList<BuildRule> empty = ImmutableList.of();

        @Override
        public Iterable<BuildRule> visit(BuildRule rule) throws NoSuchBuildTargetException {
            Iterable<BuildRule> deps = empty;
            if (rule instanceof CxxPythonExtension) {
                CxxPythonExtension extension = (CxxPythonExtension) rule;
                NativeLinkTarget target = ((CxxPythonExtension) rule).getNativeLinkTarget(pythonPlatform);
                extensions.put(target.getBuildTarget(), extension);
                omnibusRoots.addIncludedRoot(target);
                List<BuildRule> cxxpydeps = new ArrayList<>();
                for (BuildRule dep : rule.getDeps()) {
                    if (dep instanceof CxxPythonExtension) {
                        cxxpydeps.add(dep);
                    }
                }
                deps = cxxpydeps;
            } else if (rule instanceof PythonPackagable) {
                PythonPackagable packagable = (PythonPackagable) rule;
                PythonPackageComponents comps = packagable.getPythonPackageComponents(pythonPlatform, cxxPlatform);
                allComponents.addComponent(comps, rule.getBuildTarget());
                if (comps.hasNativeCode(cxxPlatform)) {
                    for (BuildRule dep : rule.getDeps()) {
                        if (dep instanceof NativeLinkable) {
                            NativeLinkable linkable = (NativeLinkable) dep;
                            nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                            omnibusRoots.addExcludedRoot(linkable);
                        }
                    }
                }
                deps = rule.getDeps();
            } else if (rule instanceof NativeLinkable) {
                NativeLinkable linkable = (NativeLinkable) rule;
                nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                omnibusRoots.addPotentialRoot(linkable);
            }
            return deps;
        }
    }.start();
    // excluded native linkable roots.
    if (nativeLinkStrategy == NativeLinkStrategy.MERGED) {
        OmnibusRoots roots = omnibusRoots.build();
        OmnibusLibraries libraries = Omnibus.getSharedLibraries(params, ruleResolver, ruleFinder, cxxBuckConfig, cxxPlatform, extraLdflags, roots.getIncludedRoots().values(), roots.getExcludedRoots().values());
        // Otherwise, add it as a native library.
        for (Map.Entry<BuildTarget, OmnibusRoot> root : libraries.getRoots().entrySet()) {
            CxxPythonExtension extension = extensions.get(root.getKey());
            if (extension != null) {
                allComponents.addModule(extension.getModule(), root.getValue().getPath(), root.getKey());
            } else {
                NativeLinkTarget target = Preconditions.checkNotNull(roots.getIncludedRoots().get(root.getKey()), "%s: linked unexpected omnibus root: %s", params.getBuildTarget(), root.getKey());
                NativeLinkTargetMode mode = target.getNativeLinkTargetMode(cxxPlatform);
                String soname = Preconditions.checkNotNull(mode.getLibraryName().orElse(null), "%s: omnibus library for %s was built without soname", params.getBuildTarget(), root.getKey());
                allComponents.addNativeLibraries(Paths.get(soname), root.getValue().getPath(), root.getKey());
            }
        }
        // Add all remaining libraries as native libraries.
        for (OmnibusLibrary library : libraries.getLibraries()) {
            allComponents.addNativeLibraries(Paths.get(library.getSoname()), library.getPath(), params.getBuildTarget());
        }
    } else {
        // For regular linking, add all extensions via the package components interface.
        Map<BuildTarget, NativeLinkable> extensionNativeDeps = new LinkedHashMap<>();
        for (Map.Entry<BuildTarget, CxxPythonExtension> entry : extensions.entrySet()) {
            allComponents.addComponent(entry.getValue().getPythonPackageComponents(pythonPlatform, cxxPlatform), entry.getValue().getBuildTarget());
            extensionNativeDeps.putAll(Maps.uniqueIndex(entry.getValue().getNativeLinkTarget(pythonPlatform).getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
        }
        // Add all the native libraries.
        ImmutableMap<BuildTarget, NativeLinkable> nativeLinkables = NativeLinkables.getTransitiveNativeLinkables(cxxPlatform, Iterables.concat(nativeLinkableRoots.values(), extensionNativeDeps.values()));
        for (NativeLinkable nativeLinkable : nativeLinkables.values()) {
            NativeLinkable.Linkage linkage = nativeLinkable.getPreferredLinkage(cxxPlatform);
            if (nativeLinkableRoots.containsKey(nativeLinkable.getBuildTarget()) || linkage != NativeLinkable.Linkage.STATIC) {
                ImmutableMap<String, SourcePath> libs = nativeLinkable.getSharedLibraries(cxxPlatform);
                for (Map.Entry<String, SourcePath> ent : libs.entrySet()) {
                    allComponents.addNativeLibraries(Paths.get(ent.getKey()), ent.getValue(), nativeLinkable.getBuildTarget());
                }
            }
        }
    }
    return allComponents.build();
}
Also used : FluentIterable(com.google.common.collect.FluentIterable) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) NativeLinkTargetMode(com.facebook.buck.cxx.NativeLinkTargetMode) OmnibusRoot(com.facebook.buck.cxx.OmnibusRoot) LinkedHashMap(java.util.LinkedHashMap) OmnibusLibraries(com.facebook.buck.cxx.OmnibusLibraries) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) OmnibusRoots(com.facebook.buck.cxx.OmnibusRoots) BuildRule(com.facebook.buck.rules.BuildRule) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SourceList(com.facebook.buck.rules.coercer.SourceList) List(java.util.List) OmnibusLibrary(com.facebook.buck.cxx.OmnibusLibrary) NativeLinkTarget(com.facebook.buck.cxx.NativeLinkTarget) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 67 with ImmutableMap

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

the class CellProvider method createForLocalBuild.

/**
   * Create a cell provider at a given root.
   */
public static CellProvider createForLocalBuild(ProjectFilesystem rootFilesystem, Watchman watchman, BuckConfig rootConfig, CellConfig rootCellConfigOverrides, KnownBuildRuleTypesFactory knownBuildRuleTypesFactory) throws IOException {
    DefaultCellPathResolver rootCellCellPathResolver = new DefaultCellPathResolver(rootFilesystem.getRootPath(), rootConfig.getConfig());
    ImmutableMap<RelativeCellName, Path> transitiveCellPathMapping = rootCellCellPathResolver.getTransitivePathMapping();
    ImmutableMap<Path, RawConfig> pathToConfigOverrides;
    try {
        pathToConfigOverrides = rootCellConfigOverrides.getOverridesByPath(transitiveCellPathMapping);
    } catch (CellConfig.MalformedOverridesException e) {
        throw new HumanReadableException(e.getMessage());
    }
    ImmutableSet<Path> allRoots = ImmutableSet.copyOf(transitiveCellPathMapping.values());
    return new CellProvider(cellProvider -> new CacheLoader<Path, Cell>() {

        @Override
        public Cell load(Path cellPath) throws IOException, InterruptedException {
            Path normalizedCellPath = cellPath.toRealPath().normalize();
            Preconditions.checkState(allRoots.contains(normalizedCellPath), "Cell %s outside of transitive closure of root cell (%s).", normalizedCellPath, allRoots);
            RawConfig configOverrides = Optional.ofNullable(pathToConfigOverrides.get(normalizedCellPath)).orElse(RawConfig.of(ImmutableMap.of()));
            Config config = Configs.createDefaultConfig(normalizedCellPath, configOverrides);
            DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(normalizedCellPath, config);
            cellPathResolver.getCellPaths().forEach((name, path) -> {
                Path pathInRootResolver = rootCellCellPathResolver.getCellPaths().get(name);
                if (pathInRootResolver == null) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must exist in the root cell's cell mappings.", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name);
                } else if (!pathInRootResolver.equals(path)) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must point to the same directory as the root " + "cell's cell mapping: (root) %s != (current) %s", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name, pathInRootResolver, path);
                }
            });
            ProjectFilesystem cellFilesystem = new ProjectFilesystem(normalizedCellPath, config);
            BuckConfig buckConfig = new BuckConfig(config, cellFilesystem, rootConfig.getArchitecture(), rootConfig.getPlatform(), rootConfig.getEnvironment(), cellPathResolver);
            return new Cell(cellPathResolver.getKnownRoots(), cellFilesystem, watchman, buckConfig, knownBuildRuleTypesFactory, cellProvider);
        }
    }, cellProvider -> {
        try {
            return new Cell(rootCellCellPathResolver.getKnownRoots(), rootFilesystem, watchman, rootConfig, knownBuildRuleTypesFactory, cellProvider);
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while loading root cell", e);
        } catch (IOException e) {
            throw new HumanReadableException("Failed to load root cell", e);
        }
    });
}
Also used : Path(java.nio.file.Path) LoadingCache(com.google.common.cache.LoadingCache) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Config(com.facebook.buck.config.Config) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Configs(com.facebook.buck.config.Configs) HumanReadableException(com.facebook.buck.util.HumanReadableException) Watchman(com.facebook.buck.io.Watchman) Function(java.util.function.Function) RawConfig(com.facebook.buck.config.RawConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Config(com.facebook.buck.config.Config) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) IOException(java.io.IOException) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) HumanReadableException(com.facebook.buck.util.HumanReadableException) CellConfig(com.facebook.buck.config.CellConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 68 with ImmutableMap

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

the class DefaultCellPathResolver method constructFullMapping.

private static void constructFullMapping(ImmutableMap.Builder<RelativeCellName, Path> result, Set<Path> pathStack, RelativeCellName parentCellPath, DefaultCellPathResolver parentStub) {
    ImmutableMap<String, Path> partialMapping = parentStub.getPartialMapping();
    for (Map.Entry<String, Path> entry : partialMapping.entrySet()) {
        Path cellRoot = entry.getValue().normalize();
        try {
            cellRoot = cellRoot.toRealPath().normalize();
        } catch (IOException e) {
            LOG.warn("cellroot [" + cellRoot + "] does not exist in filesystem");
        }
        // of the BuildTargets in the build reference them.
        if (pathStack.contains(cellRoot)) {
            continue;
        }
        pathStack.add(cellRoot);
        RelativeCellName relativeCellName = parentCellPath.withAppendedComponent(entry.getKey());
        result.put(relativeCellName, cellRoot);
        Config config;
        try {
            // We don't support overriding repositories from the command line so creating the config
            // with no overrides is OK.
            config = Configs.createDefaultConfig(cellRoot);
        } catch (IOException e) {
            LOG.debug(e, "Error when constructing cell, skipping path %s", cellRoot);
            continue;
        }
        constructFullMapping(result, pathStack, relativeCellName, new DefaultCellPathResolver(cellRoot, config));
        pathStack.remove(cellRoot);
    }
}
Also used : Path(java.nio.file.Path) Config(com.facebook.buck.config.Config) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 69 with ImmutableMap

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

the class PexStep method getStdin.

/** Return the manifest as a JSON blob to write to the pex processes stdin.
   * <p>
   * We use stdin rather than passing as an argument to the processes since
   * manifest files can occasionally get extremely large, and surpass exec/shell
   * limits on arguments.
   */
@Override
protected Optional<String> getStdin(ExecutionContext context) {
    // Convert the map of paths to a map of strings before converting to JSON.
    ImmutableMap<Path, Path> resolvedModules;
    try {
        resolvedModules = getExpandedSourcePaths(modules);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ImmutableMap.Builder<String, String> modulesBuilder = ImmutableMap.builder();
    for (ImmutableMap.Entry<Path, Path> ent : resolvedModules.entrySet()) {
        modulesBuilder.put(ent.getKey().toString(), ent.getValue().toString());
    }
    ImmutableMap.Builder<String, String> resourcesBuilder = ImmutableMap.builder();
    for (ImmutableMap.Entry<Path, Path> ent : resources.entrySet()) {
        resourcesBuilder.put(ent.getKey().toString(), ent.getValue().toString());
    }
    ImmutableMap.Builder<String, String> nativeLibrariesBuilder = ImmutableMap.builder();
    for (ImmutableMap.Entry<Path, Path> ent : nativeLibraries.entrySet()) {
        nativeLibrariesBuilder.put(ent.getKey().toString(), ent.getValue().toString());
    }
    ImmutableList.Builder<String> prebuiltLibrariesBuilder = ImmutableList.builder();
    for (Path req : prebuiltLibraries) {
        prebuiltLibrariesBuilder.add(req.toString());
    }
    try {
        return Optional.of(context.getObjectMapper().writeValueAsString(ImmutableMap.of("modules", modulesBuilder.build(), "resources", resourcesBuilder.build(), "nativeLibraries", nativeLibrariesBuilder.build(), "prebuiltLibraries", prebuiltLibrariesBuilder.build())));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 70 with ImmutableMap

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

the class AbstractReport method getLocalConfigs.

private ImmutableMap<Path, String> getLocalConfigs() {
    Path rootPath = filesystem.getRootPath();
    ImmutableSet<Path> knownUserLocalConfigs = ImmutableSet.of(Paths.get(BuckConfig.BUCK_CONFIG_OVERRIDE_FILE_NAME), LogConfigPaths.LOCAL_PATH, Paths.get(".watchman.local"), Paths.get(".buckjavaargs.local"), Paths.get(".bucklogging.local.properties"));
    ImmutableMap.Builder<Path, String> localConfigs = ImmutableMap.builder();
    for (Path localConfig : knownUserLocalConfigs) {
        try {
            localConfigs.put(localConfig, new String(Files.readAllBytes(rootPath.resolve(localConfig)), StandardCharsets.UTF_8));
        } catch (FileNotFoundException e) {
            LOG.debug("%s was not found.", localConfig);
        } catch (IOException e) {
            LOG.warn("Failed to read contents of %s.", rootPath.resolve(localConfig).toString());
        }
    }
    return localConfigs.build();
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1267 Map (java.util.Map)662 Test (org.junit.Test)313 ImmutableList (com.google.common.collect.ImmutableList)300 List (java.util.List)288 HashMap (java.util.HashMap)269 ImmutableSet (com.google.common.collect.ImmutableSet)213 IOException (java.io.IOException)202 Optional (java.util.Optional)190 Set (java.util.Set)168 ArrayList (java.util.ArrayList)158 Path (java.nio.file.Path)151 Collectors (java.util.stream.Collectors)133 File (java.io.File)117 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)102 Collection (java.util.Collection)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)85 Test (org.testng.annotations.Test)85 HashSet (java.util.HashSet)84 Collections (java.util.Collections)78