diff options
author | 2018-03-05 06:02:42 -0800 | |
---|---|---|
committer | 2018-03-05 06:04:15 -0800 | |
commit | 6144f2ae0852d030e1cc489b04d80b6256e5fba1 (patch) | |
tree | fe121d0bd5efe8c5f4021e529a52736a2e499ede /src | |
parent | 0940e6fd1390452c332d801c39ebd4404daf4ad8 (diff) |
Cleanup linking input computation in CppLinkActionBuilder
RELNOTES: None.
PiperOrigin-RevId: 187855919
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java | 89 |
1 files changed, 40 insertions, 49 deletions
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 521d92c9ef..8015fb2f6e 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 @@ -876,10 +876,25 @@ public class CppLinkActionBuilder { LinkerInputs.simpleLinkerInputs(linkstampMap.values(), ArtifactCategory.OBJECT_FILE)); uniqueLibraries = originalUniqueLibraries; } - final ImmutableSet<Artifact> objectArtifacts = - ImmutableSet.copyOf(LinkerInputs.toLibraryArtifacts(objectFileInputs)); - final ImmutableSet<Artifact> linkstampObjectArtifacts = - ImmutableSet.copyOf(LinkerInputs.toLibraryArtifacts(linkstampObjectFileInputs)); + + Map<Artifact, Artifact> ltoMapping = new HashMap<>(); + ; + if (isFinalLinkOfLtoBuild()) { + for (LtoBackendArtifacts a : allLtoArtifacts) { + ltoMapping.put(a.getBitcodeFile(), a.getObjectFile()); + } + } + Iterable<Artifact> objectArtifacts = + getArtifactsPossiblyLtoMapped(objectFileInputs, ltoMapping); + Iterable<Artifact> linkstampObjectArtifacts = + getArtifactsPossiblyLtoMapped(linkstampObjectFileInputs, ltoMapping); + Iterable<Artifact> expandedInputs = + getArtifactsPossiblyLtoMapped( + Link.mergeInputsDependencies( + uniqueLibraries, + needWholeArchive, + CppHelper.getArchiveType(cppConfiguration, toolchain)), + ltoMapping); ImmutableSet<Artifact> combinedObjectArtifacts = ImmutableSet.<Artifact>builder() @@ -1104,52 +1119,11 @@ public class CppLinkActionBuilder { dependencyInputsBuilder.add(defFile); } - Iterable<Artifact> expandedInputs = - LinkerInputs.toLibraryArtifacts( - Link.mergeInputsDependencies( - uniqueLibraries, - needWholeArchive, - CppHelper.getArchiveType(cppConfiguration, toolchain))); - ImmutableSet<Artifact> expandedNonLibraryInputs = objectArtifacts; - ImmutableSet<Artifact> expandedNonLibraryLinkstampInputs = linkstampObjectArtifacts; - - if (!isLtoIndexing && allLtoArtifacts != null) { - // We are doing LTO, and this is the real link, so substitute - // the LTO bitcode files with the real object files they were translated into. - Map<Artifact, Artifact> ltoMapping = new HashMap<>(); - for (LtoBackendArtifacts a : allLtoArtifacts) { - ltoMapping.put(a.getBitcodeFile(), a.getObjectFile()); - } - - // Handle libraries. - List<Artifact> renamedInputs = new ArrayList<>(); - for (Artifact a : expandedInputs) { - Artifact renamed = ltoMapping.get(a); - renamedInputs.add(renamed == null ? a : renamed); - } - expandedInputs = renamedInputs; - - // Handle non-libraries. - ImmutableSet.Builder<Artifact> renamedNonLibraryInputs = ImmutableSet.builder(); - for (Artifact a : expandedNonLibraryInputs) { - Artifact renamed = ltoMapping.get(a); - renamedNonLibraryInputs.add(renamed == null ? a : renamed); - } - expandedNonLibraryInputs = renamedNonLibraryInputs.build(); - - ImmutableSet.Builder<Artifact> renamedNonLibraryLinkstampInputs = ImmutableSet.builder(); - for (Artifact a : expandedNonLibraryLinkstampInputs) { - Artifact renamed = ltoMapping.get(a); - renamedNonLibraryLinkstampInputs.add(renamed == null ? a : renamed); - } - expandedNonLibraryLinkstampInputs = renamedNonLibraryLinkstampInputs.build(); - } - // getPrimaryInput returns the first element, and that is a public interface - therefore the // order here is important. IterablesChain.Builder<Artifact> inputsBuilder = IterablesChain.<Artifact>builder() - .add(ImmutableList.copyOf(expandedNonLibraryInputs)) + .add(ImmutableList.copyOf(objectArtifacts)) .add(ImmutableList.copyOf(nonCodeInputs)) .add(dependencyInputsBuilder.build()) .add(ImmutableIterable.from(expandedInputs)); @@ -1162,8 +1136,8 @@ public class CppLinkActionBuilder { // Pass along tree artifacts, so they can be properly expanded. ImmutableSet<Artifact> expandedNonLibraryTreeArtifactInputs = ImmutableSet.<Artifact>builder() - .addAll(expandedNonLibraryInputs) - .addAll(expandedNonLibraryLinkstampInputs) + .addAll(objectArtifacts) + .addAll(linkstampObjectArtifacts) .build() .stream() .filter(a -> a.isTreeArtifact()) @@ -1221,7 +1195,7 @@ public class CppLinkActionBuilder { inputsBuilder.add(linkstampMap.values()); } - inputsBuilder.add(expandedNonLibraryLinkstampInputs); + inputsBuilder.add(linkstampObjectArtifacts); return new CppLinkAction( getOwner(), @@ -1248,6 +1222,23 @@ public class CppLinkActionBuilder { toolchain.getTargetCpu()); } + /** We're doing 4-phased lto build, and this is the final link action (4-th phase). */ + private boolean isFinalLinkOfLtoBuild() { + return !isLtoIndexing && allLtoArtifacts != null; + } + + private Iterable<Artifact> getArtifactsPossiblyLtoMapped( + Iterable<LinkerInput> inputs, Map<Artifact, Artifact> ltoMapping) { + Preconditions.checkNotNull(ltoMapping); + Builder<Artifact> result = ImmutableSet.builder(); + Iterable<Artifact> artifacts = LinkerInputs.toLibraryArtifacts(inputs); + for (Artifact a : artifacts) { + Artifact renamed = ltoMapping.get(a); + result.add(renamed == null ? a : renamed); + } + return result.build(); + } + private boolean shouldUseLinkDynamicLibraryTool() { return linkType.isDynamicLibrary() && toolchain.supportsInterfaceSharedObjects() |