From 45dc41d758bc434b3a00e2083d9808d12c4be3eb Mon Sep 17 00:00:00 2001
From: Marko Petric <marko.petric@cern.ch>
Date: Fri, 20 Nov 2020 21:16:13 +0100
Subject: [PATCH] Resolve which type of linker is used and pass appropriate
 flags

---
 cmake/DD4hepBuild.cmake | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index 088abfa8c..5b5c88c08 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -80,12 +80,28 @@ macro(dd4hep_set_compiler_flags)
     message( STATUS "Unknown thread library: CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}" )
   endif()
 
-  if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
+  # resolve which linker we use
+  execute_process(COMMAND ${CMAKE_CXX_COMPILER} -Wl,--version OUTPUT_VARIABLE stdout ERROR_QUIET)
+  if("${stdout}" MATCHES "GNU ")
+    set(LINKER_TYPE "GNU")
+    dd4hep_debug("|DDD> Detected GNU compatible linker: ${stdout}" )
+  else()
+    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -Wl,-v ERROR_VARIABLE stderr )
+    if(("${stderr}" MATCHES "PROGRAM:ld") AND ("${stderr}" MATCHES "PROJECT:ld64"))
+      set(LINKER_TYPE "APPLE")
+      dd4hep_debug("|DDD> Detected Apple linker: ${stderr}" )
+    else()
+      set(LINKER_TYPE "unknown")
+      dd4hep_debug("|DDD> Detected unknown linker: ${stdout} ${stderr}" )
+    endif()
+  endif()
+
+  if("${LINKER_TYPE}" STREQUAL "APPLE")
     SET ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error")
-  elseif ( ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" ))
+  elseif("${LINKER_TYPE}" STREQUAL "GNU")
     SET ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
   else()
-    MESSAGE( WARNING "We do not test with the ${CMAKE_CXX_COMPILER_ID} compiler, use at your own discretion" )
+    MESSAGE( WARNING "No known linker (GNU or Apple) has been detected, pass no flags to linker" )
   endif()
 
  #---RPATH options-------------------------------------------------------------------------------
-- 
GitLab