1CMAKE_MINIMUM_REQUIRED(VERSION 3.5 FATAL_ERROR) 2 3# ---[ Project 4PROJECT(pthreadpool C CXX) 5 6# ---[ Options. 7SET(PTHREADPOOL_LIBRARY_TYPE "default" CACHE STRING "Type of library (shared, static, or default) to build") 8SET_PROPERTY(CACHE PTHREADPOOL_LIBRARY_TYPE PROPERTY STRINGS default static shared) 9OPTION(PTHREADPOOL_ALLOW_DEPRECATED_API "Enable deprecated API functions" ON) 10SET(PTHREADPOOL_SYNC_PRIMITIVE "default" CACHE STRING "Synchronization primitive (condvar, futex, gcd, event, or default) for worker threads") 11SET_PROPERTY(CACHE PTHREADPOOL_SYNC_PRIMITIVE PROPERTY STRINGS default condvar futex gcd event) 12IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?)$") 13 OPTION(PTHREADPOOL_ENABLE_FASTPATH "Enable fast path using atomic decrement instead of atomic compare-and-swap" ON) 14ELSE() 15 OPTION(PTHREADPOOL_ENABLE_FASTPATH "Enable fast path using atomic decrement instead of atomic compare-and-swap" OFF) 16ENDIF() 17IF("${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}") 18 OPTION(PTHREADPOOL_BUILD_TESTS "Build pthreadpool unit tests" ON) 19 OPTION(PTHREADPOOL_BUILD_BENCHMARKS "Build pthreadpool micro-benchmarks" ON) 20ELSE() 21 SET(PTHREADPOOL_BUILD_TESTS OFF CACHE BOOL "Build pthreadpool unit tests") 22 SET(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "Build pthreadpool micro-benchmarks") 23ENDIF() 24 25# ---[ CMake options 26INCLUDE(GNUInstallDirs) 27 28IF(PTHREADPOOL_BUILD_TESTS) 29 ENABLE_TESTING() 30ENDIF() 31 32MACRO(PTHREADPOOL_TARGET_ENABLE_CXX11 target) 33 SET_TARGET_PROPERTIES(${target} PROPERTIES 34 CXX_STANDARD 11 35 CXX_EXTENSIONS NO) 36ENDMACRO() 37 38# ---[ Download deps 39IF(NOT DEFINED FXDIV_SOURCE_DIR) 40 MESSAGE(STATUS "Downloading FXdiv to ${CMAKE_BINARY_DIR}/FXdiv-source (define FXDIV_SOURCE_DIR to avoid it)") 41 CONFIGURE_FILE(cmake/DownloadFXdiv.cmake "${CMAKE_BINARY_DIR}/FXdiv-download/CMakeLists.txt") 42 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . 43 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/FXdiv-download") 44 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build . 45 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/FXdiv-download") 46 SET(FXDIV_SOURCE_DIR "${CMAKE_BINARY_DIR}/FXdiv-source" CACHE STRING "FXdiv source directory") 47ENDIF() 48 49IF(PTHREADPOOL_BUILD_TESTS AND NOT DEFINED GOOGLETEST_SOURCE_DIR) 50 MESSAGE(STATUS "Downloading Google Test to ${CMAKE_BINARY_DIR}/googletest-source (define GOOGLETEST_SOURCE_DIR to avoid it)") 51 CONFIGURE_FILE(cmake/DownloadGoogleTest.cmake "${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt") 52 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . 53 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download") 54 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build . 55 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download") 56 SET(GOOGLETEST_SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-source" CACHE STRING "Google Test source directory") 57ENDIF() 58 59IF(PTHREADPOOL_BUILD_BENCHMARKS AND NOT DEFINED GOOGLEBENCHMARK_SOURCE_DIR) 60 MESSAGE(STATUS "Downloading Google Benchmark to ${CMAKE_BINARY_DIR}/googlebenchmark-source (define GOOGLEBENCHMARK_SOURCE_DIR to avoid it)") 61 CONFIGURE_FILE(cmake/DownloadGoogleBenchmark.cmake "${CMAKE_BINARY_DIR}/googlebenchmark-download/CMakeLists.txt") 62 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . 63 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googlebenchmark-download") 64 EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build . 65 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googlebenchmark-download") 66 SET(GOOGLEBENCHMARK_SOURCE_DIR "${CMAKE_BINARY_DIR}/googlebenchmark-source" CACHE STRING "Google Benchmark source directory") 67ENDIF() 68 69# ---[ pthreadpool library 70IF(PTHREADPOOL_ALLOW_DEPRECATED_API) 71 SET(PTHREADPOOL_SRCS src/legacy-api.c) 72ENDIF() 73IF(EMSCRIPTEN) 74 LIST(APPEND PTHREADPOOL_SRCS src/shim.c) 75ELSE() 76 LIST(APPEND PTHREADPOOL_SRCS src/portable-api.c src/memory.c) 77 IF(APPLE AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "gcd")) 78 LIST(APPEND PTHREADPOOL_SRCS src/gcd.c) 79 ELSEIF(CMAKE_SYSTEM_NAME MATCHES "^(Windows|CYGWIN|MSYS)$" AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event")) 80 LIST(APPEND PTHREADPOOL_SRCS src/windows.c) 81 ELSE() 82 LIST(APPEND PTHREADPOOL_SRCS src/pthreads.c) 83 ENDIF() 84 IF(PTHREADPOOL_ENABLE_FASTPATH) 85 LIST(APPEND PTHREADPOOL_SRCS src/fastpath.c) 86 ENDIF() 87ENDIF() 88 89ADD_LIBRARY(pthreadpool_interface INTERFACE) 90TARGET_INCLUDE_DIRECTORIES(pthreadpool_interface INTERFACE include) 91IF(NOT PTHREADPOOL_ALLOW_DEPRECATED_API) 92 TARGET_COMPILE_DEFINITIONS(pthreadpool_interface INTERFACE PTHREADPOOL_NO_DEPRECATED_API=1) 93ENDIF() 94INSTALL(FILES include/pthreadpool.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 95 96IF(PTHREADPOOL_LIBRARY_TYPE STREQUAL "default") 97 ADD_LIBRARY(pthreadpool ${PTHREADPOOL_SRCS}) 98ELSEIF(PTHREADPOOL_LIBRARY_TYPE STREQUAL "shared") 99 ADD_LIBRARY(pthreadpool SHARED ${PTHREADPOOL_SRCS}) 100ELSEIF(PTHREADPOOL_LIBRARY_TYPE STREQUAL "static") 101 ADD_LIBRARY(pthreadpool STATIC ${PTHREADPOOL_SRCS}) 102ELSE() 103 MESSAGE(FATAL_ERROR "Unsupported library type ${PTHREADPOOL_LIBRARY_TYPE}") 104ENDIF() 105 106IF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "condvar") 107 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=0) 108 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0) 109 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=0) 110ELSEIF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "futex") 111 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=1) 112 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0) 113 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=0) 114ELSEIF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "gcd") 115 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=0) 116 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=1) 117 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=0) 118ELSEIF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event") 119 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=0) 120 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0) 121 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=1) 122ELSEIF(NOT PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default") 123 MESSAGE(FATAL_ERROR "Unsupported synchronization primitive ${PTHREADPOOL_SYNC_PRIMITIVE}") 124ENDIF() 125IF(PTHREADPOOL_ENABLE_FASTPATH) 126 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_ENABLE_FASTPATH=1) 127ELSE() 128 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_ENABLE_FASTPATH=0) 129ENDIF() 130 131SET_TARGET_PROPERTIES(pthreadpool PROPERTIES 132 C_STANDARD 11 133 C_EXTENSIONS NO) 134TARGET_LINK_LIBRARIES(pthreadpool PUBLIC pthreadpool_interface) 135TARGET_INCLUDE_DIRECTORIES(pthreadpool PRIVATE src) 136IF(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten") 137 SET(CMAKE_THREAD_PREFER_PTHREAD TRUE) 138 IF(NOT CMAKE_GENERATOR STREQUAL "Xcode") 139 FIND_PACKAGE(Threads REQUIRED) 140 ENDIF() 141 IF(CMAKE_USE_PTHREADS_INIT) 142 IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") 143 TARGET_COMPILE_OPTIONS(pthreadpool PUBLIC -pthread) 144 ENDIF() 145 ENDIF() 146 TARGET_LINK_LIBRARIES(pthreadpool PUBLIC ${CMAKE_THREAD_LIBS_INIT}) 147ENDIF() 148IF(CMAKE_SYSTEM_NAME STREQUAL "Linux") 149 TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE _GNU_SOURCE=1) 150ENDIF() 151 152# ---[ Configure FXdiv 153IF(NOT TARGET fxdiv) 154 SET(FXDIV_BUILD_TESTS OFF CACHE BOOL "") 155 SET(FXDIV_BUILD_BENCHMARKS OFF CACHE BOOL "") 156 ADD_SUBDIRECTORY( 157 "${FXDIV_SOURCE_DIR}" 158 "${CMAKE_BINARY_DIR}/FXdiv") 159ENDIF() 160TARGET_LINK_LIBRARIES(pthreadpool PRIVATE fxdiv) 161 162INSTALL(TARGETS pthreadpool 163 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 164 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) 165 166IF(PTHREADPOOL_BUILD_TESTS) 167 # ---[ Build google test 168 IF(NOT TARGET gtest) 169 SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 170 ADD_SUBDIRECTORY( 171 "${GOOGLETEST_SOURCE_DIR}" 172 "${CMAKE_BINARY_DIR}/googletest") 173 ENDIF() 174 175 ADD_EXECUTABLE(pthreadpool-test test/pthreadpool.cc) 176 SET_TARGET_PROPERTIES(pthreadpool-test PROPERTIES 177 CXX_STANDARD 11 178 CXX_EXTENSIONS NO) 179 TARGET_LINK_LIBRARIES(pthreadpool-test pthreadpool gtest gtest_main) 180 ADD_TEST(pthreadpool pthreadpool-test) 181ENDIF() 182 183IF(PTHREADPOOL_BUILD_BENCHMARKS) 184 # ---[ Build google benchmark 185 IF(NOT TARGET benchmark) 186 SET(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "") 187 ADD_SUBDIRECTORY( 188 "${GOOGLEBENCHMARK_SOURCE_DIR}" 189 "${CMAKE_BINARY_DIR}/googlebenchmark") 190 ENDIF() 191 192 ADD_EXECUTABLE(latency-bench bench/latency.cc) 193 SET_TARGET_PROPERTIES(latency-bench PROPERTIES 194 CXX_STANDARD 11 195 CXX_EXTENSIONS NO) 196 TARGET_LINK_LIBRARIES(latency-bench pthreadpool benchmark) 197 198 ADD_EXECUTABLE(throughput-bench bench/throughput.cc) 199 SET_TARGET_PROPERTIES(throughput-bench PROPERTIES 200 CXX_STANDARD 11 201 CXX_EXTENSIONS NO) 202 TARGET_LINK_LIBRARIES(throughput-bench pthreadpool benchmark) 203ENDIF() 204