Skip to content

Commit 7a5cd48

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 787dd92 commit 7a5cd48

File tree

9 files changed

+682
-0
lines changed

9 files changed

+682
-0
lines changed

CMakeLists.txt

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
11+
include(GNUInstallDirs)
12+
13+
set(WITH_KQUEUE "" CACHE PATH "Path to kqueue headers")
14+
set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
15+
set(WITH_PTHREAD_WORKQUEUES "" CACHE PATH "Path to pthread-workqueues")
16+
17+
# TODO(compnerd) consider adding a flag for USE_GOLD_LINKER. Currently, we
18+
# expect the user to specify `-fuse-ld=gold`
19+
20+
include(DispatchAppleOptions)
21+
22+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
23+
set(ENABLE_DISPATCH_INIT_CONSTRUCTOR_DEFAULT ON)
24+
else()
25+
set(ENABLE_DISPATCH_INIT_CONSTRUCTOR_DEFAULT OFF)
26+
endif()
27+
option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor"
28+
${ENABLE_DISPATCH_INIT_CONSTRUCTOR_DEFAULT})
29+
30+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
31+
set(ENABLE_THREAD_LOCAL_STORAGE_DEFAULT ON)
32+
else()
33+
set(ENABLE_THREAD_LOCAL_STORAGE_DEFAULT OFF)
34+
endif()
35+
option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via __thread"
36+
${ENABLE_THREAD_LOCAL_STORAGE_DEFAULT})
37+
38+
if(ENABLE_DISPATCH_INIT_CONSTRUCTOR)
39+
add_definitions(-DUSE_LIBDISPATCH_INIT_CONSTRUCTOR=1)
40+
endif()
41+
if(ENABLE_THREAD_LOCAL_STORAGE)
42+
add_definitions(-DDISPATCH_USE_THREAD_LOCAL_STORAGE=1)
43+
endif()
44+
45+
include(CheckCSourceCompiles)
46+
include(CheckFunctionExists)
47+
include(CheckIncludeFiles)
48+
include(CheckLibraryExists)
49+
include(CheckSymbolExists)
50+
51+
check_c_source_compiles(
52+
"
53+
#include <features.h>
54+
#if defined(__GNU_LIBRARY__)
55+
int main() { return 0; }
56+
#endif
57+
"
58+
_GNU_SOURCE)
59+
if(_GNU_SOURCE)
60+
set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
61+
add_definitions(-D_GNU_SOURCE)
62+
endif()
63+
64+
find_library(HAVE_LIBRT NAMES rt)
65+
if(HAVE_LIBRT)
66+
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
67+
endif()
68+
69+
check_function_exists(_pthread_workqueue_init HAVE__PTHREAD_WORKQUEUE_INIT)
70+
check_function_exists(getprogname HAVE_GETPROGNAME)
71+
check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
72+
check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
73+
check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
74+
check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
75+
check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
76+
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
77+
check_function_exists(strlcpy HAVE_STRLCPY)
78+
check_function_exists(sysconf HAVE_SYSCONF)
79+
80+
if(NOT HAVE_STRLCPY AND NOT HAVE_GETPROGNAME)
81+
include(FindPkgConfig)
82+
pkg_check_modules(BSD_OVERLAY libbsd-overlay)
83+
if(BSD_OVERLAY_FOUND)
84+
set(HAVE_STRLCPY 1)
85+
set(HAVE_GETPROGNAME 1)
86+
endif()
87+
endif()
88+
89+
find_package(Threads REQUIRED)
90+
91+
check_include_files("TargetConditionals.h" HAVE_TARGETCONDITIONALS_H)
92+
check_include_files("dlfcn.h" HAVE_DLFCN_H)
93+
check_include_files("fcntl.h" HAVE_FCNTL_H)
94+
check_include_files("inttypes.h" HAVE_INTTYPES_H)
95+
check_include_files("libkern/OSAtomic.h" HAVE_LIBKERN_OSATOMIC_H)
96+
check_include_files("libkern/OSCrossEndian.h" HAVE_LIBKERN_OSCROSSENDIAN_H)
97+
check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H)
98+
check_include_files("mach/mach.h" HAVE_MACH)
99+
if(HAVE_MACH)
100+
set(__DARWIN_NON_CANCELABLE 1)
101+
set(USE_MACH_SEM 1)
102+
else()
103+
set(__DARWIN_NON_CANCELABLE 0)
104+
set(USE_MACH_SEM 0)
105+
endif()
106+
check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H)
107+
check_include_files("memory.h" HAVE_MEMORY_H)
108+
check_include_files("pthread/qos.h" HAVE_PTHREAD_QOS_H)
109+
check_include_files("pthread/workqueue_private.h" HAVE_PTHREAD_WORKQUEUE_PRIVATE_H)
110+
check_include_files("pthread_machdep.h" HAVE_PTHREAD_MACHDEP_H)
111+
check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H)
112+
check_include_files("pthread_workqueue.h" HAVE_PTHREAD_WORKQUEUE_H)
113+
check_include_files("stdint.h" HAVE_STDINT_H)
114+
check_include_files("stdlib.h" HAVE_STDLIB_H)
115+
check_include_files("string.h" HAVE_STRING_H)
116+
check_include_files("strings.h" HAVE_STRINGS_H)
117+
check_include_files("sys/cdefs.h" HAVE_SYS_CDEFS_H)
118+
check_include_files("sys/guarded.h" HAVE_SYS_GUARDED_H)
119+
check_include_files("sys/stat.h" HAVE_SYS_STAT_H)
120+
check_include_files("sys/types.h" HAVE_SYS_TYPES_H)
121+
check_include_files("unistd.h" HAVE_UNISTD_H)
122+
123+
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
124+
125+
check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
126+
check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_DECL_CLOCK_MONOTONIC)
127+
check_symbol_exists(FD_COPY "sys/select.h" HAVE_DECL_FD_COPY)
128+
check_symbol_exists(NOTE_NONE "sys/event.h" HAVE_DECL_NOTE_NONE)
129+
check_symbol_exists(NOTE_REAP "sys/event.h" HAVE_DECL_NOTE_REAP)
130+
check_symbol_exists(NOTE_REVOKE "sys/event.h" HAVE_DECL_NOTE_REVOKE)
131+
check_symbol_exists(NOTE_SIGNAL "sys/event.h" HAVE_DECL_NOTE_SIGNAL)
132+
check_symbol_exists(SIGEMT "signal.h" HAVE_DECL_SIGEMT)
133+
check_symbol_exists(VQ_QUOTA "sys/mount.h" HAVE_DECL_VQ_QUOTA)
134+
check_symbol_exists(VQ_UPDATE "sys/mount.h" HAVE_DECL_VQ_UPDATE)
135+
check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
136+
137+
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
138+
139+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
140+
add_definitions(-DDISPATCH_USE_DTRACE=1)
141+
else()
142+
add_definitions(-DDISPATCH_USE_DTRACE=0)
143+
endif()
144+
145+
# TODO(compnerd) unify the config.h naming across Darwin and others and rename
146+
# to config.h
147+
configure_file("${CMAKE_SOURCE_DIR}/cmake/config.h.in"
148+
"${CMAKE_BINARY_DIR}/config/config_ac.h")
149+
150+
add_subdirectory(dispatch)
151+
add_subdirectory(man)
152+
add_subdirectory(os)
153+
add_subdirectory(private)
154+
add_subdirectory(src)
155+
if(ENABLE_TESTING)
156+
add_subdirectory(tests)
157+
endif()
158+

0 commit comments

Comments
 (0)