aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java90
1 files changed, 32 insertions, 58 deletions
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 c90c97696c..b50306fe21 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
@@ -88,9 +88,6 @@ public final class CcCompilationHelper {
+ "hidden_header_tokens"
+ OutputGroupInfo.INTERNAL_SUFFIX;
- public static final String PIC_CONFIGURATION_ERROR =
- "PIC compilation is requested but the toolchain does not support it";
-
/**
* A group of source file types and action names for builds controlled by CcCompilationHelper.
* Determines what file types CcCompilationHelper considers sources and what action configs are
@@ -225,7 +222,8 @@ public final class CcCompilationHelper {
private boolean useDeps = true;
private boolean generateModuleMap = true;
private String purpose = null;
- private boolean generateNoPic = true;
+ private boolean generateNoPicAction;
+ private boolean generatePicAction;
// TODO(plf): Pull out of class.
private CcCompilationInfo ccCompilationInfo;
@@ -286,6 +284,12 @@ public final class CcCompilationHelper {
this.cppConfiguration =
Preconditions.checkNotNull(ruleContext.getFragment(CppConfiguration.class));
this.features = ruleContext.getFeatures();
+ setGenerateNoPicAction(
+ !CppHelper.usePicForDynamicLibraries(ruleContext, ccToolchain)
+ || !CppHelper.usePicForBinaries(ruleContext, ccToolchain));
+ setGeneratePicAction(
+ CppHelper.usePicForDynamicLibraries(ruleContext, ccToolchain)
+ || CppHelper.usePicForBinaries(ruleContext, ccToolchain));
}
/**
@@ -465,7 +469,6 @@ public final class CcCompilationHelper {
}
private boolean shouldProcessHeaders() {
- CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
// If parse_headers_verifies_modules is switched on, we verify that headers are
// self-contained by building the module instead.
return !cppConfiguration.getParseHeadersVerifiesModules()
@@ -484,7 +487,7 @@ public final class CcCompilationHelper {
}
/**
- * Add the corresponding files as linker inputs for non-PIC links. If the corresponding files are
+ * Add the corresponding files as linker inputs for no-PIC links. If the corresponding files are
* compiled with PIC, the final link may or may not fail. Note that the final link may not happen
* here, if {@code --start_end_lib} is enabled, but instead at any binary that transitively
* depends on the current rule.
@@ -545,7 +548,7 @@ public final class CcCompilationHelper {
/**
* Adds the given precompiled files to this helper. Shared and static libraries are added as
- * compilation prerequisites, and object files are added as pic or non-pic object files
+ * compilation prerequisites, and object files are added as pic or no-PIC object files
* respectively.
*/
public CcCompilationHelper addPrecompiledFiles(PrecompiledFiles precompiledFiles) {
@@ -647,9 +650,15 @@ public final class CcCompilationHelper {
return this;
}
- /** non-PIC actions won't be generated. */
- public CcCompilationHelper setGenerateNoPic(boolean generateNoPic) {
- this.generateNoPic = generateNoPic;
+ /** Whether to generate no-PIC actions. */
+ public CcCompilationHelper setGenerateNoPicAction(boolean generateNoPicAction) {
+ this.generateNoPicAction = generateNoPicAction;
+ return this;
+ }
+
+ /** Whether to generate PIC actions. */
+ public CcCompilationHelper setGeneratePicAction(boolean generatePicAction) {
+ this.generatePicAction = generatePicAction;
return this;
}
@@ -690,7 +699,7 @@ public final class CcCompilationHelper {
!compileHeaderModules || ccCompilationInfo.getCppModuleMap() != null,
"All cc rules must support module maps.");
- // Create compile actions (both PIC and non-PIC).
+ // Create compile actions (both PIC and no-PIC).
CcCompilationOutputs ccOutputs = createCcCompileActions();
if (!objectFiles.isEmpty() || !picObjectFiles.isEmpty()) {
// Merge the pre-compiled object files into the compiler outputs.
@@ -722,11 +731,10 @@ public final class CcCompilationHelper {
Map<String, NestedSet<Artifact>> outputGroups = new TreeMap<>();
outputGroups.put(OutputGroupInfo.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, ccToolchain, false);
+ boolean usePic = CppHelper.usePicForDynamicLibraries(ruleContext, ccToolchain);
outputGroups.put(
OutputGroupInfo.FILES_TO_COMPILE,
ccOutputs.getFilesToCompile(isLipoCollector, processHeadersInDependencies, usePic));
@@ -985,7 +993,7 @@ public final class CcCompilationHelper {
if (!compiled
&& featureConfiguration.isEnabled(CppRuleClasses.PARSE_HEADERS)
&& featureConfiguration.isEnabled(CppRuleClasses.USE_HEADER_MODULES)
- && ruleContext.getFragment(CppConfiguration.class).getParseHeadersVerifiesModules()) {
+ && cppConfiguration.getParseHeadersVerifiesModules()) {
// Here, we are creating a compiled module to verify that headers are self-contained and
// modules ready, but we don't use the corresponding module map or compiled file anywhere
// else.
@@ -1123,19 +1131,19 @@ public final class CcCompilationHelper {
}
private NestedSet<Artifact> getTemps(CcCompilationOutputs compilationOutputs) {
- return ruleContext.getFragment(CppConfiguration.class).isLipoContextCollector()
+ return cppConfiguration.isLipoContextCollector()
? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
: compilationOutputs.getTemps();
}
/** @return whether this target needs to generate a pic header module. */
private boolean getGeneratesPicHeaderModule() {
- return shouldProvideHeaderModules() && !fake && getGeneratePicActions();
+ return shouldProvideHeaderModules() && !fake && generatePicAction;
}
- /** @return whether this target needs to generate a non-pic header module. */
+ /** @return whether this target needs to generate a no-PIC header module. */
private boolean getGeneratesNoPicHeaderModule() {
- return shouldProvideHeaderModules() && !fake && getGenerateNoPicActions();
+ return shouldProvideHeaderModules() && !fake && generateNoPicAction;
}
/** @return whether we want to provide header modules for the current target. */
@@ -1144,39 +1152,7 @@ public final class CcCompilationHelper {
&& !cppConfiguration.isLipoContextCollector();
}
- /** @return whether this target needs to generate non-pic actions. */
- private boolean getGenerateNoPicActions() {
- if (!generateNoPic) {
- return false;
- }
- boolean picFeatureEnabled = featureConfiguration.isEnabled(CppRuleClasses.PIC);
- boolean usePicForBinaries = CppHelper.usePic(ruleContext, ccToolchain, true);
- boolean usePicForNonBinaries = CppHelper.usePic(ruleContext, ccToolchain, false);
-
- if (!usePicForNonBinaries) {
- // This means you have to be prepared to use non-pic output for dynamic libraries.
- return true;
- }
-
- // Either you're only making a dynamic library (onlySingleOutput) or pic should be used
- // in all cases.
- if (usePicForBinaries) {
- if (picFeatureEnabled) {
- return false;
- }
- ruleContext.ruleError(PIC_CONFIGURATION_ERROR);
- }
-
- return true;
- }
-
- /** @return whether this target needs to generate pic actions. */
- private boolean getGeneratePicActions() {
- return featureConfiguration.isEnabled(CppRuleClasses.PIC)
- && CppHelper.usePic(ruleContext, ccToolchain, false);
- }
-
- /** @return the non-pic header module artifact for the current target. */
+ /** @return the no-PIC header module artifact for the current target. */
private Artifact getHeaderModule(Artifact moduleMapArtifact) {
PathFragment objectDir = CppHelper.getObjDirectory(ruleContext.getLabel());
PathFragment outputName =
@@ -1317,7 +1293,7 @@ public final class CcCompilationHelper {
false);
result.addObjectFile(objectFile);
- if (getGeneratePicActions()) {
+ if (generatePicAction) {
Artifact picObjectFile =
createCompileActionTemplate(
env,
@@ -1582,13 +1558,13 @@ public final class CcCompilationHelper {
builder
.setOutputs(ruleContext, ArtifactCategory.PROCESSED_HEADER, outputNameBase, generateDotd)
// If we generate pic actions, we prefer the header actions to use the pic artifacts.
- .setPicMode(getGeneratePicActions());
+ .setPicMode(generatePicAction);
builder.setVariables(
setupCompileBuildVariables(
builder,
sourceLabel,
/* outputName= */ null,
- this.getGeneratePicActions(),
+ generatePicAction,
/* ccRelativeName= */ null,
/* autoFdoImportPath= */ null,
ccCompilationInfo.getCppModuleMap(),
@@ -1660,8 +1636,6 @@ public final class CcCompilationHelper {
CcCompilationInfo.mergeForLipo(
lipoProvider.getLipoCcCompilationInfo(), ccCompilationInfo));
}
- boolean generatePicAction = getGeneratePicActions();
- boolean generateNoPicAction = getGenerateNoPicActions();
Preconditions.checkState(generatePicAction || generateNoPicAction);
if (fake) {
boolean usePic = !generateNoPicAction;
@@ -1682,7 +1656,7 @@ public final class CcCompilationHelper {
featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)
&& CppFileTypes.LTO_SOURCE.matches(sourceArtifact.getFilename());
- // Create PIC compile actions (same as non-PIC, but use -fPIC and
+ // Create PIC compile actions (same as no-PIC, but use -fPIC and
// generate .pic.o, .pic.d, .pic.gcno instead of .o, .d, .gcno.)
if (generatePicAction) {
String picOutputBase =
@@ -1761,7 +1735,7 @@ public final class CcCompilationHelper {
CppHelper.getArtifactNameForCategory(
ruleContext, ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName);
- // Create non-PIC compile actions
+ // Create no-PIC compile actions
Artifact gcnoFile =
!CppHelper.isLipoOptimization(cppConfiguration, ccToolchain) && enableCoverage
? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName, configuration)