From ffdc05d2278d7f9c6e299c923019f689cde5fe76 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 6 Oct 2016 10:55:54 +0000 Subject: Add action_config and feature for linking on Windows Also implemented whole archive feature on Windows 1. Pulled action_configs and features from CppLinkActionConfigs.java 2. Deleted many features not working on Windows. (AFAIK) including: symbol_counts output_execpath_flags_executable global_whole_archive_open runtime_root_flags global_whole_archive_close force_pic_flags 3. Added c++-link-interface-dynamic-library action config Not sure there is such thing on Windows, but it's currently in MANDATORY_LINK_TARGET_TYPES 4. Added build variable "whole_archive_object_files_params" 5. Automatically detect if linker supports /WHOLEARCHIVE and use the corresponding build variable -- Change-Id: I232798a0eb1a3291f972b313a81e678b0121d58c Reviewed-on: https://bazel-review.googlesource.com/#/c/6414 MOS_MIGRATED_REVID=135342093 --- .../build/lib/rules/cpp/CppLinkActionBuilder.java | 54 +++++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/google') 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 f0ca362f9a..178d0688fb 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 @@ -86,6 +86,14 @@ public class CppLinkActionBuilder { public static final String WHOLE_ARCHIVE_LINKER_INPUT_PARAMS_VARIABLE = "whole_archive_linker_params"; + /** + * A build variable for flags providing object files from libraries that were supposed to go in a + * -whole_archive block. Old MSVC linker doesn't support /WHOLEARCHIVE:[LIB] option, so we link + * object files directly as a workaround. + */ + public static final String WHOLE_ARCHIVE_OBJECT_FILES_PARAMS_VARIABLE = + "whole_archive_object_files_params"; + /** A build variable whose presence indicates that whole archive flags should be applied. */ public static final String GLOBAL_WHOLE_ARCHIVE_VARIABLE = "global_whole_archive"; @@ -1120,6 +1128,7 @@ public class CppLinkActionBuilder { Set libopts; List linkerInputParams; List wholeArchiveLinkerInputParams; + List wholeArchiveObjectFilesParams; List noWholeArchiveInputs; public void setRpathRoot(String rPathRoot) { @@ -1142,6 +1151,10 @@ public class CppLinkActionBuilder { this.wholeArchiveLinkerInputParams = wholeArchiveInputParams; } + public void setWholeArchiveObjectFilesParams(List wholeArchiveObjectFilesParams) { + this.wholeArchiveObjectFilesParams = wholeArchiveObjectFilesParams; + } + public void setNoWholeArchiveInputs(List noWholeArchiveInputs) { this.noWholeArchiveInputs = noWholeArchiveInputs; } @@ -1166,6 +1179,10 @@ public class CppLinkActionBuilder { return wholeArchiveLinkerInputParams; } + public List getWholeArchiveObjectFilesParams() { + return wholeArchiveObjectFilesParams; + } + public List getNoWholeArchiveInputs() { return noWholeArchiveInputs; } @@ -1253,6 +1270,9 @@ public class CppLinkActionBuilder { buildVariables.addSequenceVariable( WHOLE_ARCHIVE_LINKER_INPUT_PARAMS_VARIABLE, linkArgCollector.getWholeArchiveLinkerInputParams()); + buildVariables.addSequenceVariable( + WHOLE_ARCHIVE_OBJECT_FILES_PARAMS_VARIABLE, + linkArgCollector.getWholeArchiveObjectFilesParams()); // global archive if (needWholeArchive) { @@ -1417,6 +1437,7 @@ public class CppLinkActionBuilder { } List wholeArchiveInputParams = new ArrayList<>(); + List wholeArchiveObjectFilesParams = new ArrayList<>(); List standardArchiveInputParams = new ArrayList<>(); for (LinkerInput input : linkerInputs) { @@ -1434,7 +1455,11 @@ public class CppLinkActionBuilder { input, standardArchiveInputParams, libOpts, solibDir, rpathRoot); } else { addStaticInputLinkOptions( - input, wholeArchiveInputParams, standardArchiveInputParams, ltoMap); + input, + wholeArchiveInputParams, + wholeArchiveObjectFilesParams, + standardArchiveInputParams, + ltoMap); } } @@ -1454,8 +1479,10 @@ public class CppLinkActionBuilder { includeRuntimeSolibDir = true; addDynamicInputLinkOptions(input, optionsList, libOpts, solibDir, rpathRoot); } else { - addStaticInputLinkOptions(input, + addStaticInputLinkOptions( + input, needWholeArchive ? noWholeArchiveInputs : wholeArchiveInputParams, + needWholeArchive ? null : wholeArchiveObjectFilesParams, needWholeArchive ? noWholeArchiveInputs : standardArchiveInputParams, ltoMap); } @@ -1476,6 +1503,7 @@ public class CppLinkActionBuilder { linkArgCollector.setLinkerInputParams(standardArchiveInputParams); linkArgCollector.setWholeArchiveLinkerInputParams(wholeArchiveInputParams); + linkArgCollector.setWholeArchiveObjectFilesParams(wholeArchiveObjectFilesParams); linkArgCollector.setNoWholeArchiveInputs(noWholeArchiveInputs); if (ltoMap != null) { @@ -1530,7 +1558,10 @@ public class CppLinkActionBuilder { * be supplied for LTO final links. */ private void addStaticInputLinkOptions( - LinkerInput input, List wholeArchiveOptions, List standardOptions, + LinkerInput input, + List wholeArchiveOptions, + List wholeArchiveObjectFilesOptions, + List standardOptions, @Nullable Map ltoMap) { Preconditions.checkState(!(input.getArtifactCategory() == ArtifactCategory.DYNAMIC_LIBRARY)); // If we had any LTO artifacts, ltoMap whould be non-null. In that case, @@ -1575,10 +1606,19 @@ public class CppLinkActionBuilder { return; } - if (input.isFake()) { - options.add(Link.FAKE_OBJECT_PREFIX + inputArtifact.getExecPathString()); - } else { - options.add(inputArtifact.getExecPathString()); + final String fakePrefix = input.isFake() ? Link.FAKE_OBJECT_PREFIX : ""; + + options.add(fakePrefix + inputArtifact.getExecPathString()); + + if (input.containsObjectFiles() && inputNeedsWholeArchive(input)) { + for (Artifact objectFile : input.getObjectFiles()) { + if (ltoMap != null && ltoMap.remove(objectFile) != null) { + // The LTO artifacts that should be included in the final link + // are listed in the linkerParamsFile. + continue; + } + wholeArchiveObjectFilesOptions.add(fakePrefix + objectFile.getExecPathString()); + } } } } -- cgit v1.2.3