Hello,

This is my first email to this list, so please tell me if I'm not following the 
contribution process properly.

Here is the scenario that I am working on:


                  .--->  -flags  A
                  |
    main ----> L -|
                  |
                  '--->  -flags  B

If "-flags" are the same text, only one occurrence appears in the resulting 
linking line in the target's generated link.txt.

This is a simple CMakeLists.txt that reproduces the above (doesn't build 
anything):

cmake_minimum_required(VERSION 3.5)

file(WRITE a.cpp "")
file(WRITE b.cpp "")
file(WRITE lib.cpp "")
file(WRITE main.cpp "")

add_library(A STATIC a.cpp)
add_library(B STATIC b.cpp)

add_library(L STATIC lib.cpp)

target_link_libraries(L
    -Wl,-whole-archive A -Wl,-no-whole-archive
    -Wl,-whole-archive B -Wl,-no-whole-archive
)

add_executable(main main.cpp)
target_link_libraries(main L)

When linking, only one occurrence of "-Wl,-whole-archive" and one of 
"-Wl,-no-whole-archive" are kept. The others are filtered out.

All the best,
Sergio
From 155d039bedfd0cd517866ea131882360700f82a5 Mon Sep 17 00:00:00 2001
From: Sergio Checa Blanco <sergio.ch...@bmw-carit.de>
Date: Mon, 14 Mar 2016 13:34:43 +0100
Subject: [PATCH] Duplicated linking flags are removed

Duplicated flags are not allowed when linking library targets
into a final executable target.

The following CMakeLists.txt can be used to test the
effect of the patch, by checking the content of the
generated file CMakeFiles/main.dir/link.txt.

  cmake_minimum_required(VERSION 3.5)

  file(WRITE a.cpp "")
  file(WRITE b.cpp "")
  file(WRITE lib.cpp "")
  file(WRITE main.cpp "")

  add_library(A STATIC a.cpp)
  add_library(B STATIC b.cpp)

  add_library(L STATIC lib.cpp)

  target_link_libraries(L
      -Wl,-whole-archive A -Wl,-no-whole-archive
      -Wl,-whole-archive B -Wl,-no-whole-archive
  )

  add_executable(main main.cpp)
  target_link_libraries(main L)
---
 Source/cmComputeLinkDepends.cxx | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/Source/cmComputeLinkDepends.cxx b/Source/cmComputeLinkDepends.cxx
index 2796fdf..08df390 100644
--- a/Source/cmComputeLinkDepends.cxx
+++ b/Source/cmComputeLinkDepends.cxx
@@ -298,6 +298,7 @@ cmComputeLinkDepends::AllocateLinkEntry(std::string const& item)
     index_entry(item, static_cast<int>(this->EntryList.size()));
   std::map<std::string, int>::iterator
     lei = this->LinkEntryIndex.insert(index_entry).first;
+  lei->second = this->EntryList.size();
   this->EntryList.push_back(LinkEntry());
   this->InferredDependSets.push_back(0);
   this->EntryConstraintGraph.push_back(EdgeList());
@@ -307,9 +308,12 @@ cmComputeLinkDepends::AllocateLinkEntry(std::string const& item)
 //----------------------------------------------------------------------------
 int cmComputeLinkDepends::AddLinkEntry(cmLinkItem const& item)
 {
+  bool itemIsFlag = (!item.Target && item[0] == '-' && item[1] != 'l' &&
+                 item.substr(0, 10) != "-framework");
+
   // Check if the item entry has already been added.
   std::map<std::string, int>::iterator lei = this->LinkEntryIndex.find(item);
-  if(lei != this->LinkEntryIndex.end())
+  if(lei != this->LinkEntryIndex.end() && !itemIsFlag)
     {
     // Yes.  We do not need to follow the item's dependencies again.
     return lei->second;
@@ -323,8 +327,7 @@ int cmComputeLinkDepends::AddLinkEntry(cmLinkItem const& item)
   LinkEntry& entry = this->EntryList[index];
   entry.Item = item;
   entry.Target = item.Target;
-  entry.IsFlag = (!entry.Target && item[0] == '-' && item[1] != 'l' &&
-                  item.substr(0, 10) != "-framework");
+  entry.IsFlag = itemIsFlag;
 
   // If the item has dependencies queue it to follow them.
   if(entry.Target)
-- 
2.1.3

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to