Skip to content

Commit 99575f9

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 012f48b commit 99575f9

File tree

6 files changed

+232
-0
lines changed

6 files changed

+232
-0
lines changed

CMakeLists.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
cmake_minimum_required(VERSION 3.4.3)
3+
4+
project(dispatch C)
5+
6+
set(CMAKE_C_VISIBILITY_PRESET hidden)
7+
8+
include(GNUInstallDirs)
9+
10+
set(WITH_KQUEUE "" CACHE PATH "Path to kqueue headers")
11+
set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
12+
set(WITH_PTHREAD_WORKQUEUES "" CACHE PATH "Path to pthread-workqueues")
13+
14+
option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" OFF)
15+
option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via __thread" OFF)
16+
17+
if(ENABLE_DISPATCH_INIT_CONSTRUCTOR)
18+
add_definitions(-DUSE_LIBDISPATCH_INIT_CONSTRUCTOR=1)
19+
endif()
20+
if(ENABLE_THREAD_LOCAL_STORAGE)
21+
add_definitions(-D DISPATCH_USE_THREAD_LOCAL_STORAGE=1)
22+
endif()
23+
24+
include(CheckCSourceCompiles)
25+
include(CheckFunctionExists)
26+
include(CheckIncludeFiles)
27+
include(CheckLibraryExists)
28+
include(CheckSymbolExists)
29+
30+
check_c_source_compiles(
31+
"
32+
#include <features.h>
33+
#if defined(__GNU_LIBRARY__)
34+
int main() { return 0; }
35+
#endif
36+
"
37+
_GNU_SOURCE)
38+
if(_GNU_SOURCE)
39+
set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
40+
add_definitions(-D_GNU_SOURCE)
41+
endif()
42+
43+
find_library(HAVE_LIBRT NAMES rt)
44+
if(HAVE_LIBRT)
45+
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
46+
endif()
47+
48+
check_function_exists(getprogname HAVE_GETPROGNAME)
49+
check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
50+
check_function_exists(sysconf HAVE_SYSCONF)
51+
52+
check_include_files("fcntl.h" HAVE_FCNTL_H)
53+
check_include_files("mach/mach.h" HAVE_MACH)
54+
if(HAVE_MACH)
55+
set(__DARWIN_NON_CANCELABLE 1)
56+
set(USE_MACH_SEM 1)
57+
else()
58+
set(__DARWIN_NON_CANCELABLE 0)
59+
set(USE_MACH_SEM 0)
60+
endif()
61+
62+
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
63+
64+
check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
65+
check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_DECL_CLOCK_MONOTONIC)
66+
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
67+
68+
if(APPLE)
69+
set(USE_APPLE_TSD_OPTIMIZATIONS 1)
70+
add_definitions(-DDISPATCH_USE_DTRACE=1)
71+
else()
72+
set(USE_APPLE_TSD_OPTIMIZATIONS 0)
73+
add_definitions(-DDISPATCH_USE_DTRACE=0)
74+
endif()
75+
76+
configure_file("${CMAKE_SOURCE_DIR}/config/config.h.in"
77+
"${CMAKE_SOURCE_DIR}/config/config_ac.h")
78+
79+
add_subdirectory(dispatch)
80+
add_subdirectory(man)
81+
add_subdirectory(os)
82+
add_subdirectory(private)
83+
add_subdirectory(src)
84+
if(ENABLE_TESTING)
85+
add_subdirectory(tests)
86+
endif()
87+

dispatch/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
install(FILES
3+
base.h
4+
block.h
5+
data.h
6+
dispatch.h
7+
group.h
8+
introspection.h
9+
io.h
10+
object.h
11+
once.h
12+
queue.h
13+
semaphore.h
14+
source.h
15+
time.h
16+
DESTINATION
17+
${CMAKE_INSTALL_FULL_INCLUDEDIR}/dispatch/)
18+
if(ENABLE_SWIFT)
19+
install(FILES
20+
module.modulemap
21+
DESTINATION
22+
${CMAKE_INSTALL_FULL_INCLUEDIR}/dispatch/)
23+
endif()
24+

