|
| 1 | +cmake_minimum_required(VERSION 3.12) |
| 2 | + |
| 3 | +project(facil.io VERSION 0.8.0) |
| 4 | + |
| 5 | +# Set the source directories |
| 6 | +set(LIB_ROOT lib) |
| 7 | +set(LIB_CONCAT_FOLDER fio-stl) |
| 8 | + |
| 9 | +include_directories(".") |
| 10 | + |
| 11 | +option(ENABLE_SHARED "Build facil.io as shared library" OFF) |
| 12 | +option(EXAMPLES_BUILD "Build Examples" ON) |
| 13 | +option(TESTS_BUILD "Build Tests" ON) |
| 14 | + |
| 15 | +# Detect endianess |
| 16 | +include(CheckTypeSize) |
| 17 | +check_type_size("void*" SIZE_OF_POINTER) |
| 18 | +if(${SIZE_OF_POINTER} EQUAL 8) |
| 19 | + set(FLAGS "__BIG_ENDIAN__") |
| 20 | +else() |
| 21 | + set(FLAGS "__BIG_ENDIAN__=0") |
| 22 | +endif() |
| 23 | + |
| 24 | +find_package(Threads REQUIRED) |
| 25 | +# Detect SSL/TLS Libraries |
| 26 | +find_package(OpenSSL 3.0) |
| 27 | + |
| 28 | +if(OpenSSL_FOUND) |
| 29 | + message(STATUS "OpenSSL found.") |
| 30 | +else() |
| 31 | + message(WARNING "OpenSSL not found. SSL/TLS support will be disabled.") |
| 32 | +endif() |
| 33 | + |
| 34 | +# Detect Sodium Library |
| 35 | +find_package(Sodium) |
| 36 | + |
| 37 | +if(Sodium_FOUND) |
| 38 | + message(STATUS "Sodium found.") |
| 39 | +else() |
| 40 | + message(WARNING "Sodium not found. Sodium support will be disabled.") |
| 41 | +endif() |
| 42 | + |
| 43 | +# Detect 'struct tm' fields |
| 44 | +include(CheckStructHasMember) |
| 45 | +check_struct_has_member("struct tm" tm_zone time.h HAVE_TM_TM_ZONE) |
| 46 | + |
| 47 | +# Detect SystemV socket libraries |
| 48 | +include(CheckLibraryExists) |
| 49 | +check_library_exists(socket connect "" HAVE_SOCKET) |
| 50 | +check_library_exists(nsl gethostname "" HAVE_NSL) |
| 51 | + |
| 52 | +if(HAVE_SOCKET AND HAVE_NSL) |
| 53 | + link_libraries(socket nsl) |
| 54 | +endif() |
| 55 | +if(WIN32) |
| 56 | + link_libraries(ws2_32) |
| 57 | +endif() |
| 58 | + |
| 59 | +# Detect the `sendfile` system call |
| 60 | +include(CheckCSourceCompiles) |
| 61 | +check_c_source_compiles(" |
| 62 | + #define _GNU_SOURCE |
| 63 | + #include <stdlib.h> |
| 64 | + #include <stdio.h> |
| 65 | + #include <sys/sendfile.h> |
| 66 | + int main(void) { |
| 67 | + off_t offset = 0; |
| 68 | + ssize_t result = sendfile(2, 1, (off_t *)&offset, 300); |
| 69 | + } |
| 70 | +" HAVE_SENDFILE_LINUX) |
| 71 | + |
| 72 | +check_c_source_compiles(" |
| 73 | + #define _GNU_SOURCE |
| 74 | + #include <stdlib.h> |
| 75 | + #include <stdio.h> |
| 76 | + #include <sys/types.h> |
| 77 | + #include <sys/socket.h> |
| 78 | + #include <sys/uio.h> |
| 79 | + int main(void) { |
| 80 | + off_t sent = 0; |
| 81 | + off_t offset = 0; |
| 82 | + ssize_t result = sendfile(2, 1, offset, (size_t)sent, NULL, &sent, 0); |
| 83 | + } |
| 84 | +" HAVE_SENDFILE_BSD) |
| 85 | + |
| 86 | +check_c_source_compiles(" |
| 87 | + #define _GNU_SOURCE |
| 88 | + #include <stdlib.h> |
| 89 | + #include <stdio.h> |
| 90 | + #include <sys/types.h> |
| 91 | + #include <sys/socket.h> |
| 92 | + #include <sys/uio.h> |
| 93 | + int main(void) { |
| 94 | + off_t sent = 0; |
| 95 | + off_t offset = 0; |
| 96 | + ssize_t result = sendfile(2, 1, offset, &sent, NULL, 0); |
| 97 | + } |
| 98 | +" HAVE_SENDFILE_APPLE) |
| 99 | + |
| 100 | +if(HAVE_SENDFILE_LINUX) |
| 101 | + set(FLAGS "${FLAGS} USE_SENDFILE_LINUX HAVE_SENDFILE") |
| 102 | +elseif(HAVE_SENDFILE_BSD) |
| 103 | + set(FLAGS "${FLAGS} USE_SENDFILE_BSD HAVE_SENDFILE") |
| 104 | +elseif(HAVE_SENDFILE_APPLE) |
| 105 | + set(FLAGS "${FLAGS} USE_SENDFILE_APPLE HAVE_SENDFILE") |
| 106 | +else() |
| 107 | + set(FLAGS "${FLAGS} USE_SENDFILE=0") |
| 108 | +endif() |
| 109 | + |
| 110 | +# Detect polling mechanisms (kqueue / epoll / poll) |
| 111 | +check_c_source_compiles(" |
| 112 | + #define _GNU_SOURCE |
| 113 | + #include <stdlib.h> |
| 114 | + #include <sys/event.h> |
| 115 | + int main(void) { |
| 116 | + int fd = kqueue(); |
| 117 | + } |
| 118 | +" HAVE_KQUEUE) |
| 119 | + |
| 120 | +check_c_source_compiles(" |
| 121 | + #define _GNU_SOURCE |
| 122 | + #include <stdlib.h> |
| 123 | + #include <stdio.h> |
| 124 | + #include <sys/types.h> |
| 125 | + #include <sys/stat.h> |
| 126 | + #include <fcntl.h> |
| 127 | + #include <sys/epoll.h> |
| 128 | + int main(void) { |
| 129 | + int fd = epoll_create1(EPOLL_CLOEXEC); |
| 130 | + } |
| 131 | +" HAVE_EPOLL) |
| 132 | + |
| 133 | +check_c_source_compiles(" |
| 134 | + #define _GNU_SOURCE |
| 135 | + #include <stdlib.h> |
| 136 | + #include <poll.h> |
| 137 | + int main(void) { |
| 138 | + struct pollfd plist[18]; |
| 139 | + memset(plist, 0, sizeof(plist[0]) * 18); |
| 140 | + poll(plist, 1, 1); |
| 141 | + } |
| 142 | +" HAVE_POLL) |
| 143 | + |
| 144 | +# Manual selection or fallback |
| 145 | +if(FIO_ENGINE_POLL) |
| 146 | + set(FLAGS "${FLAGS} FIO_ENGINE_POLL HAVE_POLL") |
| 147 | +elseif(FIO_ENGINE_EPOLL) |
| 148 | + set(FLAGS "${FLAGS} FIO_ENGINE_EPOLL HAVE_EPOLL") |
| 149 | +elseif(FIO_ENGINE_KQUEUE) |
| 150 | + set(FLAGS "${FLAGS} FIO_ENGINE_KQUEUE HAVE_KQUEUE") |
| 151 | +else() |
| 152 | + if(HAVE_KQUEUE) |
| 153 | + set(FLAGS "${FLAGS} FIO_ENGINE_KQUEUE HAVE_KQUEUE") |
| 154 | + elseif(HAVE_EPOLL) |
| 155 | + set(FLAGS "${FLAGS} FIO_ENGINE_EPOLL HAVE_EPOLL") |
| 156 | + elseif(HAVE_POLL) |
| 157 | + set(FLAGS "${FLAGS} FIO_ENGINE_POLL HAVE_POLL") |
| 158 | + else() |
| 159 | + message(WARNING "No supported polling engine detected. Unable to compile facil.io.") |
| 160 | + endif() |
| 161 | +endif() |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +# Define the source files for the library |
| 166 | +file(GLOB_RECURSE LIB_SOURCES ${LIB_ROOT}/*.c ${LIB_ROOT}/*.cpp ${LIB_ROOT}/*.cxx ${LIB_ROOT}/*.c++) |
| 167 | +set(SOURCES ${LIB_SOURCES}) |
| 168 | + |
| 169 | +# Optimization level |
| 170 | +set(OPTIMIZATION "-O3") |
| 171 | +# Optimization level in debug mode |
| 172 | +set(OPTIMIZATION_DEBUG "-O0 -g -coverage -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-builtin") |
| 173 | +# Warning flags |
| 174 | +set(WARNINGS "-Wno-missing-field-initializers -Wformat-security") |
| 175 | +# Preprocessor definitions |
| 176 | +set(FLAGS "-DFIO_LEAK_COUNTER -DFIO_FIOBJ -DFIOBJ_MALLOC") |
| 177 | +if(WIN32 AND NOT MSVC) |
| 178 | + set(FLAGS "${FLAGS} -D_GNU_SOURCE -D__MINGW32__") |
| 179 | +endif() |
| 180 | +# C specific compiler options |
| 181 | +set(C_EXTRA_OPT "") |
| 182 | +# C++ specific compiler options |
| 183 | +set(CXX_EXTRA_OPT "-Wno-keyword-macro -Wno-vla-extension -Wno-c99-extensions -Wno-zero-length-array -Wno-variadic-macros -Wno-missing-braces") |
| 184 | + |
| 185 | +# Create the library target |
| 186 | +if(ENABLE_SHARED) |
| 187 | + add_library(${PROJECT_NAME} SHARED ${SOURCES}) |
| 188 | + if(HAVE_SSL EQUAL 1) |
| 189 | + target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads OpenSSL::SSL OpenSSL::Crypto) |
| 190 | + elseif(HAVE_SODIUM EQUAL 1) |
| 191 | + target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads Sodium::Sodium) |
| 192 | + else() |
| 193 | + target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads) |
| 194 | + endif() |
| 195 | +else() |
| 196 | + add_library(${PROJECT_NAME} STATIC ${SOURCES}) |
| 197 | +endif() |
| 198 | +# Add the definitions to the target |
| 199 | +target_compile_options(${PROJECT_NAME} PRIVATE |
| 200 | + $<$<CONFIG:Debug>:${OPTIMIZATION_DEBUG} ${WARNINGS} ${CXX_EXTRA_OPT}> "-DDEBUG=1" |
| 201 | + $<$<NOT:$<CONFIG:Debug>>:${OPTIMIZATION} ${WARNINGS}> |
| 202 | +) |
| 203 | +target_compile_definitions(${PROJECT_NAME} PRIVATE ${FLAGS}) |
| 204 | + |
| 205 | +set(PKG_CONFIG_FILE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc") |
| 206 | +configure_file(${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in ${PKG_CONFIG_FILE} @ONLY) |
| 207 | + |
| 208 | +# Install the generated .pc file |
| 209 | +install(FILES ${PKG_CONFIG_FILE} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig) |
| 210 | + |
| 211 | +if(EXAMPLES_BUILD) |
| 212 | + # Define a function to build and run an example |
| 213 | + function(build_and_run_example EXAMPLE_NAME) |
| 214 | + add_executable(${EXAMPLE_NAME} examples/${EXAMPLE_NAME}.c) |
| 215 | + if(${EXAMPLE_NAME} STREQUAL server OR ${EXAMPLE_NAME} STREQUAL client) |
| 216 | + if(Sodium_FOUND) |
| 217 | + target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads Sodium::Sodium) |
| 218 | + elseif(OpenSSL_FOUND) |
| 219 | + target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads OpenSSL::SSL OpenSSL::Crypto) |
| 220 | + else() |
| 221 | + target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads) |
| 222 | + endif() |
| 223 | + else() |
| 224 | + target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads) |
| 225 | + endif() |
| 226 | + target_compile_definitions(${EXAMPLE_NAME} PRIVATE ${FLAGS}) |
| 227 | + add_custom_target(run_${EXAMPLE_NAME} |
| 228 | + COMMAND ${EXAMPLE_NAME} |
| 229 | + DEPENDS ${EXAMPLE_NAME} |
| 230 | + ) |
| 231 | + endfunction() |
| 232 | + |
| 233 | + # List of example files to build and run |
| 234 | + # comment all examples w/ issue build |
| 235 | + set(EXAMPLES |
| 236 | + array |
| 237 | + bstr |
| 238 | + chat |
| 239 | + client |
| 240 | + # fiobj |
| 241 | + map |
| 242 | + server |
| 243 | + string |
| 244 | + ) |
| 245 | + |
| 246 | + # Build and run each example |
| 247 | + foreach(EXAMPLE ${EXAMPLES}) |
| 248 | + build_and_run_example(${EXAMPLE}) |
| 249 | + endforeach() |
| 250 | +endif() |
| 251 | + |
| 252 | +if(TESTS_BUILD) |
| 253 | + # Define a function to build and run a test |
| 254 | + function(build_and_run_test TEST_NAME) |
| 255 | + if(${TEST_NAME} STREQUAL cpp) |
| 256 | + add_executable(${TEST_NAME} tests/${TEST_NAME}.cpp) |
| 257 | + else() |
| 258 | + add_executable(${TEST_NAME} tests/${TEST_NAME}.c) |
| 259 | + endif() |
| 260 | + if(${TEST_NAME} STREQUAL base64 OR ${TEST_NAME} STREQUAL stl-mutex) |
| 261 | + if(Sodium_FOUND) |
| 262 | + target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads Sodium::Sodium) |
| 263 | + elseif(OpenSSL_FOUND) |
| 264 | + target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads OpenSSL::SSL OpenSSL::Crypto) |
| 265 | + endif() |
| 266 | + else() |
| 267 | + target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads) |
| 268 | + endif() |
| 269 | + target_compile_definitions(${TEST_NAME} PRIVATE ${FLAGS} "-DTESTS=1") |
| 270 | + add_custom_target(run_${TEST_NAME} |
| 271 | + COMMAND ${TEST_NAME} |
| 272 | + DEPENDS ${TEST_NAME} |
| 273 | + ) |
| 274 | + endfunction() |
| 275 | + |
| 276 | + # List of test files to build and run |
| 277 | + # comment all tests w/ issue build |
| 278 | + set(TESTS |
| 279 | + # array |
| 280 | + base64 |
| 281 | + # cpp |
| 282 | + http1-parser-old |
| 283 | + http1-parser |
| 284 | + # json |
| 285 | + # json_find |
| 286 | + # json_minify |
| 287 | + malloc |
| 288 | + mempool |
| 289 | + # mustache |
| 290 | + noop |
| 291 | + # random |
| 292 | + slowloris |
| 293 | + # stl-mutex |
| 294 | + # stl |
| 295 | + url |
| 296 | + ) |
| 297 | + |
| 298 | + # Build and run each test |
| 299 | + foreach(TEST ${TESTS}) |
| 300 | + build_and_run_test(${TEST}) |
| 301 | + endforeach() |
| 302 | +endif() |
0 commit comments