Skip to content

Commit f1c2e33

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 f1c2e33

File tree

8 files changed

+592
-0
lines changed

8 files changed

+592
-0
lines changed

CMakeLists.txt

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

0 commit comments

Comments
 (0)