Skip to content

Commit 6df7c18

Browse files
committed
build: add a cmake based build system
This is far from complete, but is sufficient to build a Linux version of libdispatch. It shows what a potential cmake based build system could look like, and if desired can be completed to build all the various flavours with cmake.
1 parent 272e818 commit 6df7c18

File tree

9 files changed

+831
-0
lines changed

9 files changed

+831
-0
lines changed

CMakeLists.txt

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

0 commit comments

Comments
 (0)