]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | cmake_minimum_required(VERSION 3.4.3) | |
3 | ||
4 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") | |
5 | ||
6 | project(dispatch | |
7 | VERSION 1.3 | |
8 | LANGUAGES C CXX) | |
9 | enable_testing() | |
10 | ||
11 | set(CMAKE_C_VISIBILITY_PRESET hidden) | |
12 | set(CMAKE_CXX_STANDARD 11) | |
13 | ||
14 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) | |
15 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) | |
16 | find_package(Threads REQUIRED) | |
17 | ||
18 | include(CheckCSourceCompiles) | |
19 | include(CheckFunctionExists) | |
20 | include(CheckIncludeFiles) | |
21 | include(CheckLibraryExists) | |
22 | include(CheckSymbolExists) | |
23 | include(GNUInstallDirs) | |
24 | ||
25 | set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime") | |
26 | ||
27 | include(DispatchAppleOptions) | |
28 | ||
29 | option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON) | |
30 | set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR}) | |
31 | ||
32 | # TODO(compnerd) swift options | |
33 | ||
34 | option(BUILD_SHARED_LIBS "build shared libraries" ON) | |
35 | ||
36 | option(ENABLE_TESTING "build libdispatch tests" ON) | |
37 | ||
38 | if(CMAKE_SYSTEM_NAME STREQUAL Linux OR | |
39 | CMAKE_SYSTEM_NAME STREQUAL Android) | |
40 | set(USE_GOLD_LINKER_DEFAULT ON) | |
41 | else() | |
42 | set(USE_GOLD_LINKER_DEFAULT OFF) | |
43 | endif() | |
44 | option(USE_GOLD_LINKER "use the gold linker" ${USE_GOLD_LINKER_DEFAULT}) | |
45 | ||
46 | option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via __thread" ON) | |
47 | set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE}) | |
48 | ||
49 | if(CMAKE_SYSTEM_NAME STREQUAL Linux OR | |
50 | CMAKE_SYSTEM_NAME STREQUAL Android OR | |
51 | CMAKE_SYSTEM_NAME STREQUAL Windows) | |
52 | set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON) | |
53 | else() | |
54 | set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT OFF) | |
55 | endif() | |
56 | option(ENABLE_INTERNAL_PTHREAD_WORKQUEUES "use libdispatch's own implementation of pthread workqueues" ${ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT}) | |
57 | if(ENABLE_INTERNAL_PTHREAD_WORKQUEUES) | |
58 | set(DISPATCH_USE_INTERNAL_WORKQUEUE 1) | |
59 | set(HAVE_PTHREAD_WORKQUEUES 0) | |
60 | else() | |
61 | check_include_files(pthread/workqueue_private.h HAVE_PTHREAD_WORKQUEUE_PRIVATE_H) | |
62 | check_include_files(pthread_workqueue.h HAVE_PTHREAD_WORKQUEUE_H) | |
63 | if(HAVE_PTHREAD_WORKQUEUE_PRIVATE_H AND HAVE_PTHREAD_WORKQUEUE_H) | |
64 | set(HAVE_PTHREAD_WORKQUEUES 1) | |
65 | set(DISPATCH_USE_INTERNAL_WORKQUEUE 0) | |
66 | else() | |
67 | set(HAVE_PTHREAD_WORKQUEUES 0) | |
68 | set(DISPATCH_USE_INTERNAL_WORKQUEUE 1) | |
69 | endif() | |
70 | endif() | |
71 | ||
72 | if(CMAKE_SYSTEM_NAME STREQUAL Linux OR | |
73 | CMAKE_SYSTEM_NAME STREQUAL Android OR | |
74 | CMAKE_SYSTEM_NAME STREQUAL Windows) | |
75 | add_library(BlocksRuntime | |
76 | STATIC | |
77 | ${CMAKE_SOURCE_DIR}/src/BlocksRuntime/data.c | |
78 | ${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c) | |
79 | set_target_properties(BlocksRuntime | |
80 | PROPERTIES | |
81 | POSITION_INDEPENDENT_CODE TRUE) | |
82 | if(HAVE_OBJC AND CMAKE_DL_LIBS) | |
83 | set_target_properties(BlocksRuntime | |
84 | PROPERTIES | |
85 | INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS}) | |
86 | endif() | |
87 | set(WITH_BLOCKS_RUNTIME "${CMAKE_SOURCE_DIR}/src/BlocksRuntime" CACHE PATH "Path to blocks runtime" FORCE) | |
88 | else() | |
89 | # TODO(compnerd) support system installed BlocksRuntime | |
90 | # find_package(BlocksRuntime REQUIRED) | |
91 | endif() | |
92 | ||
93 | check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE) | |
94 | if(_GNU_SOURCE) | |
95 | set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE) | |
96 | endif() | |
97 | ||
98 | check_c_source_compiles("void __attribute__((__noreturn__)) main() { __builtin_trap(); }" | |
99 | __BUILTIN_TRAP) | |
100 | if(__BUILTIN_TRAP) | |
101 | set(HAVE_NORETURN_BUILTIN_TRAP 1) | |
102 | endif() | |
103 | ||
104 | find_package(LibRT) | |
105 | ||
106 | check_function_exists(_pthread_workqueue_init HAVE__PTHREAD_WORKQUEUE_INIT) | |
107 | check_function_exists(getprogname HAVE_GETPROGNAME) | |
108 | check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME) | |
109 | check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME) | |
110 | check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT) | |
111 | check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE) | |
112 | check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP) | |
113 | check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP) | |
114 | check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP) | |
115 | check_function_exists(strlcpy HAVE_STRLCPY) | |
116 | check_function_exists(sysconf HAVE_SYSCONF) | |
117 | ||
118 | if(NOT HAVE_STRLCPY AND NOT HAVE_GETPROGNAME) | |
119 | include(FindPkgConfig) | |
120 | pkg_check_modules(BSD_OVERLAY libbsd-overlay) | |
121 | if(BSD_OVERLAY_FOUND) | |
122 | set(HAVE_STRLCPY 1 CACHE INTERNAL "Have function strlcpy" FORCE) | |
123 | set(HAVE_GETPROGNAME 1 CACHE INTERNAL "Have function getprogname" FORCE) | |
124 | endif() | |
125 | endif() | |
126 | ||
127 | find_package(Threads REQUIRED) | |
128 | ||
129 | check_include_files("TargetConditionals.h" HAVE_TARGETCONDITIONALS_H) | |
130 | check_include_files("dlfcn.h" HAVE_DLFCN_H) | |
131 | check_include_files("fcntl.h" HAVE_FCNTL_H) | |
132 | check_include_files("inttypes.h" HAVE_INTTYPES_H) | |
133 | check_include_files("libkern/OSAtomic.h" HAVE_LIBKERN_OSATOMIC_H) | |
134 | check_include_files("libkern/OSCrossEndian.h" HAVE_LIBKERN_OSCROSSENDIAN_H) | |
135 | check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H) | |
136 | check_include_files("mach/mach.h" HAVE_MACH) | |
137 | if(HAVE_MACH) | |
138 | set(__DARWIN_NON_CANCELABLE 1) | |
139 | set(USE_MACH_SEM 1) | |
140 | else() | |
141 | set(__DARWIN_NON_CANCELABLE 0) | |
142 | set(USE_MACH_SEM 0) | |
143 | endif() | |
144 | check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H) | |
145 | check_include_files("memory.h" HAVE_MEMORY_H) | |
146 | check_include_files("pthread/qos.h" HAVE_PTHREAD_QOS_H) | |
147 | check_include_files("pthread/workqueue_private.h" HAVE_PTHREAD_WORKQUEUE_PRIVATE_H) | |
148 | check_include_files("pthread_machdep.h" HAVE_PTHREAD_MACHDEP_H) | |
149 | check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H) | |
150 | check_include_files("pthread_workqueue.h" HAVE_PTHREAD_WORKQUEUE_H) | |
151 | check_include_files("stdint.h" HAVE_STDINT_H) | |
152 | check_include_files("stdlib.h" HAVE_STDLIB_H) | |
153 | check_include_files("string.h" HAVE_STRING_H) | |
154 | check_include_files("strings.h" HAVE_STRINGS_H) | |
155 | check_include_files("sys/cdefs.h" HAVE_SYS_CDEFS_H) | |
156 | check_include_files("sys/guarded.h" HAVE_SYS_GUARDED_H) | |
157 | check_include_files("sys/stat.h" HAVE_SYS_STAT_H) | |
158 | check_include_files("sys/types.h" HAVE_SYS_TYPES_H) | |
159 | check_include_files("unistd.h" HAVE_UNISTD_H) | |
160 | check_include_files("objc/objc-internal.h" HAVE_OBJC) | |
161 | ||
162 | check_library_exists(pthread sem_init "" USE_POSIX_SEM) | |
163 | if(CMAKE_SYSTEM_NAME STREQUAL Windows) | |
164 | add_definitions(-DTARGET_OS_WIN32) | |
165 | add_definitions(-DUSE_WIN32_SEM) | |
166 | endif() | |
167 | ||
168 | check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME) | |
169 | check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST) | |
170 | check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_DECL_CLOCK_MONOTONIC) | |
171 | check_symbol_exists(CLOCK_REALTIME "time.h" HAVE_DECL_CLOCK_REALTIME) | |
172 | check_symbol_exists(FD_COPY "sys/select.h" HAVE_DECL_FD_COPY) | |
173 | check_symbol_exists(NOTE_LOWAT "sys/event.h" HAVE_DECL_NOTE_LOWAT) | |
174 | check_symbol_exists(NOTE_NONE "sys/event.h" HAVE_DECL_NOTE_NONE) | |
175 | check_symbol_exists(NOTE_REAP "sys/event.h" HAVE_DECL_NOTE_REAP) | |
176 | check_symbol_exists(NOTE_REVOKE "sys/event.h" HAVE_DECL_NOTE_REVOKE) | |
177 | check_symbol_exists(NOTE_SIGNAL "sys/event.h" HAVE_DECL_NOTE_SIGNAL) | |
178 | check_symbol_exists(POSIX_SPAWN_START_SUSPENDED "sys/spawn.h" HAVE_DECL_POSIX_SPAWN_START_SUSPENDED) | |
179 | check_symbol_exists(SIGEMT "signal.h" HAVE_DECL_SIGEMT) | |
180 | check_symbol_exists(VQ_DESIRED_DISK "sys/mount.h" HAVE_DECL_VQ_DESIRED_DISK) | |
181 | check_symbol_exists(VQ_NEARLOWDISK "sys/mount.h" HAVE_DECL_VQ_NEARLOWDISK) | |
182 | check_symbol_exists(VQ_QUOTA "sys/mount.h" HAVE_DECL_VQ_QUOTA) | |
183 | check_symbol_exists(VQ_UPDATE "sys/mount.h" HAVE_DECL_VQ_UPDATE) | |
184 | check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK) | |
185 | ||
186 | check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME) | |
187 | ||
188 | find_program(dtrace_EXECUTABLE dtrace) | |
189 | if(dtrace_EXECUTABLE) | |
190 | add_definitions(-DDISPATCH_USE_DTRACE=1) | |
191 | else() | |
192 | add_definitions(-DDISPATCH_USE_DTRACE=0) | |
193 | endif() | |
194 | ||
195 | find_program(leaks_EXECUTABLE leaks) | |
196 | if(leaks_EXECUTABLE) | |
197 | set(HAVE_LEAKS TRUE) | |
198 | endif() | |
199 | ||
200 | if(CMAKE_SYSTEM_NAME STREQUAL Darwin) | |
201 | add_custom_command(OUTPUT | |
202 | "${CMAKE_SOURCE_DIR}/dispatch/module.modulemap" | |
203 | "${CMAKE_SOURCE_DIR}/private/module.modulemap" | |
204 | COMMAND | |
205 | ${CMAKE_COMMAND} -E create_symlink "${CMAKE_SOURCE_DIR}/darwin/module.modulemap" "${CMAKE_SOURCE_DIR}/dispatch/module.modulemap" | |
206 | COMMAND | |
207 | ${CMAKE_COMMAND} -E create_symlink "${CMAKE_SOURCE_DIR}/darwin/module.modulemap" "${CMAKE_SOURCE_DIR}/private/module.modulemap") | |
208 | else() | |
209 | add_custom_command(OUTPUT | |
210 | "${CMAKE_SOURCE_DIR}/dispatch/module.modulemap" | |
211 | "${CMAKE_SOURCE_DIR}/private/module.modulemap" | |
212 | COMMAND | |
213 | ${CMAKE_COMMAND} -E create_symlink "${CMAKE_SOURCE_DIR}/generic/module.modulemap" "${CMAKE_SOURCE_DIR}/dispatch/module.modulemap" | |
214 | COMMAND | |
215 | ${CMAKE_COMMAND} -E create_symlink "${CMAKE_SOURCE_DIR}/generic/module.modulemap" "${CMAKE_SOURCE_DIR}/private/module.modulemap") | |
216 | endif() | |
217 | configure_file("${CMAKE_SOURCE_DIR}/cmake/config.h.in" | |
218 | "${CMAKE_BINARY_DIR}/config/config_ac.h") | |
219 | add_definitions(-DHAVE_CONFIG_H) | |
220 | ||
221 | add_subdirectory(dispatch) | |
222 | add_subdirectory(man) | |
223 | add_subdirectory(os) | |
224 | add_subdirectory(private) | |
225 | add_subdirectory(src) | |
226 | if(ENABLE_TESTING) | |
227 | add_subdirectory(tests) | |
228 | endif() | |
229 |