diff --git a/CMakeLists.txt b/CMakeLists.txt index e7d0f3314..53569728a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,11 @@ option(SENTRY_PIC "Build sentry (and dependent) libraries as position independen option(SENTRY_BUILD_TESTS "Build sentry-native tests" "${SENTRY_MAIN_PROJECT}") option(SENTRY_BUILD_EXAMPLES "Build sentry-native example(s)" "${SENTRY_MAIN_PROJECT}") +option(SENTRY_LINK_PTHREAD "Link platform threads library" ON) +if(SENTRY_LINK_PTHREAD) + find_package(Threads REQUIRED) +endif() + if(MSVC) option(SENTRY_BUILD_RUNTIMESTATIC "Build sentry-native with static runtime" OFF) endif() @@ -340,19 +345,29 @@ if(WIN32) endif() endif() -if(LINUX) - option(SENTRY_LINK_PTHREAD "Link Sentry with pthread" ON) - if(SENTRY_LINK_PTHREAD) - target_link_libraries(sentry PRIVATE pthread) - endif() - target_link_libraries(sentry PRIVATE dl) - target_link_libraries(sentry PRIVATE rt) -elseif(ANDROID) - target_link_libraries(sentry PRIVATE dl log) -elseif(MSVC) - target_link_libraries(sentry PRIVATE dbghelp version) -elseif(MINGW) - target_link_libraries(sentry PRIVATE dbghelp version) +# handle platform libraries +if(ANDROID) + set(_SENTRY_PLATFORM_LIBS "dl" "log") +elseif(LINUX) + set(_SENTRY_PLATFORM_LIBS "dl" "rt") +elseif(WIN32) + set(_SENTRY_PLATFORM_LIBS "dbghelp" "version") +endif() + +# handle platform threads library +if(SENTRY_LINK_PTHREAD) + list(APPEND _SENTRY_PLATFORM_LIBS "Threads::Threads") +endif() + +# apply platform libraries to sentry library +if(SENTRY_LIBRARY_TYPE STREQUAL "static") + target_link_libraries(sentry PUBLIC ${_SENTRY_PLATFORM_LIBS}) +else() + target_link_libraries(sentry PRIVATE ${_SENTRY_PLATFORM_LIBS}) +endif() + +# suppress some errors and warnings for MinGW target +if(MINGW) target_compile_options(sentry PRIVATE -Wno-unused-variable -Wno-unused-parameter diff --git a/README.md b/README.md index 070d46e69..33fdae003 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ using `cmake -D BUILD_SHARED_LIBS=OFF ..`. Enables linking with the static MSVC runtime. Has no effect if the compiler is not MSVC. - `SENTRY_LINK_PTHREAD` (Default: ON): - Links to the `pthread` library on unix targets. + Links platform threads library like `pthread` on unix targets. - `SENTRY_BUILD_FORCE32` (Default: OFF): Forces cross-compilation from 64-bit host to 32-bit target. Only has an effect on Linux.