Skip to content

Commit 3da8398

Browse files
authored
Merge pull request #196 from compnerd/cmake
build: add a cmake based build system
2 parents f84d21d + ffe98c4 commit 3da8398

File tree

10 files changed

+850
-0
lines changed

10 files changed

+850
-0
lines changed

CMakeLists.txt

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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(GNUInstallDirs)
19+
include(ExternalProject)
20+
21+
set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
22+
set(WITH_PTHREAD_WORKQUEUES "" CACHE PATH "Path to pthread-workqueues")
23+
24+
include(DispatchAppleOptions)
25+
26+
option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
27+
set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})
28+
29+
# TODO(compnerd) swift options
30+
31+
# TODO(compnerd) consider adding a flag for USE_GOLD_LINKER. Currently, we
32+
# expect the user to specify `-fuse-ld=gold`
33+
34+
option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via __thread" ON)
35+
set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})
36+
37+
if(EXISTS "${CMAKE_SOURCE_DIR}/libpwq/CMakeLists.txt")
38+
ExternalProject_Add(pwq
39+
SOURCE_DIR
40+
"${CMAKE_SOURCE_DIR}/libpwq"
41+
CMAKE_ARGS
42+
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
43+
-DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
44+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
45+
BUILD_BYPRODUCTS
46+
<INSTALL_DIR>/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}pthread_workqueue${CMAKE_STATIC_LIBRARY_SUFFIX})
47+
ExternalProject_Get_Property(pwq install_dir)
48+
add_library(PTHREAD::workqueue UNKNOWN IMPORTED)
49+
set_target_properties(PTHREAD::workqueue
50+
PROPERTIES
51+
IMPORTED_LOCATION ${install_dir}/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}pthread_workqueue${CMAKE_STATIC_LIBRARY_SUFFIX})
52+
set(WITH_PTHREAD_WORKQUEUES "${install_dir}" CACHE PATH "Path to pthread-workqueues" FORCE)
53+
set(HAVE_PTHREAD_WORKQUEUES 1)
54+
else()
55+
# TODO(compnerd) support system installed pthread-workqueues
56+
# find_package(pthread_workqueues REQUIRED)
57+
# set(HAVE_PTHREAD_WORKQUEUES 1)
58+
endif()
59+
60+
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
61+
CMAKE_SYSTEM_NAME STREQUAL Android OR
62+
CMAKE_SYSTEM_NAME STREQUAL Windows)
63+
add_library(BlocksRuntime
64+
STATIC
65+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/data.c
66+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c)
67+
set_target_properties(BlocksRuntime
68+
PROPERTIES
69+
POSITION_INDEPENDENT_CODE TRUE)
70+
if(HAVE_OBJC AND CMAKE_DL_LIBS)
71+
set_target_properties(BlocksRuntime
72+
PROPERTIES
73+
INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS})
74+
endif()
75+
set(WITH_BLOCKS_RUNTIME "${CMAKE_SOURCE_DIR}/src/BlocksRuntime" CACHE PATH "Path to blocks runtime" FORCE)
76+
else()
77+
# TODO(compnerd) support system installed BlocksRuntime
78+
# find_package(BlocksRuntime REQUIRED)
79+
endif()
80+
81+
include(CheckCSourceCompiles)
82+
include(CheckFunctionExists)
83+
include(CheckIncludeFiles)
84+
include(CheckLibraryExists)
85+
include(CheckSymbolExists)
86+
87+
check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
88+
if(_GNU_SOURCE)
89+
set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
90+
endif()
91+
92+
check_c_source_compiles("void __attribute__((__noreturn__)) main() { __builtin_trap(); }"
93+
__BUILTIN_TRAP)
94+
if(__BUILTIN_TRAP)
95+
set(HAVE_NORETURN_BUILTIN_TRAP 1)
96+
endif()
97+
98+
find_package(LibRT)
99+
100+
check_function_exists(_pthread_workqueue_init HAVE__PTHREAD_WORKQUEUE_INIT)
101+
check_function_exists(getprogname HAVE_GETPROGNAME)
102+
check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
103+
check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)
104+
check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
105+
check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
106+
check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
107+
check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
108+
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
109+
check_function_exists(strlcpy HAVE_STRLCPY)
110+
check_function_exists(sysconf HAVE_SYSCONF)
111+
112+
if(NOT HAVE_STRLCPY AND NOT HAVE_GETPROGNAME)
113+
include(FindPkgConfig)
114+
pkg_check_modules(BSD_OVERLAY libbsd-overlay)
115+
if(BSD_OVERLAY_FOUND)
116+
set(HAVE_STRLCPY 1 CACHE INTERNAL "Have function strlcpy" FORCE)
117+
set(HAVE_GETPROGNAME 1 CACHE INTERNAL "Have function getprogname" FORCE)
118+
endif()
119+
endif()
120+
121+
find_package(Threads REQUIRED)
122+
123+
check_include_files("TargetConditionals.h" HAVE_TARGETCONDITIONALS_H)
124+
check_include_files("dlfcn.h" HAVE_DLFCN_H)
125+
check_include_files("fcntl.h" HAVE_FCNTL_H)
126+
check_include_files("inttypes.h" HAVE_INTTYPES_H)
127+
check_include_files("libkern/OSAtomic.h" HAVE_LIBKERN_OSATOMIC_H)
128+
check_include_files("libkern/OSCrossEndian.h" HAVE_LIBKERN_OSCROSSENDIAN_H)
129+
check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H)
130+
check_include_files("mach/mach.h" HAVE_MACH)
131+
if(HAVE_MACH)
132+
set(__DARWIN_NON_CANCELABLE 1)
133+
set(USE_MACH_SEM 1)
134+
else()
135+
set(__DARWIN_NON_CANCELABLE 0)
136+
set(USE_MACH_SEM 0)
137+
endif()
138+
check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H)
139+
check_include_files("memory.h" HAVE_MEMORY_H)
140+
check_include_files("pthread/qos.h" HAVE_PTHREAD_QOS_H)
141+
check_include_files("pthread/workqueue_private.h" HAVE_PTHREAD_WORKQUEUE_PRIVATE_H)
142+
check_include_files("pthread_machdep.h" HAVE_PTHREAD_MACHDEP_H)
143+
check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H)
144+
check_include_files("pthread_workqueue.h" HAVE_PTHREAD_WORKQUEUE_H)
145+
check_include_files("stdint.h" HAVE_STDINT_H)
146+
check_include_files("stdlib.h" HAVE_STDLIB_H)
147+
check_include_files("string.h" HAVE_STRING_H)
148+
check_include_files("strings.h" HAVE_STRINGS_H)
149+
check_include_files("sys/cdefs.h" HAVE_SYS_CDEFS_H)
150+
check_include_files("sys/guarded.h" HAVE_SYS_GUARDED_H)
151+
check_include_files("sys/stat.h" HAVE_SYS_STAT_H)
152+
check_include_files("sys/types.h" HAVE_SYS_TYPES_H)
153+
check_include_files("unistd.h" HAVE_UNISTD_H)
154+
check_include_files("objc/objc-internal.h" HAVE_OBJC)
155+
156+
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
157+
158+
check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
159+
check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST)
160+
check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_DECL_CLOCK_MONOTONIC)
161+
check_symbol_exists(CLOCK_REALTIME "time.h" HAVE_DECL_CLOCK_REALTIME)
162+
check_symbol_exists(FD_COPY "sys/select.h" HAVE_DECL_FD_COPY)
163+
check_symbol_exists(NOTE_LOWAT "sys/event.h" HAVE_DECL_NOTE_LOWAT)
164+
check_symbol_exists(NOTE_NONE "sys/event.h" HAVE_DECL_NOTE_NONE)
165+
check_symbol_exists(NOTE_REAP "sys/event.h" HAVE_DECL_NOTE_REAP)
166+
check_symbol_exists(NOTE_REVOKE "sys/event.h" HAVE_DECL_NOTE_REVOKE)
167+
check_symbol_exists(NOTE_SIGNAL "sys/event.h" HAVE_DECL_NOTE_SIGNAL)
168+
check_symbol_exists(POSIX_SPAWN_START_SUSPENDED "sys/spawn.h" HAVE_DECL_POSIX_SPAWN_START_SUSPENDED)
169+
check_symbol_exists(SIGEMT "signal.h" HAVE_DECL_SIGEMT)
170+
check_symbol_exists(VQ_DESIRED_DISK "sys/mount.h" HAVE_DECL_VQ_DESIRED_DISK)
171+
check_symbol_exists(VQ_NEARLOWDISK "sys/mount.h" HAVE_DECL_VQ_NEARLOWDISK)
172+
check_symbol_exists(VQ_QUOTA "sys/mount.h" HAVE_DECL_VQ_QUOTA)
173+
check_symbol_exists(VQ_UPDATE "sys/mount.h" HAVE_DECL_VQ_UPDATE)
174+
check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
175+
176+
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
177+
178+
find_program(dtrace_EXECUTABLE dtrace)
179+
if(dtrace_EXECUTABLE)
180+
add_definitions(-DDISPATCH_USE_DTRACE=1)
181+
else()
182+
add_definitions(-DDISPATCH_USE_DTRACE=0)
183+
endif()
184+
185+
find_program(leaks_EXECUTABLE leaks)
186+
if(leaks_EXECUTABLE)
187+
set(HAVE_LEAKS TRUE)
188+
endif()
189+
190+
configure_file("${CMAKE_SOURCE_DIR}/cmake/config.h.in"
191+
"${CMAKE_BINARY_DIR}/config/config_ac.h")
192+
add_definitions(-DHAVE_CONFIG_H)
193+
194+
add_subdirectory(dispatch)
195+
add_subdirectory(man)
196+
add_subdirectory(os)
197+
add_subdirectory(private)
198+
add_subdirectory(src)
199+
if(ENABLE_TESTING)
200+
add_subdirectory(tests)
201+
endif()
202+

0 commit comments

Comments
 (0)