-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCMakeLists.txt
326 lines (275 loc) · 10.2 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
# ------------------------------------------------------- #
# CMakeLists.txt ( NCKU CCNS WindTop-DreamBBS 3.0 ) #
# ------------------------------------------------------- #
# Author: Wei-Cheng Yeh (IID) <[email protected]> #
# Target: CMakeLists for ALL #
# Create: 2019/11/30 #
# ------------------------------------------------------- #
cmake_minimum_required(VERSION 3.1)
# set the project name
project(DreamBBS LANGUAGES C CXX VERSION 3.0)
# For debugging `CMakeLists.txt`
# set(CMAKE_VERBOSE_MAKEFILE ON)
# Mark `CMakeCache.txt` as up-to-date if necessary
set(MARK_CACHE_UPDATED OFF)
# Required cmake modules
include(CheckCCompilerFlag)
include(CheckSymbolExists)
include(FindPkgConfig)
## Toolchain settings
if(NOT USE_CXX)
# specify the C standard
add_compile_options(-x c --std=gnu99)
set(USE_CXX OFF CACHE BOOL "Compile C code using C++ mode")
else()
# specify the C++ standard
add_compile_options(-x c++ --std=gnu++14)
link_libraries(stdc++)
set(USE_CXX ON CACHE BOOL "Compile C code using C++ mode")
endif()
if(DEFINED ENV{CMAKE_BUILD_TYPE})
set(CMAKE_BUILD_TYPE $ENV{CMAKE_BUILD_TYPE})
elseif(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
message(STATUS "Using build configuration: ${CMAKE_BUILD_TYPE}")
add_compile_options(
$<$<CONFIG:Release>:>
"$<$<CONFIG:Debug>:-ggdb3>"
"$<$<CONFIG:RelWithDebInfo>:-ggdb3>"
$<$<CONFIG:MinSizeRel>:>
-pipe
)
add_compile_options(-Wall -Wpointer-arith -Wcast-qual -Wwrite-strings -Werror=format)
include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include)
link_libraries(-L${PROJECT_BINARY_DIR}/lib dao crypt)
## Tool functions
function(GETVALUE conf default hdr output)
set(valueif "#ifdef ${conf}\n${conf}\n#else\n${default}\n#endif")
execute_process(
COMMAND echo ${valueif}
COMMAND ${CMAKE_C_COMPILER} -x c -E -P -imacros "${hdr}" -
COMMAND xargs
OUTPUT_VARIABLE "${output}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
set("${output}" "${${output}}" PARENT_SCOPE)
endfunction(GETVALUE)
function(LOADVALUE conf default hdr output)
if(DEFINED ENV{${conf}})
set("${output}" "$ENV{${conf}}")
elseif(DEFINED "${conf}")
set("${output}" "${${conf}}")
else()
GETVALUE("${conf}" "${default}" "${hdr}" "${output}")
endif()
set("${output}" "${${output}}" PARENT_SCOPE)
endfunction(LOADVALUE)
set(ID_DEFAULT 9999)
function(GETUID name output)
execute_process(
COMMAND sh -c "id -u ${name} || echo ${ID_DEFAULT}"
OUTPUT_VARIABLE "${output}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
set("${output}" "${${output}}" PARENT_SCOPE)
endfunction(GETUID)
function(GETGID name output)
execute_process(
COMMAND getent group ${name}
COMMAND cut -d: -f3 -
OUTPUT_VARIABLE "${output}"
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if ("${${output}}" STREQUAL "")
set("${output}" ${ID_DEFAULT})
endif()
set("${output}" "${${output}}" PARENT_SCOPE)
endfunction(GETGID)
function(DEF_TEST conf hdr output)
if("${hdr}" IS_NEWER_THAN ${CMAKE_CACHEFILE_DIR}/CMakeCache.txt)
set(MARK_CACHE_UPDATED ON)
unset("${output}" CACHE)
check_symbol_exists("${conf}" "${hdr}" "${output}")
endif()
set("${output}" "${${output}}" PARENT_SCOPE)
endfunction(DEF_TEST)
## Read variables from the configuration C files
set(BBSCONF "${PROJECT_SOURCE_DIR}/dreambbs.conf")
set(BBSCONF_ORIGIN "${PROJECT_SOURCE_DIR}/include/config.h")
set(EXPORT_PROJECT "${PROJECT_SOURCE_DIR}/make_export.conf")
set(EXPORT_MAPLE "${PROJECT_SOURCE_DIR}/maple/make_export.conf")
set(EXPORT_VERINFO "${PROJECT_SOURCE_DIR}/verinfo_export.conf")
if(NOT EXISTS ${EXPORT_PROJECT})
execute_process(COMMAND touch ${EXPORT_PROJECT})
endif()
if(NOT EXISTS ${EXPORT_MAPLE})
execute_process(COMMAND touch ${EXPORT_MAPLE})
endif()
# User names and group names
execute_process(COMMAND id -un OUTPUT_VARIABLE BBSUSR_DEFAULT OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND id -gn OUTPUT_VARIABLE BBSGROUP_DEFAULT OUTPUT_STRIP_TRAILING_WHITESPACE)
LOADVALUE(BBSUSR "${BBSUSR_DEFAULT}" ${BBSCONF_ORIGIN} BBSUSR)
LOADVALUE(BBSGROUP "${BBSGROUP_DEFAULT}" ${BBSCONF_ORIGIN} BBSGROUP)
LOADVALUE(WWWGROUP "www-data" ${BBSCONF_ORIGIN} WWWGROUP)
# UIDs and GIDs
GETUID("${BBSUSR}" BBSUID_DEFAULT)
GETGID("${BBSGROUP}" BBSGID_DEFAULT)
GETGID("${WWWGROUP}" WWWGID_DEFAULT)
LOADVALUE(BBSUID "${BBSUID_DEFAULT}" ${BBSCONF_ORIGIN} BBSUID)
LOADVALUE(BBSGID "${BBSGID_DEFAULT}" ${BBSCONF_ORIGIN} BBSGID)
LOADVALUE(WWWGID "${WWWGID_DEFAULT}" ${BBSCONF_ORIGIN} WWWGID)
# BBS path prefixes and suffixes
LOADVALUE(BBSVER_SUFFIX "" ${BBSCONF_ORIGIN} BBSVER)
execute_process(
COMMAND getent passwd ${BBSUSR}
COMMAND cut -d: -f6 -
OUTPUT_VARIABLE BBSUSR_HOME
OUTPUT_STRIP_TRAILING_WHITESPACE
)
LOADVALUE(BBSHOME "${BBSUSR_HOME}" ${BBSCONF_ORIGIN} BBSHOME)
## Numeric local timezone
execute_process(
COMMAND date +%z
OUTPUT_VARIABLE BBSUTCZONE_DEFAULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
LOADVALUE(BBSUTCZONE "${BBSUTCZONE_DEFAULT}" ${BBSCONF_ORIGIN} BBSUTCZONE)
## `mruby-config` executable path for building BBS-Ruby with mruby
execute_process(
COMMAND which mruby-config
OUTPUT_VARIABLE MRB_CONFIG_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
LOADVALUE(MRB_CONFIG "${MRB_CONFIG_PATH}" ${BBSCONF_ORIGIN} MRB_CONFIG)
## The project root directory of bbs-sshd
LOADVALUE(BBS_SSHD_ROOT "${BBSHOME}/bbs-sshd" ${BBSCONF_ORIGIN} BBS_SSHD_ROOT)
# Flags for enabling modules
DEF_TEST(M3_USE_PMORE ${BBSCONF} USE_PMORE)
DEF_TEST(M3_USE_PFTERM ${BBSCONF} USE_PFTERM)
DEF_TEST(M3_USE_BBSLUA ${BBSCONF} USE_BBSLUA)
DEF_TEST(M3_USE_BBSRUBY ${BBSCONF} USE_BBSRUBY)
DEF_TEST(BBSLUA_USE_LUAJIT ${BBSCONF} USE_LUAJIT)
DEF_TEST(BBSRUBY_USE_MRUBY ${BBSCONF} USE_MRUBY)
# Flags for disabling shared objects
if(DEFINED ENV{NO_SO})
set(NO_SO_CLI "$ENV{NO_SO}")
elseif(DEFINED NO_SO)
set(NO_SO_CLI "${NO_SO}")
endif()
if(DEFINED NO_SO_CLI)
set(NO_SO "${NO_SO_CLI}")
else()
LOADVALUE(NO_SO OFF ${EXPORT_MAPLE} NO_SO)
endif()
## Other compile options and link options settings
# Prevent `-Wformat-overflow=` warnings from halting the compilation
set(W_NO_ERROR_FORMAT_OVERFLOW -Wno-format-overflow -Wno-error=format-overflow -Wformat-overflow)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(${W_NO_ERROR_FORMAT_OVERFLOW})
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Wno-invalid-source-encoding)
endif()
if(NOT USE_CXX)
add_compile_options(-Wstrict-prototypes -Werror=incompatible-pointer-types -Werror=int-conversion -Werror=pointer-to-int-cast)
# Prevent `-Wincompatible-pointer-types-discards-qualifiers` warnings from halting the compilation
set(W_NO_INCOMPATIBLE_POINTER_TYPES_DISCARDS_QUALIFIERS -Wno-incompatible-pointer-types-discards-qualifiers -Wno-error=incompatible-pointer-types-discards-qualifiers -Wincompatible-pointer-types-discards-qualifiers)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(${W_NO_INCOMPATIBLE_POINTER_TYPES_DISCARDS_QUALIFIERS})
endif()
endif()
check_c_compiler_flag(-Wunreachable-code-aggressive CC_HAS_W_UNREACHABLE_CODE_AGGRESSIVE)
if(CC_HAS_W_UNREACHABLE_CODE_AGGRESSIVE)
add_compile_options(-Wunreachable-code-aggressive)
else()
add_compile_options(-Wunreachable-code)
endif()
check_c_compiler_flag(-fcolor-diagnostics CC_HAS_F_COLOR_DIAGNOSTICS)
if(CC_HAS_F_COLOR_DIAGNOSTICS)
add_compile_options(-fcolor-diagnostics)
endif()
# Architecture information
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
OUTPUT_VARIABLE MULTIARCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^(.*)-(.*)-(.*)-(.*)$" \\1-\\3-\\4 MULTIARCH ${MULTIARCH})
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
add_compile_options(-fPIC)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
link_libraries(resolv ${CMAKE_DL_LIBS} -rdynamic)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
link_libraries(-Wl,-export-dynamic)
endif()
# BBS-Lua & BBS-Ruby make rule definitions
if(USE_BBSLUA)
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
link_libraries(-Wl,--no-as-needed)
endif()
if(USE_LUAJIT)
pkg_search_module(LUA REQUIRED luajit)
else()
# Prevent LuaJIT from being chosen
pkg_search_module(LUA REQUIRED lua-5.1 lua5.1 luajit<-1)
endif()
endif()
if(USE_BBSRUBY)
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
link_libraries(-Wl,--no-as-needed)
endif()
if(USE_MRUBY)
execute_process(
COMMAND ${MRB_CONFIG} --cflags
OUTPUT_VARIABLE MRB_CFLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
separate_arguments(MRB_CFLAGS)
execute_process(
COMMAND ${MRB_CONFIG} --ldflags
OUTPUT_VARIABLE MRB_LDFLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
separate_arguments(MRB_LDFLAGS)
set(RUBY_CFLAGS ${MRB_CFLAGS})
set(RUBY_LDFLAGS ${MRB_LDFLAGS} mruby m)
else()
pkg_search_module(RUBY REQUIRED ruby<=2.2 ruby-2.2)
endif()
endif()
## Generate configure file
function(EXPORT_CONF export_file)
execute_process(COMMAND printf "\\033[1;36mGenerating '${export_file}'...\\033[0m\n")
configure_file(
${export_file}.in ${export_file}
NEWLINE_STYLE LF
)
file(READ ${export_file} EXPORT_FILE_CONTENT)
execute_process(COMMAND printf "\\033[1;33m${EXPORT_FILE_CONTENT}\\033[0m")
endfunction()
EXPORT_CONF(${CMAKE_CURRENT_SOURCE_DIR}/make_export.conf)
add_custom_target(verinfo
ALL
sh scripts/verinfo.sh CMake ${MULTIARCH}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
add_compile_options(-imacros "${EXPORT_VERINFO}")
set(CMAKE_INSTALL_PREFIX ${BBSHOME})
add_subdirectory(lib)
add_subdirectory(maple)
add_subdirectory(util)
add_subdirectory(innbbsd)
add_subdirectory(so)
add_subdirectory(scripts)
add_subdirectory(sample)
enable_testing()
add_subdirectory(test)
# Mark the cache file as up-to-date
if(MARK_CACHE_UPDATED AND CMAKE_CACHEFILE_DIR)
execute_process(COMMAND touch ${CMAKE_CACHEFILE_DIR}/CMakeCache.txt)
endif()