From 45c7dca6a55c2892d2b1b177123a3dea71d15090 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 10 Sep 2025 06:55:28 -0400 Subject: [PATCH] cmake: make Threads package optional Make find_package(Threads) optional because there are platforms where this package may not be required, and because find_package(Threads REQUIRED) errors obscure more detailed error messages and makes issues harder to debug. For example with CMP0155 enabled which turns CMAKE_CXX_SCAN_FOR_MODULES on, find_package(Threads) fails on freebsd and openbsd CI jobs which lack a clang-scan-deps tool. Also when CMP0137 is disabled or CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES is set to true find_package(Threadss) fails in the llvm CI job. This change lets builds in both of those cases succeed. The freebsd CMP0155 error looks like: + cmake /home/runner/work/libmultiprocess/libmultiprocess -G Ninja -- The CXX compiler identification is Clang 16.0.6 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - not found -- Check if compiler accepts -pthread -- Check if compiler accepts -pthread - no CMake Error at /usr/local/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:233 (message): Could NOT find Threads (missing: Threads_FOUND) Call Stack (most recent call first): /usr/local/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE) /usr/local/share/cmake/Modules/FindThreads.cmake:226 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) CMakeLists.txt:41 (find_package) Inside the CMakeConfigureLog.yaml file there are "/bin/sh: CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND: not found" errors. --- CMakeLists.txt | 10 ++++++++-- cmake/pthread_checks.cmake | 4 +++- example/CMakeLists.txt | 6 +++--- test/CMakeLists.txt | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf50018a..f0453f0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,13 @@ endif() include("cmake/compat_find.cmake") -find_package(Threads REQUIRED) +add_library(mpdeps INTERFACE) + +find_package(Threads) +if(Threads_FOUND) + target_link_libraries(mpdeps INTERFACE Threads::Threads) +endif() + find_package(CapnProto 0.9 NO_MODULE) if(NOT CapnProto_FOUND) message(FATAL_ERROR @@ -206,7 +212,7 @@ target_link_libraries(mpgen PRIVATE CapnProto::capnp) target_link_libraries(mpgen PRIVATE CapnProto::capnpc) target_link_libraries(mpgen PRIVATE CapnProto::kj) target_link_libraries(mpgen PRIVATE CapnProto::kj-async) -target_link_libraries(mpgen PRIVATE Threads::Threads) +target_link_libraries(mpgen PRIVATE mpdeps) # Use CAPNP_EXECUTABLE/CAPNPC_CXX_EXECUTABLE cmake variables set by # CapnProtoConfig.cmake rather than reading the CapnProto::capnp_tool # IMPORTED_LOCATION directly. On Debian/Ubuntu, IMPORTED_LOCATION is broken diff --git a/cmake/pthread_checks.cmake b/cmake/pthread_checks.cmake index 1208391b..b105728a 100644 --- a/cmake/pthread_checks.cmake +++ b/cmake/pthread_checks.cmake @@ -9,7 +9,9 @@ include(CMakePushCheckState) include(CheckCXXSourceCompiles) cmake_push_check_state() -set(CMAKE_REQUIRED_LIBRARIES Threads::Threads) +if(TARGET Threads::Threads) + set(CMAKE_REQUIRED_LIBRARIES Threads::Threads) +endif() check_cxx_source_compiles(" #include int main(int argc, char** argv) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 7da049c3..a8961c5b 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -9,20 +9,20 @@ add_executable(mpcalculator ) target_capnp_sources(mpcalculator ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp) target_include_directories(mpcalculator PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(mpcalculator PRIVATE Threads::Threads) +target_link_libraries(mpcalculator PRIVATE mpdeps) add_executable(mpprinter printer.cpp ) target_capnp_sources(mpprinter ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp) target_include_directories(mpprinter PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(mpprinter PRIVATE Threads::Threads) +target_link_libraries(mpprinter PRIVATE mpdeps) add_executable(mpexample example.cpp ) target_capnp_sources(mpexample ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp) target_include_directories(mpexample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(mpexample PRIVATE Threads::Threads) +target_link_libraries(mpexample PRIVATE mpdeps) add_custom_target(mpexamples DEPENDS mpexample mpcalculator mpprinter) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd0aa023..cbfbfe3e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,7 +35,7 @@ if(BUILD_TESTING AND TARGET CapnProto::kj-test) target_capnp_sources(mptest ${CMAKE_CURRENT_SOURCE_DIR} mp/test/foo.capnp) target_include_directories(mptest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(mptest PRIVATE CapnProto::kj-test) - target_link_libraries(mptest PRIVATE Threads::Threads) + target_link_libraries(mptest PRIVATE mpdeps) add_dependencies(mptests mptest) add_test(NAME mptest COMMAND mptest)