-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
CMakeLists.txt
267 lines (218 loc) · 8.48 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
#
# Cmake configuration file
#
############################# INITIAL SECTION ##########################
cmake_minimum_required(VERSION 3.20)
project(msd_lite C)
set(PACKAGE_VERSION_MAJOR 1)
set(PACKAGE_VERSION_MINOR 11)
set(PACKAGE_VERSION_PATCH 0)
set(PACKAGE_NAME "${PROJECT_NAME}")
set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}")
set(PACKAGE_URL "https://github.com/rozhuk-im/msd_lite")
set(PACKAGE_BUGREPORT "https://github.com/rozhuk-im/msd_lite")
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
set(PACKAGE_DESCRIPTION "Multi stream daemon")
set(PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
############################# OPTIONS SECTION ##########################
option(ENABLE_TESTS "Enable tests [default: OFF]" OFF)
if (ENABLE_TESTS)
# Enable testing functionality.
enable_testing()
set(ENABLE_LIBLCB_TESTS TRUE)
endif()
# Now CMAKE_INSTALL_PREFIX is a base prefix for everything.
if (NOT CONFDIR)
set(CONFDIR "${CMAKE_INSTALL_PREFIX}/etc/msd_lite")
endif()
if (NOT RUNDIR)
set(RUNDIR "/var/run")
endif()
############################# INCLUDE SECTION ##########################
include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckSymbolExists)
include(CheckCCompilerFlag)
find_library(PTHREAD_LIBRARY pthread)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${PTHREAD_LIBRARY})
############################# MACRO SECTION ############################
macro(try_c_flag prop flag)
# Try flag once on the C compiler
check_c_compiler_flag("-Werror ${flag}" C_FLAG_${prop})
if (C_FLAG_${prop})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
endif()
endmacro()
macro(try_linker_flag prop flag)
# Check with the C compiler
set(CMAKE_REQUIRED_FLAGS ${flag})
check_c_compiler_flag(${flag} LINKER_FLAG_${prop})
set(CMAKE_REQUIRED_FLAGS "")
if (LINKER_FLAG_${prop})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${flag}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${flag}")
endif()
endmacro()
function(install_if_not_exists src dest destname suffix)
if (NOT IS_ABSOLUTE "${src}")
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
endif()
if (NOT IS_ABSOLUTE "${dest}")
set(dest "${CMAKE_INSTALL_PREFIX}/${dest}")
endif()
get_filename_component(src_name "${src}" NAME)
get_filename_component(dest_name "${destname}" NAME)
if (NOT EXISTS "${dest}/${dest_name}${suffix}")
set(src_tmp "${CMAKE_CURRENT_BINARY_DIR}/${dest_name}${suffix}")
configure_file(${src} ${src_tmp} @ONLY)
install(FILES ${src_tmp} DESTINATION ${dest})
endif()
endfunction()
function(install_script src dest)
if (NOT IS_ABSOLUTE "${src}")
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
endif()
if (NOT IS_ABSOLUTE "${dest}")
set(dest "${CMAKE_INSTALL_PREFIX}/${dest}")
endif()
get_filename_component(src_name "${src}" NAME)
set(src_tmp "${CMAKE_CURRENT_BINARY_DIR}/${src_name}")
configure_file(${src} ${src_tmp} @ONLY)
install(PROGRAMS ${src_tmp} DESTINATION ${dest})
endfunction()
function(install_config src dest)
if (NOT IS_ABSOLUTE "${src}")
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
endif()
if (NOT IS_ABSOLUTE "${dest}")
set(dest "${CMAKE_INSTALL_PREFIX}/${dest}")
endif()
get_filename_component(src_name "${src}" NAME)
set(src_tmp "${CMAKE_CURRENT_BINARY_DIR}/${src_name}")
configure_file(${src} ${src_tmp} @ONLY)
install(FILES ${src_tmp} DESTINATION ${dest})
endfunction()
############################# CONFIG SECTION ###########################
# Prefer local include dirs to system ones.
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/"
"${CMAKE_CURRENT_SOURCE_DIR}/src"
"${CMAKE_CURRENT_SOURCE_DIR}/src/liblcb/include"
"${CMAKE_BINARY_DIR}/src")
set(TAR "tar")
# Platform specific configuration.
if (CMAKE_SYSTEM_NAME MATCHES "^.*BSD$|DragonFly")
add_definitions(-D_BSD_SOURCE -DFREEBSD)
message(STATUS "Configuring for BSD system")
include_directories("/usr/local/include/")
set(TAR "gtar")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_definitions(-D_BSD_SOURCE -DDARWIN)
message(STATUS "Configuring for Darwin")
set(TAR "gnutar")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_definitions(-D_GNU_SOURCE -DLINUX -D__USE_GNU=1)
message(STATUS "Configuring for Linux")
if (BUILD_CPU_MODE STREQUAL "32")
add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE)
endif()
list(APPEND CMAKE_REQUIRED_LIBRARIES rt)
endif()
try_c_flag(PIPE "-pipe")
try_c_flag(NO_DEL_NULL_PTR_CHKS "-fno-delete-null-pointer-checks")
if (CMAKE_BUILD_TYPE MATCHES "Debug")
# Process with warn flags.
try_c_flag(W "-W")
try_c_flag(WALL "-Wall")
try_c_flag(WEVERYTHING "-Weverything")
if (NOT C_FLAG_WEVERYTHING)
try_c_flag(WPOINTER "-Wpointer-arith")
try_c_flag(WSTRICT_PROTOTYPES "-Wstrict-prototypes")
try_c_flag(PEDANTIC "-pedantic")
try_c_flag(WNULL_DEREFERENCE "-Wnull-dereference")
try_c_flag(WDUPLICATED_COND "-Wduplicated-cond")
try_c_flag(WIMPLICIT_FALLTHROUGH "-Wimplicit-fallthrough")
endif()
try_c_flag(WCAST_FN_TYPE_STRICT "-Wno-cast-function-type-strict")
try_c_flag(WCAST_QUAL "-Wno-cast-qual")
try_c_flag(WDATE_TIME "-Wno-date-time")
try_c_flag(WDOCUMENTATION "-Wno-documentation")
try_c_flag(WDOC_UNKNOWN_CMD "-Wno-documentation-unknown-command")
try_c_flag(WFORMAT_NONLITERAL "-Wno-format-nonliteral")
try_c_flag(WPACKED "-Wno-packed")
try_c_flag(WPADDED "-Wno-padded")
#try_c_flag(WPOINTER_SIGN "-Wno-pointer-sign")
#try_c_flag(WRESERVED_ID_MACRO "-Wno-reserved-id-macro")
try_c_flag(WRESERVED_IDENTIFIER "-Wno-reserved-identifier")
#try_c_flag(WSIGN_COMPARE "-Wno-sign-compare")
try_c_flag(WSWITCH_ENUM "-Wno-switch-enum")
#try_c_flag(WUNUSED_CONST "-Wno-unused-const-variable")
#try_c_flag(WUNUSED_FUNCTION "-Wno-unused-function")
#try_c_flag(WUNUSED_PARAM "-Wno-unused-parameter")
#try_c_flag(WUNUSED_VAR "-Wno-unused-variable")
try_c_flag(WUNSAFE_BUFFER_USAGE "-Wno-unsafe-buffer-usage")
try_c_flag(WVARIADIC_MACROS "-Wno-variadic-macros")
try_c_flag(WGNU_ZERO_VAR_MACRO_ARGS "-Wno-gnu-zero-variadic-macro-arguments")
try_c_flag(WZERO_LENGTH_ARRAY "-Wno-zero-length-array")
set(CMAKE_INSTALL_DO_STRIP FALSE)
set(CMAKE_C_OPT_FLAGS "-g3 -ggdb -O0")
message(STATUS "Adding -DDEBUG to definitions.")
add_definitions(-DDEBUG)
else()
set(CMAKE_INSTALL_DO_STRIP TRUE)
message(STATUS "Adding -DNDEBUG to definitions.")
add_definitions(-DNDEBUG)
endif()
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode.")
message(STATUS "CMAKE_INSTALL_DO_STRIP is ${CMAKE_INSTALL_DO_STRIP}.")
if (NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
try_c_flag(STD11 "-std=c11")
if (NOT C_FLAG_STD11)
try_c_flag(STD99 "-std=c99")
endif()
endif()
# Hardening flags
try_c_flag(FORTIFY_SOURCE2 "-D_FORTIFY_SOURCE=2")
try_c_flag(FSTACK_PROTECTOR_ALL "-fstack-protector-all")
try_c_flag(FSANITIZE_SAFE_STACK "-fsanitize=safe-stack")
try_c_flag(FSANITIZE_CFI "-fsanitize=cfi")
try_c_flag(MRETPOLINE "-mretpoline")
try_c_flag(MFUNCTION_RETURN "-mfunction-return=thunk")
try_c_flag(MINDIRECT_BRANCH "-mindirect-branch=thunk")
try_c_flag(FWRAPV "-fwrapv")
try_c_flag(FPIE "-fPIE")
if (C_FLAG_FPIE)
try_linker_flag(PIE "-pie")
endif()
try_linker_flag(RETPOLINEPLT "-Wl,-z,retpolineplt")
try_linker_flag(ZRELRO "-Wl,-z,relro")
try_linker_flag(ZNOW "-Wl,-z,now")
try_linker_flag(ZNOEXECSTACK "-Wl,-z,noexecstack")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_OPT_FLAGS}")
# Silently strip whitespace
string(STRIP "${CMAKE_C_FLAGS}" CMAKE_C_FLAGS)
string(STRIP "${CMAKE_EXE_LINKER_FLAGS}" CMAKE_EXE_LINKER_FLAGS)
string(STRIP "${CMAKE_MODULE_LINKER_FLAGS}" CMAKE_MODULE_LINKER_FLAGS)
string(STRIP "${CMAKE_SHARED_LINKER_FLAGS}" CMAKE_SHARED_LINKER_FLAGS)
configure_file(config.h.cmake src/config.h)
add_definitions(-DHAVE_CONFIG_H)
################################ SUBDIRS SECTION #######################
include(src/liblcb/CMakeLists.txt)
add_subdirectory(src)
if (ENABLE_TESTS)
#add_subdirectory(tests)
endif()
############################ TARGETS SECTION ###########################
add_custom_target(dist ${CMAKE_CURRENT_SOURCE_DIR}/dist.sh
"${CMAKE_BINARY_DIR}/${PACKAGE_TARNAME}.tar.xz" "${TAR}"
COMMENT "Create source distribution"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
##################### INSTALLATION #####################################
# Configs
install(CODE "FILE(MAKE_DIRECTORY ${CONFDIR})")
install_if_not_exists(conf/msd_lite.conf ${CONFDIR} "msd_lite.conf" ".sample")
if (CMAKE_SYSTEM_NAME MATCHES "^.*BSD$|DragonFly")
install_script(os/freebsd/msd_lite "${CMAKE_INSTALL_PREFIX}/etc/rc.d/")
endif()