forked from opensensor/lightNVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
267 lines (230 loc) · 8.98 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_minimum_required(VERSION 3.10)
project(LightNVR VERSION 0.9.5 LANGUAGES C CXX)
# Set C/C++ standards
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Option to enable/disable SOD
option(ENABLE_SOD "Enable SOD library for object detection" ON)
# SSL/TLS options
option(ENABLE_SSL "Enable SSL/TLS support" OFF)
option(USE_MBEDTLS "Use mbedTLS instead of OpenSSL (if SSL is enabled)" OFF)
option(USE_WOLFSSL "Use WolfSSL instead of OpenSSL (if SSL is enabled)" OFF)
# Define TLS constants (from mongoose.h)
set(MG_TLS_NONE 0)
set(MG_TLS_MBED 1)
set(MG_TLS_OPENSSL 2)
set(MG_TLS_BUILTIN 3)
set(MG_TLS_WOLFSSL 5)
# Compiler flags for optimization and memory usage
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -ffunction-sections -fdata-sections -Wl,--gc-sections -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -ffunction-sections -fdata-sections -Wl,--gc-sections -pthread")
# Option to build for embedded A1 device
option(EMBEDDED_A1_DEVICE "Build for embedded A1 device with limited memory" OFF)
if(EMBEDDED_A1_DEVICE)
message(STATUS "Building for embedded A1 device with memory optimizations")
add_definitions(-DEMBEDDED_A1_DEVICE)
# Additional optimizations for embedded devices
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -fno-exceptions -fomit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -fno-exceptions -fomit-frame-pointer")
endif()
# Create project-specific include directory
set(LIGHTNVR_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
# Find required packages
find_package(PkgConfig REQUIRED)
pkg_check_modules(FFMPEG REQUIRED libavcodec libavformat libavutil libswscale)
pkg_check_modules(SQLITE REQUIRED sqlite3)
pkg_check_modules(CURL REQUIRED libcurl)
# SSL/TLS configuration for Mongoose
if(ENABLE_SSL)
if(USE_MBEDTLS)
add_definitions(-DMG_TLS=${MG_TLS_MBED})
pkg_check_modules(MBEDTLS REQUIRED mbedtls mbedcrypto mbedx509)
set(SSL_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIRS})
set(SSL_LIBRARIES ${MBEDTLS_LIBRARIES})
message(STATUS "Using mbedTLS for SSL/TLS support")
elseif(USE_WOLFSSL)
add_definitions(-DMG_TLS=${MG_TLS_WOLFSSL})
pkg_check_modules(WOLFSSL REQUIRED wolfssl)
set(SSL_INCLUDE_DIRS ${WOLFSSL_INCLUDE_DIRS})
set(SSL_LIBRARIES ${WOLFSSL_LIBRARIES})
message(STATUS "Using WolfSSL for SSL/TLS support")
else()
add_definitions(-DMG_TLS=${MG_TLS_OPENSSL})
pkg_check_modules(OPENSSL REQUIRED openssl)
set(SSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS})
set(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
message(STATUS "Using OpenSSL for SSL/TLS support")
endif()
else()
add_definitions(-DMG_TLS=${MG_TLS_NONE})
message(STATUS "SSL/TLS support is disabled for Mongoose")
# Define empty variables for SSL
set(SSL_INCLUDE_DIRS "")
set(SSL_LIBRARIES "")
endif()
# ONVIF requires mbedTLS for SHA1 and Base64 functions
pkg_check_modules(MBEDTLS REQUIRED mbedtls mbedcrypto mbedx509)
list(APPEND SSL_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIRS})
list(APPEND SSL_LIBRARIES ${MBEDTLS_LIBRARIES})
message(STATUS "ONVIF support enabled, linking with mbedTLS for SHA1 and Base64 functions")
# Mongoose is required for web server implementation
add_definitions(-DUSE_MONGOOSE)
# Find cJSON
find_package(cJSON QUIET)
if(NOT cJSON_FOUND)
# If not found via find_package, try pkg-config
pkg_check_modules(CJSON cjson)
if(NOT CJSON_FOUND)
message(STATUS "cJSON not found via find_package or pkg-config, will use bundled version")
set(CJSON_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/cjson")
set(CJSON_LIBRARIES "cjson")
set(CJSON_BUNDLED TRUE)
endif()
endif()
# Set up SOD library if enabled
if(ENABLE_SOD)
add_subdirectory(src/sod)
add_definitions(-DSOD_ENABLED)
set(SOD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/sod")
endif()
# Set up Mongoose (required component)
# Check if Mongoose is already in external directory, if not download it
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose/mongoose.c")
message(STATUS "Mongoose not found, will download it")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose")
file(DOWNLOAD
"https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c"
"${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose/mongoose.c"
SHOW_PROGRESS
)
file(DOWNLOAD
"https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h"
"${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose/mongoose.h"
SHOW_PROGRESS
)
endif()
set(MONGOOSE_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose")
set(MONGOOSE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/external/mongoose/mongoose.c")
# Set up cJSON if using bundled version
if(CJSON_BUNDLED)
# Check if cJSON is already in external directory, if not download it
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/external/cjson/cJSON.c")
message(STATUS "cJSON not found, will download it")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/external/cjson")
file(DOWNLOAD
"https://raw.githubusercontent.com/DaveGamble/cJSON/master/cJSON.c"
"${CMAKE_CURRENT_SOURCE_DIR}/external/cjson/cJSON.c"
SHOW_PROGRESS
)
file(DOWNLOAD
"https://raw.githubusercontent.com/DaveGamble/cJSON/master/cJSON.h"
"${CMAKE_CURRENT_SOURCE_DIR}/external/cjson/cJSON.h"
SHOW_PROGRESS
)
endif()
set(CJSON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/external/cjson/cJSON.c")
endif()
# Set up inih library
set(INIH_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/inih")
# Define all include directories in one place
set(LIGHTNVR_INCLUDE_DIRS
${LIGHTNVR_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${FFMPEG_INCLUDE_DIRS}
${SQLITE_INCLUDE_DIRS}
${CURL_INCLUDE_DIRS}
${SSL_INCLUDE_DIRS}
${EZXML_INCLUDE_DIR}
${INIH_INCLUDE_DIR}
)
# Add conditional include directories
if(ENABLE_SOD)
list(APPEND LIGHTNVR_INCLUDE_DIRS ${SOD_INCLUDE_DIR})
endif()
# Mongoose is required, always include it
list(APPEND LIGHTNVR_INCLUDE_DIRS ${MONGOOSE_INCLUDE_DIR})
if(CJSON_FOUND AND NOT CJSON_BUNDLED)
list(APPEND LIGHTNVR_INCLUDE_DIRS ${CJSON_INCLUDE_DIRS})
else()
list(APPEND LIGHTNVR_INCLUDE_DIRS ${CJSON_INCLUDE_DIR})
endif()
# Apply include directories
include_directories(${LIGHTNVR_INCLUDE_DIRS})
# Define source files, excluding SOD sources which are built separately
file(GLOB_RECURSE CORE_SOURCES "src/core/*.c")
file(GLOB_RECURSE DATABASE_SOURCES "src/database/*.c")
file(GLOB_RECURSE STORAGE_SOURCES "src/storage/*.c")
file(GLOB_RECURSE UTILS_SOURCES "src/utils/*.c")
file(GLOB_RECURSE WEB_SOURCES "src/web/*.c")
file(GLOB_RECURSE ROOT_SOURCES "src/*.c")
# Explicitly list video sources to exclude motion_detection_optimized.c
file(GLOB VIDEO_SOURCES "src/video/*.c")
list(FILTER VIDEO_SOURCES EXCLUDE REGEX ".*motion_detection_optimized\\.c$")
# Add HLS sources
file(GLOB HLS_SOURCES "src/video/hls/*.c")
# Combine all sources
set(SOURCES
${CORE_SOURCES}
${DATABASE_SOURCES}
${STORAGE_SOURCES}
${UTILS_SOURCES}
${VIDEO_SOURCES}
${HLS_SOURCES}
${WEB_SOURCES}
${ROOT_SOURCES}
"${INIH_INCLUDE_DIR}/ini.c"
)
# Add Mongoose source (required component)
list(APPEND SOURCES ${MONGOOSE_SOURCES})
# Add ezxml source
list(APPEND SOURCES ${EZXML_SOURCES})
# Add cJSON source if using bundled version
if(CJSON_BUNDLED)
list(APPEND SOURCES ${CJSON_SOURCES})
endif()
# Define the executable
add_executable(lightnvr ${SOURCES})
# Link libraries
target_link_libraries(lightnvr
${FFMPEG_LIBRARIES}
${SQLITE_LIBRARIES}
${CURL_LIBRARIES}
${SSL_LIBRARIES}
pthread
dl
m
)
# Link cJSON if not using bundled version
if(NOT CJSON_BUNDLED AND CJSON_FOUND)
target_link_libraries(lightnvr ${CJSON_LIBRARIES})
endif()
# Link SOD library if enabled
if(ENABLE_SOD)
target_link_libraries(lightnvr sod)
endif()
# Install targets
install(TARGETS lightnvr DESTINATION bin)
install(DIRECTORY config/ DESTINATION /etc/lightnvr)
# Add subdirectories for tests if testing is enabled
option(BUILD_TESTS "Build the test suite" OFF)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
# Output binary to a 'bin' directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Create a version.h file
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/core/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/include/core/version.h
)
# Print build information
message(STATUS "Building LightNVR ${PROJECT_VERSION} with the following configuration:")
message(STATUS "- SOD object detection: ${ENABLE_SOD}")
message(STATUS "- Embedded A1 device optimizations: ${EMBEDDED_A1_DEVICE}")
message(STATUS "- Include directories:")
foreach(dir ${LIGHTNVR_INCLUDE_DIRS})
message(STATUS " * ${dir}")
endforeach()
message(STATUS "Ensure all dependencies are optimized for low memory usage")