Skip to content

Commit 4084671

Browse files
djasper-ghcopybara-github
authored andcommitted
Add support for crosstool feature to prefer PIC compiles even for optimized binaries. This can have performance penalty, but in configurations where dynamic linking is used for tests can lead to a substantially better sharing of artifacts between tests and binaries. In contrast to the existing --force_pic, this can be enabled per crosstool and respects whether PIC is available for the used crosstool.
PiperOrigin-RevId: 492940462 Change-Id: I178088730d50f057d45f41b46ca94bad5bd231da
1 parent de7b26a commit 4084671

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ public static boolean usePicForBinaries(
603603
FeatureConfiguration featureConfiguration) {
604604
return cppConfiguration.forcePic()
605605
|| (toolchain.usePicForDynamicLibraries(cppConfiguration, featureConfiguration)
606-
&& cppConfiguration.getCompilationMode() != CompilationMode.OPT);
606+
&& (cppConfiguration.getCompilationMode() != CompilationMode.OPT
607+
|| featureConfiguration.isEnabled(CppRuleClasses.PREFER_PIC_FOR_OPT_BINARIES)));
607608
}
608609

609610
/**

src/main/java/com/google/devtools/build/lib/rules/cpp/CppRuleClasses.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ public static ToolchainTypeRequirement ccToolchainTypeRequirement(RuleDefinition
254254
/** A string constant for a feature that indicates that the toolchain can produce PIC objects. */
255255
public static final String SUPPORTS_PIC = "supports_pic";
256256

257+
/**
258+
* A string constant for a feature that indicates that PIC compiles are preferred for binaries
259+
* even in optimized builds. For configurations that use dynamic linking for tests, this provides
260+
* increases sharing of artifacts between tests and binaries at the cost of performance overhead.
261+
*/
262+
public static final String PREFER_PIC_FOR_OPT_BINARIES = "prefer_pic_for_opt_binaries";
263+
257264
/** A string constant for the feature the represents preprocessor defines. */
258265
public static final String PREPROCESSOR_DEFINES = "preprocessor_defines";
259266

src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ _FEATURE_NAMES = struct(
7777
implicit_fsafdo = "implicit_fsafdo",
7878
enable_fsafdo = "enable_fsafdo",
7979
supports_pic = "supports_pic",
80+
prefer_pic_for_opt_binaries = "prefer_pic_for_opt_binaries",
8081
copy_dynamic_libraries_to_binary = "copy_dynamic_libraries_to_binary",
8182
per_object_debug_info = "per_object_debug_info",
8283
supports_start_end_lib = "supports_start_end_lib",
@@ -816,6 +817,11 @@ _supports_start_end_lib_feature = feature(
816817

817818
_supports_pic_feature = feature(name = _FEATURE_NAMES.supports_pic, enabled = True)
818819

820+
_prefer_pic_for_opt_binaries_feature = feature(
821+
name = _FEATURE_NAMES.prefer_pic_for_opt_binaries,
822+
enabled = True,
823+
)
824+
819825
_targets_windows_feature = feature(
820826
name = _FEATURE_NAMES.targets_windows,
821827
enabled = True,
@@ -1342,6 +1348,7 @@ _feature_name_to_feature = {
13421348
_FEATURE_NAMES.copy_dynamic_libraries_to_binary: _copy_dynamic_libraries_to_binary_feature,
13431349
_FEATURE_NAMES.supports_start_end_lib: _supports_start_end_lib_feature,
13441350
_FEATURE_NAMES.supports_pic: _supports_pic_feature,
1351+
_FEATURE_NAMES.prefer_pic_for_opt_binaries: _prefer_pic_for_opt_binaries_feature,
13451352
_FEATURE_NAMES.targets_windows: _targets_windows_feature,
13461353
_FEATURE_NAMES.archive_param_file: _archive_param_file_feature,
13471354
_FEATURE_NAMES.compiler_param_file: _compiler_param_file_feature,

src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,66 @@ public void testWhenSupportsPicDisabledButForcePicSetPICAreGenerated() throws Ex
10131013
assertThat(outputs).contains("a.pic.o");
10141014
}
10151015

1016+
@Test
1017+
public void testPreferPicForOptBinaryFeature() throws Exception {
1018+
getAnalysisMock()
1019+
.ccSupport()
1020+
.setupCcToolchainConfig(
1021+
mockToolsConfig,
1022+
CcToolchainConfig.builder()
1023+
.withFeatures(
1024+
CppRuleClasses.NO_LEGACY_FEATURES,
1025+
CppRuleClasses.SUPPORTS_PIC,
1026+
CppRuleClasses.PREFER_PIC_FOR_OPT_BINARIES)
1027+
.withActionConfigs(
1028+
CppActionNames.CPP_LINK_STATIC_LIBRARY,
1029+
CppActionNames.CPP_COMPILE,
1030+
CppActionNames.CPP_LINK_NODEPS_DYNAMIC_LIBRARY));
1031+
useConfiguration("--cpu=k8", "--compilation_mode=opt");
1032+
1033+
scratch.file("x/BUILD", "cc_library(name = 'foo', srcs = ['a.cc'])");
1034+
scratch.file("x/a.cc");
1035+
1036+
RuleConfiguredTarget ccLibrary = (RuleConfiguredTarget) getConfiguredTarget("//x:foo");
1037+
ImmutableList<ActionAnalysisMetadata> actions = ccLibrary.getActions();
1038+
ImmutableList<String> outputs =
1039+
actions.stream()
1040+
.map(ActionAnalysisMetadata::getPrimaryOutput)
1041+
.map(Artifact::getFilename)
1042+
.collect(ImmutableList.toImmutableList());
1043+
assertThat(outputs).doesNotContain("a.o");
1044+
assertThat(outputs).contains("a.pic.o");
1045+
}
1046+
1047+
@Test
1048+
public void testPreferPicForOptBinaryFeatureNeedsPicSupport() throws Exception {
1049+
getAnalysisMock()
1050+
.ccSupport()
1051+
.setupCcToolchainConfig(
1052+
mockToolsConfig,
1053+
CcToolchainConfig.builder()
1054+
.withFeatures(
1055+
CppRuleClasses.NO_LEGACY_FEATURES, CppRuleClasses.PREFER_PIC_FOR_OPT_BINARIES)
1056+
.withActionConfigs(
1057+
CppActionNames.CPP_LINK_STATIC_LIBRARY,
1058+
CppActionNames.CPP_COMPILE,
1059+
CppActionNames.CPP_LINK_NODEPS_DYNAMIC_LIBRARY));
1060+
useConfiguration("--cpu=k8", "--compilation_mode=opt");
1061+
1062+
scratch.file("x/BUILD", "cc_library(name = 'foo', srcs = ['a.cc'])");
1063+
scratch.file("x/a.cc");
1064+
1065+
RuleConfiguredTarget ccLibrary = (RuleConfiguredTarget) getConfiguredTarget("//x:foo");
1066+
ImmutableList<ActionAnalysisMetadata> actions = ccLibrary.getActions();
1067+
ImmutableList<String> outputs =
1068+
actions.stream()
1069+
.map(ActionAnalysisMetadata::getPrimaryOutput)
1070+
.map(Artifact::getFilename)
1071+
.collect(ImmutableList.toImmutableList());
1072+
assertThat(outputs).doesNotContain("a.pic.o");
1073+
assertThat(outputs).contains("a.o");
1074+
}
1075+
10161076
@Test
10171077
public void testWhenSupportsPicNotPresentAndForcePicPassedIsError() throws Exception {
10181078
reporter.removeHandler(failFastHandler);

0 commit comments

Comments
 (0)