summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele E. Domenichelli <daniele.domenichelli@iit.it>2014-11-09 17:35:20 +0100
committerBrad King <brad.king@kitware.com>2014-11-10 14:46:22 -0500
commit4db31095e5f4ce410c013e0b92bc5d1baee03934 (patch)
tree8ec4f16583bc150b3f933e3b48cff004ab5d536e
parent06c3b7a8224b9864f1771de62926c1d1fb3476cc (diff)
downloadcmake-4db31095e5f4ce410c013e0b92bc5d1baee03934.tar.gz
Fix link line order when shared libraries are de-duplicated
Since commit v3.1.0-rc1~227^2~1 (De-duplicate shared library targets in generated link lines, 2014-07-30) we de-duplicate shared library targets on the link line. However, some toolchains will fail linking if an executable is linking to a shared library that is not used directly and a static library that depends on the shared one. The linker may not keep the reference to the shared library the first time and then the symbols needed by the static library may not be found. Fix this by reversing the direction of the for loop that removes the duplicate shared libraries, in order to ensure that the last occurrence of the library is left instead of the first one. Extend Tests/Dependency with a case covering this behavior. Create an executable that links to a shared library and a static library but only needs the shared library as a dependency of the static library. Co-Author: Brad King <brad.king@kitware.com>
-rw-r--r--Source/cmComputeLinkDepends.cxx13
-rw-r--r--Tests/Dependency/CMakeLists.txt1
-rw-r--r--Tests/Dependency/Case5/CMakeLists.txt8
-rw-r--r--Tests/Dependency/Case5/bar.c12
-rw-r--r--Tests/Dependency/Case5/foo.c9
-rw-r--r--Tests/Dependency/Case5/main.c7
6 files changed, 46 insertions, 4 deletions
diff --git a/Source/cmComputeLinkDepends.cxx b/Source/cmComputeLinkDepends.cxx
index b13a125a39..1fb8f30f4a 100644
--- a/Source/cmComputeLinkDepends.cxx
+++ b/Source/cmComputeLinkDepends.cxx
@@ -263,21 +263,26 @@ cmComputeLinkDepends::Compute()
this->OrderLinkEntires();
// Compute the final set of link entries.
+ // Iterate in reverse order so we can keep only the last occurrence
+ // of a shared library.
std::set<int> emmitted;
- for(std::vector<int>::const_iterator li = this->FinalLinkOrder.begin();
- li != this->FinalLinkOrder.end(); ++li)
+ for(std::vector<int>::const_reverse_iterator
+ li = this->FinalLinkOrder.rbegin(),
+ le = this->FinalLinkOrder.rend();
+ li != le; ++li)
{
int i = *li;
LinkEntry const& e = this->EntryList[i];
cmTarget const* t = e.Target;
- // Entries that we know the linker will re-use for symbols
- // needed by later entries do not need to be repeated.
+ // Entries that we know the linker will re-use do not need to be repeated.
bool uniquify = t && t->GetType() == cmTarget::SHARED_LIBRARY;
if(!uniquify || emmitted.insert(i).second)
{
this->FinalLinkEntries.push_back(e);
}
}
+ // Reverse the resulting order since we iterated in reverse.
+ std::reverse(this->FinalLinkEntries.begin(), this->FinalLinkEntries.end());
// Display the final set.
if(this->DebugMode)
diff --git a/Tests/Dependency/CMakeLists.txt b/Tests/Dependency/CMakeLists.txt
index ef4204803a..ebc2d1066f 100644
--- a/Tests/Dependency/CMakeLists.txt
+++ b/Tests/Dependency/CMakeLists.txt
@@ -51,3 +51,4 @@ add_subdirectory(Case1)
add_subdirectory(Case2)
add_subdirectory(Case3)
add_subdirectory(Case4)
+add_subdirectory(Case5)
diff --git a/Tests/Dependency/Case5/CMakeLists.txt b/Tests/Dependency/Case5/CMakeLists.txt
new file mode 100644
index 0000000000..e954b02eae
--- /dev/null
+++ b/Tests/Dependency/Case5/CMakeLists.txt
@@ -0,0 +1,8 @@
+project(CASE5 C)
+
+add_library(case5Foo SHARED foo.c)
+add_library(case5Bar STATIC bar.c)
+target_link_libraries(case5Bar case5Foo)
+
+add_executable(case5 main.c)
+target_link_libraries(case5 case5Foo case5Bar)
diff --git a/Tests/Dependency/Case5/bar.c b/Tests/Dependency/Case5/bar.c
new file mode 100644
index 0000000000..4cb1b1bd1d
--- /dev/null
+++ b/Tests/Dependency/Case5/bar.c
@@ -0,0 +1,12 @@
+#ifdef _WIN32
+__declspec(dllimport)
+#endif
+void foo(void);
+
+#include <stdio.h>
+
+void bar(void)
+{
+ foo();
+ printf("bar()\n");
+}
diff --git a/Tests/Dependency/Case5/foo.c b/Tests/Dependency/Case5/foo.c
new file mode 100644
index 0000000000..794833dceb
--- /dev/null
+++ b/Tests/Dependency/Case5/foo.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+void foo(void)
+{
+ printf("foo()\n");
+}
diff --git a/Tests/Dependency/Case5/main.c b/Tests/Dependency/Case5/main.c
new file mode 100644
index 0000000000..ae3dc9570c
--- /dev/null
+++ b/Tests/Dependency/Case5/main.c
@@ -0,0 +1,7 @@
+void bar(void);
+
+int main(int argc, char *argv[])
+{
+ bar();
+ return 0;
+}