diff options
author | 2018-03-27 08:02:39 -0700 | |
---|---|---|
committer | 2018-03-27 08:04:11 -0700 | |
commit | eed7c75c5baa0e83e5915feade3ba4268b42c0f7 (patch) | |
tree | bd5f4d2488acad1cb0d7463b36ec9b68d70fff29 /src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java | |
parent | f5c8c0bb0f53cd7289d65672955b37ffcff7d6c4 (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/com/google/devtools/build/lib/rules/cpp/CppHelper.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java | 30 |
1 files changed, 11 insertions, 19 deletions
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, |