CMakeLists.txt revision 747be414
1# Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. 2cmake_minimum_required(VERSION 2.8) 3 4 5PROJECT(lsquic C) 6 7IF (CMAKE_SYSTEM_NAME STREQUAL "Linux") 8 # If using older glibc, need to link with -lrt. See clock_getres(2). 9 EXECUTE_PROCESS( 10 COMMAND ${PROJECT_SOURCE_DIR}/print-glibc-version.sh ${CMAKE_C_COMPILER} 11 OUTPUT_VARIABLE GLIBC_VERSION) 12 IF(NOT GLIBC_VERSION EQUAL "" AND GLIBC_VERSION VERSION_LESS 2.17) 13 SET(LIBS ${LIBS} rt) 14 ENDIF() 15ENDIF() 16 17IF("${CMAKE_BUILD_TYPE}" STREQUAL "") 18 SET(CMAKE_BUILD_TYPE Debug) 19ENDIF() 20 21MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}") 22 23OPTION(LSQUIC_FIU "Use Fault Injection in Userspace (FIU)" OFF) 24 25IF (NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-DLSQUIC_DEBUG_NEXT_ADV_TICK") 26 SET(MY_CMAKE_FLAGS "-DLSQUIC_DEBUG_NEXT_ADV_TICK=1") 27ENDIF() 28 29IF (NOT MSVC) 30 31SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter") 32SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer") 33INCLUDE(CheckCCompilerFlag) 34CHECK_C_COMPILER_FLAG(-Wno-implicit-fallthrough HAS_NO_IMPLICIT_FALLTHROUGH) 35IF (HAS_NO_IMPLICIT_FALLTHROUGH) 36 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-implicit-fallthrough") 37ENDIF() 38 39IF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3) 40 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers") 41ENDIF() 42 43IF(LSQUIC_FIU) 44 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 45 SET(LIBS ${LIBS} fiu) 46ENDIF() 47 48IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 49 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3") 50 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Werror") 51 IF(CMAKE_C_COMPILER MATCHES "clang" AND 52 NOT "$ENV{TRAVIS}" MATCHES "^true$" AND 53 NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-fsanitize") 54 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address") 55 SET(LIBS ${LIBS} -fsanitize=address) 56 ENDIF() 57 # Uncomment to enable cleartext protocol mode (no crypto): 58 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_ENABLE_HANDSHAKE_DISABLE=1") 59ELSE() 60 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0") 61 # Comment out the following line to compile out debug messages: 62 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 63ENDIF() 64 65IF(LSQUIC_PROFILE EQUAL 1) 66 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg") 67ENDIF() 68 69IF(LSQUIC_COVERAGE EQUAL 1) 70 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage") 71ENDIF() 72 73IF(MY_CMAKE_FLAGS MATCHES "fsanitize=address") 74 MESSAGE(STATUS "AddressSanitizer is ON") 75ELSE() 76 MESSAGE(STATUS "AddressSanitizer is OFF") 77ENDIF() 78 79#MSVC 80ELSE() 81SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214") # nonstandard extension used: bit field types other than int 82SET(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") 83IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 84 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od") 85 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 86 #SET(LIBS ${LIBS} fiu) 87ELSE() 88 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox") 89 # Comment out the following line to compile out debug messages: 90 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 91ENDIF() 92 93ENDIF() #MSVC 94 95set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}") 96 97MESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}") 98 99IF (NOT DEFINED BORINGSSL_INCLUDE AND DEFINED BORINGSSL_DIR) 100 FIND_PATH(BORINGSSL_INCLUDE NAMES openssl/ssl.h 101 PATHS ${BORINGSSL_DIR}/include 102 NO_DEFAULT_PATH) 103ENDIF() 104# This must be done before adding other include directories to take 105# precedence over header files from other SSL installs. 106 107IF (BORINGSSL_INCLUDE) 108 MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE}") 109 INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE}) 110ELSE() 111 MESSAGE(FATAL_ERROR "BoringSSL headers not found") 112ENDIF() 113 114IF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR) 115 FOREACH(LIB_NAME ssl crypto decrepit) 116 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 117 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 118 NAMES ${LIB_NAME} 119 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 120 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 121 NO_DEFAULT_PATH) 122 ELSE() 123 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 124 NAMES lib${LIB_NAME}.a 125 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 126 NO_DEFAULT_PATH) 127 ENDIF() 128 IF(BORINGSSL_LIB_${LIB_NAME}) 129 MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}") 130 ELSE() 131 MESSAGE(STATUS "${LIB_NAME} library not found") 132 ENDIF() 133 ENDFOREACH() 134 135ELSE() 136 137 138 FOREACH(LIB_NAME ssl crypto decrepit) 139 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 140 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 141 NAMES ${LIB_NAME} 142 PATHS ${BORINGSSL_LIB} 143 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 144 NO_DEFAULT_PATH) 145 ELSE() 146 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 147 NAMES lib${LIB_NAME}.a 148 PATHS ${BORINGSSL_LIB} 149 NO_DEFAULT_PATH) 150 ENDIF() 151 IF(BORINGSSL_LIB_${LIB_NAME}) 152 MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${LIB_NAME}}") 153 ELSE() 154 MESSAGE(STATUS "${BORINGSSL_LIB} library not found") 155 ENDIF() 156 ENDFOREACH() 157#endif 158 159ENDIF() 160 161SET(CMAKE_INCLUDE_CURRENT_DIR ON) 162INCLUDE_DIRECTORIES(include src/lshpack) 163IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin") 164 # Find libevent on FreeBSD: 165 include_directories( /usr/local/include ) 166 link_directories( /usr/local/lib ) 167ENDIF() 168 169# Find zlib and libevent header files and library files 170# TODO: libevent is not strictly necessary to build the library. 171FIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h) 172IF (ZLIB_INCLUDE_DIR) 173 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR}) 174ELSE() 175 MESSAGE(FATAL_ERROR "zlib.h was not found") 176ENDIF() 177IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 178 FIND_LIBRARY(ZLIB_LIB zlib) 179ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin) 180 # XXX somehow FIND_LIBRARY() does not find zlib on Travis? 181 SET(ZLIB_LIB z) 182ELSE() 183 FIND_LIBRARY(ZLIB_LIB libz.a) 184ENDIF() 185IF(ZLIB_LIB) 186 MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}") 187ELSE() 188 MESSAGE(STATUS "zlib not found") 189ENDIF() 190FIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h) 191IF (EVENT_INCLUDE_DIR) 192 INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR}) 193ELSE() 194 MESSAGE(FATAL_ERROR "event2/event.h was not found") 195ENDIF() 196IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 197 FIND_LIBRARY(EVENT_LIB event) 198ELSE() 199 FIND_LIBRARY(EVENT_LIB libevent.a) 200 IF(NOT EVENT_LIB) 201 FIND_LIBRARY(EVENT_LIB libevent.so) 202 ENDIF() 203ENDIF() 204IF(EVENT_LIB) 205 MESSAGE(STATUS "Found event: ${EVENT_LIB}") 206ELSE() 207 MESSAGE(STATUS "libevent not found") 208ENDIF() 209 210add_executable(http_server test/http_server.c test/prog.c test/test_common.c test/test_cert.c) 211add_executable(md5_server test/md5_server.c test/prog.c test/test_common.c test/test_cert.c) 212add_executable(md5_client test/md5_client.c test/prog.c test/test_common.c test/test_cert.c) 213add_executable(echo_server test/echo_server.c test/prog.c test/test_common.c test/test_cert.c) 214add_executable(echo_client test/echo_client.c test/prog.c test/test_common.c test/test_cert.c) 215 216 217SET(LIBS lsquic ${EVENT_LIB} ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${ZLIB_LIB} ${LIBS}) 218 219IF (NOT MSVC) 220 221add_executable(http_client 222 test/http_client.c 223 test/prog.c 224 test/test_common.c 225 test/test_cert.c 226) 227LIST(APPEND LIBS pthread m) 228 229#MSVC 230ELSE() 231 232add_executable(http_client 233 test/http_client.c 234 test/prog.c 235 test/test_common.c 236 wincompat/getopt.c 237 wincompat/getopt1.c 238) 239LIST(APPEND LIBS ws2_32) 240 241ENDIF() 242 243TARGET_LINK_LIBRARIES(http_client ${LIBS}) 244TARGET_LINK_LIBRARIES(http_server ${LIBS}) 245TARGET_LINK_LIBRARIES(md5_server ${LIBS}) 246TARGET_LINK_LIBRARIES(md5_client ${LIBS}) 247TARGET_LINK_LIBRARIES(echo_server ${LIBS}) 248TARGET_LINK_LIBRARIES(echo_client ${LIBS}) 249 250add_subdirectory(src) 251 252add_subdirectory(test) 253 254IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 255 # Our test framework relies on assertions, only compile if assertions are 256 # enabled. 257 # 258 enable_testing() 259ENDIF() 260 261 262ADD_CUSTOM_TARGET(docs doxygen dox.cfg) 263