Skip to content

Commit 9441e5a

Browse files
committed
cmake: fix find_package(Threads REQUIRED) error with new cmake policies
With latest cmake policies, find_package(Threads REQUIRED) fails in freebsd and openbsd builds. The specific failures in these jobs happens due to the CMP0155 policy turning CMAKE_CXX_SCAN_FOR_MODULES on, which is turned off in the next commit. But there are other things that could cause the threads package not to be found, and there are platforms where it may not be required, so it is better to make it an optional instead of required dependency. Technically this change is not needed to make all CI jobs pass as long as the next commit is present. But since this find_package error obscures other errors that would be clearer, and since there are other conditions that could trigger it, it is worth fixing generally. For example, this same failure also seems to happen in the llvm job as well when CMP0137 is disabled or CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES is set to true. 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) and inside the CMakeConfigureLog.yaml file there are "/bin/sh: CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND: not found" errors.
1 parent 6ba1050 commit 9441e5a

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ endif()
1212

1313
include("cmake/compat_find.cmake")
1414

15-
find_package(Threads REQUIRED)
15+
add_library(mpdeps INTERFACE)
16+
17+
find_package(Threads)
18+
19+
if(Threads_FOUND)
20+
target_link_libraries(mpdeps INTERFACE Threads::Threads)
21+
endif()
22+
1623
find_package(CapnProto 0.7 QUIET NO_MODULE)
1724
if(NOT CapnProto_FOUND)
1825
message(FATAL_ERROR
@@ -202,7 +209,7 @@ target_link_libraries(mpgen PRIVATE CapnProto::capnp)
202209
target_link_libraries(mpgen PRIVATE CapnProto::capnp-rpc)
203210
target_link_libraries(mpgen PRIVATE CapnProto::capnpc)
204211
target_link_libraries(mpgen PRIVATE CapnProto::kj)
205-
target_link_libraries(mpgen PRIVATE Threads::Threads)
212+
target_link_libraries(mpgen PRIVATE mpdeps)
206213
set_target_properties(mpgen PROPERTIES
207214
INSTALL_RPATH_USE_LINK_PATH TRUE)
208215
set_target_properties(mpgen PROPERTIES

cmake/pthread_checks.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include(CMakePushCheckState)
99
include(CheckCXXSourceCompiles)
1010

1111
cmake_push_check_state()
12-
set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
12+
set(CMAKE_REQUIRED_LIBRARIES mpdeps)
1313
check_cxx_source_compiles("
1414
#include <pthread.h>
1515
int main(int argc, char** argv)

example/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ add_executable(mpcalculator
99
)
1010
target_capnp_sources(mpcalculator ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
1111
target_include_directories(mpcalculator PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
12-
target_link_libraries(mpcalculator PRIVATE Threads::Threads)
12+
target_link_libraries(mpcalculator PRIVATE mpdeps)
1313

1414
add_executable(mpprinter
1515
printer.cpp
1616
)
1717
target_capnp_sources(mpprinter ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
1818
target_include_directories(mpprinter PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
19-
target_link_libraries(mpprinter PRIVATE Threads::Threads)
19+
target_link_libraries(mpprinter PRIVATE mpdeps)
2020

2121
add_executable(mpexample
2222
example.cpp
2323
)
2424
target_capnp_sources(mpexample ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
2525
target_include_directories(mpexample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
26-
target_link_libraries(mpexample PRIVATE Threads::Threads)
26+
target_link_libraries(mpexample PRIVATE mpdeps)
2727

2828
add_custom_target(mpexamples DEPENDS mpexample mpcalculator mpprinter)

src/mp/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <mp/config.h>
5+
#include <mp/config.h> // IWYU pragma: keep
66
#include <mp/util.h>
77

88
#include <cerrno>
99
#include <cstdio>
1010
#include <kj/common.h>
1111
#include <kj/string-tree.h>
12-
#include <pthread.h>
12+
#include <pthread.h> // IWYU pragma: keep
1313
#include <sstream>
1414
#include <string>
1515
#include <sys/resource.h>

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if(BUILD_TESTING AND TARGET CapnProto::kj-test)
3232
target_capnp_sources(mptest ${CMAKE_CURRENT_SOURCE_DIR} mp/test/foo.capnp)
3333
target_include_directories(mptest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
3434
target_link_libraries(mptest PRIVATE CapnProto::kj-test)
35-
target_link_libraries(mptest PRIVATE Threads::Threads)
35+
target_link_libraries(mptest PRIVATE mpdeps)
3636

3737
add_dependencies(mptests mptest)
3838
add_test(NAME mptest COMMAND mptest)

0 commit comments

Comments
 (0)