CMakeLists.txt revision c0b26eab
1# Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. 2cmake_minimum_required(VERSION 2.8) 3 4 5PROJECT(lsquic C) 6 7OPTION(LSQUIC_FIU "Use Fault Injection in Userspace (FIU)" OFF) 8OPTION(LSQUIC_BIN "Compile example binaries that use the library" ON) 9OPTION(LSQUIC_TESTS "Compile library unit tests" ON) 10OPTION(LSQUIC_SHARED_LIB "Compile as shared librarry" OFF) 11OPTION(LSQUIC_DEVEL "Compile in development mode" OFF) 12 13INCLUDE(GNUInstallDirs) 14 15IF (CMAKE_SYSTEM_NAME STREQUAL "Linux") 16 # If using older glibc, need to link with -lrt. See clock_getres(2). 17 EXECUTE_PROCESS( 18 COMMAND ${PROJECT_SOURCE_DIR}/print-glibc-version.sh ${CMAKE_C_COMPILER} 19 OUTPUT_VARIABLE GLIBC_VERSION) 20 IF(NOT GLIBC_VERSION EQUAL "" AND GLIBC_VERSION VERSION_LESS 2.17) 21 SET(LIBS ${LIBS} rt) 22 ENDIF() 23ELSEIF (CMAKE_SYSTEM_NAME STREQUAL "Android") 24 # for android-ndk >= r19b 25 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "BOTH") 26 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE "BOTH") 27 set(CMAKE_FIND_ROOT_PATH_MODE_PATH "BOTH") 28ENDIF() 29 30IF("${CMAKE_BUILD_TYPE}" STREQUAL "") 31 SET(CMAKE_BUILD_TYPE Debug) 32ENDIF() 33 34MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}") 35 36IF (NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-DLSQUIC_DEBUG_NEXT_ADV_TICK") 37 SET(MY_CMAKE_FLAGS "-DLSQUIC_DEBUG_NEXT_ADV_TICK=1") 38ENDIF() 39 40IF (NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-DLSQUIC_CONN_STATS=") 41 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_CONN_STATS=1") 42ENDIF() 43 44IF (NOT MSVC) 45 46SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter") 47SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer") 48INCLUDE(CheckCCompilerFlag) 49CHECK_C_COMPILER_FLAG(-Wno-implicit-fallthrough HAS_NO_IMPLICIT_FALLTHROUGH) 50IF (HAS_NO_IMPLICIT_FALLTHROUGH) 51 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-implicit-fallthrough") 52ENDIF() 53 54IF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3) 55 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers") 56ENDIF() 57 58IF(LSQUIC_FIU) 59 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 60 SET(LIBS ${LIBS} fiu) 61ENDIF() 62 63IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 64 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3") 65 IF(CMAKE_C_COMPILER MATCHES "clang" AND 66 NOT "$ENV{TRAVIS}" MATCHES "^true$" AND 67 NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-fsanitize") 68 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address") 69 SET(LIBS ${LIBS} -fsanitize=address) 70 ENDIF() 71 # Uncomment to enable cleartext protocol mode (no crypto): 72 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_ENABLE_HANDSHAKE_DISABLE=1") 73ELSE() 74 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0") 75 # Comment out the following line to compile out debug messages: 76 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 77ENDIF() 78 79IF (LSQUIC_DEVEL) 80 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_DEVEL=1") 81ENDIF() 82 83IF(LSQUIC_PROFILE EQUAL 1) 84 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg") 85ENDIF() 86 87IF(LSQUIC_COVERAGE EQUAL 1) 88 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage") 89ENDIF() 90 91IF(MY_CMAKE_FLAGS MATCHES "fsanitize=address") 92 MESSAGE(STATUS "AddressSanitizer is ON") 93ELSE() 94 MESSAGE(STATUS "AddressSanitizer is OFF") 95ENDIF() 96 97#MSVC 98ELSE() 99SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4100") # unreferenced formal parameter 100SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4115") # unnamed type definition in parentheses 101SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4116") # named type definition in parentheses 102SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4146") # unary minus operator applied to unsigned type, result still unsigned 103SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4132") # const initialization 104SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4200") # zero-sized array 105SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4204") # non-constant aggregate initializer 106SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4244") # integer conversion 107SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4245") # conversion from 'int' to 'unsigned int', signed/unsigned mismatch 108SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4267") # integer conversion 109SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214") # nonstandard extension used: bit field types other than int 110SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4295") # array is too small to include a terminating null character 111SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4334") # result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) 112SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4456") # hide previous local declaration 113SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4459") # hide global declaration 114SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4706") # assignment within conditional expression 115SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4090") # different 'const' qualifier (TODO: debug ls-sfparser.c) 116SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -W4 -WX -Zi -DWIN32_LEAN_AND_MEAN -DNOMINMAX -D_CRT_SECURE_NO_WARNINGS -I${CMAKE_CURRENT_SOURCE_DIR}/wincompat") 117IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 118 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od") 119 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 120 #SET(LIBS ${LIBS} fiu) 121ELSE() 122 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox") 123 # Comment out the following line to compile out debug messages: 124 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 125ENDIF() 126 127ENDIF() #MSVC 128 129set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}") 130 131MESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}") 132 133find_package(Perl) 134IF(NOT PERL_FOUND) 135 MESSAGE(FATAL_ERROR "Perl not found -- need it to generate source code") 136ENDIF() 137 138IF(LSQUIC_SHARED_LIB) 139 SET(LIB_SUFFIX .so) 140ELSE() 141 SET(LIB_SUFFIX .a) 142ENDIF() 143 144IF (NOT DEFINED BORINGSSL_INCLUDE AND DEFINED BORINGSSL_DIR) 145 FIND_PATH(BORINGSSL_INCLUDE NAMES openssl/ssl.h 146 PATHS ${BORINGSSL_DIR}/include 147 NO_DEFAULT_PATH) 148ENDIF() 149# This must be done before adding other include directories to take 150# precedence over header files from other SSL installs. 151 152IF (BORINGSSL_INCLUDE) 153 MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE}") 154 INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE}) 155ELSE() 156 MESSAGE(FATAL_ERROR "BoringSSL headers not found") 157ENDIF() 158 159IF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR) 160 FOREACH(LIB_NAME ssl crypto decrepit) 161 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 162 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 163 NAMES ${LIB_NAME} 164 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 165 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 166 NO_DEFAULT_PATH) 167 ELSE() 168 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 169 NAMES lib${LIB_NAME}${LIB_SUFFIX} 170 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 171 NO_DEFAULT_PATH) 172 ENDIF() 173 IF(BORINGSSL_LIB_${LIB_NAME}) 174 MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}") 175 ELSE() 176 MESSAGE(STATUS "${LIB_NAME} library not found") 177 ENDIF() 178 ENDFOREACH() 179 180ELSE() 181 182 183 FOREACH(LIB_NAME ssl crypto decrepit) 184 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 185 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 186 NAMES ${LIB_NAME} 187 PATHS ${BORINGSSL_LIB} 188 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 189 NO_DEFAULT_PATH) 190 ELSE() 191 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 192 NAMES lib${LIB_NAME}${LIB_SUFFIX} 193 PATHS ${BORINGSSL_LIB} 194 PATH_SUFFIXES ${LIB_NAME} 195 NO_DEFAULT_PATH) 196 ENDIF() 197 IF(BORINGSSL_LIB_${LIB_NAME}) 198 MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${LIB_NAME}}") 199 ELSE() 200 MESSAGE(STATUS "${BORINGSSL_LIB} library not found") 201 ENDIF() 202 ENDFOREACH() 203 204ENDIF() 205 206SET(CMAKE_INCLUDE_CURRENT_DIR ON) 207INCLUDE_DIRECTORIES(include) 208IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin") 209 # Find libevent on FreeBSD: 210 include_directories( /usr/local/include ) 211 link_directories( /usr/local/lib ) 212ENDIF() 213 214# Find zlib and libevent header files and library files 215# TODO: libevent is not strictly necessary to build the library. 216FIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h) 217IF (ZLIB_INCLUDE_DIR) 218 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR}) 219ELSE() 220 MESSAGE(FATAL_ERROR "zlib.h was not found") 221ENDIF() 222IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 223 FIND_LIBRARY(ZLIB_LIB zlib) 224ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin) 225 # XXX somehow FIND_LIBRARY() does not find zlib on Travis? 226 SET(ZLIB_LIB z) 227ELSE() 228 FIND_LIBRARY(ZLIB_LIB libz${LIB_SUFFIX}) 229ENDIF() 230IF(ZLIB_LIB) 231 MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}") 232ELSE() 233 MESSAGE(STATUS "zlib not found") 234ENDIF() 235 236IF (LSQUIC_BIN OR LSQUIC_TESTS) 237FIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h) 238IF (EVENT_INCLUDE_DIR) 239 INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR}) 240ELSE() 241 MESSAGE(FATAL_ERROR "event2/event.h was not found") 242ENDIF() 243IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 244 FIND_LIBRARY(EVENT_LIB event) 245ELSE() 246 FIND_LIBRARY(EVENT_LIB libevent${LIB_SUFFIX}) 247 IF(NOT EVENT_LIB) 248 FIND_LIBRARY(EVENT_LIB libevent.so) 249 ENDIF() 250ENDIF() 251IF(EVENT_LIB) 252 MESSAGE(STATUS "Found event: ${EVENT_LIB}") 253ELSE() 254 MESSAGE(STATUS "libevent not found") 255ENDIF() 256SET(LIBS lsquic ${EVENT_LIB} ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${ZLIB_LIB} ${LIBS}) 257IF(MSVC) 258FIND_LIBRARY(PCRE_LIB pcre) 259IF(PCRE_LIB) 260 MESSAGE(STATUS "Found pcre: ${PCRE_LIB}") 261 LIST(APPEND LIBS ${PCRE_LIB}) 262ELSE() 263 MESSAGE(STATUS "pcre not found: http_server won't work") 264ENDIF() 265FIND_LIBRARY(PCREPOSIX_LIB pcreposix) 266IF(PCREPOSIX_LIB) 267 MESSAGE(STATUS "Found pcreposix: ${PCREPOSIX_LIB}") 268 LIST(APPEND LIBS ${PCREPOSIX_LIB}) 269ELSE() 270 MESSAGE(STATUS "pcreposix not found: http_server won't work") 271ENDIF() 272LIST(APPEND LIBS ws2_32) 273ENDIF() 274ENDIF() # LSQUIC_BIN OR LSQUIC_TESTS 275 276IF(LSQUIC_BIN) 277IF(MSVC) 278 SET(GETOPT_C wincompat/getopt.c) 279ENDIF() 280add_executable(http_server bin/http_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 281IF(NOT MSVC) # TODO: port MD5 server and client to Windows 282add_executable(md5_server bin/md5_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 283add_executable(md5_client bin/md5_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 284ENDIF() 285add_executable(echo_server bin/echo_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 286add_executable(echo_client bin/echo_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 287add_executable(duck_server bin/duck_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 288add_executable(duck_client bin/duck_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C}) 289 290 291IF (NOT MSVC) 292 293add_executable(http_client 294 bin/http_client.c 295 bin/prog.c 296 bin/test_common.c 297 bin/test_cert.c 298) 299LIST(APPEND LIBS pthread m) 300 301#MSVC 302ELSE() 303 304add_executable(http_client 305 bin/http_client.c 306 bin/prog.c 307 bin/test_common.c 308 bin/test_cert.c 309 wincompat/getopt.c 310 wincompat/getopt1.c 311) 312LIST(APPEND LIBS ws2_32) 313 314ENDIF() 315 316TARGET_LINK_LIBRARIES(http_client ${LIBS}) 317TARGET_LINK_LIBRARIES(http_server ${LIBS}) 318IF(NOT MSVC) 319TARGET_LINK_LIBRARIES(md5_server ${LIBS}) 320TARGET_LINK_LIBRARIES(md5_client ${LIBS}) 321ENDIF() 322TARGET_LINK_LIBRARIES(echo_server ${LIBS}) 323TARGET_LINK_LIBRARIES(echo_client ${LIBS}) 324TARGET_LINK_LIBRARIES(duck_server ${LIBS}) 325TARGET_LINK_LIBRARIES(duck_client ${LIBS}) 326 327add_subdirectory(bin) 328ENDIF() # LSQUIC_BIN 329 330add_subdirectory(src) 331 332IF(LSQUIC_TESTS AND CMAKE_BUILD_TYPE STREQUAL "Debug") 333 # Our test framework relies on assertions, only compile if assertions are 334 # enabled. 335 # 336 enable_testing() 337 add_subdirectory(tests) 338ENDIF() 339 340 341FIND_PROGRAM(SPHINX NAMES sphinx-build) 342IF(SPHINX) 343 ADD_CUSTOM_TARGET(docs 344 ${SPHINX} -b html 345 docs 346 docs/_build 347 ) 348ELSE() 349 MESSAGE(STATUS "sphinx-build not found: docs won't be made") 350ENDIF() 351 352INSTALL(FILES 353 include/lsquic.h 354 include/lsquic_types.h 355 include/lsxpack_header.h 356 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lsquic 357) 358