use of com.google.devtools.build.lib.util.Pair in project bazel by bazelbuild.
the class EagerInvalidatorTest method getValuesToInvalidate.
/** Returns a subset of {@code nodes} that are still valid and so can be invalidated. */
private Set<Pair<SkyKey, InvalidationType>> getValuesToInvalidate(SkyKey[] nodes) {
Set<Pair<SkyKey, InvalidationType>> result = new HashSet<>();
Random random = new Random(TestUtils.getRandomSeed());
for (SkyKey node : nodes) {
if (!isInvalidated(node)) {
if (result.isEmpty() || random.nextInt(3) == 0) {
// Add at least one node, if we can.
result.add(Pair.of(node, defaultInvalidationType()));
}
}
}
return result;
}
use of com.google.devtools.build.lib.util.Pair in project bazel by bazelbuild.
the class CcCommon method getSources.
/**
* Returns a list of ({@link Artifact}, {@link Label}) pairs. Each pair represents an input
* source file and the label of the rule that generates it (or the label of the source file
* itself if it is an input file).
*/
List<Pair<Artifact, Label>> getSources() {
Map<Artifact, Label> map = Maps.newLinkedHashMap();
Iterable<? extends TransitiveInfoCollection> providers = ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class);
for (TransitiveInfoCollection provider : providers) {
for (Artifact artifact : provider.getProvider(FileProvider.class).getFilesToBuild()) {
// TODO(bazel-team): We currently do not produce an error for duplicate headers and other
// non-source artifacts with different labels, as that would require cleaning up the code
// base without significant benefit; we should eventually make this consistent one way or
// the other.
Label oldLabel = map.put(artifact, provider.getLabel());
boolean isHeader = CppFileTypes.CPP_HEADER.matches(artifact.getExecPath());
if (!isHeader && SourceCategory.CC_AND_OBJC.getSourceTypes().matches(artifact.getExecPathString()) && oldLabel != null && !oldLabel.equals(provider.getLabel())) {
ruleContext.attributeError("srcs", String.format("Artifact '%s' is duplicated (through '%s' and '%s')", artifact.getExecPathString(), oldLabel, provider.getLabel()));
}
}
}
ImmutableList.Builder<Pair<Artifact, Label>> result = ImmutableList.builder();
for (Map.Entry<Artifact, Label> entry : map.entrySet()) {
result.add(Pair.of(entry.getKey(), entry.getValue()));
}
return result.build();
}
use of com.google.devtools.build.lib.util.Pair in project bazel by bazelbuild.
the class CcCommon method getHeaders.
/**
* Returns the files from headers and does some sanity checks. Note that this method reports
* warnings to the {@link RuleContext} as a side effect, and so should only be called once for any
* given rule.
*/
public static List<Pair<Artifact, Label>> getHeaders(RuleContext ruleContext) {
Map<Artifact, Label> map = Maps.newLinkedHashMap();
for (TransitiveInfoCollection target : ruleContext.getPrerequisitesIf("hdrs", Mode.TARGET, FileProvider.class)) {
FileProvider provider = target.getProvider(FileProvider.class);
for (Artifact artifact : provider.getFilesToBuild()) {
if (CppRuleClasses.DISALLOWED_HDRS_FILES.matches(artifact.getFilename())) {
ruleContext.attributeWarning("hdrs", "file '" + artifact.getFilename() + "' from target '" + target.getLabel() + "' is not allowed in hdrs");
continue;
}
Label oldLabel = map.put(artifact, target.getLabel());
if (oldLabel != null && !oldLabel.equals(target.getLabel())) {
ruleContext.attributeWarning("hdrs", String.format("Artifact '%s' is duplicated (through '%s' and '%s')", artifact.getExecPathString(), oldLabel, target.getLabel()));
}
}
}
ImmutableList.Builder<Pair<Artifact, Label>> result = ImmutableList.builder();
for (Map.Entry<Artifact, Label> entry : map.entrySet()) {
result.add(Pair.of(entry.getKey(), entry.getValue()));
}
return result.build();
}
use of com.google.devtools.build.lib.util.Pair in project bazel by bazelbuild.
the class CcToolchain method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException, InterruptedException {
TransitiveInfoCollection lipoContextCollector = ruleContext.getPrerequisite(":lipo_context_collector", Mode.DONT_CHECK);
if (lipoContextCollector != null && lipoContextCollector.getProvider(LipoContextProvider.class) == null) {
ruleContext.ruleError("--lipo_context must point to a cc_binary or a cc_test rule");
return null;
}
CppConfiguration cppConfiguration = Preconditions.checkNotNull(ruleContext.getFragment(CppConfiguration.class));
Path fdoZip = ruleContext.getConfiguration().getCompilationMode() == CompilationMode.OPT ? cppConfiguration.getFdoZip() : null;
SkyKey fdoKey = FdoSupportValue.key(cppConfiguration.getLipoMode(), fdoZip, cppConfiguration.getFdoInstrument());
SkyFunction.Environment skyframeEnv = ruleContext.getAnalysisEnvironment().getSkyframeEnv();
FdoSupportValue fdoSupport;
try {
fdoSupport = (FdoSupportValue) skyframeEnv.getValueOrThrow(fdoKey, FdoException.class, IOException.class);
} catch (FdoException | IOException e) {
ruleContext.ruleError("cannot initialize FDO: " + e.getMessage());
return null;
}
if (skyframeEnv.valuesMissing()) {
return null;
}
final Label label = ruleContext.getLabel();
final NestedSet<Artifact> crosstool = ruleContext.getPrerequisite("all_files", Mode.HOST).getProvider(FileProvider.class).getFilesToBuild();
final NestedSet<Artifact> crosstoolMiddleman = getFiles(ruleContext, "all_files");
final NestedSet<Artifact> compile = getFiles(ruleContext, "compiler_files");
final NestedSet<Artifact> strip = getFiles(ruleContext, "strip_files");
final NestedSet<Artifact> objcopy = getFiles(ruleContext, "objcopy_files");
final NestedSet<Artifact> link = getFiles(ruleContext, "linker_files");
final NestedSet<Artifact> dwp = getFiles(ruleContext, "dwp_files");
final NestedSet<Artifact> libcLink = inputsForLibc(ruleContext);
String purposePrefix = Actions.escapeLabel(label) + "_";
String runtimeSolibDirBase = "_solib_" + "_" + Actions.escapeLabel(label);
final PathFragment runtimeSolibDir = ruleContext.getConfiguration().getBinFragment().getRelative(runtimeSolibDirBase);
// Static runtime inputs.
TransitiveInfoCollection staticRuntimeLibDep = selectDep(ruleContext, "static_runtime_libs", cppConfiguration.getStaticRuntimeLibsLabel());
final NestedSet<Artifact> staticRuntimeLinkInputs;
final Artifact staticRuntimeLinkMiddleman;
if (cppConfiguration.supportsEmbeddedRuntimes()) {
staticRuntimeLinkInputs = staticRuntimeLibDep.getProvider(FileProvider.class).getFilesToBuild();
} else {
staticRuntimeLinkInputs = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
if (!staticRuntimeLinkInputs.isEmpty()) {
NestedSet<Artifact> staticRuntimeLinkMiddlemanSet = CompilationHelper.getAggregatingMiddleman(ruleContext, purposePrefix + "static_runtime_link", staticRuntimeLibDep);
staticRuntimeLinkMiddleman = staticRuntimeLinkMiddlemanSet.isEmpty() ? null : Iterables.getOnlyElement(staticRuntimeLinkMiddlemanSet);
} else {
staticRuntimeLinkMiddleman = null;
}
Preconditions.checkState((staticRuntimeLinkMiddleman == null) == staticRuntimeLinkInputs.isEmpty());
// Dynamic runtime inputs.
TransitiveInfoCollection dynamicRuntimeLibDep = selectDep(ruleContext, "dynamic_runtime_libs", cppConfiguration.getDynamicRuntimeLibsLabel());
NestedSet<Artifact> dynamicRuntimeLinkSymlinks;
List<Artifact> dynamicRuntimeLinkInputs = new ArrayList<>();
Artifact dynamicRuntimeLinkMiddleman;
if (cppConfiguration.supportsEmbeddedRuntimes()) {
NestedSetBuilder<Artifact> dynamicRuntimeLinkSymlinksBuilder = NestedSetBuilder.stableOrder();
for (Artifact artifact : dynamicRuntimeLibDep.getProvider(FileProvider.class).getFilesToBuild()) {
if (CppHelper.SHARED_LIBRARY_FILETYPES.matches(artifact.getFilename())) {
dynamicRuntimeLinkInputs.add(artifact);
dynamicRuntimeLinkSymlinksBuilder.add(SolibSymlinkAction.getCppRuntimeSymlink(ruleContext, artifact, runtimeSolibDirBase, ruleContext.getConfiguration()));
}
}
dynamicRuntimeLinkSymlinks = dynamicRuntimeLinkSymlinksBuilder.build();
} else {
dynamicRuntimeLinkSymlinks = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
if (!dynamicRuntimeLinkInputs.isEmpty()) {
List<Artifact> dynamicRuntimeLinkMiddlemanSet = CppHelper.getAggregatingMiddlemanForCppRuntimes(ruleContext, purposePrefix + "dynamic_runtime_link", dynamicRuntimeLinkInputs, runtimeSolibDirBase, ruleContext.getConfiguration());
dynamicRuntimeLinkMiddleman = dynamicRuntimeLinkMiddlemanSet.isEmpty() ? null : Iterables.getOnlyElement(dynamicRuntimeLinkMiddlemanSet);
} else {
dynamicRuntimeLinkMiddleman = null;
}
Preconditions.checkState((dynamicRuntimeLinkMiddleman == null) == dynamicRuntimeLinkSymlinks.isEmpty());
CppCompilationContext.Builder contextBuilder = new CppCompilationContext.Builder(ruleContext);
CppModuleMap moduleMap = createCrosstoolModuleMap(ruleContext);
if (moduleMap != null) {
contextBuilder.setCppModuleMap(moduleMap);
}
final CppCompilationContext context = contextBuilder.build();
boolean supportsParamFiles = ruleContext.attributes().get("supports_param_files", BOOLEAN);
boolean supportsHeaderParsing = ruleContext.attributes().get("supports_header_parsing", BOOLEAN);
NestedSetBuilder<Pair<String, String>> coverageEnvironment = NestedSetBuilder.compileOrder();
coverageEnvironment.add(Pair.of("COVERAGE_GCOV_PATH", cppConfiguration.getGcovExecutable().getPathString()));
if (cppConfiguration.getFdoInstrument() != null) {
coverageEnvironment.add(Pair.of("FDO_DIR", cppConfiguration.getFdoInstrument().getPathString()));
}
CcToolchainProvider provider = new CcToolchainProvider(cppConfiguration, crosstool, fullInputsForCrosstool(ruleContext, crosstoolMiddleman), compile, strip, objcopy, fullInputsForLink(ruleContext, link), ruleContext.getPrerequisiteArtifact("$interface_library_builder", Mode.HOST), dwp, libcLink, staticRuntimeLinkInputs, staticRuntimeLinkMiddleman, dynamicRuntimeLinkSymlinks, dynamicRuntimeLinkMiddleman, runtimeSolibDir, context, supportsParamFiles, supportsHeaderParsing, getBuildVariables(ruleContext), getBuiltinIncludes(ruleContext), coverageEnvironment.build(), ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool", Mode.HOST), getEnvironment(ruleContext));
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext).add(CcToolchainProvider.class, provider).add(FdoSupportProvider.class, fdoSupport.getFdoSupport().createFdoSupportProvider(ruleContext)).setFilesToBuild(new NestedSetBuilder<Artifact>(Order.STABLE_ORDER).build()).add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));
// If output_license is specified on the cc_toolchain rule, override the transitive licenses
// with that one. This is necessary because cc_toolchain is used in the target configuration,
// but it is sort-of-kind-of a tool, but various parts of it are linked into the output...
// ...so we trust the judgment of the author of the cc_toolchain rule to figure out what
// licenses should be propagated to C++ targets.
// TODO(elenairina): Remove this and use Attribute.Builder.useOutputLicenses() on the
// :cc_toolchain attribute instead.
final License outputLicense = ruleContext.getRule().getToolOutputLicense(ruleContext.attributes());
if (outputLicense != null && outputLicense != License.NO_LICENSE) {
final NestedSet<TargetLicense> license = NestedSetBuilder.create(Order.STABLE_ORDER, new TargetLicense(ruleContext.getLabel(), outputLicense));
LicensesProvider licensesProvider = new LicensesProvider() {
@Override
public NestedSet<TargetLicense> getTransitiveLicenses() {
return license;
}
@Override
public TargetLicense getOutputLicenses() {
return new TargetLicense(label, outputLicense);
}
@Override
public boolean hasOutputLicenses() {
return true;
}
};
builder.add(LicensesProvider.class, licensesProvider);
}
return builder.build();
}
use of com.google.devtools.build.lib.util.Pair in project bazel by bazelbuild.
the class ArtifactFunction method createAggregatingValue.
private static AggregatingArtifactValue createAggregatingValue(Artifact artifact, ActionAnalysisMetadata action, FileArtifactValue value, SkyFunction.Environment env) throws InterruptedException {
// This artifact aggregates other artifacts. Keep track of them so callers can find them.
ImmutableList.Builder<Pair<Artifact, FileArtifactValue>> inputs = ImmutableList.builder();
for (Map.Entry<SkyKey, SkyValue> entry : env.getValues(ArtifactSkyKey.mandatoryKeys(action.getInputs())).entrySet()) {
Artifact input = ArtifactSkyKey.artifact(entry.getKey());
SkyValue inputValue = entry.getValue();
Preconditions.checkNotNull(inputValue, "%s has null dep %s", artifact, input);
if (!(inputValue instanceof FileArtifactValue)) {
// We do not recurse in aggregating middleman artifacts.
Preconditions.checkState(!(inputValue instanceof AggregatingArtifactValue), "%s %s %s", artifact, action, inputValue);
continue;
}
inputs.add(Pair.of(input, (FileArtifactValue) inputValue));
}
return new AggregatingArtifactValue(inputs.build(), value);
}
Aggregations