Skip to content

Commit d6d9e3e

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 d6d9e3e

File tree

9 files changed

+705
-0
lines changed

9 files changed

+705
-0
lines changed

CMakeLists.txt

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

0 commit comments

Comments
 (0)