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