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)