use of com.google.common.collect.ImmutableBiMap in project buck by facebook.
the class SymlinkTree method resolveDuplicateRelativePaths.
/**
* Because of cross-cell, multiple {@link SourcePath}s can resolve to the same relative path,
* despite having distinct absolute paths. This presents a challenge for rules that require
* gathering all of the inputs in one directory.
*
* @param sourcePaths set of SourcePaths to process
* @param resolver resolver
* @return a map that assigns a unique relative path to each of the SourcePaths.
*/
public static ImmutableBiMap<SourcePath, Path> resolveDuplicateRelativePaths(ImmutableSortedSet<SourcePath> sourcePaths, SourcePathResolver resolver) {
// This serves a dual purpose - it keeps track of whether a particular relative path had been
// assigned to a SourcePath and how many times a particular relative path had been seen.
Multiset<Path> assignedPaths = HashMultiset.create();
ImmutableBiMap.Builder<SourcePath, Path> builder = ImmutableBiMap.builder();
List<SourcePath> conflicts = new ArrayList<>();
for (SourcePath sourcePath : sourcePaths) {
Path relativePath = resolver.getRelativePath(sourcePath);
if (!assignedPaths.contains(relativePath)) {
builder.put(sourcePath, relativePath);
assignedPaths.add(relativePath);
} else {
conflicts.add(sourcePath);
}
}
for (SourcePath conflict : conflicts) {
Path relativePath = resolver.getRelativePath(conflict);
Path parent = MorePaths.getParentOrEmpty(relativePath);
String extension = MorePaths.getFileExtension(relativePath);
String name = MorePaths.getNameWithoutExtension(relativePath);
while (true) {
StringBuilder candidateName = new StringBuilder(name);
candidateName.append('-');
int suffix = assignedPaths.count(relativePath);
candidateName.append(suffix);
if (!extension.isEmpty()) {
candidateName.append('.');
candidateName.append(extension);
}
Path candidate = parent.resolve(candidateName.toString());
if (!assignedPaths.contains(candidate)) {
assignedPaths.add(candidate);
builder.put(conflict, candidate);
break;
} else {
assignedPaths.add(relativePath);
}
}
}
return builder.build();
}
use of com.google.common.collect.ImmutableBiMap in project bazel by bazelbuild.
the class JavaImport method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
ImmutableList<Artifact> srcJars = ImmutableList.of();
ImmutableList<Artifact> jars = collectJars(ruleContext);
Artifact srcJar = ruleContext.getPrerequisiteArtifact("srcjar", Mode.TARGET);
if (ruleContext.hasErrors()) {
return null;
}
ImmutableList<TransitiveInfoCollection> targets = ImmutableList.<TransitiveInfoCollection>builder().addAll(ruleContext.getPrerequisites("deps", Mode.TARGET)).addAll(ruleContext.getPrerequisites("exports", Mode.TARGET)).build();
final JavaCommon common = new JavaCommon(ruleContext, semantics, /*srcs=*/
ImmutableList.<Artifact>of(), targets, targets, targets);
semantics.checkRule(ruleContext, common);
// No need for javac options - no compilation happening here.
ImmutableBiMap.Builder<Artifact, Artifact> compilationToRuntimeJarMapBuilder = ImmutableBiMap.builder();
ImmutableList<Artifact> interfaceJars = processWithIjar(jars, ruleContext, compilationToRuntimeJarMapBuilder);
JavaCompilationArtifacts javaArtifacts = collectJavaArtifacts(jars, interfaceJars);
common.setJavaCompilationArtifacts(javaArtifacts);
CppCompilationContext transitiveCppDeps = common.collectTransitiveCppDeps();
NestedSet<LinkerInput> transitiveJavaNativeLibraries = common.collectTransitiveJavaNativeLibraries();
boolean neverLink = JavaCommon.isNeverLink(ruleContext);
JavaCompilationArgs javaCompilationArgs = common.collectJavaCompilationArgs(false, neverLink, false);
JavaCompilationArgs recursiveJavaCompilationArgs = common.collectJavaCompilationArgs(true, neverLink, false);
NestedSet<Artifact> transitiveJavaSourceJars = collectTransitiveJavaSourceJars(ruleContext, srcJar);
if (srcJar != null) {
srcJars = ImmutableList.of(srcJar);
}
// The "neverlink" attribute is transitive, so if it is enabled, we don't add any
// runfiles from this target or its dependencies.
Runfiles runfiles = neverLink ? Runfiles.EMPTY : new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addArtifacts(javaArtifacts.getRuntimeJars()).addTargets(targets, RunfilesProvider.DEFAULT_RUNFILES).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).addTargets(targets, JavaRunfilesProvider.TO_RUNFILES).add(ruleContext, JavaRunfilesProvider.TO_RUNFILES).build();
CcLinkParamsStore ccLinkParamsStore = new CcLinkParamsStore() {
@Override
protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) {
builder.addTransitiveTargets(common.targetsTreatedAsDeps(ClasspathType.BOTH), JavaCcLinkParamsProvider.TO_LINK_PARAMS, CcLinkParamsProvider.TO_LINK_PARAMS);
}
};
RuleConfiguredTargetBuilder ruleBuilder = new RuleConfiguredTargetBuilder(ruleContext);
NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
filesBuilder.addAll(jars);
ImmutableBiMap<Artifact, Artifact> compilationToRuntimeJarMap = compilationToRuntimeJarMapBuilder.build();
semantics.addProviders(ruleContext, common, ImmutableList.<String>of(), null, /* classJar */
srcJar, /* srcJar */
null, /* genJar */
null, /* gensrcJar */
compilationToRuntimeJarMap, filesBuilder, ruleBuilder);
NestedSet<Artifact> filesToBuild = filesBuilder.build();
JavaSourceInfoProvider javaSourceInfoProvider = new JavaSourceInfoProvider.Builder().setJarFiles(jars).setSourceJarsForJarFiles(srcJars).build();
JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = JavaRuleOutputJarsProvider.builder();
for (Artifact jar : jars) {
ruleOutputJarsProviderBuilder.addOutputJar(jar, compilationToRuntimeJarMap.inverse().get(jar), srcJars);
}
NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs();
JavaRuleOutputJarsProvider ruleOutputJarsProvider = ruleOutputJarsProviderBuilder.build();
JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars);
JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs);
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(compilationArgsProvider);
common.addTransitiveInfoProviders(ruleBuilder, skylarkApiProvider, filesToBuild, null);
return ruleBuilder.setFilesToBuild(filesToBuild).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).add(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())).add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(neverLink)).add(RunfilesProvider.class, RunfilesProvider.simple(runfiles)).add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore)).add(JavaCompilationArgsProvider.class, compilationArgsProvider).add(JavaNativeLibraryProvider.class, new JavaNativeLibraryProvider(transitiveJavaNativeLibraries)).add(CppCompilationContext.class, transitiveCppDeps).add(JavaSourceInfoProvider.class, javaSourceInfoProvider).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)).addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveJavaSourceJars).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, proguardSpecs).build();
}
use of com.google.common.collect.ImmutableBiMap in project error-prone by google.
the class ScannerSupplier method applyOverrides.
/**
* Applies options to this {@link ScannerSupplier}.
*
* <p>Command-line options to override check severities may do any of the following:
*
* <ul>
* <li>Enable a check that is currently off
* <li>Disable a check that is currently on
* <li>Change the severity of a check that is on, promoting a warning to an error or demoting an
* error to a warning
* </ul>
*
* @param errorProneOptions an {@link ErrorProneOptions} object that encapsulates the overrides
* for this compilation
* @throws InvalidCommandLineOptionException if the override map attempts to disable a check that
* may not be disabled
*/
@CheckReturnValue
public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) throws InvalidCommandLineOptionException {
Map<String, Severity> severityOverrides = errorProneOptions.getSeverityMap();
if (severityOverrides.isEmpty() && errorProneOptions.getFlags().isEmpty() && !errorProneOptions.isEnableAllChecksAsWarnings() && !errorProneOptions.isDropErrorsToWarnings() && !errorProneOptions.isDisableAllChecks()) {
return this;
}
// Initialize result allChecks map and enabledChecks set with current state of this Supplier.
ImmutableBiMap<String, BugCheckerInfo> checks = getAllChecks();
Map<String, SeverityLevel> severities = new LinkedHashMap<>(severities());
Set<String> disabled = new HashSet<>(disabled());
if (errorProneOptions.isEnableAllChecksAsWarnings()) {
disabled.forEach(c -> severities.put(c, SeverityLevel.WARNING));
disabled.clear();
}
if (errorProneOptions.isDropErrorsToWarnings()) {
getAllChecks().values().stream().filter(c -> c.defaultSeverity() == SeverityLevel.ERROR && c.disableable()).forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING));
}
if (errorProneOptions.isDisableAllChecks()) {
getAllChecks().values().stream().filter(c -> c.disableable()).forEach(c -> disabled.add(c.canonicalName()));
}
// Process overrides
severityOverrides.forEach((checkName, newSeverity) -> {
BugCheckerInfo check = getAllChecks().get(checkName);
if (check == null) {
if (errorProneOptions.ignoreUnknownChecks()) {
return;
}
throw new InvalidCommandLineOptionException(checkName + " is not a valid checker name");
}
switch(newSeverity) {
case OFF:
if (!check.disableable()) {
throw new InvalidCommandLineOptionException(check.canonicalName() + " may not be disabled");
}
severities.remove(check.canonicalName());
disabled.add(check.canonicalName());
break;
case DEFAULT:
severities.put(check.canonicalName(), check.defaultSeverity());
disabled.remove(check.canonicalName());
break;
case WARN:
// Demoting an enabled check from an error to a warning is a form of disabling
if (!disabled().contains(check.canonicalName()) && !check.disableable() && check.defaultSeverity() == SeverityLevel.ERROR) {
throw new InvalidCommandLineOptionException(check.canonicalName() + " is not disableable and may not be demoted to a warning");
}
severities.put(check.canonicalName(), SeverityLevel.WARNING);
disabled.remove(check.canonicalName());
break;
case ERROR:
severities.put(check.canonicalName(), SeverityLevel.ERROR);
disabled.remove(check.canonicalName());
break;
default:
throw new IllegalStateException("Unexpected severity level: " + newSeverity);
}
});
return new ScannerSupplierImpl(checks, ImmutableMap.copyOf(severities), ImmutableSet.copyOf(disabled), errorProneOptions.getFlags());
}
use of com.google.common.collect.ImmutableBiMap in project gerrit by GerritCodeReview.
the class GroupNameNotes method updateAllGroups.
/**
* Replaces the map of name/UUID pairs with a new version which matches exactly the passed {@code
* GroupReference}s.
*
* <p>All old entries are discarded and replaced by the new ones.
*
* <p>This operation also works if the previous map has invalid entries or can't be read anymore.
*
* <p><strong>Note: </strong>This method doesn't flush the {@code ObjectInserter}. It doesn't
* execute the {@code BatchRefUpdate} either.
*
* @param repository the repository which holds the commits of the notes
* @param inserter an {@code ObjectInserter} for that repository
* @param bru a {@code BatchRefUpdate} to which this method adds commands
* @param groupReferences all {@code GroupReference}s (name/UUID pairs) which should be contained
* in the map of name/UUID pairs
* @param ident the {@code PersonIdent} which is used as author and committer for commits
* @throws IOException if the repository can't be accessed for some reason
*/
public static void updateAllGroups(Repository repository, ObjectInserter inserter, BatchRefUpdate bru, Collection<GroupReference> groupReferences, PersonIdent ident) throws IOException {
// Not strictly necessary for iteration; throws IAE if it encounters duplicates, which is nice.
ImmutableBiMap<AccountGroup.UUID, String> biMap = toBiMap(groupReferences);
try (ObjectReader reader = inserter.newReader();
RevWalk rw = new RevWalk(reader)) {
// Always start from an empty map, discarding old notes.
NoteMap noteMap = NoteMap.newEmptyMap();
Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
RevCommit oldCommit = ref != null ? rw.parseCommit(ref.getObjectId()) : null;
for (Map.Entry<AccountGroup.UUID, String> e : biMap.entrySet()) {
AccountGroup.NameKey nameKey = AccountGroup.nameKey(e.getValue());
ObjectId noteKey = getNoteKey(nameKey);
noteMap.set(noteKey, getAsNoteData(e.getKey(), nameKey), inserter);
}
ObjectId newTreeId = noteMap.writeTree(inserter);
if (oldCommit != null && newTreeId.equals(oldCommit.getTree())) {
return;
}
CommitBuilder cb = new CommitBuilder();
if (oldCommit != null) {
cb.addParentId(oldCommit);
}
cb.setTreeId(newTreeId);
cb.setAuthor(ident);
cb.setCommitter(ident);
int n = groupReferences.size();
cb.setMessage("Store " + n + " group name" + (n != 1 ? "s" : ""));
ObjectId newId = inserter.insert(cb).copy();
ObjectId oldId = ObjectIds.copyOrZero(oldCommit);
bru.addCommand(new ReceiveCommand(oldId, newId, RefNames.REFS_GROUPNAMES));
}
}
use of com.google.common.collect.ImmutableBiMap in project buck by facebook.
the class NdkCxxPlatforms method build.
@VisibleForTesting
static NdkCxxPlatform build(CxxBuckConfig config, ProjectFilesystem filesystem, Flavor flavor, Platform platform, Path ndkRoot, NdkCxxPlatformTargetConfiguration targetConfiguration, CxxRuntime cxxRuntime, ExecutableFinder executableFinder, boolean strictToolchainPaths) {
// Create a version string to use when generating rule keys via the NDK tools we'll generate
// below. This will be used in lieu of hashing the contents of the tools, so that builds from
// different host platforms (which produce identical output) will share the cache with one
// another.
NdkCxxPlatformCompiler.Type compilerType = targetConfiguration.getCompiler().getType();
String version = Joiner.on('-').join(ImmutableList.of(readVersion(ndkRoot), targetConfiguration.getToolchain(), targetConfiguration.getTargetAppPlatform(), compilerType, targetConfiguration.getCompiler().getVersion(), targetConfiguration.getCompiler().getGccVersion(), cxxRuntime));
Host host = Preconditions.checkNotNull(BUILD_PLATFORMS.get(platform));
NdkCxxToolchainPaths toolchainPaths = new NdkCxxToolchainPaths(filesystem, ndkRoot, targetConfiguration, host.toString(), cxxRuntime, strictToolchainPaths);
// Sanitized paths will have magic placeholders for parts of the paths that
// are machine/host-specific. See comments on ANDROID_NDK_ROOT and
// BUILD_HOST_SUBST above.
NdkCxxToolchainPaths sanitizedPaths = toolchainPaths.getSanitizedPaths();
// Build up the map of paths that must be sanitized.
ImmutableBiMap.Builder<Path, Path> sanitizePathsBuilder = ImmutableBiMap.builder();
sanitizePathsBuilder.put(toolchainPaths.getNdkToolRoot(), sanitizedPaths.getNdkToolRoot());
if (compilerType != NdkCxxPlatformCompiler.Type.GCC) {
sanitizePathsBuilder.put(toolchainPaths.getNdkGccToolRoot(), sanitizedPaths.getNdkGccToolRoot());
}
sanitizePathsBuilder.put(ndkRoot, Paths.get(ANDROID_NDK_ROOT));
CxxToolProvider.Type type = compilerType == NdkCxxPlatformCompiler.Type.CLANG ? CxxToolProvider.Type.CLANG : CxxToolProvider.Type.GCC;
ToolProvider ccTool = new ConstantToolProvider(getCTool(toolchainPaths, compilerType.getCc(), version, executableFinder));
ToolProvider cxxTool = new ConstantToolProvider(getCTool(toolchainPaths, compilerType.getCxx(), version, executableFinder));
CompilerProvider cc = new CompilerProvider(ccTool, type);
PreprocessorProvider cpp = new PreprocessorProvider(ccTool, type);
CompilerProvider cxx = new CompilerProvider(cxxTool, type);
PreprocessorProvider cxxpp = new PreprocessorProvider(cxxTool, type);
CxxPlatform.Builder cxxPlatformBuilder = CxxPlatform.builder();
ImmutableBiMap<Path, Path> sanitizePaths = sanitizePathsBuilder.build();
PrefixMapDebugPathSanitizer compilerDebugPathSanitizer = new PrefixMapDebugPathSanitizer(config.getDebugPathSanitizerLimit(), File.separatorChar, Paths.get("."), sanitizePaths, filesystem.getRootPath().toAbsolutePath(), type, filesystem);
MungingDebugPathSanitizer assemblerDebugPathSanitizer = new MungingDebugPathSanitizer(config.getDebugPathSanitizerLimit(), File.separatorChar, Paths.get("."), sanitizePaths);
cxxPlatformBuilder.setFlavor(flavor).setAs(cc).addAllAsflags(getAsflags(targetConfiguration, toolchainPaths)).setAspp(cpp).setCc(cc).addAllCflags(getCflagsInternal(targetConfiguration, toolchainPaths)).setCpp(cpp).addAllCppflags(getCppflags(targetConfiguration, toolchainPaths)).setCxx(cxx).addAllCxxflags(getCxxflagsInternal(targetConfiguration, toolchainPaths)).setCxxpp(cxxpp).addAllCxxppflags(getCxxppflags(targetConfiguration, toolchainPaths)).setLd(new DefaultLinkerProvider(LinkerProvider.Type.GNU, new ConstantToolProvider(getCcLinkTool(targetConfiguration, toolchainPaths, compilerType.getCxx(), version, cxxRuntime, executableFinder)))).addAllLdflags(targetConfiguration.getLinkerFlags(compilerType)).addLdflags(// We use it to find symbols from arbitrary binaries.
"-Wl,--build-id", // Enforce the NX (no execute) security feature
"-Wl,-z,noexecstack", // Strip unused code
"-Wl,--gc-sections", // Refuse to produce dynamic objects with undefined symbols
"-Wl,-z,defs", // Forbid dangerous copy "relocations"
"-Wl,-z,nocopyreloc", // means the resulting link will only use it if it was actually needed it.
"-Wl,--as-needed").setStrip(getGccTool(toolchainPaths, "strip", version, executableFinder)).setSymbolNameTool(new PosixNmSymbolNameTool(getGccTool(toolchainPaths, "nm", version, executableFinder))).setAr(new GnuArchiver(getGccTool(toolchainPaths, "ar", version, executableFinder))).setRanlib(getGccTool(toolchainPaths, "ranlib", version, executableFinder)).setCompilerDebugPathSanitizer(compilerDebugPathSanitizer).setAssemblerDebugPathSanitizer(assemblerDebugPathSanitizer).setSharedLibraryExtension("so").setSharedLibraryVersionedExtensionFormat("so.%s").setStaticLibraryExtension("a").setObjectFileExtension("o").setSharedLibraryInterfaceFactory(config.shouldUseSharedLibraryInterfaces() ? Optional.of(ElfSharedLibraryInterfaceFactory.of(new ConstantToolProvider(getGccTool(toolchainPaths, "objcopy", version, executableFinder)))) : Optional.empty());
// Add the NDK root path to the white-list so that headers from the NDK won't trigger the
// verification warnings. Ideally, long-term, we'd model NDK libs/headers via automatically
// generated nodes/descriptions so that they wouldn't need to special case it here.
HeaderVerification headerVerification = config.getHeaderVerification();
try {
headerVerification = headerVerification.withPlatformWhitelist(ImmutableList.of("^" + Pattern.quote(ndkRoot.toRealPath().toString() + File.separatorChar) + ".*"));
} catch (IOException e) {
LOG.warn(e, "NDK path could not be resolved: %s", ndkRoot);
}
cxxPlatformBuilder.setHeaderVerification(headerVerification);
LOG.debug("NDK root: %s", ndkRoot.toString());
LOG.debug("Headers verification platform whitelist: %s", headerVerification.getPlatformWhitelist());
if (cxxRuntime != CxxRuntime.SYSTEM) {
cxxPlatformBuilder.putRuntimeLdflags(Linker.LinkableDepType.SHARED, "-l" + cxxRuntime.getSharedName());
cxxPlatformBuilder.putRuntimeLdflags(Linker.LinkableDepType.STATIC, "-l" + cxxRuntime.getStaticName());
}
CxxPlatform cxxPlatform = cxxPlatformBuilder.build();
NdkCxxPlatform.Builder builder = NdkCxxPlatform.builder();
builder.setCxxPlatform(cxxPlatform).setCxxRuntime(cxxRuntime).setObjdump(getGccTool(toolchainPaths, "objdump", version, executableFinder));
if (cxxRuntime != CxxRuntime.SYSTEM) {
builder.setCxxSharedRuntimePath(toolchainPaths.getCxxRuntimeLibsDirectory().resolve(cxxRuntime.getSoname()));
}
return builder.build();
}
Aggregations