forked from babelouest/ulfius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
479 lines (400 loc) · 15.3 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#
# Ulfius library
#
# CMake file used to build all programs
#
# Copyright 2018 Silvio Clecio <[email protected]>
# Copyright 2018 Nicolas Mora <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the MIT License
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
cmake_minimum_required(VERSION 3.5)
project(ulfius C)
set(CMAKE_C_STANDARD 99)
if (NOT MSVC)
set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")
endif()
# library info
set(PROJECT_DESCRIPTION "Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services")
set(PROJECT_HOMEPAGE_URL "https://github.com/babelouest/ulfius/")
set(PROJECT_BUGREPORT_PATH "https://github.com/babelouest/ulfius/issues")
set(LIBRARY_VERSION_MAJOR "2")
set(LIBRARY_VERSION_MINOR "6")
set(LIBRARY_VERSION_PATCH "4")
set(PROJECT_VERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}.${LIBRARY_VERSION_PATCH}")
set(LIBRARY_VERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}.${LIBRARY_VERSION_PATCH}")
set(LIBRARY_SOVERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}")
set(ORCANIA_VERSION_REQUIRED "2.0.1")
set(YDER_VERSION_REQUIRED "1.4.8")
# cmake modules
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)
include(GNUInstallDirs)
include(CheckSymbolExists)
# check if _GNU_SOURCE is available
if (NOT _GNU_SOURCE)
check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
if (NOT _GNU_SOURCE)
unset(_GNU_SOURCE CACHE)
check_symbol_exists(_GNU_SOURCE "features.h" _GNU_SOURCE)
endif ()
endif ()
if (_GNU_SOURCE)
add_definitions(-D_GNU_SOURCE)
endif ()
# directories and source
set(INC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(UWSC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tools/uwsc)
include_directories(${INC_DIR})
set(LIB_SRC
${INC_DIR}/ulfius.h
${INC_DIR}/u_private.h
${INC_DIR}/yuarel.h
${SRC_DIR}/u_map.c
${SRC_DIR}/u_request.c
${SRC_DIR}/u_response.c
${SRC_DIR}/u_send_request.c
${SRC_DIR}/u_websocket.c
${SRC_DIR}/yuarel.c
${SRC_DIR}/ulfius.c)
# pthread libraries
find_package (Threads)
set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
# GNU TLS support
option(WITH_GNUTLS "GNU TLS support" ON)
if (WITH_GNUTLS)
include(FindGnuTLS)
find_package(GnuTLS REQUIRED)
if (GNUTLS_FOUND)
set(LIBS ${LIBS} ${GNUTLS_LIBRARIES})
endif ()
endif ()
# websocket support
option(WITH_WEBSOCKET "Websocket support" ON)
if (WIN32)
set(WITH_WEBSOCKET OFF)
endif ()
# current websocket implementation depends on GNU TLS
if (NOT WITH_GNUTLS)
set(WITH_WEBSOCKET OFF)
endif ()
if (WITH_WEBSOCKET)
set(MHD_MIN_VERSION 0.9.53)
else ()
set(MHD_MIN_VERSION 0.9.51)
endif ()
if (WITH_GNUTLS)
set(U_DISABLE_GNUTLS OFF)
else ()
set(U_DISABLE_GNUTLS ON)
endif ()
include(FindMHD)
find_package(MHD ${MHD_MIN_VERSION} REQUIRED)
if (MHD_FOUND)
set(LIBS ${LIBS} ${MHD_LIBRARIES})
include_directories(${MHD_INCLUDE_DIRS})
if (MHD_VERSION_STRING VERSION_LESS "0.9.53")
set(WITH_WEBSOCKET OFF)
endif ()
endif ()
if (WITH_WEBSOCKET)
set(U_DISABLE_WEBSOCKET OFF)
else ()
set(U_DISABLE_WEBSOCKET ON)
endif ()
option(WITH_CURL "Use Curl library" ON)
if (WITH_CURL)
include(FindCURL)
find_package(CURL REQUIRED)
if (CURL_FOUND)
set(LIBS ${LIBS} ${CURL_LIBRARIES})
include_directories(${CURL_INCLUDE_DIRS})
set(U_DISABLE_CURL OFF)
endif ()
else ()
set(U_DISABLE_CURL ON)
endif ()
option(WITH_JANSSON "Use jansson library" ON)
if (WITH_JANSSON)
include(FindJansson)
set(JANSSON_MIN_VERSION 2.4)
find_package(Jansson ${JANSSON_MIN_VERSION} REQUIRED)
if (JANSSON_FOUND)
include_directories(${JANSSON_INCLUDE_DIRS})
set(LIBS ${LIBS} ${JANSSON_LIBRARIES})
set(U_DISABLE_JANSSON OFF)
endif ()
else ()
set(U_DISABLE_JANSSON ON)
endif ()
# TO MY FUTURE SELF
# The following 2 blocks are put BEFORE searching for Orcania and Yder by design
# Otherwise it will lead to cmake errors
# DON'T MOVE IT BELOW PLEASE!
# static library
option(BUILD_STATIC "Build static library." OFF)
if (BUILD_STATIC)
add_library(ulfius_static STATIC ${LIB_SRC})
target_compile_definitions(ulfius_static PUBLIC -DO_STATIC_LIBRARY)
set_target_properties(ulfius_static PROPERTIES
OUTPUT_NAME ulfius)
endif ()
# shared library
add_library(ulfius SHARED ${LIB_SRC})
if (NOT MSVC)
set_target_properties(ulfius PROPERTIES
COMPILE_OPTIONS -Wextra
PUBLIC_HEADER "${INC_DIR}/ulfius.h;${PROJECT_BINARY_DIR}/ulfius-cfg.h"
VERSION "${LIBRARY_VERSION}"
SOVERSION "${LIBRARY_SOVERSION}")
endif()
if (WIN32)
set_target_properties(ulfius PROPERTIES SUFFIX "-${LIBRARY_VERSION_MAJOR}.dll")
endif ()
target_link_libraries(ulfius ${LIBS})
# dependencies
option(SEARCH_ORCANIA_U "Search for ORCANIA library" ON)
if (SEARCH_ORCANIA_U)
set(Orcania_FIND_QUIETLY ON) # force to find Orcania quietly
include(FindOrcania)
find_package(Orcania ${ORCANIA_VERSION_REQUIRED} QUIET) # try to find orcania
if (NOT ORCANIA_FOUND)
include(DownloadProject)
download_project(PROJ orcania # ... otherwise, download archive
URL "https://github.com/babelouest/orcania/archive/v${ORCANIA_VERSION_REQUIRED}.tar.gz"
QUIET)
add_subdirectory(${orcania_SOURCE_DIR} ${orcania_BINARY_DIR})
include_directories(${orcania_SOURCE_DIR}/include)
add_dependencies(ulfius orcania)
set(ORCANIA_LIBRARIES orcania)
set(LIBS ${LIBS} ${ORCANIA_LIBRARIES})
include_directories(${orcania_BINARY_DIR})
else()
set(LIBS ${LIBS} ${ORCANIA_LIBRARIES})
include_directories(${ORCANIA_INCLUDE_DIRS})
endif ()
target_link_libraries(ulfius ${LIBS} ${ORCANIA_LIBRARIES})
endif ()
option(WITH_YDER "Use Yder library to log messages" ON)
option(SEARCH_YDER "Search for Yder library" ON)
if (WITH_YDER)
set(U_DISABLE_YDER OFF)
set(SEARCH_ORCANIA OFF CACHE BOOL "Force to false") # Avoid to search and download orcania during yder search and download
if (SEARCH_YDER)
set(Yder_FIND_QUIETLY ON) # force to find Yder quietly
include(FindYder)
find_package(Yder ${YDER_VERSION_REQUIRED} QUIET) # try to find Yder
if (NOT YDER_FOUND)
include(DownloadProject)
download_project(PROJ yder # ... otherwise, download archive
URL "https://github.com/babelouest/yder/archive/v${YDER_VERSION_REQUIRED}.tar.gz"
QUIET)
add_subdirectory(${yder_SOURCE_DIR} ${yder_BINARY_DIR})
include_directories(${yder_SOURCE_DIR}/include)
add_dependencies(ulfius yder)
set(YDER_LIBRARIES yder orcania)
include_directories(${yder_BINARY_DIR})
else()
set(LIBS ${LIBS} ${YDER_LIBRARIES})
include_directories(${YDER_INCLUDE_DIRS})
endif ()
target_link_libraries(ulfius ${LIBS} ${YDER_LIBRARIES})
endif ()
else ()
set(U_DISABLE_YDER ON)
endif ()
# build uwsc
option(BUILD_UWSC "Build uwsc application." ON)
if (NOT WITH_WEBSOCKET)
set(BUILD_UWSC OFF)
endif ()
if (BUILD_UWSC)
add_executable(uwsc ${UWSC_DIR}/uwsc.c ${INC_DIR}/ulfius.h ${INC_DIR}/u_private.h ${PROJECT_BINARY_DIR}/ulfius-cfg.h)
set_target_properties(uwsc PROPERTIES SKIP_BUILD_RPATH TRUE)
add_dependencies(uwsc ulfius)
target_link_libraries(uwsc ulfius ${LIBS})
install(TARGETS uwsc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
set(PKGCONF_REQ "")
set(PKGCONF_REQ_PRIVATE "liborcania, libyder")
if (WITH_CURL)
set (PKGCONF_REQ_PRIVATE "${PKGCONF_REQ_PRIVATE}, libcurl")
endif ()
if (WITH_JANSSON)
set (PKGCONF_REQ_PRIVATE "${PKGCONF_REQ_PRIVATE}, jansson")
endif ()
if (WITH_GNUTLS)
set (PKGCONF_REQ_PRIVATE "${PKGCONF_REQ_PRIVATE}, gnutls >= 3.5.0")
endif ()
if (WITH_WEBSOCKET)
set (PKGCONF_REQ_PRIVATE "${PKGCONF_REQ_PRIVATE}, libmicrohttpd >= 0.9.53")
else ()
set (PKGCONF_REQ_PRIVATE "${PKGCONF_REQ_PRIVATE}, libmicrohttpd >= 0.9.51")
endif ()
# build ulfius-cfg.h file
configure_file(${INC_DIR}/ulfius-cfg.h.in ${PROJECT_BINARY_DIR}/ulfius-cfg.h)
set (CMAKE_EXTRA_INCLUDE_FILES ${PROJECT_BINARY_DIR})
include_directories(${PROJECT_BINARY_DIR})
# tests
option(BUILD_ULFIUS_TESTING "Build the testing tree." OFF) # because we do not use include(CTest)
if (BUILD_ULFIUS_TESTING)
include(FindCheck)
find_package(Check REQUIRED)
if (CHECK_FOUND)
if (NOT WIN32 AND NOT APPLE)
include(FindSubunit)
find_package(Subunit REQUIRED)
endif ()
enable_testing()
set(CMAKE_CTEST_COMMAND ctest -V)
set(TST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
set(LIBS ulfius ${LIBS} ${CHECK_LIBRARIES})
if (NOT WIN32)
find_package(Threads REQUIRED)
set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT} m)
endif ()
if (NOT APPLE)
set(LIBS ${LIBS} rt)
endif ()
if (NOT WIN32 AND NOT APPLE)
set(LIBS ${LIBS} ${SUBUNIT_LIBRARIES} rt)
endif ()
set(TESTS core u_map framework)
if (WITH_WEBSOCKET)
set(TESTS ${TESTS} websocket)
endif ()
configure_file(
"${CMAKE_MODULE_PATH}/CTestCustom.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake"
@ONLY)
foreach (t ${TESTS})
add_executable(${t} EXCLUDE_FROM_ALL ${TST_DIR}/${t}.c)
target_include_directories(${t} PUBLIC ${TST_DIR})
target_link_libraries(${t} PUBLIC ${LIBS})
add_test(NAME ${t}
WORKING_DIRECTORY ${TST_DIR}
COMMAND ${t})
endforeach ()
endif ()
endif ()
# install target
option(INSTALL_HEADER "Install the header files" ON) # Install ulfius.h or not
configure_file(libulfius.pc.in libulfius.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libulfius.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
SET (TARGETS ulfius)
if (BUILD_STATIC)
SET (TARGETS ${TARGETS} ulfius_static)
endif ()
if (INSTALL_HEADER)
install(TARGETS ${TARGETS}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY example_callbacks/
DESTINATION ${CMAKE_INSTALL_DOCDIR}/example_callbacks/ COMPONENT runtime)
install(DIRECTORY example_programs/
DESTINATION ${CMAKE_INSTALL_DOCDIR}/example_programs/ COMPONENT runtime)
install(FILES README.md
DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT runtime)
install(FILES INSTALL.md
DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT runtime)
install(FILES API.md
DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT runtime)
else ()
install(TARGETS ${TARGETS}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif ()
# uninstall target
if (NOT TARGET uninstall)
configure_file(
"${CMAKE_MODULE_PATH}/CMakeUninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif ()
# packaging
set(CPACK_PACKAGE_VERSION_MAJOR ${LIBRARY_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${LIBRARY_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${LIBRARY_VERSION_PATCH})
if (INSTALL_HEADER)
set(PACKAGE_FILE_NAME
"lib${CMAKE_PROJECT_NAME}-dev_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
else ()
set(PACKAGE_FILE_NAME
"lib${CMAKE_PROJECT_NAME}_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
endif ()
set(PACKAGE_IGNORED_FILES
"${CMAKE_CURRENT_BINARY_DIR}/;/.git/;.gitignore;~$;${CPACK_SOURCE_IGNORE_FILES}")
set(CPACK_PACKAGE_NAME "libulfius")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Web Framework for C programs")
set(CPACK_GENERATOR "TGZ;DEB")
set(CPACK_PACKAGE_VERSION_MAJOR ${LIBRARY_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${LIBRARY_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${LIBRARY_VERSION_PATCH})
option(BUILD_RPM "Build a RPM for your system" OFF)
if (BUILD_RPM)
set(CPACK_GENERATOR "TGZ;DEB;RPM")
set(CPACK_RPM_PACKAGE_LICENSE "LGPL")
set(CPACK_RPM_PACKAGE_URL "http://babelouest.github.io/ulfius/")
endif ()
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "[email protected]")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${PROJECT_DESCRIPTION})
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "http://babelouest.github.io/ulfius/")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.4), liborcania (>= ${ORCANIA_VERSION_REQUIRED}), libyder (>= ${YDER_VERSION_REQUIRED})")
if (INSTALL_HEADER)
if (WITH_CURL)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libcurl4-gnutls-dev (>= 7.16.2) | libcurl4-nss-dev (>= 7.16.2) | libcurl4-openssl-dev (>= 7.16.2)")
endif ()
if (WITH_JANSSON)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libjansson-dev (>= 2.1)")
endif ()
if (WITH_GNUTLS)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libgnutls28-dev (>= 3.5.0)")
endif ()
if (WITH_WEBSOCKET)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libmicrohttpd-dev (>= 0.9.53)")
else ()
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libmicrohttpd-dev (>= 0.9.51)")
endif ()
else ()
if (WITH_CURL)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libcurl3-gnutls (>= 7.16.2) | libcurl3-nss (>= 7.16.2) | libcurl4 (>= 7.16.2)")
endif ()
if (WITH_JANSSON)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libjansson4 (>= 2.1)")
endif ()
if (WITH_GNUTLS)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libgnutls30 (>= 3.5.0)")
endif ()
if (WITH_WEBSOCKET)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libmicrohttpd12 (>= 0.9.53)")
else ()
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libmicrohttpd12 (>= 0.9.51)")
endif ()
endif ()
set(CPACK_PACKAGE_FILE_NAME ${PACKAGE_FILE_NAME})
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_PACKAGE_FILE_NAME ${PACKAGE_FILE_NAME})
set(CPACK_SOURCE_IGNORE_FILES ${PACKAGE_IGNORED_FILES})
include(CPack)
add_custom_target(dist_u COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
message(STATUS "GNU TLS support: ${WITH_GNUTLS}")
message(STATUS "Websocket support: ${WITH_WEBSOCKET}")
message(STATUS "Outgoing requests support: ${WITH_CURL}")
message(STATUS "Jansson library support: ${WITH_JANSSON}")
message(STATUS "Yder support: ${WITH_YDER}")
message(STATUS "Build uwsc application: ${BUILD_UWSC}")
message(STATUS "Build static library: ${BUILD_STATIC}")
message(STATUS "Build testing tree: ${BUILD_ULFIUS_TESTING}")
message(STATUS "Install the header files: ${INSTALL_HEADER}")
message(STATUS "Build RPM package: ${BUILD_RPM}")