use of com.google.devtools.build.lib.collect.nestedset.NestedSet in project bazel by bazelbuild.
the class CppHelper method resolveLinkstamps.
/**
* Resolves the linkstamp collection from the {@code CcLinkParams} into a map.
*
* <p>Emits a warning on the rule if there are identical linkstamp artifacts with different
* compilation contexts.
*/
public static Map<Artifact, NestedSet<Artifact>> resolveLinkstamps(RuleErrorConsumer listener, CcLinkParams linkParams) {
Map<Artifact, NestedSet<Artifact>> result = new LinkedHashMap<>();
for (Linkstamp pair : linkParams.getLinkstamps()) {
Artifact artifact = pair.getArtifact();
if (result.containsKey(artifact)) {
listener.ruleWarning("rule inherits the '" + artifact.toDetailString() + "' linkstamp file from more than one cc_library rule");
}
result.put(artifact, pair.getDeclaredIncludeSrcs());
}
return result;
}
use of com.google.devtools.build.lib.collect.nestedset.NestedSet in project bazel by bazelbuild.
the class ProtobufSupport method getInputsToOutputsMap.
private ImmutableSetMultimap<ImmutableSet<Artifact>, Artifact> getInputsToOutputsMap() {
Iterable<ObjcProtoProvider> objcProtoProviders = getObjcProtoProviders();
Iterable<ProtoSourcesProvider> protoProviders = getProtoSourcesProviders();
ImmutableList.Builder<NestedSet<Artifact>> protoSets = new ImmutableList.Builder<NestedSet<Artifact>>();
// all the transitive groups of proto.
for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
protoSets.addAll(objcProtoProvider.getProtoGroups());
}
for (ProtoSourcesProvider protoProvider : protoProviders) {
protoSets.add(protoProvider.getTransitiveProtoSources());
}
HashMap<Artifact, Set<Artifact>> protoToGroupMap = new HashMap<>();
// group will be considered the smallest input group with which the proto can be generated.
for (NestedSet<Artifact> nestedProtoSet : protoSets.build()) {
ImmutableSet<Artifact> protoSet = ImmutableSet.copyOf(nestedProtoSet.toSet());
for (Artifact proto : protoSet) {
// generated with the runtime library.
if (attributes.isProtoWellKnown(proto)) {
continue;
}
if (!protoToGroupMap.containsKey(proto)) {
protoToGroupMap.put(proto, protoSet);
} else {
protoToGroupMap.put(proto, Sets.intersection(protoSet, protoToGroupMap.get(proto)));
}
}
}
// Now that we have the smallest proto inputs groups for each proto to be generated, we reverse
// that map into a multimap to take advantage of the fact that multiple protos can be generated
// with the same inputs, to avoid having multiple generation actions with the same inputs and
// different ouputs. This only applies for the generation actions, as the compilation actions
// compile one generated file at a time.
// It's OK to use ImmutableSet<Artifact> as the key, since Artifact caches it's hashCode, and
// ImmutableSet calculates it's hashCode in O(n).
ImmutableSetMultimap.Builder<ImmutableSet<Artifact>, Artifact> inputsToOutputsMapBuilder = ImmutableSetMultimap.builder();
for (Artifact proto : protoToGroupMap.keySet()) {
inputsToOutputsMapBuilder.put(ImmutableSet.copyOf(protoToGroupMap.get(proto)), proto);
}
return inputsToOutputsMapBuilder.build();
}
use of com.google.devtools.build.lib.collect.nestedset.NestedSet in project bazel by bazelbuild.
the class NativeDepsHelper method createNativeDepsAction.
public static NativeDepsRunfiles createNativeDepsAction(final RuleContext ruleContext, CcLinkParams linkParams, Collection<String> extraLinkOpts, BuildConfiguration configuration, CcToolchainProvider toolchain, Artifact nativeDeps, String libraryIdentifier, Root bindirIfShared, boolean useDynamicRuntime) throws InterruptedException {
Preconditions.checkState(ruleContext.isLegalFragment(CppConfiguration.class), "%s does not have access to CppConfiguration", ruleContext.getRule().getRuleClass());
List<String> linkopts = new ArrayList<>(extraLinkOpts);
linkopts.addAll(linkParams.flattenedLinkopts());
Map<Artifact, NestedSet<Artifact>> linkstamps = CppHelper.resolveLinkstamps(ruleContext, linkParams);
List<Artifact> buildInfoArtifacts = linkstamps.isEmpty() ? ImmutableList.<Artifact>of() : ruleContext.getAnalysisEnvironment().getBuildInfo(ruleContext, CppBuildInfo.KEY, configuration);
boolean shareNativeDeps = configuration.getFragment(CppConfiguration.class).shareNativeDeps();
NestedSet<LibraryToLink> linkerInputs = linkParams.getLibraries();
Artifact sharedLibrary;
if (shareNativeDeps) {
PathFragment sharedPath = getSharedNativeDepsPath(LinkerInputs.toLibraryArtifacts(linkerInputs), linkopts, linkstamps.keySet(), buildInfoArtifacts, ruleContext.getFeatures());
libraryIdentifier = sharedPath.getPathString();
sharedLibrary = ruleContext.getShareableArtifact(sharedPath.replaceName(sharedPath.getBaseName() + ".so"), configuration.getBinDirectory(ruleContext.getRule().getRepository()));
} else {
sharedLibrary = nativeDeps;
}
FdoSupportProvider fdoSupport = CppHelper.getFdoSupport(ruleContext, ":cc_toolchain");
CppLinkActionBuilder builder = new CppLinkActionBuilder(ruleContext, sharedLibrary, configuration, toolchain, fdoSupport);
if (useDynamicRuntime) {
builder.setRuntimeInputs(ArtifactCategory.DYNAMIC_LIBRARY, toolchain.getDynamicRuntimeLinkMiddleman(), toolchain.getDynamicRuntimeLinkInputs());
} else {
builder.setRuntimeInputs(ArtifactCategory.STATIC_LIBRARY, toolchain.getStaticRuntimeLinkMiddleman(), toolchain.getStaticRuntimeLinkInputs());
}
CppLinkAction linkAction = builder.setLinkArtifactFactory(SHAREABLE_LINK_ARTIFACT_FACTORY).setCrosstoolInputs(toolchain.getLink()).addLibraries(linkerInputs).setLinkType(LinkTargetType.DYNAMIC_LIBRARY).setLinkStaticness(LinkStaticness.MOSTLY_STATIC).setLibraryIdentifier(libraryIdentifier).addLinkopts(linkopts).setNativeDeps(true).addLinkstamps(linkstamps).build();
ruleContext.registerAction(linkAction);
Artifact linkerOutput = linkAction.getPrimaryOutput();
if (shareNativeDeps) {
// Collect dynamic-linker-resolvable symlinks for C++ runtime library dependencies.
// Note we only need these symlinks when --share_native_deps is on, as shared native deps
// mangle path names such that the library's conventional _solib RPATH entry
// no longer resolves (because the target directory's relative depth gets lost).
List<Artifact> runtimeSymlinks;
if (useDynamicRuntime) {
runtimeSymlinks = new LinkedList<>();
for (final Artifact runtimeInput : toolchain.getDynamicRuntimeLinkInputs()) {
final Artifact runtimeSymlink = ruleContext.getPackageRelativeArtifact(getRuntimeLibraryPath(ruleContext, runtimeInput), bindirIfShared);
// Since runtime library symlinks are underneath the target's output directory and
// multiple targets may share the same output directory, we need to make sure this
// symlink's generating action is only set once.
ruleContext.registerAction(new SymlinkAction(ruleContext.getActionOwner(), runtimeInput, runtimeSymlink, null));
runtimeSymlinks.add(runtimeSymlink);
}
} else {
runtimeSymlinks = ImmutableList.of();
}
ruleContext.registerAction(new SymlinkAction(ruleContext.getActionOwner(), linkerOutput, nativeDeps, null));
return new NativeDepsRunfiles(nativeDeps, runtimeSymlinks);
}
return new NativeDepsRunfiles(linkerOutput, ImmutableList.<Artifact>of());
}
use of com.google.devtools.build.lib.collect.nestedset.NestedSet in project bazel by bazelbuild.
the class CcLibraryHelper method build.
/**
* Create the C++ compile and link actions, and the corresponding C++-related providers.
*
* @throws RuleErrorException
*/
public Info build() throws RuleErrorException, InterruptedException {
// Fail early if there is no lipo context collector on the rule - otherwise we end up failing
// in lipo optimization.
Preconditions.checkState(// 'cc_inc_library' rules do not compile, and thus are not affected by LIPO.
ruleContext.getRule().getRuleClass().equals("cc_inc_library") || ruleContext.isAttrDefined(":lipo_context_collector", BuildType.LABEL));
if (checkDepsGenerateCpp) {
for (LanguageDependentFragment dep : AnalysisUtils.getProviders(deps, LanguageDependentFragment.class)) {
LanguageDependentFragment.Checker.depSupportsLanguage(ruleContext, dep, CppRuleClasses.LANGUAGE);
}
}
CppModel model = initializeCppModel();
CppCompilationContext cppCompilationContext = initializeCppCompilationContext(model);
model.setContext(cppCompilationContext);
boolean compileHeaderModules = featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULES);
Preconditions.checkState(!compileHeaderModules || cppCompilationContext.getCppModuleMap() != null, "All cc rules must support module maps.");
// Create compile actions (both PIC and non-PIC).
CcCompilationOutputs ccOutputs = model.createCcCompileActions();
if (!objectFiles.isEmpty() || !picObjectFiles.isEmpty()) {
// Merge the pre-compiled object files into the compiler outputs.
ccOutputs = new CcCompilationOutputs.Builder().merge(ccOutputs).addLTOBitcodeFile(ccOutputs.getLtoBitcodeFiles()).addObjectFiles(objectFiles).addPicObjectFiles(picObjectFiles).build();
}
// Create link actions (only if there are object files or if explicitly requested).
CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
if (emitLinkActions && (emitLinkActionsIfEmpty || !ccOutputs.isEmpty())) {
// generate any link actions, effectively disabling header checking in some cases.
if (linkType.staticness() == Staticness.STATIC) {
// TODO(bazel-team): This can't create the link action for a cc_binary yet.
ccLinkingOutputs = model.createCcLinkActions(ccOutputs, nonCodeLinkerInputs);
}
}
CcLinkingOutputs originalLinkingOutputs = ccLinkingOutputs;
if (!(staticLibraries.isEmpty() && picStaticLibraries.isEmpty() && dynamicLibraries.isEmpty())) {
CcLinkingOutputs.Builder newOutputsBuilder = new CcLinkingOutputs.Builder();
if (!ccOutputs.isEmpty()) {
// Add the linked outputs of this rule iff we had anything to put in them, but then
// make sure we're not colliding with some library added from the inputs.
newOutputsBuilder.merge(originalLinkingOutputs);
ImmutableSetMultimap<String, LibraryToLink> precompiledLibraryMap = CcLinkingOutputs.getLibrariesByIdentifier(Iterables.concat(staticLibraries, picStaticLibraries, dynamicLibraries));
ImmutableSetMultimap<String, LibraryToLink> linkedLibraryMap = originalLinkingOutputs.getLibrariesByIdentifier();
for (String matchingIdentifier : Sets.intersection(precompiledLibraryMap.keySet(), linkedLibraryMap.keySet())) {
Iterable<Artifact> matchingInputLibs = LinkerInputs.toNonSolibArtifacts(precompiledLibraryMap.get(matchingIdentifier));
Iterable<Artifact> matchingOutputLibs = LinkerInputs.toNonSolibArtifacts(linkedLibraryMap.get(matchingIdentifier));
ruleContext.ruleError("Can't put " + Joiner.on(", ").join(Iterables.transform(matchingInputLibs, FileType.TO_FILENAME)) + " into the srcs of a " + ruleContext.getRuleClassNameForLogging() + " with the same name (" + ruleContext.getRule().getName() + ") which also contains other code or objects to link; it shares a name with " + Joiner.on(", ").join(Iterables.transform(matchingOutputLibs, FileType.TO_FILENAME)) + " (output compiled and linked from the non-library sources of this rule), " + "which could cause confusion");
}
}
// Merge the pre-compiled libraries (static & dynamic) into the linker outputs.
ccLinkingOutputs = newOutputsBuilder.addStaticLibraries(staticLibraries).addPicStaticLibraries(picStaticLibraries).addDynamicLibraries(dynamicLibraries).addExecutionDynamicLibraries(dynamicLibraries).build();
}
DwoArtifactsCollector dwoArtifacts = DwoArtifactsCollector.transitiveCollector(ruleContext, ccOutputs, deps, /*generateDwo=*/
false, /*ltoBackendArtifactsUsePic=*/
false, /*ltoBackendArtifacts=*/
ImmutableList.<LTOBackendArtifacts>of());
Runfiles cppStaticRunfiles = collectCppRunfiles(ccLinkingOutputs, true);
Runfiles cppSharedRunfiles = collectCppRunfiles(ccLinkingOutputs, false);
// By very careful when adding new providers here - it can potentially affect a lot of rules.
// We should consider merging most of these providers into a single provider.
TransitiveInfoProviderMap.Builder providers = TransitiveInfoProviderMap.builder().add(new CppRunfilesProvider(cppStaticRunfiles, cppSharedRunfiles), cppCompilationContext, new CppDebugFileProvider(dwoArtifacts.getDwoArtifacts(), dwoArtifacts.getPicDwoArtifacts()), collectTransitiveLipoInfo(ccOutputs));
Map<String, NestedSet<Artifact>> outputGroups = new TreeMap<>();
if (shouldAddLinkerOutputArtifacts(ruleContext, ccOutputs)) {
addLinkerOutputArtifacts(outputGroups, ccOutputs);
}
outputGroups.put(OutputGroupProvider.TEMP_FILES, getTemps(ccOutputs));
CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
if (emitCompileProviders) {
boolean isLipoCollector = cppConfiguration.isLipoContextCollector();
boolean processHeadersInDependencies = cppConfiguration.processHeadersInDependencies();
boolean usePic = CppHelper.usePic(ruleContext, false);
outputGroups.put(OutputGroupProvider.FILES_TO_COMPILE, ccOutputs.getFilesToCompile(isLipoCollector, processHeadersInDependencies, usePic));
outputGroups.put(OutputGroupProvider.COMPILATION_PREREQUISITES, CcCommon.collectCompilationPrerequisites(ruleContext, cppCompilationContext));
}
// used.
if (emitCcNativeLibrariesProvider) {
providers.add(new CcNativeLibraryProvider(collectNativeCcLibraries(ccLinkingOutputs)));
}
providers.put(CcExecutionDynamicLibrariesProvider.class, collectExecutionDynamicLibraryArtifacts(ccLinkingOutputs.getExecutionDynamicLibraries()));
boolean forcePic = cppConfiguration.forcePic();
if (emitCcSpecificLinkParamsProvider) {
providers.add(new CcSpecificLinkParamsProvider(createCcLinkParamsStore(ccLinkingOutputs, cppCompilationContext, forcePic)));
} else {
providers.add(new CcLinkParamsProvider(createCcLinkParamsStore(ccLinkingOutputs, cppCompilationContext, forcePic)));
}
return new Info(providers.build(), outputGroups, ccOutputs, ccLinkingOutputs, originalLinkingOutputs, cppCompilationContext);
}
use of com.google.devtools.build.lib.collect.nestedset.NestedSet in project bazel by bazelbuild.
the class GenRuleBase method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException, InterruptedException {
NestedSet<Artifact> filesToBuild = NestedSetBuilder.wrap(Order.STABLE_ORDER, ruleContext.getOutputArtifacts());
NestedSetBuilder<Artifact> resolvedSrcsBuilder = NestedSetBuilder.stableOrder();
if (filesToBuild.isEmpty()) {
ruleContext.attributeError("outs", "Genrules without outputs don't make sense");
}
if (ruleContext.attributes().get("executable", Type.BOOLEAN) && Iterables.size(filesToBuild) > 1) {
ruleContext.attributeError("executable", "if genrules produce executables, they are allowed only one output. " + "If you need the executable=1 argument, then you should split this genrule into " + "genrules producing single outputs");
}
ImmutableMap.Builder<Label, NestedSet<Artifact>> labelMap = ImmutableMap.builder();
for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("srcs", Mode.TARGET)) {
// This target provides specific types of files for genrules.
GenRuleSourcesProvider provider = dep.getProvider(GenRuleSourcesProvider.class);
NestedSet<Artifact> files = (provider != null) ? provider.getGenruleFiles() : dep.getProvider(FileProvider.class).getFilesToBuild();
resolvedSrcsBuilder.addTransitive(files);
labelMap.put(AliasProvider.getDependencyLabel(dep), files);
}
NestedSet<Artifact> resolvedSrcs = resolvedSrcsBuilder.build();
CommandHelper commandHelper = new CommandHelper(ruleContext, ruleContext.getPrerequisites("tools", Mode.HOST), labelMap.build());
if (ruleContext.hasErrors()) {
return null;
}
String baseCommand = commandHelper.resolveCommandAndExpandLabels(ruleContext.attributes().get("heuristic_label_expansion", Type.BOOLEAN), false);
// Adds the genrule environment setup script before the actual shell command
String command = String.format("source %s; %s", ruleContext.getPrerequisiteArtifact("$genrule_setup", Mode.HOST).getExecPath(), baseCommand);
command = resolveCommand(command, ruleContext, resolvedSrcs, filesToBuild);
String message = ruleContext.attributes().get("message", Type.STRING);
if (message.isEmpty()) {
message = "Executing genrule";
}
ImmutableMap<String, String> env = ruleContext.getConfiguration().getLocalShellEnvironment();
ImmutableSet<String> clientEnvVars = ruleContext.getConfiguration().getVariableShellEnvironment();
Map<String, String> executionInfo = Maps.newLinkedHashMap();
executionInfo.putAll(TargetUtils.getExecutionInfo(ruleContext.getRule()));
if (ruleContext.attributes().get("local", Type.BOOLEAN)) {
executionInfo.put("local", "");
}
executionInfo.putAll(getExtraExecutionInfo(ruleContext, baseCommand));
NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
inputs.addAll(resolvedSrcs);
inputs.addAll(commandHelper.getResolvedTools());
FilesToRunProvider genruleSetup = ruleContext.getPrerequisite("$genrule_setup", Mode.HOST, FilesToRunProvider.class);
inputs.addAll(genruleSetup.getFilesToRun());
List<String> argv = commandHelper.buildCommandLine(command, inputs, ".genrule_script.sh", ImmutableMap.copyOf(executionInfo));
// TODO(bazel-team): Make the make variable expander pass back a list of these.
if (requiresCrosstool(baseCommand)) {
// If cc is used, silently throw in the crosstool filegroup as a dependency.
inputs.addTransitive(CppHelper.getToolchain(ruleContext, ":cc_toolchain").getCrosstoolMiddleman());
}
if (requiresJdk(baseCommand)) {
// If javac is used, silently throw in the jdk filegroup as a dependency.
// Note we expand Java-related variables with the *host* configuration.
inputs.addTransitive(JavaHelper.getHostJavabaseInputs(ruleContext));
}
for (NestedSet<Artifact> extraInputs : getExtraInputArtifacts(ruleContext, baseCommand)) {
inputs.addTransitive(extraInputs);
}
if (isStampingEnabled(ruleContext)) {
inputs.add(ruleContext.getAnalysisEnvironment().getStableWorkspaceStatusArtifact());
inputs.add(ruleContext.getAnalysisEnvironment().getVolatileWorkspaceStatusArtifact());
}
ruleContext.registerAction(new GenRuleAction(ruleContext.getActionOwner(), ImmutableList.copyOf(commandHelper.getResolvedTools()), inputs.build(), filesToBuild, argv, env, clientEnvVars, ImmutableMap.copyOf(executionInfo), new CompositeRunfilesSupplier(commandHelper.getToolsRunfilesSuppliers()), message + ' ' + ruleContext.getLabel()));
RunfilesProvider runfilesProvider = RunfilesProvider.withData(// No runfiles provided if not a data dependency.
Runfiles.EMPTY, // configuration.
new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).build());
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).setRunfilesSupport(null, getExecutable(ruleContext, filesToBuild)).addProvider(RunfilesProvider.class, runfilesProvider);
builder = updateBuilder(builder, ruleContext, filesToBuild);
return builder.build();
}
Aggregations