Skip to content

Commit

Permalink
Cmake: Create 'get_link_libraries' to make it recursive (#2709)
Browse files Browse the repository at this point in the history
The issue was spotted when building with Caliper master. It returns MPI as a target and hence we need this fix.
  • Loading branch information
alkino authored Feb 6, 2024
1 parent 77130a8 commit c2566ea
Showing 1 changed file with 53 additions and 41 deletions.
94 changes: 53 additions & 41 deletions cmake/CMakeListsNrnMech.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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<libname>. 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<libname>. 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_<LANG>_FLAGS
string(TOUPPER "${CMAKE_BUILD_TYPE}" _BUILD_TYPE)
Expand Down

0 comments on commit c2566ea

Please sign in to comment.