Compile tests with pthreads support

This commit is contained in:
Přemysl Eric Janouch 2016-01-01 19:15:18 +01:00
parent 66340e08d7
commit b5724a654a
2 changed files with 28 additions and 2 deletions

View File

@ -11,9 +11,11 @@ endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUC)
find_package (PkgConfig REQUIRED)
pkg_check_modules (libssl REQUIRED libssl libcrypto)
# -lpthread is only there for debugging (gdb & errno)
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include (AddThreads)
# -lrt is only for glibc < 2.17
set (common_libraries ${libssl_LIBRARIES} rt pthread)
set (common_libraries ${libssl_LIBRARIES} rt)
include_directories (${libssl_INCLUDE_DIRS})
link_directories (${libssl_LIBRARY_DIRS})
@ -29,6 +31,7 @@ set (common_sources ${PROJECT_BINARY_DIR}/liberty-config.h)
enable_testing ()
foreach (name liberty proto)
add_executable (test-${name} tests/${name}.c ${common_sources})
add_threads (test-${name})
target_link_libraries (test-${name} ${common_libraries})
add_test (NAME test-${name} COMMAND test-${name})
endforeach (name)

23
cmake/AddThreads.cmake Normal file
View File

@ -0,0 +1,23 @@
# Public Domain
# We're looking for pthreads only, while preferring the -pthread flag
set (CMAKE_THREAD_PREFER_PTHREAD ON)
set (THREADS_PREFER_PTHREAD_FLAG ON)
find_package (Threads)
# Prepares the given target for threads
function (add_threads target)
if (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
message (FATAL_ERROR "pthreads not found")
endif (NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
if (THREADS_HAVE_PTHREAD_ARG)
set_property (TARGET ${target} PROPERTY
COMPILE_OPTIONS "-pthread")
set_property (TARGET ${target} PROPERTY
INTERFACE_COMPILE_OPTIONS "-pthread")
endif (THREADS_HAVE_PTHREAD_ARG)
if (CMAKE_THREAD_LIBS_INIT)
target_link_libraries (${target} "${CMAKE_THREAD_LIBS_INIT}")
endif (CMAKE_THREAD_LIBS_INIT)
endfunction (add_threads)