C++: Remove usages of rule context from CcCompilationHelper
The Starlark API won't take the rule context. Here we replace usages of rule context in CcCompilationHelper with other fields. We still pass the context to CcCompilationHelper because this will be done incrementally over several CLs.
RELNOTES:none
PiperOrigin-RevId: 231939377
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
index 4e00092..5b2dbf4 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -751,10 +751,7 @@
return getUniqueDirectoryArtifact(uniqueDirectorySuffix, relative, getBinOrGenfilesDirectory());
}
- /**
- * Creates an artifact in a directory that is unique to the rule, thus guaranteeing that it never
- * clashes with artifacts created by other rules.
- */
+ @Override
public Artifact getUniqueDirectoryArtifact(
String uniqueDirectory, PathFragment relative, ArtifactRoot root) {
return getDerivedArtifact(getUniqueDirectory(uniqueDirectory).getRelative(relative), root);
@@ -1402,10 +1399,7 @@
return outs.get(0);
}
- /**
- * Returns an artifact with a given file extension. All other path components
- * are the same as in {@code pathFragment}.
- */
+ @Override
public final Artifact getRelatedArtifact(PathFragment pathFragment, String extension) {
PathFragment file = FileSystemUtils.replaceExtension(pathFragment, extension);
return getDerivedArtifact(file, getConfiguration().getBinDirectory(rule.getRepository()));
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
index ce9c327..5fed24e9 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
@@ -73,6 +73,12 @@
Artifact getImplicitOutputArtifact(ImplicitOutputsFunction function) throws InterruptedException;
/**
+ * Returns an artifact with a given file extension. All other path components are the same as in
+ * {@code pathFragment}.
+ */
+ Artifact getRelatedArtifact(PathFragment pathFragment, String extension);
+
+ /**
* Creates an artifact in a directory that is unique to the rule, thus guaranteeing that it never
* clashes with artifacts created by other rules.
*
@@ -89,6 +95,13 @@
Artifact getUniqueDirectoryArtifact(String uniqueDirectorySuffix, PathFragment relative);
/**
+ * Creates an artifact in a directory that is unique to the rule, thus guaranteeing that it never
+ * clashes with artifacts created by other rules.
+ */
+ Artifact getUniqueDirectoryArtifact(
+ String uniqueDirectory, PathFragment relative, ArtifactRoot root);
+
+ /**
* Returns a path fragment qualified by the rule name and unique fragment to
* disambiguate artifacts produced from the source file appearing in
* multiple rules.
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
index 9853f7b..df507ac 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
@@ -610,7 +610,8 @@
return false;
}
if (targetUnchecked == Runtime.NONE) {
- return InstrumentedFilesCollector.shouldIncludeLocalSources(ruleContext);
+ return InstrumentedFilesCollector.shouldIncludeLocalSources(
+ ruleContext.getConfiguration(), ruleContext.getLabel(), ruleContext.isTestTarget());
}
TransitiveInfoCollection target = (TransitiveInfoCollection) targetUnchecked;
return (target.get(InstrumentedFilesInfo.SKYLARK_CONSTRUCTOR) != null)
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java
index e10f4fc..4bd6348 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java
@@ -158,7 +158,8 @@
// Local sources.
NestedSet<Artifact> localSources = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
- if (shouldIncludeLocalSources(ruleContext)) {
+ if (shouldIncludeLocalSources(
+ ruleContext.getConfiguration(), ruleContext.getLabel(), ruleContext.isTestTarget())) {
NestedSetBuilder<Artifact> localSourcesBuilder = NestedSetBuilder.stableOrder();
for (TransitiveInfoCollection dep :
getAllPrerequisites(ruleContext, spec.sourceAttributes)) {
@@ -205,15 +206,6 @@
}
/**
- * Return whether the sources of the rule in {@code ruleContext} should be instrumented based on
- * the --instrumentation_filter and --instrument_test_targets config settings.
- */
- public static boolean shouldIncludeLocalSources(RuleContext ruleContext) {
- return shouldIncludeLocalSources(ruleContext.getConfiguration(), ruleContext.getLabel(),
- ruleContext.isTestTarget());
- }
-
- /**
* Return whether the sources included by {@code target} (a {@link TransitiveInfoCollection}
* representing a rule) should be instrumented according the --instrumentation_filter and
* --instrument_test_targets settings in {@code config}.
@@ -224,8 +216,12 @@
target.getProvider(TestProvider.class) != null);
}
- private static boolean shouldIncludeLocalSources(BuildConfiguration config, Label label,
- boolean isTest) {
+ /**
+ * Return whether the sources of the rule in {@code ruleContext} should be instrumented based on
+ * the --instrumentation_filter and --instrument_test_targets config settings.
+ */
+ public static boolean shouldIncludeLocalSources(
+ BuildConfiguration config, Label label, boolean isTest) {
return ((config.shouldInstrumentTestTargets() || !isTest)
&& config.getInstrumentationFilter().isIncluded(label.toString()));
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
index 1d2331a..fc8e4e6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
@@ -1031,7 +1031,8 @@
ccCompilationContext.getVirtualToOriginalHeaders());
NestedSet<Artifact> headerTokens =
- CcCompilationHelper.collectHeaderTokens(ruleContext, ccCompilationOutputs);
+ CcCompilationHelper.collectHeaderTokens(
+ ruleContext, cppConfiguration, ccCompilationOutputs);
NestedSet<Artifact> filesToCompile =
ccCompilationOutputs.getFilesToCompile(
cppConfiguration.processHeadersInDependencies(),
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationContext.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationContext.java
index f3288d9..9b4accf 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationContext.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationContext.java
@@ -23,8 +23,10 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.MiddlemanFactory;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
-import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
+import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
@@ -53,7 +55,10 @@
// TODO(b/77669139): Rename to CcCompilationContext.
public final class CcCompilationContext implements CcCompilationContextApi {
/** An empty {@code CcCompilationContext}. */
- public static final CcCompilationContext EMPTY = new Builder(null).build();
+ public static final CcCompilationContext EMPTY =
+ new Builder(
+ /* actionConstructionContext= */ null, /* configuration= */ null, /* label= */ null)
+ .build();
private final CommandLineCcCompilationContext commandLineCcCompilationContext;
@@ -417,7 +422,8 @@
public static CcCompilationContext merge(Collection<CcCompilationContext> ccCompilationContexts) {
CcCompilationContext.Builder builder =
- new CcCompilationContext.Builder(/* ruleContext= */ null);
+ new CcCompilationContext.Builder(
+ /* actionConstructionContext= */ null, /* configuration= */ null, /* label= */ null);
builder.mergeDependentCcCompilationContexts(ccCompilationContexts);
return builder.build();
}
@@ -479,11 +485,19 @@
NestedSetBuilder.stableOrder();
/** The rule that owns the context */
- private final RuleContext ruleContext;
+ private final ActionConstructionContext actionConstructionContext;
+
+ private final BuildConfiguration configuration;
+ private final Label label;
/** Creates a new builder for a {@link CcCompilationContext} instance. */
- public Builder(RuleContext ruleContext) {
- this.ruleContext = ruleContext;
+ public Builder(
+ ActionConstructionContext actionConstructionContext,
+ BuildConfiguration configuration,
+ Label label) {
+ this.actionConstructionContext = actionConstructionContext;
+ this.configuration = configuration;
+ this.label = label;
}
/**
@@ -717,8 +731,10 @@
/** Builds the {@link CcCompilationContext}. */
public CcCompilationContext build() {
return build(
- ruleContext == null ? null : ruleContext.getActionOwner(),
- ruleContext == null ? null : ruleContext.getAnalysisEnvironment().getMiddlemanFactory());
+ actionConstructionContext == null ? null : actionConstructionContext.getActionOwner(),
+ actionConstructionContext == null
+ ? null
+ : actionConstructionContext.getAnalysisEnvironment().getMiddlemanFactory());
}
@VisibleForTesting // productionVisibility = Visibility.PRIVATE
@@ -782,13 +798,13 @@
// Such middleman will be ignored by the dependency checker yet will still
// represent an edge in the action dependency graph - forcing proper execution
// order and error propagation.
- String name =
- cppModuleMap != null ? cppModuleMap.getName() : ruleContext.getLabel().toString();
+ String name = cppModuleMap != null ? cppModuleMap.getName() : label.toString();
return middlemanFactory.createErrorPropagatingMiddleman(
- owner, name, purpose,
+ owner,
+ name,
+ purpose,
ImmutableList.copyOf(compilationPrerequisites),
- ruleContext.getConfiguration().getMiddlemanDirectory(
- ruleContext.getRule().getRepository()));
+ configuration.getMiddlemanDirectory(label.getPackageIdentifier().getRepository()));
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
index 84b8038..e4dfe36 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
@@ -25,13 +25,14 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
+import com.google.devtools.build.lib.actions.ActionRegistry;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
-import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.OutputGroupInfo;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
+import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.SymlinkAction;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.PerLabelOptions;
@@ -45,6 +46,7 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
+import com.google.devtools.build.lib.packages.RuleErrorConsumer;
import com.google.devtools.build.lib.rules.cpp.CcCommon.CoptsFilter;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.VariablesExtension;
@@ -294,6 +296,11 @@
// TODO(plf): Pull out of class.
private CcCompilationContext ccCompilationContext;
+ private final RuleErrorConsumer ruleErrorConsumer;
+ private final ActionRegistry actionRegistry;
+ private final ActionConstructionContext actionConstructionContext;
+ private final Label label;
+
/**
* Creates a CcCompilationHelper.
*
@@ -355,6 +362,10 @@
setGeneratePicAction(
ccToolchain.usePicForDynamicLibraries(featureConfiguration)
|| CppHelper.usePicForBinaries(ruleContext, ccToolchain, featureConfiguration));
+ ruleErrorConsumer = ruleContext;
+ actionRegistry = ruleContext;
+ actionConstructionContext = ruleContext;
+ label = ruleContext.getLabel();
}
/**
@@ -396,7 +407,7 @@
*/
public CcCompilationHelper addPublicHeaders(Collection<Artifact> headers) {
for (Artifact header : headers) {
- addHeader(header, ruleContext.getLabel());
+ addHeader(header, label);
}
return this;
}
@@ -447,7 +458,7 @@
public CcCompilationHelper addPrivateHeaders(Collection<Artifact> privateHeaders) {
for (Artifact privateHeader : privateHeaders) {
- addPrivateHeader(privateHeader, ruleContext.getLabel());
+ addPrivateHeader(privateHeader, label);
}
return this;
}
@@ -481,7 +492,7 @@
*/
public CcCompilationHelper addSources(Collection<Artifact> sources) {
for (Artifact source : sources) {
- addSource(source, ruleContext.getLabel());
+ addSource(source, label);
}
return this;
}
@@ -748,11 +759,11 @@
*/
public CompilationInfo compile() throws RuleErrorException {
if (checkDepsGenerateCpp) {
- CppHelper.checkProtoLibrariesInDeps(ruleContext, deps);
+ CppHelper.checkProtoLibrariesInDeps(ruleErrorConsumer, deps);
}
if (!generatePicAction && !generateNoPicAction) {
- ruleContext.ruleError("Either PIC or no PIC actions have to be created.");
+ ruleErrorConsumer.ruleError("Either PIC or no PIC actions have to be created.");
}
ccCompilationContext = initializeCcCompilationContext();
@@ -829,32 +840,31 @@
if (includePrefix != null) {
prefix = PathFragment.create(includePrefix);
if (PathFragment.containsUplevelReferences(includePrefix)) {
- ruleContext.ruleError("include prefix should not contain uplevel references");
+ ruleErrorConsumer.ruleError("include prefix should not contain uplevel references");
}
if (prefix.isAbsolute()) {
- ruleContext.ruleError("include prefix should be a relative path");
+ ruleErrorConsumer.ruleError("include prefix should be a relative path");
}
}
PathFragment stripPrefix;
if (stripIncludePrefix != null) {
if (PathFragment.containsUplevelReferences(stripIncludePrefix)) {
- ruleContext.ruleError("strip include prefix should not contain uplevel references");
+ ruleErrorConsumer.ruleError("strip include prefix should not contain uplevel references");
}
stripPrefix = PathFragment.create(stripIncludePrefix);
if (stripPrefix.isAbsolute()) {
stripPrefix =
- ruleContext
- .getLabel()
+ label
.getPackageIdentifier()
.getRepository()
.getSourceRoot()
.getRelative(stripPrefix.toRelative());
} else {
- stripPrefix = ruleContext.getPackageDirectory().getRelative(stripPrefix);
+ stripPrefix = actionConstructionContext.getPackageDirectory().getRelative(stripPrefix);
}
} else if (prefix != null) {
- stripPrefix = ruleContext.getPackageDirectory();
+ stripPrefix = actionConstructionContext.getPackageDirectory();
} else {
stripPrefix = null;
}
@@ -868,7 +878,7 @@
/* virtualToOriginalHeaders= */ NestedSetBuilder.create(Order.STABLE_ORDER));
}
- if (ruleContext.hasErrors()) {
+ if (ruleErrorConsumer.hasErrors()) {
return new PublicHeaders(
/* headers= */ ImmutableList.of(),
/* moduleMapHeaders */ ImmutableList.of(),
@@ -881,7 +891,7 @@
NestedSetBuilder.stableOrder();
for (Artifact originalHeader : publicHeaders) {
if (!originalHeader.getRootRelativePath().startsWith(stripPrefix)) {
- ruleContext.ruleError(
+ ruleErrorConsumer.ruleError(
String.format(
"header '%s' is not under the specified strip prefix '%s'",
originalHeader.getExecPathString(), stripPrefix.getPathString()));
@@ -895,13 +905,16 @@
if (!originalHeader.getExecPath().equals(includePath)) {
Artifact virtualHeader =
- ruleContext.getUniqueDirectoryArtifact(
- "_virtual_includes", includePath, ruleContext.getBinOrGenfilesDirectory());
- ruleContext.registerAction(SymlinkAction.toArtifact(
- ruleContext.getActionOwner(),
- originalHeader,
- virtualHeader,
- "Symlinking virtual headers for " + ruleContext.getLabel()));
+ actionConstructionContext.getUniqueDirectoryArtifact(
+ "_virtual_includes",
+ includePath,
+ actionConstructionContext.getBinOrGenfilesDirectory());
+ actionRegistry.registerAction(
+ SymlinkAction.toArtifact(
+ actionConstructionContext.getActionOwner(),
+ originalHeader,
+ virtualHeader,
+ "Symlinking virtual headers for " + label));
moduleHeadersBuilder.add(virtualHeader);
if (configuration.isCodeCoverageEnabled()) {
virtualToOriginalHeaders.add(
@@ -922,10 +935,12 @@
return new PublicHeaders(
virtualHeaders,
moduleMapHeaders,
- ruleContext
+ actionConstructionContext
.getBinOrGenfilesDirectory()
.getExecPath()
- .getRelative(ruleContext.getUniqueDirectory("_virtual_includes")),
+ .getRelative(
+ actionConstructionContext.getUniqueDirectory(
+ PathFragment.create("_virtual_includes"))),
virtualToOriginalHeaders.build());
}
@@ -934,7 +949,7 @@
*/
private CcCompilationContext initializeCcCompilationContext() {
CcCompilationContext.Builder ccCompilationContextBuilder =
- new CcCompilationContext.Builder(ruleContext);
+ new CcCompilationContext.Builder(actionConstructionContext, configuration, label);
// Setup the include path; local include directories come before those inherited from deps or
// from the toolchain; in case of aliasing (same include file found on different entries),
@@ -945,12 +960,12 @@
// before the genfilesFragment to preferably pick up source files. Otherwise
// we might pick up stale generated files.
PathFragment repositoryPath =
- ruleContext.getLabel().getPackageIdentifier().getRepository().getPathUnderExecRoot();
+ label.getPackageIdentifier().getRepository().getPathUnderExecRoot();
ccCompilationContextBuilder.addQuoteIncludeDir(repositoryPath);
ccCompilationContextBuilder.addQuoteIncludeDir(
- ruleContext.getConfiguration().getGenfilesFragment().getRelative(repositoryPath));
+ configuration.getGenfilesFragment().getRelative(repositoryPath));
ccCompilationContextBuilder.addQuoteIncludeDir(
- ruleContext.getConfiguration().getBinFragment().getRelative(repositoryPath));
+ configuration.getBinFragment().getRelative(repositoryPath));
ccCompilationContextBuilder.addSystemIncludeDirs(systemIncludeDirs);
@@ -992,8 +1007,7 @@
// Add this package's dir to declaredIncludeDirs, & this rule's headers to declaredIncludeSrcs
// Note: no include dir for STRICT mode.
if (headersCheckingMode == HeadersCheckingMode.LOOSE) {
- ccCompilationContextBuilder.addDeclaredIncludeDir(
- ruleContext.getLabel().getPackageFragment());
+ ccCompilationContextBuilder.addDeclaredIncludeDir(label.getPackageFragment());
for (PathFragment looseIncludeDir : looseIncludeDirs) {
ccCompilationContextBuilder.addDeclaredIncludeDir(looseIncludeDir);
}
@@ -1002,7 +1016,9 @@
if (featureConfiguration.isEnabled(CppRuleClasses.MODULE_MAPS)) {
if (cppModuleMap == null) {
- cppModuleMap = CppHelper.createDefaultCppModuleMap(ruleContext, /*suffix=*/ "");
+ cppModuleMap =
+ CppHelper.createDefaultCppModuleMap(
+ actionConstructionContext, configuration, label, /*suffix=*/ "");
}
ccCompilationContextBuilder.setPropagateCppModuleMapAsActionInput(
@@ -1021,11 +1037,11 @@
if (generateModuleMap) {
Optional<Artifact> umbrellaHeader = cppModuleMap.getUmbrellaHeader();
if (umbrellaHeader.isPresent()) {
- ruleContext.registerAction(
+ actionRegistry.registerAction(
createUmbrellaHeaderAction(umbrellaHeader.get(), publicHeaders));
}
- ruleContext.registerAction(
+ actionRegistry.registerAction(
createModuleMapAction(cppModuleMap, publicHeaders, dependentModuleMaps, compiled));
}
if (getGeneratesPicHeaderModule()) {
@@ -1043,8 +1059,9 @@
// modules ready, but we don't use the corresponding module map or compiled file anywhere
// else.
CppModuleMap verificationMap =
- CppHelper.createDefaultCppModuleMap(ruleContext, /*suffix=*/ ".verify");
- ruleContext.registerAction(
+ CppHelper.createDefaultCppModuleMap(
+ actionConstructionContext, configuration, label, /*suffix=*/ ".verify");
+ actionRegistry.registerAction(
createModuleMapAction(
verificationMap, publicHeaders, dependentModuleMaps, /*compiledModule=*/ true));
ccCompilationContextBuilder.setVerificationModuleMap(verificationMap);
@@ -1060,13 +1077,15 @@
* Collects all preprocessed header files (*.h.processed) from dependencies and the current rule.
*/
public static NestedSet<Artifact> collectHeaderTokens(
- RuleContext ruleContext, CcCompilationOutputs ccCompilationOutputs) {
+ RuleContext ruleContext,
+ CppConfiguration cppConfiguration,
+ CcCompilationOutputs ccCompilationOutputs) {
NestedSetBuilder<Artifact> headerTokens = NestedSetBuilder.stableOrder();
for (OutputGroupInfo dep :
ruleContext.getPrerequisites("deps", Mode.TARGET, OutputGroupInfo.SKYLARK_CONSTRUCTOR)) {
headerTokens.addTransitive(dep.getOutputGroup(CcCompilationHelper.HIDDEN_HEADER_TOKENS));
}
- if (ruleContext.getFragment(CppConfiguration.class).processHeadersInDependencies()) {
+ if (cppConfiguration.processHeadersInDependencies()) {
headerTokens.addAll(ccCompilationOutputs.getHeaderTokenFiles());
}
return headerTokens.build();
@@ -1097,7 +1116,7 @@
private UmbrellaHeaderAction createUmbrellaHeaderAction(
Artifact umbrellaHeader, PublicHeaders publicHeaders) {
return new UmbrellaHeaderAction(
- ruleContext.getActionOwner(),
+ actionConstructionContext.getActionOwner(),
umbrellaHeader,
featureConfiguration.isEnabled(CppRuleClasses.ONLY_DOTH_HEADERS_IN_MODULE_MAPS)
? Iterables.filter(publicHeaders.getModuleMapHeaders(), CppFileTypes.MODULE_MAP_HEADER)
@@ -1111,7 +1130,7 @@
Iterable<CppModuleMap> dependentModuleMaps,
boolean compiledModule) {
return new CppModuleMapAction(
- ruleContext.getActionOwner(),
+ actionConstructionContext.getActionOwner(),
moduleMap,
featureConfiguration.isEnabled(CppRuleClasses.EXCLUDE_PRIVATE_HEADERS_IN_MODULE_MAPS)
? ImmutableList.of()
@@ -1173,18 +1192,18 @@
/** @return the no-PIC header module artifact for the current target. */
private Artifact getHeaderModule(Artifact moduleMapArtifact) {
- PathFragment objectDir = CppHelper.getObjDirectory(ruleContext.getLabel());
+ PathFragment objectDir = CppHelper.getObjDirectory(label);
PathFragment outputName =
objectDir.getRelative(moduleMapArtifact.getRootRelativePath().getBaseName());
- return ruleContext.getRelatedArtifact(outputName, ".pcm");
+ return actionConstructionContext.getRelatedArtifact(outputName, ".pcm");
}
/** @return the pic header module artifact for the current target. */
private Artifact getPicHeaderModule(Artifact moduleMapArtifact) {
- PathFragment objectDir = CppHelper.getObjDirectory(ruleContext.getLabel());
+ PathFragment objectDir = CppHelper.getObjDirectory(label);
PathFragment outputName =
objectDir.getRelative(moduleMapArtifact.getRootRelativePath().getBaseName());
- return ruleContext.getRelatedArtifact(outputName, ".pic.pcm");
+ return actionConstructionContext.getRelatedArtifact(outputName, ".pic.pcm");
}
/**
@@ -1274,7 +1293,6 @@
private CcCompilationOutputs createCcCompileActions() throws RuleErrorException {
CcCompilationOutputs.Builder result = new CcCompilationOutputs.Builder();
Preconditions.checkNotNull(ccCompilationContext);
- AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
if (shouldProvideHeaderModules()) {
Label moduleMapLabel =
@@ -1326,14 +1344,13 @@
switch (source.getType()) {
case HEADER:
createHeaderAction(
- sourceLabel, outputName, result, env, builder, isGenerateDotdFile(sourceArtifact));
+ sourceLabel, outputName, result, builder, isGenerateDotdFile(sourceArtifact));
break;
default:
createSourceAction(
sourceLabel,
outputName,
result,
- env,
sourceArtifact,
builder,
// TODO(plf): Continue removing CLIF logic from C++. Follow up changes would include
@@ -1357,7 +1374,6 @@
case HEADER:
Artifact headerTokenFile =
createCompileActionTemplate(
- env,
source,
outputName,
builder,
@@ -1369,7 +1385,6 @@
case SOURCE:
Artifact objectFile =
createCompileActionTemplate(
- env,
source,
outputName,
builder,
@@ -1380,7 +1395,6 @@
if (generatePicAction) {
Artifact picObjectFile =
createCompileActionTemplate(
- env,
source,
outputName,
builder,
@@ -1400,7 +1414,6 @@
}
private Artifact createCompileActionTemplate(
- AnalysisEnvironment env,
CppSource source,
String outputName,
CppCompileActionBuilder builder,
@@ -1431,8 +1444,8 @@
builder,
ccToolchain,
outputCategories,
- ruleContext.getActionOwner());
- env.registerAction(actionTemplate);
+ actionConstructionContext.getActionOwner());
+ actionRegistry.registerAction(actionTemplate);
return outputFiles;
}
@@ -1497,7 +1510,7 @@
cppConfiguration.getFdoInstrument());
}
return CompileBuildVariables.setupVariablesOrReportRuleError(
- ruleContext,
+ ruleErrorConsumer,
featureConfiguration,
ccToolchain,
toPathString(sourceFile),
@@ -1510,7 +1523,7 @@
cppModuleMap,
usePic,
builder.getTempOutputFile(),
- CppHelper.getFdoBuildStamp(ruleContext, fdoContext, featureConfiguration),
+ CppHelper.getFdoBuildStamp(cppConfiguration, fdoContext, featureConfiguration),
dotdFileExecPath,
ImmutableList.copyOf(variablesExtensions),
allAdditionalBuildVariables.build(),
@@ -1556,16 +1569,22 @@
builder.setSemantics(semantics);
builder.setPicMode(pic);
builder.setOutputs(
- ruleContext, ArtifactCategory.OBJECT_FILE, outputName, isGenerateDotdFile(module));
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ ArtifactCategory.OBJECT_FILE,
+ outputName,
+ isGenerateDotdFile(module));
PathFragment ccRelativeName = module.getRootRelativePath();
String gcnoFileName =
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
// TODO(djasper): This is now duplicated. Refactor the various create..Action functions.
Artifact gcnoFile =
isCodeCoverageEnabled()
- ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration)
+ ? CppHelper.getCompileOutputArtifact(
+ actionConstructionContext, label, gcnoFileName, configuration)
: null;
boolean generateDwo = ccToolchain.shouldCreatePerObjectDebugInfo(featureConfiguration);
@@ -1592,9 +1611,8 @@
builder.setDwoFile(dwoFile);
semantics.finalizeCompileActionBuilder(ruleContext, builder);
- CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleContext);
- AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
- env.registerAction(compileAction);
+ CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(compileAction);
Artifact objectFile = compileAction.getOutputFile();
if (pic) {
result.addPicObjectFile(objectFile);
@@ -1613,16 +1631,21 @@
Label sourceLabel,
String outputName,
CcCompilationOutputs.Builder result,
- AnalysisEnvironment env,
CppCompileActionBuilder builder,
boolean generateDotd)
throws RuleErrorException {
String outputNameBase =
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.GENERATED_HEADER, outputName);
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.GENERATED_HEADER, outputName);
builder
- .setOutputs(ruleContext, ArtifactCategory.PROCESSED_HEADER, outputNameBase, generateDotd)
+ .setOutputs(
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ ArtifactCategory.PROCESSED_HEADER,
+ outputNameBase,
+ generateDotd)
// If we generate pic actions, we prefer the header actions to use the pic artifacts.
.setPicMode(generatePicAction);
builder.setVariables(
@@ -1637,15 +1660,14 @@
/* ltoIndexingFile= */ null,
/* additionalBuildVariables= */ ImmutableMap.of()));
semantics.finalizeCompileActionBuilder(ruleContext, builder);
- CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleContext);
- env.registerAction(compileAction);
+ CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(compileAction);
Artifact tokenFile = compileAction.getOutputFile();
result.addHeaderTokenFile(tokenFile);
}
private Collection<Artifact> createModuleAction(
CcCompilationOutputs.Builder result, CppModuleMap cppModuleMap) throws RuleErrorException {
- AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
Artifact moduleMapArtifact = cppModuleMap.getArtifact();
CppCompileActionBuilder builder = initializeCompileAction(moduleMapArtifact);
@@ -1658,7 +1680,6 @@
Label.parseAbsoluteUnchecked(cppModuleMap.getName()),
FileSystemUtils.removeExtension(moduleMapArtifact.getRootRelativePath()).getBaseName(),
result,
- env,
moduleMapArtifact,
builder,
ArtifactCategory.CPP_MODULE,
@@ -1673,7 +1694,6 @@
Label sourceLabel,
String outputName,
CcCompilationOutputs.Builder result,
- AnalysisEnvironment env,
Artifact sourceArtifact,
CppCompileActionBuilder builder,
ArtifactCategory outputCategory,
@@ -1691,7 +1711,6 @@
sourceLabel,
outputName,
result,
- env,
builder,
outputCategory,
addObject,
@@ -1708,15 +1727,16 @@
if (generatePicAction) {
String picOutputBase =
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.PIC_FILE, outputName);
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.PIC_FILE, outputName);
CppCompileActionBuilder picBuilder =
copyAsPicBuilder(builder, picOutputBase, outputCategory, generateDotd);
String gcnoFileName =
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase);
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase);
Artifact gcnoFile =
enableCoverage
- ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration)
+ ? CppHelper.getCompileOutputArtifact(
+ actionConstructionContext, label, gcnoFileName, configuration)
: null;
Artifact dwoFile = generateDwo ? getDwoFile(picBuilder.getOutputFile()) : null;
Artifact ltoIndexingFile =
@@ -1749,8 +1769,8 @@
picBuilder.setLtoIndexingFile(ltoIndexingFile);
semantics.finalizeCompileActionBuilder(ruleContext, picBuilder);
- CppCompileAction picAction = picBuilder.buildOrThrowRuleError(ruleContext);
- env.registerAction(picAction);
+ CppCompileAction picAction = picBuilder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(picAction);
directOutputs.add(picAction.getOutputFile());
if (addObject) {
result.addPicObjectFile(picAction.getOutputFile());
@@ -1769,19 +1789,27 @@
if (generateNoPicAction) {
Artifact noPicOutputFile =
CppHelper.getCompileOutputArtifact(
- ruleContext,
+ actionConstructionContext,
+ label,
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, outputCategory, outputName),
+ ruleErrorConsumer, ccToolchain, outputCategory, outputName),
configuration);
- builder.setOutputs(ruleContext, outputCategory, outputName, generateDotd);
+ builder.setOutputs(
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ outputCategory,
+ outputName,
+ generateDotd);
String gcnoFileName =
CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
// Create no-PIC compile actions
Artifact gcnoFile =
enableCoverage
- ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration)
+ ? CppHelper.getCompileOutputArtifact(
+ actionConstructionContext, label, gcnoFileName, configuration)
: null;
Artifact noPicDwoFile = generateDwo ? getDwoFile(noPicOutputFile) : null;
@@ -1815,8 +1843,8 @@
builder.setLtoIndexingFile(ltoIndexingFile);
semantics.finalizeCompileActionBuilder(ruleContext, builder);
- CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleContext);
- env.registerAction(compileAction);
+ CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(compileAction);
Artifact objectFile = compileAction.getOutputFile();
directOutputs.add(objectFile);
if (addObject) {
@@ -1846,7 +1874,15 @@
boolean generateDotd)
throws RuleErrorException {
CppCompileActionBuilder picBuilder = new CppCompileActionBuilder(builder);
- picBuilder.setPicMode(true).setOutputs(ruleContext, outputCategory, outputName, generateDotd);
+ picBuilder
+ .setPicMode(true)
+ .setOutputs(
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ outputCategory,
+ outputName,
+ generateDotd);
return picBuilder;
}
@@ -1854,7 +1890,7 @@
String getOutputNameBaseWith(String base, boolean usePic) throws RuleErrorException {
return usePic
? CppHelper.getArtifactNameForCategory(
- ruleContext, ccToolchain, ArtifactCategory.PIC_FILE, base)
+ ruleErrorConsumer, ccToolchain, ArtifactCategory.PIC_FILE, base)
: base;
}
@@ -1862,7 +1898,6 @@
Label sourceLabel,
String outputName,
CcCompilationOutputs.Builder result,
- AnalysisEnvironment env,
CppCompileActionBuilder builder,
ArtifactCategory outputCategory,
boolean addObject,
@@ -1872,20 +1907,25 @@
throws RuleErrorException {
String outputNameBase = getOutputNameBaseWith(outputName, usePic);
String tempOutputName =
- ruleContext
- .getConfiguration()
+ configuration
.getBinFragment()
- .getRelative(CppHelper.getObjDirectory(ruleContext.getLabel()))
+ .getRelative(CppHelper.getObjDirectory(label))
.getRelative(
CppHelper.getArtifactNameForCategory(
- ruleContext,
+ ruleErrorConsumer,
ccToolchain,
outputCategory,
getOutputNameBaseWith(outputName + ".temp", usePic)))
.getPathString();
builder
.setPicMode(usePic)
- .setOutputs(ruleContext, outputCategory, outputNameBase, generateDotd)
+ .setOutputs(
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ outputCategory,
+ outputNameBase,
+ generateDotd)
.setTempOutputFile(PathFragment.create(tempOutputName));
builder.setVariables(
@@ -1900,8 +1940,8 @@
/* ltoIndexingFile= */ null,
/* additionalBuildVariables= */ ImmutableMap.of()));
semantics.finalizeCompileActionBuilder(ruleContext, builder);
- CppCompileAction action = builder.buildOrThrowRuleError(ruleContext);
- env.registerAction(action);
+ CppCompileAction action = builder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(action);
if (addObject) {
if (usePic) {
result.addPicObjectFile(action.getOutputFile());
@@ -1918,7 +1958,8 @@
}
if (configuration.isCodeCoverageEnabled()) {
// If rule is matched by the instrumentation filter, enable instrumentation
- if (InstrumentedFilesCollector.shouldIncludeLocalSources(ruleContext)) {
+ if (InstrumentedFilesCollector.shouldIncludeLocalSources(
+ configuration, label, ruleContext.isTestTarget())) {
return true;
}
// At this point the rule itself is not matched by the instrumentation filter. However, we
@@ -1961,12 +2002,12 @@
}
private Artifact getDwoFile(Artifact outputFile) {
- return ruleContext.getRelatedArtifact(outputFile.getRootRelativePath(), ".dwo");
+ return actionConstructionContext.getRelatedArtifact(outputFile.getRootRelativePath(), ".dwo");
}
private Artifact getLtoIndexingFile(Artifact outputFile) {
String ext = Iterables.getOnlyElement(CppFileTypes.LTO_INDEXING_OBJECT_FILE.getExtensions());
- return ruleContext.getRelatedArtifact(outputFile.getRootRelativePath(), ext);
+ return actionConstructionContext.getRelatedArtifact(outputFile.getRootRelativePath(), ext);
}
/** Create the actions for "--save_temps". */
@@ -1997,7 +2038,13 @@
String outputArtifactNameBase = getOutputNameBaseWith(outputName, usePic);
CppCompileActionBuilder dBuilder = new CppCompileActionBuilder(builder);
- dBuilder.setOutputs(ruleContext, category, outputArtifactNameBase, generateDotd);
+ dBuilder.setOutputs(
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ category,
+ outputArtifactNameBase,
+ generateDotd);
dBuilder.setVariables(
setupCompileBuildVariables(
dBuilder,
@@ -2012,12 +2059,17 @@
CompileBuildVariables.OUTPUT_PREPROCESS_FILE.getVariableName(),
dBuilder.getRealOutputFilePath().getSafePathString())));
semantics.finalizeCompileActionBuilder(ruleContext, dBuilder);
- CppCompileAction dAction = dBuilder.buildOrThrowRuleError(ruleContext);
- ruleContext.registerAction(dAction);
+ CppCompileAction dAction = dBuilder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(dAction);
CppCompileActionBuilder sdBuilder = new CppCompileActionBuilder(builder);
sdBuilder.setOutputs(
- ruleContext, ArtifactCategory.GENERATED_ASSEMBLY, outputArtifactNameBase, generateDotd);
+ actionConstructionContext,
+ ruleErrorConsumer,
+ label,
+ ArtifactCategory.GENERATED_ASSEMBLY,
+ outputArtifactNameBase,
+ generateDotd);
sdBuilder.setVariables(
setupCompileBuildVariables(
sdBuilder,
@@ -2032,8 +2084,8 @@
CompileBuildVariables.OUTPUT_ASSEMBLY_FILE.getVariableName(),
sdBuilder.getRealOutputFilePath().getSafePathString())));
semantics.finalizeCompileActionBuilder(ruleContext, sdBuilder);
- CppCompileAction sdAction = sdBuilder.buildOrThrowRuleError(ruleContext);
- ruleContext.registerAction(sdAction);
+ CppCompileAction sdAction = sdBuilder.buildOrThrowRuleError(ruleErrorConsumer);
+ actionRegistry.registerAction(sdAction);
return ImmutableList.of(dAction.getOutputFile(), sdAction.getOutputFile());
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcInfo.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcInfo.java
index 8d69a01..15187b5 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcInfo.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcInfo.java
@@ -62,7 +62,8 @@
ccLinkingContexts.add(ccInfo.getCcLinkingContext());
}
CcCompilationContext.Builder builder =
- new CcCompilationContext.Builder(/* ruleContext= */ null);
+ new CcCompilationContext.Builder(
+ /* actionConstructionContext= */ null, /* configuration= */ null, /* label= */ null);
return new CcInfo(
builder.mergeDependentCcCompilationContexts(ccCompilationContexts.build()).build(),
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
index 886c1df..b691075 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
@@ -476,7 +476,10 @@
ruleContext, ccToolchain, ccCompilationOutputs, featureConfiguration))
.addOutputGroup(
CcCompilationHelper.HIDDEN_HEADER_TOKENS,
- CcCompilationHelper.collectHeaderTokens(ruleContext, ccCompilationOutputs));
+ CcCompilationHelper.collectHeaderTokens(
+ ruleContext,
+ ruleContext.getFragment(CppConfiguration.class),
+ ccCompilationOutputs));
}
private static NestedSet<Artifact> collectHiddenTopLevelArtifacts(
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
index 115a4aa..59fb92b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
@@ -433,7 +433,8 @@
Object headers, Object systemIncludes, Object includes, Object quoteIncludes, Object defines)
throws EvalException {
CcCompilationContext.Builder ccCompilationContext =
- new CcCompilationContext.Builder(/* ruleContext= */ null);
+ new CcCompilationContext.Builder(
+ /* actionConstructionContext= */ null, /* configuration= */ null, /* label= */ null);
ccCompilationContext.addDeclaredIncludeSrcs(
toNestedSetOfArtifacts(headers, "headers").getSet(Artifact.class));
ccCompilationContext.addSystemIncludeDirs(
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java
index be1d746..dc1f4de 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java
@@ -606,7 +606,8 @@
== (dynamicRuntimeLinkSymlinks == null || dynamicRuntimeLinkSymlinks.isEmpty()));
CcCompilationContext.Builder ccCompilationContextBuilder =
- new CcCompilationContext.Builder(ruleContext);
+ new CcCompilationContext.Builder(
+ ruleContext, ruleContext.getConfiguration(), ruleContext.getLabel());
CppModuleMap moduleMap = createCrosstoolModuleMap(attributes);
if (moduleMap != null) {
ccCompilationContextBuilder.setCppModuleMap(moduleMap);
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java
index 0d103c9..eebe52d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java
@@ -19,8 +19,8 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.packages.RuleErrorConsumer;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.StringSequenceBuilder;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.VariablesExtension;
@@ -106,7 +106,7 @@
}
public static CcToolchainVariables setupVariablesOrReportRuleError(
- RuleContext ruleContext,
+ RuleErrorConsumer ruleErrorConsumer,
FeatureConfiguration featureConfiguration,
CcToolchainProvider ccToolchainProvider,
String sourceFile,
@@ -156,7 +156,7 @@
|| CppFileTypes.CPP_MODULE_MAP.matches(sourceFile)
|| CppFileTypes.CLIF_INPUT_PROTO.matches(sourceFile));
} catch (EvalException e) {
- ruleContext.ruleError(e.getMessage());
+ ruleErrorConsumer.ruleError(e.getMessage());
return CcToolchainVariables.EMPTY;
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java
index 9629b55..c190a71 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java
@@ -23,11 +23,14 @@
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
+import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
+import com.google.devtools.build.lib.packages.RuleErrorConsumer;
import com.google.devtools.build.lib.rules.cpp.CcCommon.CoptsFilter;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppCompileAction.DotdFile;
@@ -228,14 +231,15 @@
* <p>This method may be called multiple times to create multiple compile actions (usually after
* calling some setters to modify the generated action).
*/
- public CppCompileAction buildOrThrowRuleError(RuleContext ruleContext) throws RuleErrorException {
+ public CppCompileAction buildOrThrowRuleError(RuleErrorConsumer ruleErrorConsumer)
+ throws RuleErrorException {
List<String> errorMessages = new ArrayList<>();
CppCompileAction result =
buildAndVerify((String errorMessage) -> errorMessages.add(errorMessage));
if (!errorMessages.isEmpty()) {
for (String errorMessage : errorMessages) {
- ruleContext.ruleError(errorMessage);
+ ruleErrorConsumer.ruleError(errorMessage);
}
throw new RuleErrorException();
@@ -247,8 +251,8 @@
/**
* Builds the Action as configured and performs some validations on the action. Throws {@link
* IllegalStateException} to report errors. Prefer {@link
- * CppCompileActionBuilder#buildOrThrowRuleError(RuleContext)} over this method whenever possible
- * (meaning whenever you have access to {@link RuleContext}).
+ * CppCompileActionBuilder#buildOrThrowRuleError(RuleErrorConsumer)} over this method whenever
+ * possible (meaning whenever you have access to {@link RuleContext}).
*
* <p>This method may be called multiple times to create multiple compile actions (usually after
* calling some setters to modify the generated action).
@@ -464,28 +468,37 @@
}
public CppCompileActionBuilder setOutputs(
- RuleContext ruleContext,
+ ActionConstructionContext actionConstructionContext,
+ RuleErrorConsumer ruleErrorConsumer,
+ Label label,
ArtifactCategory outputCategory,
String outputName,
boolean generateDotd)
throws RuleErrorException {
- this.outputFile = CppHelper.getCompileOutputArtifact(
- ruleContext,
- CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, outputCategory, outputName),
- configuration);
+ this.outputFile =
+ CppHelper.getCompileOutputArtifact(
+ actionConstructionContext,
+ label,
+ CppHelper.getArtifactNameForCategory(
+ ruleErrorConsumer, ccToolchain, outputCategory, outputName),
+ configuration);
if (generateDotd && !useHeaderModules()) {
String dotdFileName =
- CppHelper.getDotdFileName(ruleContext, ccToolchain, outputCategory, outputName);
+ CppHelper.getDotdFileName(ruleErrorConsumer, ccToolchain, outputCategory, outputName);
if (cppConfiguration.getInmemoryDotdFiles()) {
// Just set the path, no artifact is constructed
- BuildConfiguration configuration = ruleContext.getConfiguration();
- dotdFile = new DotdFile(
- configuration.getBinDirectory(ruleContext.getRule().getRepository()).getExecPath()
- .getRelative(CppHelper.getObjDirectory(ruleContext.getLabel()))
- .getRelative(dotdFileName));
+ dotdFile =
+ new DotdFile(
+ configuration
+ .getBinDirectory(label.getPackageIdentifier().getRepository())
+ .getExecPath()
+ .getRelative(CppHelper.getObjDirectory(label))
+ .getRelative(dotdFileName));
} else {
- dotdFile = new DotdFile(CppHelper.getCompileOutputArtifact(ruleContext, dotdFileName,
- configuration));
+ dotdFile =
+ new DotdFile(
+ CppHelper.getCompileOutputArtifact(
+ actionConstructionContext, label, dotdFileName, configuration));
}
} else {
dotdFile = null;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
index 1db7bc3..36b4cfa 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
@@ -39,6 +39,7 @@
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
+import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.actions.SymlinkAction;
@@ -518,17 +519,20 @@
* Creates a CppModuleMap object for pure c++ builds. The module map artifact becomes a candidate
* input to a CppCompileAction.
*/
- public static CppModuleMap createDefaultCppModuleMap(RuleContext ruleContext, String suffix) {
+ public static CppModuleMap createDefaultCppModuleMap(
+ ActionConstructionContext actionConstructionContext,
+ BuildConfiguration configuration,
+ Label label,
+ String suffix) {
// Create the module map artifact as a genfile.
Artifact mapFile =
- ruleContext.getPackageRelativeArtifact(
- ruleContext.getLabel().getName()
- + suffix
- + Iterables.getOnlyElement(CppFileTypes.CPP_MODULE_MAP.getExtensions()),
- ruleContext
- .getConfiguration()
- .getGenfilesDirectory(ruleContext.getRule().getRepository()));
- return new CppModuleMap(mapFile, ruleContext.getLabel().toString());
+ actionConstructionContext.getPackageRelativeArtifact(
+ PathFragment.create(
+ label.getName()
+ + suffix
+ + Iterables.getOnlyElement(CppFileTypes.CPP_MODULE_MAP.getExtensions())),
+ configuration.getGenfilesDirectory(label.getPackageIdentifier().getRepository()));
+ return new CppModuleMap(mapFile, label.toString());
}
/**
@@ -619,8 +623,9 @@
/** Returns the FDO build subtype. */
public static String getFdoBuildStamp(
- RuleContext ruleContext, FdoContext fdoContext, FeatureConfiguration featureConfiguration) {
- CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
+ CppConfiguration cppConfiguration,
+ FdoContext fdoContext,
+ FeatureConfiguration featureConfiguration) {
FdoContext.BranchFdoProfile branchFdoProfile = fdoContext.getBranchFdoProfile();
if (branchFdoProfile != null) {
@@ -698,11 +703,15 @@
}
}
- static Artifact getCompileOutputArtifact(RuleContext ruleContext, String outputName,
+ static Artifact getCompileOutputArtifact(
+ ActionConstructionContext actionConstructionContext,
+ Label label,
+ String outputName,
BuildConfiguration config) {
- PathFragment objectDir = getObjDirectory(ruleContext.getLabel());
- return ruleContext.getDerivedArtifact(objectDir.getRelative(outputName),
- config.getBinDirectory(ruleContext.getRule().getRepository()));
+ PathFragment objectDir = getObjDirectory(label);
+ return actionConstructionContext.getDerivedArtifact(
+ objectDir.getRelative(outputName),
+ config.getBinDirectory(label.getPackageIdentifier().getRepository()));
}
/** Returns the corresponding compiled TreeArtifact given the source TreeArtifact. */
@@ -715,7 +724,7 @@
}
public static String getArtifactNameForCategory(
- RuleContext ruleContext,
+ RuleErrorConsumer ruleErrorConsumer,
CcToolchainProvider toolchain,
ArtifactCategory category,
String outputName)
@@ -723,24 +732,25 @@
try {
return toolchain.getFeatures().getArtifactNameForCategory(category, outputName);
} catch (EvalException e) {
- ruleContext.throwWithRuleError(e.getMessage());
+ ruleErrorConsumer.throwWithRuleError(e.getMessage());
throw new IllegalStateException("Should not be reached");
}
}
static String getDotdFileName(
- RuleContext ruleContext,
+ RuleErrorConsumer ruleErrorConsumer,
CcToolchainProvider toolchain,
ArtifactCategory outputCategory,
String outputName)
throws RuleErrorException {
- String baseName = outputCategory == ArtifactCategory.OBJECT_FILE
- || outputCategory == ArtifactCategory.PROCESSED_HEADER
- ? outputName
- : getArtifactNameForCategory(ruleContext, toolchain, outputCategory, outputName);
+ String baseName =
+ outputCategory == ArtifactCategory.OBJECT_FILE
+ || outputCategory == ArtifactCategory.PROCESSED_HEADER
+ ? outputName
+ : getArtifactNameForCategory(ruleErrorConsumer, toolchain, outputCategory, outputName);
return getArtifactNameForCategory(
- ruleContext, toolchain, ArtifactCategory.INCLUDED_FILE_LIST, baseName);
+ ruleErrorConsumer, toolchain, ArtifactCategory.INCLUDED_FILE_LIST, baseName);
}
/**
@@ -843,11 +853,12 @@
return new CcNativeLibraryProvider(result.build());
}
- public static void checkProtoLibrariesInDeps(RuleContext ruleContext,
- Iterable<TransitiveInfoCollection> deps) {
+ public static void checkProtoLibrariesInDeps(
+ RuleErrorConsumer ruleErrorConsumer, Iterable<TransitiveInfoCollection> deps) {
for (TransitiveInfoCollection dep : deps) {
if (dep.get(ProtoInfo.PROVIDER) != null && dep.get(CcInfo.PROVIDER) == null) {
- ruleContext.attributeError("deps",
+ ruleErrorConsumer.attributeError(
+ "deps",
String.format("proto_library '%s' does not produce output for C++", dep.getLabel()));
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
index c33a0f6..ed01566 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
@@ -1062,7 +1062,7 @@
toolchain,
configuration.isCodeCoverageEnabled(),
cppConfiguration,
- CppHelper.getFdoBuildStamp(ruleContext, fdoContext, featureConfiguration),
+ CppHelper.getFdoBuildStamp(cppConfiguration, fdoContext, featureConfiguration),
featureConfiguration,
cppConfiguration.forcePic()
|| (linkType.isDynamicLibrary()
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
index 551f9db..e2c81c4 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
@@ -374,9 +374,11 @@
private boolean shouldInstrumentJar() {
// TODO(bazel-team): What about source jars?
+ RuleContext ruleContext = getRuleContext();
return getConfiguration().isCodeCoverageEnabled()
&& attributes.hasSourceFiles()
- && InstrumentedFilesCollector.shouldIncludeLocalSources(getRuleContext());
+ && InstrumentedFilesCollector.shouldIncludeLocalSources(
+ ruleContext.getConfiguration(), ruleContext.getLabel(), ruleContext.isTestTarget());
}
private boolean shouldUseHeaderCompilation() {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
index fd307ff..ce4a26b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
@@ -439,7 +439,8 @@
}
CcCompilationContext.Builder ccCompilationContextBuilder =
- new CcCompilationContext.Builder(ruleContext);
+ new CcCompilationContext.Builder(
+ ruleContext, ruleContext.getConfiguration(), ruleContext.getLabel());
ccCompilationContextBuilder.mergeDependentCcCompilationContexts(
Arrays.asList(
objcArcCompilationInfo.getCcCompilationContext(),
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcLibrary.java
index 24d60ba..6f97111 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcLibrary.java
@@ -95,7 +95,8 @@
.addTransitive(ruleContext.getPrerequisites("deps", Mode.TARGET,
J2ObjcEntryClassProvider.class)).build();
CcCompilationContext ccCompilationContext =
- new CcCompilationContext.Builder(ruleContext)
+ new CcCompilationContext.Builder(
+ ruleContext, ruleContext.getConfiguration(), ruleContext.getLabel())
.addDeclaredIncludeSrcs(
CompilationAttributes.Builder.fromRuleContext(ruleContext)
.build()