Skip to content

Commit

Permalink
C++: More CcLinkParams removal
Browse files Browse the repository at this point in the history
This was rolled back from bazelbuild@2a01403 to make a different rollback easier. Nothing wrong with this CL.

RELNOTES:none
PiperOrigin-RevId: 229917327
  • Loading branch information
oquenchil authored and Copybara-Service committed Jan 18, 2019
1 parent 3759e38 commit 0b66fdc
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.rules.android;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand All @@ -33,12 +34,11 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.rules.cpp.CcLinkParams;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider;
import com.google.devtools.build.lib.rules.cpp.CppFileTypes;
import com.google.devtools.build.lib.rules.cpp.CppHelper;
import com.google.devtools.build.lib.rules.cpp.CppSemantics;
import com.google.devtools.build.lib.rules.cpp.LinkerInput;
import com.google.devtools.build.lib.rules.cpp.LibraryToLinkWrapper;
import com.google.devtools.build.lib.rules.nativedeps.NativeDepsHelper;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -68,17 +68,15 @@ public static NativeLibs fromLinkedNativeDeps(
String nativeDepsLibraryBasename = null;
for (Map.Entry<String, Collection<TransitiveInfoCollection>> entry :
getSplitDepsByArchitecture(ruleContext, depsAttributes).asMap().entrySet()) {
CcLinkParams linkParams =
CcInfo ccInfo =
AndroidCommon.getCcInfo(
entry.getValue(),
ImmutableList.of("-Wl,-soname=lib" + ruleContext.getLabel().getName()))
.getCcLinkingInfo()
.getStaticModeParamsForDynamicLibrary();
entry.getValue(),
ImmutableList.of("-Wl,-soname=lib" + ruleContext.getLabel().getName()));

Artifact nativeDepsLibrary =
NativeDepsHelper.linkAndroidNativeDepsIfPresent(
ruleContext,
linkParams,
ccInfo,
configurationMap.get(entry.getKey()),
toolchainsByCpu.get(entry.getKey()),
cppSemantics);
Expand All @@ -89,7 +87,8 @@ public static NativeLibs fromLinkedNativeDeps(
nativeDepsLibraryBasename = nativeDepsLibrary.getExecPath().getBaseName();
}
librariesBuilder.addAll(
filterUniqueSharedLibraries(ruleContext, nativeDepsLibrary, linkParams.getLibraries()));
filterUniqueSharedLibraries(
ruleContext, nativeDepsLibrary, ccInfo.getCcLinkingContext().getLibraries()));
NestedSet<Artifact> libraries = librariesBuilder.build();

