From c2566eac21dcc0fce1102da051c6bcaf7149d01c Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 6 Feb 2024 19:49:27 +0100 Subject: [PATCH] Cmake: Create 'get_link_libraries' to make it recursive (#2709) The issue was spotted when building with Caliper master. It returns MPI as a target and hence we need this fix. --- cmake/CMakeListsNrnMech.cmake | 94 ++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/cmake/CMakeListsNrnMech.cmake b/cmake/CMakeListsNrnMech.cmake index 38ea5084c8..6d53842273 100644 --- a/cmake/CMakeListsNrnMech.cmake +++ b/cmake/CMakeListsNrnMech.cmake @@ -23,48 +23,60 @@ endif() # Interview might have linked to libnrniv but we don't want to link to special list(REMOVE_ITEM NRN_LINK_LIBS "interviews") -# CMake does some magic to transform sys libs to -l. We replicate it -foreach(link_lib ${NRN_LINK_LIBS}) - # skip static readline library as it will be linked to nrniv (e.g. with wheel) also stub libraries - # from OSX can be skipped - if("${link_lib}" MATCHES "(libreadline.a|/*.tbd)") - continue() - endif() +function(get_link_libraries libs) + # CMake does some magic to transform sys libs to -l. We replicate it + foreach(link_lib ${libs}) + # skip static readline library as it will be linked to nrniv (e.g. with wheel) also stub + # libraries from OSX can be skipped + if("${link_lib}" MATCHES "(libreadline.a|/*.tbd)") + continue() + endif() - get_filename_component(dir_path ${link_lib} DIRECTORY) - if(TARGET ${link_lib}) - get_property( - link_flag - TARGET ${link_lib} - PROPERTY INTERFACE_LINK_LIBRARIES) - set(description - "Extracting link flags from target '${link_lib}', beware that this can be fragile.") - # Not use it yet because it can be generator expressions get_property(compile_flag TARGET - # ${link_lib} PROPERTY INTERFACE_COMPILE_OPTIONS) string(APPEND NRN_COMPILE_DEFS - # ${compile_flag}) - elseif(NOT dir_path) - set(link_flag "-l${link_lib}") - set(description - "Generating link flags from name '${link_lib}', beware that this can be fragile.") - # avoid library paths from special directory /nrnwheel which used to build wheels under docker - # container - elseif("${dir_path}" MATCHES "^/nrnwheel") - continue() - elseif("${dir_path}" MATCHES "^(/lib|/lib64|/usr/lib|/usr/lib64)$") - # NAME_WLE not avaialble with CMake version < 3.14 - get_filename_component(libname ${link_lib} NAME) - string(REGEX REPLACE "\\.[^.]*$" "" libname_wle ${libname}) - string(REGEX REPLACE "^lib" "" libname_wle ${libname_wle}) - set(link_flag "-l${libname_wle}") - set(description - "Extracting link flags from path '${link_lib}', beware that this can be fragile.") - else() - set(link_flag "${link_lib} -Wl,-rpath,${dir_path}") - set(description "Generating link flags from path ${link_lib}") - endif() - message(NOTICE "${description} Got: ${link_flag}") - string(APPEND NRN_LINK_DEFS " ${link_flag}") -endforeach() + get_filename_component(dir_path ${link_lib} DIRECTORY) + if(TARGET ${link_lib}) + get_property( + sublink_flag + TARGET ${link_lib} + PROPERTY INTERFACE_LINK_LIBRARIES) + set(description + "Extracting link flags from target '${link_lib}', beware that this can be fragile.") + # Not use it yet because it can be generator expressions get_property(compile_flag TARGET + # ${link_lib} PROPERTY INTERFACE_COMPILE_OPTIONS) string(APPEND NRN_COMPILE_DEFS + # ${compile_flag}) + foreach(sublink_lib ${sublink_flag}) + if(TARGET ${sublink_lib}) + message(NOTICE "For '${link_lib}' going to see TARGET '${sublink_lib}' recursively.") + get_link_libraries(${sublink_lib}) + else() + set(link_flag "${link_flag} ${sublink_flag}") + endif() + endforeach() + elseif(NOT dir_path) + set(link_flag "-l${link_lib}") + set(description + "Generating link flags from name '${link_lib}', beware that this can be fragile.") + # avoid library paths from special directory /nrnwheel which used to build wheels under docker + # container + elseif("${dir_path}" MATCHES "^/nrnwheel") + continue() + elseif("${dir_path}" MATCHES "^(/lib|/lib64|/usr/lib|/usr/lib64)$") + # NAME_WLE not avaialble with CMake version < 3.14 + get_filename_component(libname ${link_lib} NAME) + string(REGEX REPLACE "\\.[^.]*$" "" libname_wle ${libname}) + string(REGEX REPLACE "^lib" "" libname_wle ${libname_wle}) + set(link_flag "-l${libname_wle}") + set(description + "Extracting link flags from path '${link_lib}', beware that this can be fragile.") + else() + set(link_flag "${link_lib} -Wl,-rpath,${dir_path}") + set(description "Generating link flags from path ${link_lib}") + endif() + message(NOTICE "${description} Got: ${link_flag}") + string(APPEND NRN_LINK_DEFS " ${link_flag}") + endforeach() +endfunction(get_link_libraries) + +get_link_libraries("${NRN_LINK_LIBS}") # Compiler flags depending on cmake build type from BUILD_TYPE__FLAGS string(TOUPPER "${CMAKE_BUILD_TYPE}" _BUILD_TYPE)