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