diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java index 7d31b969243471..7b79b4922c461c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java @@ -736,7 +736,8 @@ private boolean createDynamicLinkAction( "-Wl,-soname=" + SolibSymlinkAction.getDynamicLibrarySoname( linkerOutput.getRootRelativePath(), - /* preserveName= */ false, + /* preserveName= */ dynamicLinkType + != LinkTargetType.NODEPS_DYNAMIC_LIBRARY, actionConstructionContext.getConfiguration().getMnemonic())); } } @@ -841,6 +842,10 @@ private boolean createDynamicLinkAction( if (dynamicLinkActionBuilder.getAllLtoBackendArtifacts() != null) { ccLinkingOutputs.addAllLtoArtifacts(dynamicLinkActionBuilder.getAllLtoBackendArtifacts()); } + Artifact implLibraryLinkArtifact = getDynamicLibrarySolibSymlinkOutput(linkerOutput); + if (implLibraryLinkArtifact != null) { + dynamicLinkActionBuilder.setDynamicLibrarySolibSymlinkOutput(implLibraryLinkArtifact); + } CppLinkAction dynamicLinkAction = dynamicLinkActionBuilder.build(); if (dynamicLinkType.isExecutable()) { ccLinkingOutputs.setExecutable(linkerOutput); @@ -866,14 +871,16 @@ private boolean createDynamicLinkAction( } libraryToLinkBuilder.setDynamicLibrary(dynamicLibrary.getArtifact()); } else { - Artifact implLibraryLinkArtifact = - SolibSymlinkAction.getDynamicLibrarySymlink( - /* actionRegistry= */ actionRegistry, - /* actionConstructionContext= */ actionConstructionContext, - ccToolchain.getSolibDirectory(), - dynamicLibrary.getArtifact(), - /* preserveName= */ false, - /* prefixConsumer= */ false); + if (dynamicLinkType == LinkTargetType.NODEPS_DYNAMIC_LIBRARY) { + implLibraryLinkArtifact = + SolibSymlinkAction.getDynamicLibrarySymlink( + /* actionRegistry= */ actionRegistry, + /* actionConstructionContext= */ actionConstructionContext, + ccToolchain.getSolibDirectory(), + dynamicLibrary.getArtifact(), + /* preserveName= */ false, + /* prefixConsumer= */ false); + } libraryToLinkBuilder.setDynamicLibrary(implLibraryLinkArtifact); libraryToLinkBuilder.setResolvedSymlinkDynamicLibrary(dynamicLibrary.getArtifact()); @@ -884,7 +891,8 @@ private boolean createDynamicLinkAction( /* actionConstructionContext= */ actionConstructionContext, ccToolchain.getSolibDirectory(), interfaceLibrary.getArtifact(), - /* preserveName= */ false, + // Need to preserve name for transitive shared libraries that may be distributed. + /* preserveName= */ dynamicLinkType != LinkTargetType.NODEPS_DYNAMIC_LIBRARY, /* prefixConsumer= */ false); libraryToLinkBuilder.setInterfaceLibrary(libraryLinkArtifact); libraryToLinkBuilder.setResolvedSymlinkInterfaceLibrary(interfaceLibrary.getArtifact()); @@ -1048,4 +1056,24 @@ private static List convertLibraryToLinkListToLinker } return librariesToLinkBuilder.build(); } + + @Nullable + private Artifact getDynamicLibrarySolibSymlinkOutput(Artifact linkerOutputArtifact) { + if (dynamicLinkType != LinkTargetType.DYNAMIC_LIBRARY + || neverlink + || featureConfiguration.isEnabled(CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY)) { + return null; + } + return SolibSymlinkAction.getDynamicLibrarySymlink( + /* actionRegistry= */ actionRegistry, + /* actionConstructionContext= */ actionConstructionContext, + ccToolchain.getSolibDirectory(), + linkerOutputArtifact, + // For transitive shared libraries we want to preserve the name of the original library so + // that distribution artifacts can be linked against it and not against the mangled name. + // This makes it possible to find the library on the system if the RPATH has been set + // properly. + /* preserveName= */ true, + /* prefixConsumer= */ false); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java index 7a1968150e1844..6e959e14cc01be 100755 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java @@ -93,6 +93,18 @@ import net.starlark.java.eval.StarlarkThread; import net.starlark.java.eval.Structure; import net.starlark.java.eval.Tuple; +import com.google.devtools.build.docgen.annot.DocCategory; +import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi; +import net.starlark.java.annot.Param; +import net.starlark.java.annot.ParamType; +import net.starlark.java.annot.StarlarkBuiltin; +import net.starlark.java.annot.StarlarkMethod; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.BuiltinProvider; +import com.google.devtools.build.lib.packages.NativeInfo; +import com.google.devtools.build.docgen.annot.StarlarkConstructor; +import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi; +import net.starlark.java.eval.Starlark; /** * A module that contains Starlark utilities for C++ support. @@ -139,11 +151,120 @@ public abstract class CcModule public abstract CppSemantics getSemantics(Language language); + /** + * This class exists only for a 6.3 cherrypick. For 7.0 this will be in Starlark. + */ + @StarlarkBuiltin( + name = "CcSharedLibraryHintInfo", + category = DocCategory.PROVIDER, + documented = false + ) + @Immutable + private static final class CcSharedLibraryHintInfo extends NativeInfo implements StructApi { + private static final Provider PROVIDER = new Provider(); + + private final List attributes; + private final List