use of 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();
}
use of 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);
}
});
}
use of 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);
}
}
use of 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);
}
}
use of 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();
}
Aggregations