forked from Snaipe/BoxFort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
232 lines (183 loc) · 6.57 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright (C) 2016 Franklin "Snaipe" Mathieu.
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the LICENSE file distributed with BoxFort.
cmake_minimum_required (VERSION 2.8)
project (BoxFort C)
include (CheckSymbolExists)
include (CheckLibraryExists)
set (MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.cmake/Modules")
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${MODULE_DIR})
include (CheckCompile)
set (I386 "^(i[3-7]|x)86$")
set (AMD64 "^(x86_|x86-|AMD|amd|x)64$")
if (CMAKE_GENERATOR_PLATFORM)
set (_ARCH "${CMAKE_GENERATOR_PLATFORM}")
else ()
set (_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
endif ()
set (CMAKE_C_FLAGS_DEFAULT "${CMAKE_C_FLAGS}")
set (BXF_MANGLING "none")
if (MSVC)
if (NOT CMAKE_GENERATOR_PLATFORM)
set (_ARCH "x86")
endif ()
enable_language (ASM_MASM)
if (CMAKE_GENERATOR MATCHES "^.*Win64$" OR _ARCH MATCHES "${AMD64}")
set (BXF_ARCH "x86_64")
set (BXF_ABI "win")
set (BXF_BITS 64)
set (BXF_ARCH_x86_64 1)
elseif (_ARCH MATCHES "${I386}")
set (BXF_ARCH "i386")
set (BXF_ABI "cdecl")
set (BXF_BITS 32)
set (BXF_ARCH_x86 1)
set (BXF_MANGLING "leading-underscore")
set (CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
else ()
message (FATAL_ERROR "Architecture '${_ARCH}' is not supported.")
endif ()
set (ASM_EXTENSION ".asm")
else ()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -pedantic -fvisibility=hidden")
enable_language (ASM)
if (_ARCH MATCHES "${I386}")
set (BXF_ARCH "i386")
set (BXF_ABI "cdecl")
set (BXF_BITS 32)
set (BXF_ARCH_x86 1)
if (WIN32)
set (BXF_MANGLING "leading-underscore")
endif ()
elseif (_ARCH MATCHES "${AMD64}")
if (WIN32)
set (BXF_ABI "win")
else ()
set (BXF_ABI "systemv")
endif ()
set (BXF_ARCH "x86_64")
set (BXF_BITS 64)
set (BXF_ARCH_x86_64 1)
else ()
message (FATAL_ERROR "Architecture '${_ARCH}' is not supported.")
endif ()
set (ASM_EXTENSION ".S")
endif ()
option (USE_QEMU "Use QEMU to run the tests" OFF)
option (BXF_ARENA_REOPEN_SHM "Reopen the arena shm in child processes (POSIX)" OFF)
option (BXF_ARENA_FILE_BACKED "Arena memory is file-backed (POSIX)" OFF)
option (BXF_STATIC_LIB "Build a static library" OFF)
option (BXF_SAMPLES "Build the library samples" ON)
option (BXF_TESTS "Build the library tests" ON)
option (BXF_FORK_RESILIENCE "Build with fork resilience" ON)
add_definitions(-DBXF_BUILDING_LIB)
# Assume ELF on POSIX initially, override values when needed
set (BXF_EXE_FORMAT elf)
set (BXF_OS_FAMILY posix)
if (CMAKE_SYSTEM_NAME MATCHES "(Linux|(Free|Net|Open)?BSD|Solaris|SunOS)")
add_definitions(-D_XOPEN_SOURCE=600 -D_GNU_SOURCE)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
set (BXF_EXE_FORMAT mach-o)
set (BXF_MANGLING "leading-underscore")
# This is mandatory because OS X fails silently when mmap'ing an inherited
# shm file descriptor
set (BXF_ARENA_REOPEN_SHM 1)
set (BXF_ARENA_FILE_BACKED 1)
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
set (BXF_EXE_FORMAT pe)
set (BXF_OS_FAMILY windows)
add_definitions (-D_CRT_SECURE_NO_WARNINGS)
add_definitions (-DVC_EXTRALEAN)
add_definitions (-DWIN32_LEAN_AND_MEAN)
add_definitions (-D_WIN32_WINNT=0x600)
else ()
message (FATAL_ERROR "Platform '${CMAKE_SYSTEM_NAME}' is not supported.")
endif ()
# Set BXF_EXE_FMT_{format} to 1
string (TOUPPER "${BXF_EXE_FORMAT}" _EXE_SUFFIX)
string (REGEX REPLACE "[^A-Z0-9_]" "_" _EXE_SUFFIX "${_EXE_SUFFIX}")
set ("BXF_EXE_FMT_${_EXE_SUFFIX}" 1)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
if (BXF_EXE_FMT_ELF)
check_symbol_exists(_r_debug link.h HAVE__R_DEBUG)
check_symbol_exists(_DYNAMIC link.h HAVE__DYNAMIC)
endif ()
check_symbol_exists(PR_SET_PDEATHSIG sys/prctl.h HAVE_PR_SET_PDEATHSIG)
check_library_exists(rt shm_open "sys/mman.h" HAVE_SHM)
if (HAVE_SHM AND NOT HAVE_LIBRT)
set (HAVE_LIBRT 1)
endif ()
check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME_RT)
if (HAVE_CLOCK_GETTIME_RT AND NOT HAVE_LIBRT)
set (HAVE_LIBRT 1)
endif ()
if (NOT HAVE_CLOCK_GETTIME_RT)
check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME)
else ()
set (HAVE_CLOCK_GETTIME "${HAVE_CLOCK_GETTIME_RT}" CACHE INTERNAL "Have symbol clock_gettime")
endif ()
check_symbol_exists(CLOCK_MONOTONIC_RAW "time.h" HAVE_CLOCK_MONOTONIC_RAW)
check_symbol_exists(gettimeofday "sys/time.h" HAVE_GETTIMEOFDAY)
check_symbol_exists(environ "unistd.h" HAVE_ENVIRON)
check_library_exists(pthread pthread_cond_timedwait_relative_np
"pthread.h" HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
check_library_exists(m trunc "math.h" HAVE_LIBM)
check_asm_directive_exists(".hidden test" ASSEMBLER_SUPPORTS_HIDDEN)
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
include_directories(include src ${CMAKE_CURRENT_BINARY_DIR}/src)
add_subdirectory (src)
if (BXF_STATIC_LIB)
add_library (boxfort STATIC ${SOURCE_FILES})
add_definitions (-DBXF_STATIC_LIB)
else ()
add_library (boxfort SHARED ${SOURCE_FILES})
endif ()
if (HAVE_LIBRT)
target_link_libraries(boxfort rt)
endif ()
if (HAVE_LIBM)
target_link_libraries(boxfort m)
endif ()
find_package(Threads REQUIRED)
if (THREADS_HAVE_PTHREAD_ARG)
set_property(TARGET boxfort PROPERTY COMPILE_OPTIONS "-pthread")
set_property(TARGET boxfort PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
endif ()
if (CMAKE_THREAD_LIBS_INIT)
target_link_libraries(boxfort "${CMAKE_THREAD_LIBS_INIT}")
endif ()
foreach (F ${INTERFACE_FILES})
get_filename_component(DEST "${F}" PATH)
install(FILES "${F}" DESTINATION "${DEST}" ${install_flags})
endforeach ()
install(TARGETS boxfort
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
add_custom_target(boxfort_tests ALL)
function (add_boxfort_sample _NAME)
add_dependencies (boxfort_tests ${_NAME})
if (USE_QEMU)
add_test (${_NAME} qemu-${BXF_ARCH} -L "${CMAKE_FIND_ROOT_PATH}" ${_NAME})
else ()
add_test (${_NAME} ${_NAME})
endif ()
endfunction ()
function (add_boxfort_test _NAME)
add_executable (${_NAME} EXCLUDE_FROM_ALL ${ARGN})
if (NOT MSVC)
foreach (ARG ${ARGN})
set_source_files_properties (${ARG} PROPERTIES COMPILE_FLAGS -O0)
endforeach ()
endif ()
target_link_libraries (${_NAME} boxfort)
add_boxfort_sample(${_NAME})
endfunction ()
enable_testing ()
if (BXF_TESTS)
add_subdirectory (test)
endif ()
if (BXF_SAMPLES)
add_subdirectory (sample)
endif ()