aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java
diff options
context:
space:
mode:
authorGravatar plf <plf@google.com>2018-03-27 08:02:39 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-27 08:04:11 -0700
commiteed7c75c5baa0e83e5915feade3ba4268b42c0f7 (patch)
treebd5f4d2488acad1cb0d7463b36ec9b68d70fff29 /src/main/java
parentf5c8c0bb0f53cd7289d65672955b37ffcff7d6c4 (diff)
C++: Fixes Blaze crashing on CLIF in toolchains that don't need PIC
For CLIF it doesn't matter whether we use PIC or no-PIC, the important thing is we get an output protobuf. Before https://github.com/bazelbuild/bazel/commit/35773928532c132e3229b490ad98f4ebfd3e5770, using no-PIC was hardcoded. In this CL it was decided to generate PIC instead because the toolchains used by CLIF targets appeared to all have needsPic. In b/73955395 it has been shown not to be the case. In this CL I'm changing the logic again to use no-PIC for CLIF and to circumvent the logic that checks what the configuration and the toolchain say. At the same time, SWIG also used the method setGenerateNoPic() after the variable onlySingleOutput was removed in https://github.com/bazelbuild/bazel/commit/4e9c9f93b15dd2594097644c6b9ca5a579c712fb. In this CL I use the enum to apply PIC and no-PIC in the same cases as before for SWIG. This CL also refactors the methods and boolean variables used to determine whether to use PIC or not, hopefully making it clearer. RELNOTES:none PiperOrigin-RevId: 190615548
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java21
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcBuildVariables.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java13
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java90
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java21
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java30
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java7
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java3
9 files changed, 83 insertions, 106 deletions
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 845ee1f3f9..2059b96c84 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
@@ -157,8 +157,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
// or header modules.
builder.addSymlinksToArtifacts(ccCompilationInfo.getAdditionalInputs());
builder.addSymlinksToArtifacts(
- ccCompilationInfo.getTransitiveModules(
- CppHelper.usePic(context, toolchain, !isLinkShared(context))));
+ ccCompilationInfo.getTransitiveModules(usePic(context, toolchain)));
}
return builder.build();
}
@@ -368,7 +367,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
// Store immutable context for use in other *_binary rules that are implemented by
// linking the interpreter (Java, Python, etc.) together with native deps.
CppLinkAction.Context linkContext = new CppLinkAction.Context(linkActionBuilder);
- boolean usePic = CppHelper.usePic(ruleContext, ccToolchain, !isLinkShared(ruleContext));
+ boolean usePic = usePic(ruleContext, ccToolchain);
if (linkActionBuilder.hasLtoBitcodeInputs()
&& featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
@@ -585,7 +584,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
builder.addLibrary(library);
}
} else {
- boolean usePic = CppHelper.usePic(context, toolchain, !isLinkShared(context));
+ boolean usePic = usePic(context, toolchain);
Iterable<Artifact> objectFiles = compilationOutputs.getObjectFiles(usePic);
if (fake) {
@@ -687,9 +686,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
CcToolchainProvider toolchain,
NestedSet<Artifact> picDwoArtifacts,
NestedSet<Artifact> dwoArtifacts) {
- return CppHelper.usePic(context, toolchain, !isLinkShared(context))
- ? picDwoArtifacts
- : dwoArtifacts;
+ return usePic(context, toolchain) ? picDwoArtifacts : dwoArtifacts;
}
/**
@@ -893,7 +890,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
ccCompilationOutputs.getFilesToCompile(
cppConfiguration.isLipoContextCollector(),
cppConfiguration.processHeadersInDependencies(),
- CppHelper.usePic(ruleContext, toolchain, false));
+ CppHelper.usePicForDynamicLibraries(ruleContext, toolchain));
builder
.setFilesToBuild(filesToBuild)
.addNativeDeclaredProvider(ccCompilationInfo)
@@ -963,4 +960,12 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory {
? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
: compilationOutputs.getTemps();
}
+
+ private static boolean usePic(RuleContext ruleContext, CcToolchainProvider ccToolchainProvider) {
+ if (isLinkShared(ruleContext)) {
+ return CppHelper.usePicForDynamicLibraries(ruleContext, ccToolchainProvider);
+ } else {
+ return CppHelper.usePicForBinaries(ruleContext, ccToolchainProvider);
+ }
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBuildVariables.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBuildVariables.java
index 00cb717f76..857eb9913f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBuildVariables.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBuildVariables.java
@@ -257,7 +257,7 @@ public class CcBuildVariables {
if (usePic) {
if (!featureConfiguration.isEnabled(CppRuleClasses.PIC)) {
- ruleContext.ruleError(CcCompilationHelper.PIC_CONFIGURATION_ERROR);
+ ruleContext.ruleError(CcCommon.PIC_CONFIGURATION_ERROR);
}
buildVariables.addStringVariable(CompileBuildVariables.PIC.getVariableName(), "");
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
index 8d4abd3e6c..8ddd0a1e5f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
@@ -77,6 +77,9 @@ public final class CcCommon {
/** Name of the build variable for the path to the input file being processed. */
public static final String INPUT_FILE_VARIABLE_NAME = "input_file";
+ public static final String PIC_CONFIGURATION_ERROR =
+ "PIC compilation is requested but the toolchain does not support it";
+
private static final String NO_COPTS_ATTRIBUTE = "nocopts";
/**
@@ -788,10 +791,10 @@ public final class CcCommon {
allRequestedFeaturesBuilder.addAll(DEFAULT_ACTION_CONFIGS);
try {
- FeatureConfiguration configuration =
+ FeatureConfiguration featureConfiguration =
toolchain.getFeatures().getFeatureConfiguration(allRequestedFeaturesBuilder.build());
for (String feature : unsupportedFeatures) {
- if (configuration.isEnabled(feature)) {
+ if (featureConfiguration.isEnabled(feature)) {
ruleContext.ruleError(
"The C++ toolchain '"
+ ruleContext
@@ -803,7 +806,11 @@ public final class CcCommon {
+ "This is most likely a misconfiguration in the C++ toolchain.");
}
}
- return configuration;
+ if ((cppConfiguration.forcePic() || toolchain.toolchainNeedsPic())
+ && !featureConfiguration.isEnabled(CppRuleClasses.PIC)) {
+ ruleContext.ruleError(PIC_CONFIGURATION_ERROR);
+ }
+ return featureConfiguration;
} catch (CollidingProvidesException e) {
ruleContext.ruleError(e.getMessage());
return FeatureConfiguration.EMPTY;
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)
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 84f86c7864..80f3950d4a 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
@@ -358,7 +358,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory {
CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
boolean isLipoCollector = cppConfiguration.isLipoContextCollector();
boolean processHeadersInDependencies = cppConfiguration.processHeadersInDependencies();
- boolean usePic = CppHelper.usePic(ruleContext, toolchain, false);
+ boolean usePic = CppHelper.usePicForDynamicLibraries(ruleContext, toolchain);
artifactsToForceBuilder.addTransitive(
ccCompilationOutputs.getFilesToCompile(
isLipoCollector, processHeadersInDependencies, usePic));
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java
index f633949994..f8da2e705a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java
@@ -719,9 +719,8 @@ public final class CcLinkingHelper {
return result.build();
}
AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
- boolean usePicForBinaries = CppHelper.usePic(ruleContext, ccToolchain, /* forBinary= */ true);
- boolean usePicForSharedLibs =
- CppHelper.usePic(ruleContext, ccToolchain, /* forBinary= */ false);
+ boolean usePicForBinaries = CppHelper.usePicForBinaries(ruleContext, ccToolchain);
+ boolean usePicForDynamicLibs = CppHelper.usePicForDynamicLibraries(ruleContext, ccToolchain);
PathFragment labelName = PathFragment.create(ruleContext.getLabel().getName());
String libraryIdentifier =
@@ -735,14 +734,14 @@ public final class CcLinkingHelper {
result,
env,
usePicForBinaries,
- usePicForSharedLibs,
+ usePicForDynamicLibs,
libraryIdentifier,
ccOutputs,
nonCodeLinkerInputs);
}
if (shouldCreateDynamicLibrary) {
- createDynamicLibrary(result, env, usePicForSharedLibs, libraryIdentifier, ccOutputs);
+ createDynamicLibrary(result, env, usePicForDynamicLibs, libraryIdentifier, ccOutputs);
}
return result.build();
@@ -752,14 +751,14 @@ public final class CcLinkingHelper {
CcLinkingOutputs.Builder result,
AnalysisEnvironment env,
boolean usePicForBinaries,
- boolean usePicForSharedLibs,
+ boolean usePicForDynamicLibs,
String libraryIdentifier,
CcCompilationOutputs ccOutputs,
Iterable<Artifact> nonCodeLinkerInputs)
throws RuleErrorException, InterruptedException {
// Create static library (.a). The linkType only reflects whether the library is alwayslink or
// not. The PIC-ness is determined by whether we need to use PIC or not. There are three cases
- // for (usePicForSharedLibs usePicForBinaries):
+ // for (usePicForDynamicLibs usePicForBinaries):
//
// (1) (false false) -> no pic code
// (2) (true false) -> shared libraries as pic, but not binaries
@@ -798,7 +797,7 @@ public final class CcLinkingHelper {
// static libraries. In that case, the first static library contains the non-PIC code, and
// this
// one contains the PIC code, so the names match the content.
- if (usePicForSharedLibs) {
+ if (usePicForDynamicLibs) {
LinkTargetType picLinkType =
(linkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY)
? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY
@@ -827,7 +826,7 @@ public final class CcLinkingHelper {
private void createDynamicLibrary(
CcLinkingOutputs.Builder result,
AnalysisEnvironment env,
- boolean usePicForSharedLibs,
+ boolean usePicForDynamicLibs,
String libraryIdentifier,
CcCompilationOutputs ccOutputs)
throws RuleErrorException, InterruptedException {
@@ -872,7 +871,7 @@ public final class CcLinkingHelper {
CppLinkActionBuilder dynamicLinkActionBuilder =
newLinkActionBuilder(soImpl)
.setInterfaceOutput(soInterface)
- .addObjectFiles(ccOutputs.getObjectFiles(usePicForSharedLibs))
+ .addObjectFiles(ccOutputs.getObjectFiles(usePicForDynamicLibs))
.addNonCodeInputs(ccOutputs.getHeaderTokenFiles())
.addLtoBitcodeFiles(ccOutputs.getLtoBitcodeFiles())
.setLinkType(LinkTargetType.NODEPS_DYNAMIC_LIBRARY)
@@ -922,7 +921,7 @@ public final class CcLinkingHelper {
if (!ccOutputs.getLtoBitcodeFiles().isEmpty()
&& featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
dynamicLinkActionBuilder.setLtoIndexing(true);
- dynamicLinkActionBuilder.setUsePicForLtoBackendActions(usePicForSharedLibs);
+ dynamicLinkActionBuilder.setUsePicForLtoBackendActions(usePicForDynamicLibs);
CppLinkAction indexAction = dynamicLinkActionBuilder.build();
if (indexAction != null) {
env.registerAction(indexAction);
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 992486e230..adf2e0a1f3 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
@@ -757,36 +757,28 @@ public class CppHelper {
* "Provide a way to turn off -fPIC for targets that can't be built that way").
*
* @param ruleContext the context of the rule to check
- * @param forBinary true if compiling for a binary, false if for a shared library
* @return true if this rule's compilations should apply -fPIC, false otherwise
*/
- public static boolean usePic(
- RuleContext ruleContext, CcToolchainProvider toolchain, boolean forBinary) {
- if (CcCommon.noCoptsMatches("-fPIC", ruleContext)) {
- return false;
- }
- CppConfiguration config = ruleContext.getFragment(CppConfiguration.class);
- return forBinary ? usePicObjectsForBinaries(config, toolchain) : needsPic(config, toolchain);
+ public static boolean usePicForDynamicLibraries(
+ RuleContext ruleContext, CcToolchainProvider toolchain) {
+ return ruleContext.getFragment(CppConfiguration.class).forcePic()
+ || toolchain.toolchainNeedsPic();
}
/** Returns whether binaries must be compiled with position independent code. */
- public static boolean usePicForBinaries(CppConfiguration config, CcToolchainProvider toolchain) {
- return toolchain.toolchainNeedsPic() && config.getCompilationMode() != CompilationMode.OPT;
- }
-
- /** Returns true iff we should use ".pic.o" files when linking executables. */
- public static boolean usePicObjectsForBinaries(
- CppConfiguration config, CcToolchainProvider toolchain) {
- return config.forcePic() || usePicForBinaries(config, toolchain);
+ public static boolean usePicForBinaries(RuleContext ruleContext, CcToolchainProvider toolchain) {
+ CppConfiguration config = ruleContext.getFragment(CppConfiguration.class);
+ if (CcCommon.noCoptsMatches("-fPIC", ruleContext)) {
+ return false;
+ }
+ return config.forcePic()
+ || (toolchain.toolchainNeedsPic() && config.getCompilationMode() != CompilationMode.OPT);
}
/**
* Returns true if shared libraries must be compiled with position independent code for the build
* implied by the given config and toolchain.
*/
- public static boolean needsPic(CppConfiguration config, CcToolchainProvider toolchain) {
- return config.forcePic() || toolchain.toolchainNeedsPic();
- }
/**
* Returns the LIPO context provider for configured target,
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java
index b70ab0a875..6ec2c38831 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/Link.java
@@ -60,11 +60,10 @@ public abstract class Link {
CppFileTypes.ALWAYS_LINK_LIBRARY,
CppFileTypes.ALWAYS_LINK_PIC_LIBRARY);
-
/** The set of object files */
- public static final FileTypeSet OBJECT_FILETYPES = FileTypeSet.of(
- CppFileTypes.OBJECT_FILE,
- CppFileTypes.PIC_OBJECT_FILE);
+ public static final FileTypeSet OBJECT_FILETYPES =
+ FileTypeSet.of(
+ CppFileTypes.OBJECT_FILE, CppFileTypes.PIC_OBJECT_FILE, CppFileTypes.CLIF_OUTPUT_PROTO);
/**
* Prefix that is prepended to command line entries that refer to the output
diff --git a/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java b/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
index 729ac2b75d..d8ddf994d9 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
@@ -267,7 +267,8 @@ public abstract class NativeDepsHelper {
if (builder.hasLtoBitcodeInputs() && featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
builder.setLtoIndexing(true);
- builder.setUsePicForLtoBackendActions(CppHelper.usePic(ruleContext, toolchain, false));
+ builder.setUsePicForLtoBackendActions(
+ CppHelper.usePicForDynamicLibraries(ruleContext, toolchain));
CppLinkAction indexAction = builder.build();
if (indexAction != null) {
ruleContext.registerAction(indexAction);