-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
365 lines (287 loc) · 10.7 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
358
359
360
361
362
363
364
365
cmake_minimum_required(VERSION 3.5)
cmake_policy(SET CMP0091 NEW) # for CMAKE_MSVC_RUNTIME_LIBRARY
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) # cmake_policy(SET CMP0077 NEW)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
project(httpd)
################################################################################
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/cmake")
include(CheckCXXCompilerFlag)
include(CheckLibraryExists)
include(CheckCXXLinkerFlag)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
################################################################################
if (ENABLE_GIT_VERSION)
find_package(Git)
gitGetVersion(${CMAKE_CURRENT_SOURCE_DIR} httpd)
set(VERSION_GIT ${httpd_WC_REVISION_HASH})
string(TIMESTAMP PVERSION "%Y%m%d-${VERSION_GIT}-${CMAKE_SYSTEM_NAME}")
endif()
# Disable in-source builds to prevent source tree corruption.
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "
FATAL: In-source builds are not allowed.
You should create a separate directory for build files.
")
endif()
message(STATUS "CMAKE Version: ${CMAKE_VERSION}")
message(STATUS "Source Dir: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "Host System name: ${CMAKE_HOST_SYSTEM_NAME}")
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
message(STATUS "System name: ${CMAKE_SYSTEM_NAME}")
message(STATUS "System version: ${CMAKE_SYSTEM_VERSION}")
if (WIN32)
message(STATUS "Platform version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "SYSTEM NAME: ${CMAKE_SYSTEM_NAME}")
################################################################################
option(ENABLE_BUILD_WERROR "All warnings being treated as errors" ON)
option(ENABLE_GIT_VERSION "Enable git version" ON)
option(ENABLE_USE_IO_URING "Enable auto use io_uring" OFF)
option(ENABLE_BUILD_WITH_LIBCXX "Build with libc++" OFF)
option(ENABLE_SYSTEM_ZLIB "Build with system zlib support" OFF)
option(ENABLE_SYSTEM_OPENSSL "Build with system openssl support" OFF)
option(ENABLE_MOLD "build using mold" OFF)
option(ENABLE_LLD "build using lld" OFF)
option(ENABLE_TCMALLOC_STATIC "Build with Tcmalloc support" OFF)
option(ENABLE_JEMALLOC_STATIC "Build with Jemalloc support" OFF)
################################################################################
find_program(MOLD_LINKER mold)
find_program(LLD_LINKER lld)
set(CMAKE_C_STANDARD 11)
################################################################################
if (MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_definitions(
-D_CRT_SECURE_NO_DEPRECATE
-D_CRT_SECURE_NO_WARNINGS
-D_CRT_NONSTDC_NO_DEPRECATE
-D_CRT_NONSTDC_NO_WARNINGS
-D_SCL_SECURE_NO_DEPRECATE
-D_SCL_SECURE_NO_WARNINGS
)
add_definitions(
-DWIN32_LEAN_AND_MEAN
-D_WIN32_WINNT=0x0601
-DNOMINMAX
-DUNICODE
-D_UNICODE
-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
)
add_definitions(
-DBOOST_ALL_STATIC_LINK
-DBOOST_THREAD_USE_LIB
-DBOOST_FILESYSTEM_STATIC_LINK
-DBOOST_USE_WINAPI_VERSION=0x0601
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /MP /Zc:__cplusplus")
message(STATUS "Using parallel compiling (/MP)")
set(CMAKE_CXX_STACK_SIZE "100000000") #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:100000000")
message(STATUS "Set stack size: 100000000 bytes")
CHECK_CXX_COMPILER_FLAG(/std:c++20 COMPILER_HAS_STD_CXX20)
if (COMPILER_HAS_STD_CXX20)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Set default cxx standard: C++20")
elseif (MSVC_VERSION GREATER_EQUAL "1900")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Set default cxx standard: C++17")
endif()
elseif(WIN32)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_definitions(
-DWIN32_LEAN_AND_MEAN
-D_WIN32_WINNT=0x0601
-DNOMINMAX
-DUNICODE
-D_UNICODE
-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
)
add_definitions(
-DBOOST_ALL_STATIC_LINK
-DBOOST_THREAD_USE_LIB
-DBOOST_FILESYSTEM_STATIC_LINK
-DBOOST_USE_WINAPI_VERSION=0x0601
)
if (NOT MINGW)
add_compile_options(/utf8)
endif()
endif()
################################################################################
CHECK_CXX_COMPILER_FLAG(-fvisibility-inlines-hidden COMPILER_HAS_VISIBILITY_INLINE_HIDDEN)
CHECK_CXX_COMPILER_FLAG(-fvisibility=hidden COMPILER_HAS_VISIBILITY_HIDDEN)
CHECK_CXX_COMPILER_FLAG(-fdiagnostics-color=always COMPILER_HAS_COLOR)
CHECK_CXX_COMPILER_FLAG(-fcoroutines COMPILER_HAS_FCOROUTINES)
CHECK_CXX_COMPILER_FLAG(-fcoroutines-ts COMPILER_HAS_FCOROUTINES_TS)
CHECK_CXX_COMPILER_FLAG(-std=c++20 COMPILER_HAS_STD_CXX20)
if (${COMPILER_HAS_VISIBILITY_INLINE_HIDDEN})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
endif()
if (${COMPILER_HAS_VISIBILITY_HIDDEN})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
endif()
if (${COMPILER_HAS_COLOR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
endif()
if (COMPILER_HAS_FCOROUTINES)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines")
endif()
if (ENABLE_SYSTEMD_LOGGING)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_SYSTEMD_LOGGING")
endif()
################################################################################
if (NOT MSVC)
if(NOT APPLE)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (ENABLE_BUILD_WITH_LIBCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
CHECK_LINKER_FLAG(CXX "-rtlib=compiler-rt" IS_COMPILER_RT_SUPPORTED)
if (IS_COMPILER_RT_SUPPORTED)
message(STATUS "clang supports compiler-rt, use it")
add_link_options(-rtlib=compiler-rt -unwindlib=libunwind)
endif()
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (MINGW)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libstdc++ -static-libgcc")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif()
endif()
endif()
if (ANDROID)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
endif()
if(COMPILER_HAS_STD_CXX20)
set(CMAKE_CXX_STANDARD 20)
add_definitions(-DUSE_STD_STRING_VIEW)
message(STATUS "Set default cxx standard: C++20")
else()
message(FATAL_ERROR "need at least GCC 11 or clang 14")
endif()
if (ENABLE_MOLD)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (MOLD_LINKER)
list(APPEND CMAKE_EXE_LINKER_FLAGS "-fuse-ld=mold")
endif()
endif()
endif()
if (ENABLE_LLD)
if (LLD_LINKER)
list(APPEND CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld")
endif()
endif()
endif(NOT MSVC)
find_package(Threads)
link_libraries(${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})
################################################################################
if (ENABLE_SYSTEM_OPENSSL)
find_package(OpenSSL)
else()
set(OpenSSL_FOUND TRUE)
add_subdirectory(third_party/openssl)
if (NOT MSVC AND NOT APPLE)
if (NOT (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--exclude-libs,ALL ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--exclude-libs,ALL ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--exclude-libs,ALL ${CMAKE_EXE_LINKER_FLAGS}")
endif()
endif()
endif()
include_directories(${OPENSSL_INCLUDE_DIR})
################################################################################
set(BUILD_ASIO_BEAST ON)
set(USE_BOOST_FILESYSTEM ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost)
add_definitions(-DBOOST_ASIO_SEPARATE_COMPILATION)
add_definitions(-DBOOST_BEAST_SEPARATE_COMPILATION)
add_definitions(-DBOOST_LOCALE_HIDE_AUTO_PTR)
add_definitions(-DBOOST_BIND_GLOBAL_PLACEHOLDERS)
add_definitions(-DBOOST_DISABLE_PRAGMA_MESSAGE)
add_definitions(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)
if (ENABLE_USE_IO_URING)
find_package(IOUring)
if (IOUring_FOUND)
message(STATUS "Linux using io_uring...")
add_definitions(-DBOOST_ASIO_HAS_IO_URING -DBOOST_ASIO_DISABLE_EPOLL)
link_libraries(${IOUring_LIBRARIES})
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_definitions(-DBOOST_ASIO_HAS_CO_AWAIT)
endif()
add_subdirectory(third_party/boost EXCLUDE_FROM_ALL)
################################################################################
if (ENABLE_TCMALLOC_STATIC)
find_package(Tcmalloc)
link_libraries(${Tcmalloc_LIBRARY})
endif()
if (ENABLE_JEMALLOC_STATIC)
find_package(JeMalloc)
link_libraries(${JEMALLOC_LIBRARIES})
endif()
if (WIN32 OR NOT ENABLE_SYSTEM_ZLIB)
add_subdirectory(third_party/zlib EXCLUDE_FROM_ALL)
set(ZLIB_LIBRARIES zlibstatic)
set(ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib ${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib/contrib/minizip)
else()
find_package(ZLIB REQUIRED)
endif()
################################################################################
add_subdirectory(third_party/fmt)
set(FMT_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt/include)
################################################################################
include_directories(third_party
${ZLIB_INCLUDE_DIRS}
${FMT_INCLUDE_DIRS}
)
################################################################################
link_libraries(
Boost::thread
Boost::date_time
Boost::program_options
Boost::filesystem
fmt
${ZLIB_LIBRARIES}
)
################################################################################
if (WIN32)
if (MINGW)
link_libraries(
ws2_32
mswsock
bcrypt
)
else()
link_libraries(
Secur32.lib
Bcrypt.lib
Winmm.lib
Mswsock.lib
)
endif()
endif()
################################################################################
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if (WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin/release)
endif()
################################################################################
add_subdirectory(httpd)
################################################################################
configure_file(httpd.service.in httpd.service)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/httpd.service DESTINATION /usr/lib/systemd/system/)