CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. cmake_minimum_required(VERSION 3.15.1)
  2. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
  3. # NOTE(compnerd) enable CMP0091 - select MSVC runtime based on
  4. # CMAKE_MSVC_RUNTIME_LIBRARY. Requires CMake 3.15 or newer.
  5. if(POLICY CMP0091)
  6. cmake_policy(SET CMP0091 NEW)
  7. endif()
  8. project(dispatch
  9. VERSION 1.3
  10. LANGUAGES C CXX)
  11. if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
  12. include(ClangClCompileRules)
  13. endif()
  14. if(CMAKE_SYSTEM_NAME STREQUAL Windows)
  15. if(NOT MINGW)
  16. include(DispatchWindowsSupport)
  17. dispatch_windows_arch_spelling(${CMAKE_SYSTEM_PROCESSOR} DISPATCH_MSVC_ARCH)
  18. dispatch_windows_include_for_arch(${DISPATCH_MSVC_ARCH} DISPATCH_INCLUDES)
  19. include_directories(BEFORE SYSTEM ${DISPATCH_INCLUDES})
  20. dispatch_windows_lib_for_arch(${CMAKE_SYSTEM_PROCESSOR} DISPATCH_LIBDIR)
  21. link_directories(${DISPATCH_LIBDIR})
  22. endif()
  23. include(CheckCSourceCompiles)
  24. include(CheckSymbolExists)
  25. check_c_source_compiles([=[
  26. #include <Windows.h>
  27. int main(int argc, char *argv[]) {
  28. switch ((LOGICAL_PROCESSOR_RELATIONSHIP)0) {
  29. case RelationProcessorDie:
  30. case RelationNumaNodeEx:
  31. return 0;
  32. }
  33. return 0;
  34. }
  35. ]=] DISPATCH_HAVE_EXTENDED_SLPI_20348)
  36. if(DISPATCH_HAVE_EXTENDED_SLPI_20348)
  37. add_compile_definitions(DISPATCH_HAVE_EXTENDED_SLPI_20348)
  38. endif()
  39. check_c_source_compiles([=[
  40. #include <Windows.h>
  41. int main(int argc, char *argv[]) {
  42. switch ((LOGICAL_PROCESSOR_RELATIONSHIP)0) {
  43. case RelationProcessorModule:
  44. return 0;
  45. }
  46. return 0;
  47. }
  48. ]=] DISPATCH_HAVE_EXTENDED_SLPI_22000)
  49. if(DISPATCH_HAVE_EXTENDED_SLPI_22000)
  50. add_compile_definitions(DISPATCH_HAVE_EXTENDED_SLPI_22000)
  51. endif()
  52. check_c_source_compiles([=[
  53. #include <Windows.h>
  54. #include <winternl.h>
  55. int main(int argc, char *argv[]) {
  56. FILE_PIPE_LOCAL_INFORMATION fpli;
  57. }
  58. ]=] HAVE_FILE_PIPE_LOCAL_INFORMATION)
  59. if(HAVE_FILE_PIPE_LOCAL_INFORMATION)
  60. add_compile_definitions(HAVE_FILE_PIPE_LOCAL_INFORMATION)
  61. endif()
  62. check_symbol_exists(mkstemp "stdlib.h" HAVE_MKSTEMP)
  63. if(HAVE_MKSTEMP)
  64. add_compile_definitions(HAVE_MKSTEMP)
  65. endif()
  66. check_c_source_compiles([=[
  67. #include <sys/types.h>
  68. int main(int argc, char *argv[]) {
  69. mode_t mode;
  70. }
  71. ]=] HAVE_MODE_T)
  72. if(HAVE_MODE_T)
  73. add_compile_definitions(HAVE_MODE_T)
  74. endif()
  75. check_c_source_compiles([=[
  76. #include <sys/types.h>
  77. int main(int argc, char *argv[]) {
  78. pid_t mode;
  79. }
  80. ]=] HAVE_PID_T)
  81. if(HAVE_PID_T)
  82. add_compile_definitions(HAVE_PID_T)
  83. endif()
  84. endif()
  85. set(CMAKE_C_STANDARD 11)
  86. set(CMAKE_C_STANDARD_REQUIRED YES)
  87. set(CMAKE_CXX_STANDARD 11)
  88. set(CMAKE_C_VISIBILITY_PRESET hidden)
  89. set(CMAKE_C_VISIBILITY_INLINES_HIDDEN YES)
  90. # NOTE(compnerd) this is a horrible workaround for Windows to ensure that the
  91. # tests can run as there is no rpath equivalent and `PATH` is used to lookup the
  92. # libraries.
  93. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  94. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  95. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  96. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  97. if(ANDROID)
  98. set(CMAKE_HAVE_LIBC_PTHREAD YES)
  99. endif()
  100. find_package(Threads REQUIRED)
  101. include(CheckCCompilerFlag)
  102. include(CheckCSourceCompiles)
  103. include(CheckFunctionExists)
  104. include(CheckIncludeFiles)
  105. include(CheckLibraryExists)
  106. include(CheckSymbolExists)
  107. include(GNUInstallDirs)
  108. include(CTest)
  109. include(DispatchAppleOptions)
  110. include(DispatchSanitization)
  111. include(DispatchCompilerWarnings)
  112. include(DTrace)
  113. include(SwiftSupport)
  114. # NOTE(abdulras) this is the CMake supported way to control whether we generate
  115. # shared or static libraries. This impacts the behaviour of `add_library` in
  116. # what type of library it generates.
  117. option(BUILD_SHARED_LIBS "build shared libraries" ON)
  118. option(DISPATCH_ENABLE_ASSERTS "enable debug assertions" FALSE)
  119. option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
  120. set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})
  121. option(ENABLE_DTRACE "enable dtrace support" "")
  122. if(CMAKE_SYSTEM_NAME STREQUAL Darwin OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
  123. set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT OFF)
  124. else()
  125. set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
  126. endif()
  127. option(ENABLE_INTERNAL_PTHREAD_WORKQUEUES "use libdispatch's own implementation of pthread workqueues" ${ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT})
  128. if(ENABLE_INTERNAL_PTHREAD_WORKQUEUES)
  129. set(DISPATCH_USE_INTERNAL_WORKQUEUE 1)
  130. set(HAVE_PTHREAD_WORKQUEUES 0)
  131. else()
  132. check_include_files(pthread/workqueue_private.h HAVE_PTHREAD_WORKQUEUE_PRIVATE_H)
  133. if(HAVE_PTHREAD_WORKQUEUE_PRIVATE_H)
  134. set(HAVE_PTHREAD_WORKQUEUES 1)
  135. set(DISPATCH_USE_INTERNAL_WORKQUEUE 0)
  136. else()
  137. set(HAVE_PTHREAD_WORKQUEUES 0)
  138. set(DISPATCH_USE_INTERNAL_WORKQUEUE 1)
  139. endif()
  140. endif()
  141. option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as the public ones" OFF)
  142. option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
  143. if(ENABLE_SWIFT)
  144. enable_language(Swift)
  145. endif()
  146. option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
  147. set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})
  148. check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
  149. if(_GNU_SOURCE)
  150. set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
  151. endif()
  152. check_c_source_compiles("void __attribute__((__noreturn__)) main() { __builtin_trap(); }"
  153. __BUILTIN_TRAP)
  154. if(__BUILTIN_TRAP)
  155. set(HAVE_NORETURN_BUILTIN_TRAP 1)
  156. endif()
  157. if(NOT CMAKE_SYSTEM_NAME STREQUAL Android)
  158. find_package(LibRT)
  159. endif()
  160. check_function_exists(_pthread_workqueue_init HAVE__PTHREAD_WORKQUEUE_INIT)
  161. check_function_exists(getprogname HAVE_GETPROGNAME)
  162. check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
  163. check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)
  164. check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
  165. check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
  166. check_function_exists(posix_fadvise HAVE_POSIX_FADVISE)
  167. check_function_exists(posix_spawnp HAVE_POSIX_SPAWNP)
  168. check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
  169. check_function_exists(pthread_attr_setcpupercent_np HAVE_PTHREAD_ATTR_SETCPUPERCENT_NP)
  170. check_function_exists(pthread_yield_np HAVE_PTHREAD_YIELD_NP)
  171. check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
  172. check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
  173. check_function_exists(strlcpy HAVE_STRLCPY)
  174. check_function_exists(sysconf HAVE_SYSCONF)
  175. check_function_exists(arc4random HAVE_ARC4RANDOM)
  176. check_include_files("TargetConditionals.h" HAVE_TARGETCONDITIONALS_H)
  177. check_include_files("dlfcn.h" HAVE_DLFCN_H)
  178. check_include_files("fcntl.h" HAVE_FCNTL_H)
  179. check_include_files("inttypes.h" HAVE_INTTYPES_H)
  180. check_include_files("libkern/OSAtomic.h" HAVE_LIBKERN_OSATOMIC_H)
  181. check_include_files("libkern/OSCrossEndian.h" HAVE_LIBKERN_OSCROSSENDIAN_H)
  182. check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H)
  183. check_include_files("mach/mach.h" HAVE_MACH)
  184. if(HAVE_MACH)
  185. set(__DARWIN_NON_CANCELABLE 1)
  186. else()
  187. set(__DARWIN_NON_CANCELABLE 0)
  188. endif()
  189. check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H)
  190. check_include_files("memory.h" HAVE_MEMORY_H)
  191. check_include_files("pthread/qos.h" HAVE_PTHREAD_QOS_H)
  192. check_include_files("pthread/workqueue_private.h" HAVE_PTHREAD_WORKQUEUE_PRIVATE_H)
  193. check_include_files("pthread_machdep.h" HAVE_PTHREAD_MACHDEP_H)
  194. check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H)
  195. check_include_files("pthread_workqueue.h" HAVE_PTHREAD_WORKQUEUE_H)
  196. check_include_files("stdint.h" HAVE_STDINT_H)
  197. check_include_files("stdlib.h" HAVE_STDLIB_H)
  198. check_include_files("string.h" HAVE_STRING_H)
  199. check_include_files("strings.h" HAVE_STRINGS_H)
  200. check_include_files("sys/guarded.h" HAVE_SYS_GUARDED_H)
  201. check_include_files("sys/stat.h" HAVE_SYS_STAT_H)
  202. check_include_files("sys/types.h" HAVE_SYS_TYPES_H)
  203. check_include_files("objc/objc-internal.h" HAVE_OBJC)
  204. if(HAVE_MACH)
  205. set(USE_MACH_SEM 1)
  206. else()
  207. set(USE_MACH_SEM 0)
  208. endif()
  209. if(CMAKE_SYSTEM_NAME STREQUAL Windows)
  210. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:USE_WIN32_SEM>)
  211. endif()
  212. check_library_exists(pthread sem_init "" USE_POSIX_SEM)
  213. # NOTE: android has not always provided a libpthread, but uses the pthreads API
  214. if(CMAKE_SYSTEM_NAME STREQUAL Android)
  215. set(USE_POSIX_SEM 1)
  216. endif()
  217. check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
  218. check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST)
  219. check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_DECL_CLOCK_MONOTONIC)
  220. check_symbol_exists(CLOCK_REALTIME "time.h" HAVE_DECL_CLOCK_REALTIME)
  221. check_symbol_exists(CLOCK_MONOTONIC_COARSE "time.h" HAVE_DECL_CLOCK_MONOTONIC_COARSE)
  222. check_symbol_exists(FD_COPY "sys/select.h" HAVE_DECL_FD_COPY)
  223. check_symbol_exists(NOTE_LOWAT "sys/event.h" HAVE_DECL_NOTE_LOWAT)
  224. check_symbol_exists(NOTE_NONE "sys/event.h" HAVE_DECL_NOTE_NONE)
  225. check_symbol_exists(NOTE_REAP "sys/event.h" HAVE_DECL_NOTE_REAP)
  226. check_symbol_exists(NOTE_REVOKE "sys/event.h" HAVE_DECL_NOTE_REVOKE)
  227. check_symbol_exists(NOTE_SIGNAL "sys/event.h" HAVE_DECL_NOTE_SIGNAL)
  228. check_symbol_exists(POSIX_SPAWN_START_SUSPENDED "sys/spawn.h" HAVE_DECL_POSIX_SPAWN_START_SUSPENDED)
  229. check_symbol_exists(SIGEMT "signal.h" HAVE_DECL_SIGEMT)
  230. check_symbol_exists(VQ_DESIRED_DISK "sys/mount.h" HAVE_DECL_VQ_DESIRED_DISK)
  231. check_symbol_exists(VQ_NEARLOWDISK "sys/mount.h" HAVE_DECL_VQ_NEARLOWDISK)
  232. check_symbol_exists(VQ_QUOTA "sys/mount.h" HAVE_DECL_VQ_QUOTA)
  233. check_symbol_exists(VQ_UPDATE "sys/mount.h" HAVE_DECL_VQ_UPDATE)
  234. check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
  235. check_symbol_exists(VQ_FREE_SPACE_CHANGE "sys/mount.h" HAVE_DECL_VQ_FREE_SPACE_CHANGE)
  236. check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
  237. check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
  238. if (HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
  239. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_GNU_SOURCE=1>)
  240. endif()
  241. check_symbol_exists(__printflike "bsd/sys/cdefs.h" HAVE_PRINTFLIKE)
  242. if(CMAKE_SYSTEM_NAME STREQUAL Android)
  243. set(ENABLE_DTRACE_DEFAULT OFF)
  244. endif()
  245. if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
  246. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_WITH_DPRINTF>)
  247. endif()
  248. if(ENABLE_DTRACE)
  249. find_program(dtrace_EXECUTABLE dtrace)
  250. if(NOT dtrace_EXECUTABLE AND NOT ENABLE_DTRACE STREQUAL "")
  251. message(FATAL_ERROR "dtrace not found but explicitly requested")
  252. endif()
  253. endif()
  254. if(dtrace_EXECUTABLE)
  255. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=1>)
  256. else()
  257. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=0>)
  258. endif()
  259. find_program(leaks_EXECUTABLE leaks)
  260. if(leaks_EXECUTABLE)
  261. set(HAVE_LEAKS TRUE)
  262. endif()
  263. if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  264. add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap>
  265. $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/darwin/module.modulemap>)
  266. else()
  267. add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap>
  268. $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/generic/module.modulemap>)
  269. endif()
  270. configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
  271. "${PROJECT_BINARY_DIR}/config/config_ac.h")
  272. add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:HAVE_CONFIG_H>)
  273. if(ENABLE_SWIFT)
  274. if(NOT SWIFT_SYSTEM_NAME)
  275. if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  276. set(SWIFT_SYSTEM_NAME macosx)
  277. else()
  278. set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
  279. endif()
  280. endif()
  281. set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}" CACHE PATH "Path where the libraries will be installed")
  282. set(INSTALL_DISPATCH_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
  283. set(INSTALL_BLOCK_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
  284. set(INSTALL_OS_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/os" CACHE PATH "Path where the os/ headers will be installed")
  285. else()
  286. set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Path where the libraries will be installed")
  287. set(INSTALL_DISPATCH_HEADERS_DIR "include/dispatch" CACHE PATH "Path where the headers will be installed")
  288. set(INSTALL_BLOCK_HEADERS_DIR "include" CACHE PATH "Path where the headers will be installed for the blocks runtime")
  289. set(INSTALL_OS_HEADERS_DIR "include/os" CACHE PATH "Path where the headers will be installed")
  290. endif()
  291. add_subdirectory(dispatch)
  292. add_subdirectory(man)
  293. add_subdirectory(os)
  294. add_subdirectory(private)
  295. add_subdirectory(src)
  296. if(BUILD_TESTING)
  297. add_subdirectory(tests)
  298. endif()
  299. add_subdirectory(cmake/modules)