CMakeLists.txt revision 8ca33e0e
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 21 22MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}") 23 24 25IF (NOT MSVC) 26 27SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter") 28SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer") 29 30IF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3) 31 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers") 32ENDIF() 33IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 34 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3") 35 IF(CMAKE_C_COMPILER MATCHES "clang" AND 36 NOT "$ENV{TRAVIS}" MATCHES "^true$") 37 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address") 38 ENDIF() 39 # Uncomment to enable fault injection testing via libfiu: 40 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 41 #SET(LIBS ${LIBS} fiu) 42ELSE() 43 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0") 44 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DNDEBUG") 45 # Comment out the following line to compile out debug messages: 46 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 47ENDIF() 48 49IF(LSQUIC_PROFILE EQUAL 1) 50 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg") 51ENDIF() 52 53IF(LSQUIC_COVERAGE EQUAL 1) 54 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage") 55ENDIF() 56 57IF(MY_CMAKE_FLAGS MATCHES "fsanitize=address") 58 MESSAGE(STATUS "AddressSanitizer is ON") 59ELSE() 60 MESSAGE(STATUS "AddressSanitizer is OFF") 61ENDIF() 62 63#MSVC 64ELSE() 65SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214") # nonstandard extension used: bit field types other than int 66SET(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") 67IF(CMAKE_BUILD_TYPE STREQUAL "Debug") 68 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od") 69 #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1") 70 #SET(LIBS ${LIBS} fiu) 71ELSE() 72 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox") 73 SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DNDEBUG") 74 # Comment out the following line to compile out debug messages: 75 #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO") 76ENDIF() 77 78ENDIF() #MSVC 79 80set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}") 81 82MESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}") 83 84IF (NOT DEFINED BORINGSSL_INCLUDE AND DEFINED BORINGSSL_DIR) 85 FIND_PATH(BORINGSSL_INCLUDE NAMES openssl/ssl.h 86 PATHS ${BORINGSSL_DIR}/include 87 NO_DEFAULT_PATH) 88ENDIF() 89# This must be done before adding other include directories to take 90# precedence over header files from other SSL installs. 91 92IF (BORINGSSL_INCLUDE) 93 MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE}") 94 INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE}) 95ELSE() 96 MESSAGE(FATAL_ERROR "BoringSSL headers not found") 97ENDIF() 98 99IF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR) 100 FOREACH(LIB_NAME ssl crypto decrepit) 101 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 102 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 103 NAMES ${LIB_NAME} 104 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 105 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 106 NO_DEFAULT_PATH) 107 ELSE() 108 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 109 NAMES lib${LIB_NAME}.a 110 PATHS ${BORINGSSL_DIR}/${LIB_NAME} 111 NO_DEFAULT_PATH) 112 ENDIF() 113 IF(BORINGSSL_LIB_${LIB_NAME}) 114 MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}") 115 ELSE() 116 MESSAGE(STATUS "${LIB_NAME} library not found") 117 ENDIF() 118 ENDFOREACH() 119 120ELSE() 121 122 123 FOREACH(LIB_NAME ssl crypto decrepit) 124 IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 125 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 126 NAMES ${LIB_NAME} 127 PATHS ${BORINGSSL_LIB} 128 PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo 129 NO_DEFAULT_PATH) 130 ELSE() 131 FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME} 132 NAMES lib${LIB_NAME}.a 133 PATHS ${BORINGSSL_LIB} 134 NO_DEFAULT_PATH) 135 ENDIF() 136 IF(BORINGSSL_LIB_${LIB_NAME}) 137 MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${LIB_NAME}}") 138 ELSE() 139 MESSAGE(STATUS "${BORINGSSL_LIB} library not found") 140 ENDIF() 141 ENDFOREACH() 142#endif 143 144ENDIF() 145 146SET(CMAKE_INCLUDE_CURRENT_DIR ON) 147INCLUDE_DIRECTORIES(include src/lshpack) 148IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin") 149 # Find libevent on FreeBSD: 150 include_directories( /usr/local/include ) 151 link_directories( /usr/local/lib ) 152ENDIF() 153 154# Find zlib and libevent header files and library files 155# TODO: libevent is not strictly necessary to build the library. 156FIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h) 157IF (ZLIB_INCLUDE_DIR) 158 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR}) 159ELSE() 160 MESSAGE(FATAL_ERROR "zlib.h was not found") 161ENDIF() 162IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 163 FIND_LIBRARY(ZLIB_LIB zlib) 164ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin) 165 # XXX somehow FIND_LIBRARY() does not find zlib on Travis? 166 SET(ZLIB_LIB z) 167ELSE() 168 FIND_LIBRARY(ZLIB_LIB libz.a) 169ENDIF() 170IF(ZLIB_LIB) 171 MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}") 172ELSE() 173 MESSAGE(STATUS "zlib not found") 174ENDIF() 175FIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h) 176IF (EVENT_INCLUDE_DIR) 177 INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR}) 178ELSE() 179 MESSAGE(FATAL_ERROR "event2/event.h was not found") 180ENDIF() 181IF (CMAKE_SYSTEM_NAME STREQUAL Windows) 182 FIND_LIBRARY(EVENT_LIB event) 183ELSE() 184 FIND_LIBRARY(EVENT_LIB libevent.a) 185ENDIF() 186IF(EVENT_LIB) 187 MESSAGE(STATUS "Found event: ${EVENT_LIB}") 188ELSE() 189 MESSAGE(STATUS "libevent not found") 190ENDIF() 191 192 193SET(LIBS lsquic ${EVENT_LIB} ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${ZLIB_LIB} ${LIBS}) 194 195IF (NOT MSVC) 196add_executable(http_client 197 test/http_client.c 198 test/prog.c 199 test/test_common.c 200) 201LIST(APPEND LIBS pthread m) 202 203#MSVC 204ELSE() 205add_executable(http_client 206 test/http_client.c 207 test/prog.c 208 test/test_common.c 209 wincompat/getopt.c 210 wincompat/getopt1.c 211) 212LIST(APPEND LIBS ws2_32) 213 214ENDIF() 215 216TARGET_LINK_LIBRARIES(http_client ${LIBS}) 217 218 219add_subdirectory(src) 220 221add_subdirectory(test) 222 223IF(NOT (CMAKE_C_FLAGS MATCHES "-DNDEBUG")) 224 # Our test framework relies on assertions, only compile if assertions are 225 # enabled. 226 # 227 enable_testing() 228ENDIF() 229 230 231ADD_CUSTOM_TARGET(docs doxygen dox.cfg) 232