forked from monkey/monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
357 lines (306 loc) · 11 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Let's have fun!
cmake_minimum_required(VERSION 2.8)
project(monkey C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
set(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
# CMake includes
include(CheckSymbolExists)
include(CheckLibraryExists)
include(CheckIncludeFile)
include(ExternalProject)
include(GNUInstallDirs)
# Set default compiler options
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
# Monkey Version
set(MK_VERSION_MAJOR 1)
set(MK_VERSION_MINOR 7)
set(MK_VERSION_PATCH 0)
set(MK_VERSION_STR "${MK_VERSION_MAJOR}.${MK_VERSION_MINOR}.${MK_VERSION_PATCH}")
# ============================================
# ============= BUILD OPTIONS ================
# ============================================
# Project
option(BUILD_LOCAL "Build locally, no install" No)
# Monkey Core
option(WITH_DEBUG "Build with debug symbols" No)
option(WITH_ACCEPT "Use accept(2) system call" No)
option(WITH_ACCEPT4 "Use accept4(2) system call" Yes)
option(WITH_LINUX_KQUEUE "Use Linux kqueue emulator" No)
option(WITH_TRACE "Enable Trace mode" No)
option(WITH_UCLIB "Enable uClib libc support" No)
option(WITH_MUSL "Enable Musl libc support" No)
option(WITH_BACKTRACE "Enable Backtrace feature" Yes)
option(WITH_LINUX_TRACE "Enable Lttng support" No)
option(WITH_PTHREAD_TLS "Use old Pthread TLS mode" No)
option(WITH_SYSTEM_MALLOC "Use system memory allocator" No)
option(WITH_MBEDTLS_SHARED "Use mbedtls shared lib" No)
# Plugins: what should be build ?, these options
# will be processed later on the plugins/CMakeLists.txt file
option(WITH_PLUGIN_AUTH "Basic authentication" Yes)
option(WITH_PLUGIN_CGI "CGI support" Yes)
option(WITH_PLUGIN_CHEETAH "Cheetah Shell Interface" Yes)
option(WITH_PLUGIN_DIRLISTING "Directory Listing" Yes)
option(WITH_PLUGIN_FASTCGI "FastCGI" Yes)
option(WITH_PLUGIN_LIANA "Basic network layer" Yes)
option(WITH_PLUGIN_LOGGER "Log Writer" Yes)
option(WITH_PLUGIN_MANDRIL "Security" Yes)
option(WITH_PLUGIN_TLS "TLS/SSL support" No)
# Options to build Monkey with/without binary and
# static/dynamic library modes (default is always just
# one target binary).
option(WITHOUT_BIN "Do not build binary" No)
option(WITHOUT_CONF "Skip configuration files" No)
option(WITH_STATIC_LIB_MODE "Static library mode" No)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(WITH_ACCEPT 1)
set(WITH_ACCEPT4 0)
set(WITH_SYSTEM_MALLOC 1)
endif()
# This variable allows to define a list of plugins that must
# be included when building the project. The value are the plugins
# names separated by a colon, e.g: -DWITH_PLUGINS=cgi,mbedtls
if(WITH_PLUGINS)
string(REPLACE "," ";" plugins ${WITH_PLUGINS})
foreach(name ${plugins})
message(STATUS "Plugin force enable: ${name}")
string(TOUPPER ${name} NAME)
set(WITH_PLUGIN_${NAME} 1)
endforeach()
endif()
if(WITHOUT_PLUGINS)
string(REPLACE "," ";" plugins ${WITHOUT_PLUGINS})
foreach(name ${plugins})
message(STATUS "Plugin force disable: ${name}")
string(TOUPPER ${name} NAME)
set(WITH_PLUGIN_${NAME} 0)
endforeach()
endif()
if(STATIC_PLUGINS)
set(STATIC_PLUGINS "${STATIC_PLUGINS},liana")
else()
set(STATIC_PLUGINS "liana")
endif()
# Variable to be populated by plugins/CMakeLists.txt. It will contain the
# code required to initialize any static plugin.
set(STATIC_PLUGINS_INIT "")
set(STATIC_PLUGINS_LIBS "")
# ===========================================
# ============== DEPENDENCIES ===============
# ===========================================
# Find pthreads
find_package(Threads)
if(WITH_DEBUG)
set(CMAKE_BUILD_TYPE "Debug")
endif()
# It set's a definition and register into the mk_info.h file */
macro(MK_DEFINITION var)
add_definitions(-D${var})
set(MK_BUILD_FLAGS "${MK_BUILD_FLAGS}#ifndef ${var}\n#define ${var}\n#endif\n")
endmacro()
# Check for accept(2) v/s accept(4)
if(WITH_ACCEPT)
set(WITH_ACCEPT4 No)
MK_DEFINITION(ACCEPT_GENERIC)
elseif(WITH_ACCEPT4)
# accept(4) requires _GNU_SOURCE defined
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(accept4 sys/socket.h HAVE_ACCEPT4)
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
# check the results
if(NOT HAVE_ACCEPT4)
# switch back to accept(2)
set(WITH_ACCEPT Yes)
endif()
endif()
# Check for Linux Kqueue library emulator
if(WITH_LINUX_KQUEUE)
find_package(Libkqueue REQUIRED)
if(NOT LIBKQUEUE_FOUND)
message(FATAL_ERROR "Linux libkqueue was not found." )
else()
MK_DEFINITION(LINUX_KQUEUE)
endif()
endif()
# Check Trace
if(WITH_TRACE)
MK_DEFINITION(TRACE)
endif()
# Check Uclib library
if(WITH_UCLIB)
MK_DEFINITION(UCLIB_MODE)
endif()
# Check Musl library
if(WITH_MUSL)
MK_DEFINITION(MUSL_MODE)
endif()
# Check Backtrace support
if(WITH_BACKTRACE)
check_include_file("execinfo.h" HAVE_BACKTRACE)
if (NOT HAVE_BACKTRACE)
set(WITH_BACKTRACE No)
else()
MK_DEFINITION(HAVE_BACKTRACE)
endif()
else()
MK_DEFINITION(NO_BACKTRACE)
endif()
# Check for LTTng-UST
if(WITH_LINUX_TRACE)
check_include_file("lttng/tracepoint.h" HAVE_LTTNG)
if (NOT HAVE_LTTNG)
message(FATAL_ERROR "LTTng-UST is not installed in your system." )
else()
MK_DEFINITION(LINUX_TRACE)
endif()
endif()
# Use old Pthread TLS
if(WITH_PTHREAD_TLS)
MK_DEFINITION(-DPTHREAD_TLS)
endif()
# Use system memory allocator instead of Jemalloc
if(WITH_SYSTEM_MALLOC)
MK_DEFINITION(MALLOC_LIBC)
else()
# Prepare the Jemalloc build
MK_DEFINITION(MALLOC_JEMALLOC)
MK_DEFINITION(JEMALLOC_MANGLE)
# Link to Jemalloc as an external dependency
ExternalProject_Add(jemalloc
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/jemalloc
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/deps/jemalloc/configure --with-jemalloc-prefix=je_ --enable-cc-silence --prefix=<INSTALL_DIR>
CFLAGS=-std=gnu99\ -Wall\ -pipe\ -g3\ -O3\ -funroll-loops
BUILD_COMMAND ${MAKE}
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/
INSTALL_COMMAND $(MAKE) install_lib_static install_include)
add_library(libjemalloc STATIC IMPORTED GLOBAL)
set_target_properties(libjemalloc PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lib/libjemalloc_pic.a")
add_dependencies(libjemalloc jemalloc)
include_directories("${CMAKE_CURRENT_BINARY_DIR}/include/")
endif()
# ============================================
# =========== CONFIGURATION FILES=============
# ============================================
# Default values for conf/monkey.conf
set(MK_CONF_LISTEN "2001")
set(MK_CONF_WORKERS "0")
set(MK_CONF_TIMEOUT "15")
set(MK_CONF_PIDFILE "monkey.pid")
set(MK_CONF_USERDIR "public_html")
set(MK_CONF_INDEXFILE "index.html index.htm index.php")
set(MK_CONF_HIDEVERSION "Off")
set(MK_CONF_RESUME "On")
set(MK_CONF_USER "www-data")
set(MK_CONF_KA "On")
set(MK_CONF_KA_TIMEOUT "5")
set(MK_CONF_KA_MAXREQ "1000")
set(MK_CONF_REQ_SIZE "32")
set(MK_CONF_SYMLINK "Off")
set(MK_CONF_DEFAULT_MIME "text/plain")
set(MK_CONF_FDT "On")
set(MK_CONF_OVERCAPACITY "Resist")
# Default values for conf/sites/default
set(MK_VH_SERVERNAME "127.0.0.1")
set(MK_VH_DOCUMENT_ROOT MK_DATADIR)
set(MK_VH_LOG_ACCESS MK_LOGDIR)
set(MK_VH_LOG_ERROR MK_LOGDIR)
# Paths
if(APPLE)
set(CMAKE_MACOSX_RPATH ${CMAKE_MACOSX_RPATH};${CMAKE_INSTALL_FULL_LIBDIR}/monkey)
endif()
if(DEFAULT_PORT)
set(MK_CONF_LISTEN ${DEFAULT_PORT})
endif()
if(DEFAULT_USER)
set(MK_CONF_USER ${DEFAULT_USER})
endif()
if(BUILD_LOCAL)
# This mode aims to be backward compatible with older versions of Monkey where
# a './configure && make' were enough to have the server running without installing
# any component.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib/monkey")
set(MK_PATH_CONF "${CMAKE_CURRENT_BINARY_DIR}/conf/")
set(MK_PATH_PIDPATH "${CMAKE_CURRENT_BINARY_DIR}")
set(MK_PIDFILE "${MK_CONF_PIDFILE}")
set(MK_PATH_WWW "${CMAKE_CURRENT_SOURCE_DIR}/htdocs/")
set(MK_PATH_LOG "${CMAKE_CURRENT_BINARY_DIR}/log/")
file(MAKE_DIRECTORY ${MK_PATH_LOG})
else()
# Custom SYSCONFDIR
if(NOT INSTALL_SYSCONFDIR)
set(MK_PATH_CONF ${CMAKE_INSTALL_FULL_SYSCONFDIR}/monkey/ CACHE STRING "Server configuration")
else()
set(MK_PATH_CONF ${INSTALL_SYSCONFDIR}/ CACHE STRING "Server configuration")
endif()
# Custom LOGDIR
if(NOT INSTALL_LOGDIR)
set(MK_PATH_LOG ${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/log/monkey CACHE STRING "Server logs")
else()
set(MK_PATH_LOG ${INSTALL_LOGDIR} CACHE STRING "Server logs")
endif()
# Custom PIDPATH
if(NOT PID_PATH)
set(MK_PATH_PIDPATH ${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/run/ CACHE STRING "Server PID path")
else()
set(MK_PATH_PIDPATH ${PID_PATH} CACHE STRING "Server PID path")
endif()
# Custom PIDFILE
if(NOT PID_FILE)
set(MK_PIDFILE ${MK_CONF_PIDFILE} CACHE STRING "Server pid file name")
else()
set(MK_PIDFILE ${PID_FILE} CACHE STRING "Server pid file name")
endif()
# Custom WEBROOT
if(NOT INSTALL_WEBROOTDIR)
set(MK_PATH_WWW ${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/www/monkey CACHE STRING "Server Web documents")
else()
set(MK_PATH_WWW ${INSTALL_WEBROOTDIR} CACHE STRING "Server Web documents")
endif()
# Headers
if(NOT INSTALL_INCLUDEDIR)
set(MK_PATH_HEADERS ${CMAKE_INSTALL_INCLUDEDIR}/monkey CACHE STRING "Server header files (development)")
else()
set(MK_PATH_HEADERS ${INSTALL_INCLUDEDIR} CACHE STRING "Server header files (development)")
endif()
endif()
configure_file(
"${PROJECT_SOURCE_DIR}/include/monkey/mk_info.h.in"
"${PROJECT_SOURCE_DIR}/include/monkey/mk_info.h"
)
configure_file(
"${PROJECT_SOURCE_DIR}/include/monkey/mk_env.h.in"
"${PROJECT_SOURCE_DIR}/include/monkey/mk_env.h"
)
# General Headers
include_directories(./)
include_directories(include)
include_directories(mk_core/include)
include_directories(monkey)
# Instruct CMake to build the the code base
# =========================================
# mk_core : generic utilities
# plugins : plugins for mk_server
# mk_server: server code base: plugins, protocols, scheduler.. (no executable)
# mk_bin : server executable
add_subdirectory(man)
add_subdirectory(mk_core)
add_subdirectory(plugins)
add_subdirectory(mk_server)
if(NOT WITHOUT_BIN)
add_subdirectory(mk_bin)
endif()
# Configuration, headers generation and others
if(NOT WITHOUT_CONF)
add_subdirectory(conf)
endif()
add_subdirectory(htdocs)
add_subdirectory(include)
# Install (missings ?) paths
install(DIRECTORY DESTINATION ${MK_PATH_LOG})
install(DIRECTORY DESTINATION ${MK_PATH_PIDPATH})
install(DIRECTORY DESTINATION ${MK_PATH_WWW})
if(NOT SKIP_EMPTY_DIRS)
install(DIRECTORY DESTINATION ${MK_PATH_PIDPATH})
install(DIRECTORY DESTINATION ${MK_PATH_LOG})
endif()