Skip to content

Commit

Permalink
Use CheckSymbolExists to verify existence of lgamma_r
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daljit46 committed Apr 4, 2024
1 parent a8800e2 commit d5d9ca2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include(CheckCXXSourceCompiles)
include(CheckSymbolExists)

file(GLOB_RECURSE CORE_SRCS *.cpp *.h)

Expand All @@ -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")
Expand Down Expand Up @@ -71,6 +70,10 @@ target_compile_definitions(mrtrix-core PUBLIC
MRTRIX_HAVE_LGAMMA_R=$<BOOL:${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)
Expand Down

0 comments on commit d5d9ca2

Please sign in to comment.