man/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# TODO(compnerd) add symlinks
3+
if(NOT ENABLE_SWIFT)
4+
install(FILES
5+
dispatch.3
6+
dispatch_after.3
7+
dispatch_api.3
8+
dispatch_apply.3
9+
dispatch_async.3
10+
dispatch_data_create.3
11+
dispatch_group_create.3
12+
dispatch_io_create.3
13+
dispatch_io_read.3
14+
dispatch_object.3
15+
dispatch_once.3
16+
dispatch_queue_create.3
17+
dispatch_read.3
18+
dispatch_semaphore_create.3
19+
dispatch_source_create.3
20+
dispatch_time.3
21+
DESTINATION
22+
"${CMAKE_INSTALL_FULL_MANDIR}/man3")
23+
endif()

os/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# TODO(compnerd) ensure that object_private.h voucher_activity_private.h
3+
# voucher_private.h are included in the source tarball
4+
5+
install(FILES
6+
object.h
7+
linux_base.h
8+
DESTINATION
9+
"${CMAKE_INSTALL_FULL_INCLUDEDIR}/os")
10+

private/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# TODO(compnerd) ensure that benchmark.h data_private.h introduction_private.h
3+
# io_private.h layout_private.h mach_private.h private.h queue_private.h
4+
# source_private.h are included in the source tarball
5+

src/CMakeLists.txt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
add_library(dispatch
3+
allocator.c
4+
apply.c
5+
benchmark.c
6+
data.c
7+
introspection.c
8+
init.c
9+
io.c
10+
object.c
11+
once.c
12+
queue.c
13+
semaphore.c
14+
source.c
15+
time.c
16+
transform.c
17+
voucher.c
18+
protocol.defs
19+
provider.d
20+
allocator_internal.h
21+
data_internal.h
22+
inline_internal.h
23+
internal.h
24+
introspection_internal.h
25+
io_internal.h
26+
object_internal.h
27+
queue_internal.h
28+
semaphore_internal.h
29+
shims.h
30+
source_internal.h
31+
trace.h
32+
voucher_internal.h
33+
firehose/firehose_internal.h
34+
shims/android_stubs.h
35+
shims/atomic.h
36+
shims/atomic_sfb.h
37+
shims/getprogname.h
38+
shims/hw_config.h
39+
shims/linux_stubs.c
40+
shims/linux_stubs.h
41+
shims/lock.c
42+
shims/lock.h
43+
shims/perfmon.h
44+
shims/time.h
45+
shims/tsd.h
46+
shims/yield.h)
47+
target_include_directories(dispatch
48+
PRIVATE
49+
${CMAKE_SOURCE_DIR}
50+
${CMAKE_BINARY_DIR}
51+
${CMAKE_CURRENT_SOURCE_DIR}
52+
${CMAKE_SOURCE_DIR}/private)
53+
target_compile_options(dispatch
54+
PRIVATE
55+
-Wall
56+
-fblocks
57+
-momit-leaf-frame-pointer)
58+
if(WITH_KQUEUE)
59+
target_include_directories(dispatch
60+
SYSTEM BEFORE PRIVATE
61+
"${WITH_KQUEUE}/include")
62+
endif()
63+
if(WITH_BLOCKS_RUNTIME)
64+
target_include_directories(dispatch
65+
SYSTEM BEFORE PRIVATE
66+
"${WITH_BLOCKS_RUNTIME}")
67+
endif()
68+
if(WITH_PTHREAD_WORKQUEUES)
69+
target_include_directories(dispatch
70+
SYSTEM BEFORE PRIVATE
71+
"${WITH_PTHREAD_WORKQUEUES}/include")
72+
endif()
73+
if(DISPATCH_ENABLE_ASSERTS)
74+
target_compile_definitions(dispatch
75+
PRIVATE
76+
-DDISPATCH_DEBUG=1)
77+
endif()
78+
79+
install(TARGETS
80+
dispatch
81+
DESTINATION
82+
"${CMAKE_INSTALL_FULL_LIBDIR}")
83+

0 commit comments

Comments
 (0)