From d5d9ca2294cf48ec31d6c81c584a0420d952af12 Mon Sep 17 00:00:00 2001 From: Daljit Singh Date: Thu, 4 Apr 2024 13:46:37 +0100 Subject: [PATCH] Use CheckSymbolExists to verify existence of lgamma_r Using CheckSourceCompiles doesn't actually test linking. CheckSymbolExists does. On MacOS, it seems that lgamma_r needs _REENTRANT macro to defined to the thread-safe version of lgamma to be visible, so we make the check by making use of CMAKE_REQUIRED_DEFINITIONS. --- core/CMakeLists.txt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index a504160e54..cda07574af 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,4 +1,4 @@ -include(CheckCXXSourceCompiles) +include(CheckSymbolExists) file(GLOB_RECURSE CORE_SRCS *.cpp *.h) @@ -24,11 +24,10 @@ add_library(mrtrix-core SHARED ${CORE_SRCS}) add_library(mrtrix::core ALIAS mrtrix-core) # Check to see if we can use lgamma_r() function in custom Math::betaincreg() -check_cxx_source_compiles(" - extern \"C\" double lgamma_r(double, int *); - int main() { int sign; lgamma_r(1.0, &sign); }" - MRTRIX_HAVE_LGAMMA_R -) +# The function is defined under _REENTRANT on some systems (e.g. MacOS) +list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_REENTRANT) +check_symbol_exists(lgamma_r "math.h" MRTRIX_HAVE_LGAMMA_R) +list(REMOVE_AT CMAKE_REQUIRED_DEFINITIONS -1) if(MRTRIX_HAVE_LGAMMA_R) message(STATUS "Found lgamma_r() function") @@ -71,6 +70,10 @@ target_compile_definitions(mrtrix-core PUBLIC MRTRIX_HAVE_LGAMMA_R=$ ) +if(APPLE AND MRTRIX_HAVE_LGAMMA_R) + target_compile_definitions(mrtrix-core PRIVATE _REENTRANT=1) +endif() + if(PNG_FOUND) target_compile_definitions(mrtrix-core PUBLIC MRTRIX_PNG_SUPPORT) target_link_libraries(mrtrix-core PUBLIC PNG::PNG)