CMake: test for the presence of POSIX threads

We chose a centralised approach, since threads are needed in several
modules: the find_package() is done only once in the rood directory.
Not sure it's the best approach though, but this test is several lines
long so doing that avoids duplication of this code.
This commit is contained in:
Matteo Cypriani 2013-05-20 16:17:10 -04:00
parent be6d369fcc
commit 8e46aae55c
4 changed files with 36 additions and 7 deletions

View File

@ -14,6 +14,18 @@ set(GENERATED_INCLUDE "${PROJECT_BINARY_DIR}/include")
include_directories("${GENERATED_INCLUDE}")
### Dependencies ###
# Note: we test here the dependencies relevant for more than one module;
# other tests are done in each module's directory.
# Check if we've got POSIX threads
set(CMAKE_THREAD_PREFER_PTHREAD on)
find_package(Threads)
if (CMAKE_THREAD_LIBS_INIT AND CMAKE_USE_PTHREADS_INIT)
set(HAVE_PTHREADS on)
endif()
### OwlPS' version ###
# First, try to get it from the environment variable OWLPS_VERSION
@ -94,7 +106,7 @@ if ("${CMAKE_EXE_LINKER_FLAGS}" STREQUAL " ")
endif()
### Libraries ###
### Libraries targets ###
add_subdirectory(libowlps)
include_directories(libowlps)
@ -106,9 +118,14 @@ add_subdirectory(libowlps-resultreader)
include_directories(libowlps-resultreader)
### Programs ###
### Programs targets ###
add_subdirectory(owlps-aggregator)
if (HAVE_PTHREADS)
add_subdirectory(owlps-aggregator)
else()
message(WARNING
"POSIX threads are not available, OwlPS Aggregator will not be built")
endif()
add_subdirectory(owlps-client)
@ -141,7 +158,12 @@ else() # we're not using GCC
"OwlPS Positioner requires GCC (>= ${POSITIONER_MIN_GCC_VERSION})")
endif()
add_subdirectory(owlps-udp-to-http)
if (HAVE_PTHREADS)
add_subdirectory(owlps-udp-to-http)
else()
message(WARNING
"POSIX threads are not available, OwlPS UDP-to-HTTP will not be built")
endif()
### Meta-targets ###

View File

@ -1,4 +1,4 @@
set(EXTRA_LIBS ${EXTRA_LIBS} pthread confuse)
set(EXTRA_LIBS ${EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT} confuse)
add_executable(owlps-aggregatord owlps-aggregatord.c)
target_link_libraries(owlps-aggregatord

View File

@ -26,7 +26,14 @@ if (OWLPS_LISTENER_USES_CONFIG_FILE)
endif()
if (OWLPS_LISTENER_USES_PTHREAD)
set(EXTRA_LIBS ${EXTRA_LIBS} pthread)
if (HAVE_PTHREADS)
set(EXTRA_LIBS ${EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
else()
message(WARNING
"POSIX threads are not available: disabling OWLPS_LISTENER_USES_PTHREAD")
set(OWLPS_LISTENER_USES_PTHREAD off CACHE BOOL
"No pthreads: disable OWLPS_LISTENER_USES_PTHREAD" FORCE)
endif()
endif()

View File

@ -1,4 +1,4 @@
set(EXTRA_LIBS ${EXTRA_LIBS} pthread)
set(EXTRA_LIBS ${EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
add_executable(owlps-udp-to-http owlps-udp-to-http.c)
target_link_libraries(owlps-udp-to-http