aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java
diff options
context:
space:
mode:
authorGravatar Michael Staib <mstaib@google.com>2016-07-20 21:33:57 +0000
committerGravatar John Cater <jcater@google.com>2016-07-21 20:34:56 +0000
commit5bbc7a4f1747138205fc67a45a3af547b027f984 (patch)
treeabbf1573ddfbb51b39b6361e6fca59ccca6fba50 /src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java
parent8fa303b1ac0a4bffcc535d85c07c86b3c8657d32 (diff)
Stop input and output of cc_library from clobbering each other.
Before this change: Any given cc_library can only contribute one library with a given name to targets which depend on it. If an input library has the same name as the cc_library which it is an input to, the decision of which to use is based on the link mode. e.g., cc_library( name = "foo", srcs = ["foo.c", "libfoo.so"], ) will only contribute libfoo.a (a static library containing foo.o) in static mode, while it will only contribute libfoo.so (the precompiled shared library) in dynamic mode. This change alters cc_library's behavior in this case: * If libfoo.a would be empty (i.e., there are no linkable sources), then this is allowed. The libfoo.so from srcs is simply passed through. (Previously, the empty libfoo.a would be forwarded.) * Otherwise, this is an error. In the case where there are multiple libraries in the srcs with the same library identifier (lib[name].[a|so|lo]), cc_library will still choose one based on the link mode. This behavior has not changed. Similarly, cc_library will still choose one of its own outputs based on the link mode. That behavior has not changed either. RELNOTES[INC]: It is now an error to include a precompiled library (.a, .lo, .so) in a cc_library which would generate a library with the same name (e.g., libfoo.so in cc_library foo) if that library also contains other linkable sources. -- MOS_MIGRATED_REVID=127989348
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java51
1 files changed, 44 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java
index d6b6a2b2f1..c43404e3b9 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java
@@ -15,6 +15,7 @@
package com.google.devtools.build.lib.rules.cpp;
import com.google.common.base.Function;
+import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@@ -913,14 +914,50 @@ public final class CcLibraryHelper {
CcLinkingOutputs originalLinkingOutputs = ccLinkingOutputs;
if (!(
staticLibraries.isEmpty() && picStaticLibraries.isEmpty() && dynamicLibraries.isEmpty())) {
+
+ CcLinkingOutputs.Builder newOutputsBuilder = new CcLinkingOutputs.Builder();
+ if (!ccOutputs.isEmpty()) {
+ // Add the linked outputs of this rule iff we had anything to put in them, but then
+ // make sure we're not colliding with some library added from the inputs.
+ newOutputsBuilder.merge(originalLinkingOutputs);
+ Iterable<LibraryToLink> allLibraries =
+ Iterables.concat(staticLibraries, picStaticLibraries, dynamicLibraries);
+ for (LibraryToLink precompiledLibrary : allLibraries) {
+ List<LibraryToLink> matchingLibs =
+ originalLinkingOutputs.getLibrariesWithSameIdentifierAs(precompiledLibrary);
+ if (!matchingLibs.isEmpty()) {
+ Iterable<String> matchingLibArtifactNames =
+ Iterables.transform(
+ matchingLibs,
+ new Function<LibraryToLink, String>() {
+ @Override
+ public String apply(LibraryToLink input) {
+ return input.getOriginalLibraryArtifact().getFilename();
+ }
+ });
+ ruleContext.ruleError(
+ "Can't put "
+ + precompiledLibrary.getArtifact().getFilename()
+ + " into the srcs of a "
+ + ruleContext.getRuleClassNameForLogging()
+ + " with the same name ("
+ + ruleContext.getRule().getName()
+ + ") which also contains other code or objects to link; it shares a name with "
+ + Joiner.on(", ").join(matchingLibArtifactNames)
+ + " (output compiled and linked from the non-library sources of this rule), "
+ + "which could cause confusion");
+ }
+ }
+ }
+
// Merge the pre-compiled libraries (static & dynamic) into the linker outputs.
- ccLinkingOutputs = new CcLinkingOutputs.Builder()
- .merge(ccLinkingOutputs)
- .addStaticLibraries(staticLibraries)
- .addPicStaticLibraries(picStaticLibraries)
- .addDynamicLibraries(dynamicLibraries)
- .addExecutionDynamicLibraries(dynamicLibraries)
- .build();
+ ccLinkingOutputs =
+ newOutputsBuilder
+ .addStaticLibraries(staticLibraries)
+ .addPicStaticLibraries(picStaticLibraries)
+ .addDynamicLibraries(dynamicLibraries)
+ .addExecutionDynamicLibraries(dynamicLibraries)
+ .build();
}
DwoArtifactsCollector dwoArtifacts =