if (!libraries.isEmpty()) {
Expand Down Expand Up @@ -246,20 +245,33 @@ private static Map<String, CcToolchainProvider> getToolchainsByCpu(RuleContext r
}

private static Iterable<Artifact> filterUniqueSharedLibraries(
RuleContext ruleContext, Artifact linkedLibrary, NestedSet<? extends LinkerInput> libraries) {
RuleContext ruleContext, Artifact linkedLibrary, NestedSet<LibraryToLinkWrapper> libraries) {
Map<String, Artifact> basenames = new HashMap<>();
Set<Artifact> artifacts = new HashSet<>();
if (linkedLibrary != null) {
basenames.put(linkedLibrary.getExecPath().getBaseName(), linkedLibrary);
}
for (LinkerInput linkerInput : libraries) {
String name = linkerInput.getArtifact().getFilename();
if (!(CppFileTypes.SHARED_LIBRARY.matches(name)
|| CppFileTypes.VERSIONED_SHARED_LIBRARY.matches(name))) {
for (LibraryToLinkWrapper linkerInput : libraries) {
if (linkerInput.getPicStaticLibrary() != null || linkerInput.getStaticLibrary() != null) {
// This is not a shared library and will not be loaded by Android, so skip it.
continue;
}
Artifact artifact = linkerInput.getOriginalLibraryArtifact();
Artifact artifact = null;
if (linkerInput.getInterfaceLibrary() != null) {
if (linkerInput.getResolvedSymlinkInterfaceLibrary() != null) {
artifact = linkerInput.getResolvedSymlinkInterfaceLibrary();
} else {
artifact = linkerInput.getInterfaceLibrary();
}
} else {
if (linkerInput.getResolvedSymlinkDynamicLibrary() != null) {
artifact = linkerInput.getResolvedSymlinkDynamicLibrary();
} else {
artifact = linkerInput.getDynamicLibrary();
}
}
Preconditions.checkNotNull(artifact);

if (!artifacts.add(artifact)) {
// We have already reached this library, e.g., through a different solib symlink.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public Artifact create(
private final Set<LinkerInput> objectFiles = new LinkedHashSet<>();
private final Set<Artifact> nonCodeInputs = new LinkedHashSet<>();
private final NestedSetBuilder<LibraryToLink> libraries = NestedSetBuilder.linkOrder();
private final NestedSetBuilder<LibraryToLinkWrapper> libraryToLinkWrappers =
NestedSetBuilder.linkOrder();
private NestedSet<Artifact> linkerFiles = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
private Artifact runtimeMiddleman;
private ArtifactCategory toolchainLibrariesType = null;
Expand Down Expand Up @@ -576,8 +578,63 @@ boolean canSplitCommandLine() {
}
}

private List<LibraryToLink> convertLibraryToLinkWrapperListToLibraryToLinkList(
NestedSet<LibraryToLinkWrapper> libraryToLinkWrappers) {
ImmutableList.Builder<LibraryToLink> librariesToLink = ImmutableList.builder();
for (LibraryToLinkWrapper libraryToLinkWrapper : libraryToLinkWrappers) {
LibraryToLink staticLibraryToLink =
libraryToLinkWrapper.getStaticLibrary() == null
? null
: libraryToLinkWrapper.getStaticLibraryToLink();
LibraryToLink picStaticLibraryToLink =
libraryToLinkWrapper.getPicStaticLibrary() == null
? null
: libraryToLinkWrapper.getPicStaticLibraryToLink();
LibraryToLink libraryToLinkToUse = null;
if (linkingMode == LinkingMode.STATIC) {
if (linkType.isDynamicLibrary()) {
if (picStaticLibraryToLink != null) {
libraryToLinkToUse = picStaticLibraryToLink;
} else if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;
}
} else {
if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;
} else if (picStaticLibraryToLink != null) {
libraryToLinkToUse = picStaticLibraryToLink;
}
}
}
if (libraryToLinkToUse == null) {
if (libraryToLinkWrapper.getInterfaceLibrary() != null) {
libraryToLinkToUse = libraryToLinkWrapper.getInterfaceLibraryToLink();
} else if (libraryToLinkWrapper.getDynamicLibrary() != null) {
libraryToLinkToUse = libraryToLinkWrapper.getDynamicLibraryToLink();
}
}
Preconditions.checkNotNull(libraryToLinkToUse);
checkLibrary(libraryToLinkToUse);
librariesToLink.add(libraryToLinkToUse);
}
return librariesToLink.build();
}

/** Builds the Action as configured and returns it. */
public CppLinkAction build() throws InterruptedException {
NestedSet<LibraryToLink> originalUniqueLibraries = null;

if (libraryToLinkWrappers.isEmpty()) {
originalUniqueLibraries = libraries.build();
} else {
Preconditions.checkState(libraries.isEmpty());
originalUniqueLibraries =
NestedSetBuilder.<LibraryToLink>linkOrder()
.addAll(
convertLibraryToLinkWrapperListToLibraryToLinkList(libraryToLinkWrappers.build()))
.build();
}

// Executable links do not have library identifiers.
boolean hasIdentifier = (libraryIdentifier != null);
boolean isExecutable = linkType.isExecutable();
Expand Down Expand Up @@ -630,8 +687,6 @@ public CppLinkAction build() throws InterruptedException {
includeLinkStaticInLtoIndexing
|| (linkingMode == Link.LinkingMode.DYNAMIC && !ltoCompilationContext.isEmpty());

NestedSet<LibraryToLink> originalUniqueLibraries = libraries.build();

PathFragment ltoOutputRootPrefix = null;
if (isLtoIndexing) {
Preconditions.checkState(allLtoArtifacts == null);
Expand Down Expand Up @@ -1316,6 +1371,7 @@ public CppLinkActionBuilder addLibrary(LibraryToLink input) {
* libraries.
*/
public CppLinkActionBuilder addLibraries(Iterable<LibraryToLink> inputs) {
Preconditions.checkState(libraryToLinkWrappers.isEmpty());
for (LibraryToLink input : inputs) {
checkLibrary(input);
if (input.isMustKeepDebug()) {
Expand All @@ -1326,6 +1382,17 @@ public CppLinkActionBuilder addLibraries(Iterable<LibraryToLink> inputs) {
return this;
}

public CppLinkActionBuilder addLibraryToLinkWrappers(Iterable<LibraryToLinkWrapper> inputs) {
Preconditions.checkState(libraries.isEmpty());
for (LibraryToLinkWrapper input : inputs) {
if (input.getMustKeepDebug()) {
mustKeepDebug = true;
}
}
this.libraryToLinkWrappers.addAll(inputs);
return this;
}

/**
* Sets the type of ELF file to be created (.a, .so, .lo, executable). The default is {@link
* LinkTargetType#STATIC_LIBRARY}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ public static List<Artifact> getStaticModeParamsForExecutableLibraries(
return getStaticModeParamsForExecutableLibraries(fromCcLinkingInfo(ccLinkingInfo));
}

public static List<Artifact> getStaticModeParamsForDynamicLibraryLibraries(
CcLinkingContext ccLinkingContext) {
ImmutableList.Builder<Artifact> artifactListBuilder = ImmutableList.builder();
for (LibraryToLinkWrapper library : ccLinkingContext.getLibraries()) {
if (library.getPicStaticLibrary() != null) {
artifactListBuilder.add(library.getPicStaticLibrary());
} else if (library.getStaticLibrary() != null) {
artifactListBuilder.add(library.getStaticLibrary());
} else if (library.getInterfaceLibrary() != null) {
artifactListBuilder.add(library.getInterfaceLibrary());
} else {
artifactListBuilder.add(library.getDynamicLibrary());
}
}
return artifactListBuilder.build();
}

public NestedSet<LibraryToLinkWrapper> getLibraries() {
return libraries;
}
Expand Down Expand Up @@ -381,16 +398,28 @@ public Artifact getDynamicLibrary() {
return dynamicLibrary;
}

public Artifact getResolvedSymlinkDynamicLibrary() {
return resolvedSymlinkDynamicLibrary;
}

@Override
public Artifact getInterfaceLibrary() {
return interfaceLibrary;
}

public Artifact getResolvedSymlinkInterfaceLibrary() {
return resolvedSymlinkInterfaceLibrary;
}

@Override
public boolean getAlwayslink() {
return alwayslink;
}

public boolean getMustKeepDebug() {
return mustKeepDebug;
}

public static Builder builder() {
return new Builder();
}
Expand Down
Loading

0 comments on commit 0b66fdc

Please sign in to comment.