CMakeLists.txt revision 0adf085a
1229fce07SDmitri Tikhonov# Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE.
250aadb33SDmitri Tikhonovcmake_minimum_required(VERSION 2.8)
350aadb33SDmitri Tikhonov
450aadb33SDmitri Tikhonov
5b1af35d6SDmitri TikhonovPROJECT(lsquic C)
6461e84d8SAmol Deshpande
712693a01SDmitri TikhonovIF (CMAKE_SYSTEM_NAME STREQUAL "Linux")
812693a01SDmitri Tikhonov    # If using older glibc, need to link with -lrt.  See clock_getres(2).
912693a01SDmitri Tikhonov    EXECUTE_PROCESS(
1012693a01SDmitri Tikhonov        COMMAND ${PROJECT_SOURCE_DIR}/print-glibc-version.sh ${CMAKE_C_COMPILER}
1112693a01SDmitri Tikhonov        OUTPUT_VARIABLE GLIBC_VERSION)
1212693a01SDmitri Tikhonov    IF(NOT GLIBC_VERSION EQUAL "" AND GLIBC_VERSION VERSION_LESS 2.17)
1312693a01SDmitri Tikhonov        SET(LIBS ${LIBS} rt)
1412693a01SDmitri Tikhonov    ENDIF()
15ccd74161SDmitri TikhonovENDIF()
16ccd74161SDmitri Tikhonov
171e75f938SDmitri TikhonovIF("${CMAKE_BUILD_TYPE}" STREQUAL "")
181e75f938SDmitri Tikhonov    SET(CMAKE_BUILD_TYPE Debug)
1950aadb33SDmitri TikhonovENDIF()
2050aadb33SDmitri Tikhonov
211e75f938SDmitri TikhonovMESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
2250aadb33SDmitri Tikhonov
23662de5e1SDmitri TikhonovOPTION(LSQUIC_FIU "Use Fault Injection in Userspace (FIU)" OFF)
24662de5e1SDmitri Tikhonov
25b93f59beSBob Perper
2646a5b669SDmitri TikhonovIF (NOT MSVC)
27461e84d8SAmol Deshpande
2850aadb33SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter")
2950aadb33SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer")
305392f7a3SLiteSpeed TechINCLUDE(CheckCCompilerFlag)
315392f7a3SLiteSpeed TechCHECK_C_COMPILER_FLAG(-Wno-implicit-fallthrough HAS_NO_IMPLICIT_FALLTHROUGH)
325392f7a3SLiteSpeed TechIF (HAS_NO_IMPLICIT_FALLTHROUGH)
335392f7a3SLiteSpeed Tech    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-implicit-fallthrough")
345392f7a3SLiteSpeed TechENDIF()
3550aadb33SDmitri Tikhonov
36e0197994SDmitri TikhonovIF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3)
3750aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers")
3850aadb33SDmitri TikhonovENDIF()
395392f7a3SLiteSpeed Tech
40662de5e1SDmitri TikhonovIF(LSQUIC_FIU)
41662de5e1SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
42662de5e1SDmitri Tikhonov    SET(LIBS ${LIBS} fiu)
43662de5e1SDmitri TikhonovENDIF()
44662de5e1SDmitri Tikhonov
451e75f938SDmitri TikhonovIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
4650aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3")
475392f7a3SLiteSpeed Tech    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Werror")
48db8ac9eeSDmitri Tikhonov    IF(CMAKE_C_COMPILER MATCHES "clang" AND
49db8ac9eeSDmitri Tikhonov                        NOT "$ENV{TRAVIS}" MATCHES "^true$")
5050aadb33SDmitri Tikhonov        SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address")
5150aadb33SDmitri Tikhonov    ENDIF()
525392f7a3SLiteSpeed Tech    # Uncomment to enable cleartext protocol mode (no crypto):
535392f7a3SLiteSpeed Tech    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_ENABLE_HANDSHAKE_DISABLE=1")
5450aadb33SDmitri TikhonovELSE()
5550aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0")
5650aadb33SDmitri Tikhonov    # Comment out the following line to compile out debug messages:
5750aadb33SDmitri Tikhonov    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
5850aadb33SDmitri TikhonovENDIF()
5950aadb33SDmitri Tikhonov
6067b0dc15SDmitri TikhonovIF(LSQUIC_PROFILE EQUAL 1)
6167b0dc15SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg")
6267b0dc15SDmitri TikhonovENDIF()
6367b0dc15SDmitri Tikhonov
6416a9b66aSDmitri TikhonovIF(LSQUIC_COVERAGE EQUAL 1)
6516a9b66aSDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage")
6616a9b66aSDmitri TikhonovENDIF()
6716a9b66aSDmitri Tikhonov
6850aadb33SDmitri TikhonovIF(MY_CMAKE_FLAGS MATCHES "fsanitize=address")
6950aadb33SDmitri Tikhonov    MESSAGE(STATUS "AddressSanitizer is ON")
7050aadb33SDmitri TikhonovELSE()
7150aadb33SDmitri Tikhonov    MESSAGE(STATUS "AddressSanitizer is OFF")
7250aadb33SDmitri TikhonovENDIF()
7350aadb33SDmitri Tikhonov
74461e84d8SAmol Deshpande#MSVC
75461e84d8SAmol DeshpandeELSE()
762d296031SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214")	# nonstandard extension used: bit field types other than int
77461e84d8SAmol DeshpandeSET(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")
781e75f938SDmitri TikhonovIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
79b5d685fcSBob Perper    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od")
80461e84d8SAmol Deshpande    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
81ccd74161SDmitri Tikhonov    #SET(LIBS ${LIBS} fiu)
82461e84d8SAmol DeshpandeELSE()
83b5d685fcSBob Perper    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox")
84461e84d8SAmol Deshpande    # Comment out the following line to compile out debug messages:
85461e84d8SAmol Deshpande    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
86461e84d8SAmol DeshpandeENDIF()
87461e84d8SAmol Deshpande
88461e84d8SAmol DeshpandeENDIF() #MSVC
89461e84d8SAmol Deshpande
9050aadb33SDmitri Tikhonovset(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}")
9150aadb33SDmitri Tikhonov
9250aadb33SDmitri TikhonovMESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}")
9350aadb33SDmitri Tikhonov
948ca33e0eSDmitri TikhonovIF (NOT DEFINED BORINGSSL_INCLUDE AND DEFINED BORINGSSL_DIR)
958ca33e0eSDmitri Tikhonov    FIND_PATH(BORINGSSL_INCLUDE NAMES openssl/ssl.h
968ca33e0eSDmitri Tikhonov                PATHS ${BORINGSSL_DIR}/include
978ca33e0eSDmitri Tikhonov                NO_DEFAULT_PATH)
988ca33e0eSDmitri TikhonovENDIF()
99b1af35d6SDmitri Tikhonov# This must be done before adding other include directories to take
100b1af35d6SDmitri Tikhonov# precedence over header files from other SSL installs.
1018ca33e0eSDmitri Tikhonov
1028ca33e0eSDmitri TikhonovIF (BORINGSSL_INCLUDE)
1038ca33e0eSDmitri Tikhonov    MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE}")
1048ca33e0eSDmitri Tikhonov    INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE})
105b1af35d6SDmitri TikhonovELSE()
106b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "BoringSSL headers not found")
107e0197994SDmitri TikhonovENDIF()
10850aadb33SDmitri Tikhonov
1098ca33e0eSDmitri TikhonovIF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR)
1108ca33e0eSDmitri Tikhonov    FOREACH(LIB_NAME ssl crypto decrepit)
1118ca33e0eSDmitri Tikhonov        IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
1128ca33e0eSDmitri Tikhonov            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
1138ca33e0eSDmitri Tikhonov                NAMES ${LIB_NAME}
1148ca33e0eSDmitri Tikhonov                PATHS ${BORINGSSL_DIR}/${LIB_NAME}
1158ca33e0eSDmitri Tikhonov                PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
1168ca33e0eSDmitri Tikhonov                NO_DEFAULT_PATH)
1178ca33e0eSDmitri Tikhonov        ELSE()
1188ca33e0eSDmitri Tikhonov            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
1198ca33e0eSDmitri Tikhonov                NAMES lib${LIB_NAME}.a
1208ca33e0eSDmitri Tikhonov                PATHS ${BORINGSSL_DIR}/${LIB_NAME}
1218ca33e0eSDmitri Tikhonov                NO_DEFAULT_PATH)
1228ca33e0eSDmitri Tikhonov        ENDIF()
1238ca33e0eSDmitri Tikhonov        IF(BORINGSSL_LIB_${LIB_NAME})
1248ca33e0eSDmitri Tikhonov            MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}")
1258ca33e0eSDmitri Tikhonov        ELSE()
1268ca33e0eSDmitri Tikhonov            MESSAGE(STATUS "${LIB_NAME} library not found")
1278ca33e0eSDmitri Tikhonov        ENDIF()
1288ca33e0eSDmitri Tikhonov    ENDFOREACH()
1298ca33e0eSDmitri Tikhonov
1308ca33e0eSDmitri TikhonovELSE()
1318ca33e0eSDmitri Tikhonov
1328ca33e0eSDmitri Tikhonov
1338ca33e0eSDmitri Tikhonov    FOREACH(LIB_NAME ssl crypto decrepit)
1348ca33e0eSDmitri Tikhonov        IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
1358ca33e0eSDmitri Tikhonov            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
1368ca33e0eSDmitri Tikhonov                NAMES ${LIB_NAME}
1378ca33e0eSDmitri Tikhonov                PATHS ${BORINGSSL_LIB}
1388ca33e0eSDmitri Tikhonov                PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
1398ca33e0eSDmitri Tikhonov                NO_DEFAULT_PATH)
1408ca33e0eSDmitri Tikhonov        ELSE()
1418ca33e0eSDmitri Tikhonov            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
1428ca33e0eSDmitri Tikhonov                NAMES lib${LIB_NAME}.a
1438ca33e0eSDmitri Tikhonov                PATHS ${BORINGSSL_LIB}
1448ca33e0eSDmitri Tikhonov                NO_DEFAULT_PATH)
1458ca33e0eSDmitri Tikhonov        ENDIF()
1468ca33e0eSDmitri Tikhonov        IF(BORINGSSL_LIB_${LIB_NAME})
1478ca33e0eSDmitri Tikhonov            MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${LIB_NAME}}")
1488ca33e0eSDmitri Tikhonov        ELSE()
1498ca33e0eSDmitri Tikhonov            MESSAGE(STATUS "${BORINGSSL_LIB} library not found")
1508ca33e0eSDmitri Tikhonov        ENDIF()
1518ca33e0eSDmitri Tikhonov    ENDFOREACH()
1528ca33e0eSDmitri Tikhonov#endif
153461e84d8SAmol Deshpande
1548ca33e0eSDmitri TikhonovENDIF()
1558ca33e0eSDmitri Tikhonov
1568ca33e0eSDmitri TikhonovSET(CMAKE_INCLUDE_CURRENT_DIR ON)
1578ca33e0eSDmitri TikhonovINCLUDE_DIRECTORIES(include src/lshpack)
158b1af35d6SDmitri TikhonovIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
15950aadb33SDmitri Tikhonov    # Find libevent on FreeBSD:
1608ca33e0eSDmitri Tikhonov    include_directories( /usr/local/include )
1618ca33e0eSDmitri Tikhonov    link_directories( /usr/local/lib )
162b1af35d6SDmitri TikhonovENDIF()
163b1af35d6SDmitri Tikhonov
164b1af35d6SDmitri Tikhonov# Find zlib and libevent header files and library files
165b1af35d6SDmitri Tikhonov# TODO: libevent is not strictly necessary to build the library.
166b1af35d6SDmitri TikhonovFIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h)
167b1af35d6SDmitri TikhonovIF (ZLIB_INCLUDE_DIR)
168b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
169b1af35d6SDmitri TikhonovELSE()
170b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "zlib.h was not found")
171b1af35d6SDmitri TikhonovENDIF()
172b1af35d6SDmitri TikhonovIF (CMAKE_SYSTEM_NAME STREQUAL Windows)
173b1af35d6SDmitri Tikhonov    FIND_LIBRARY(ZLIB_LIB zlib)
174b1af35d6SDmitri TikhonovELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin)
175b1af35d6SDmitri Tikhonov    # XXX somehow FIND_LIBRARY() does not find zlib on Travis?
176b1af35d6SDmitri Tikhonov    SET(ZLIB_LIB z)
177b1af35d6SDmitri TikhonovELSE()
178b1af35d6SDmitri Tikhonov    FIND_LIBRARY(ZLIB_LIB libz.a)
179b1af35d6SDmitri TikhonovENDIF()
180b1af35d6SDmitri TikhonovIF(ZLIB_LIB)
181b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}")
182b1af35d6SDmitri TikhonovELSE()
183b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "zlib not found")
184b1af35d6SDmitri TikhonovENDIF()
185b1af35d6SDmitri TikhonovFIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h)
186b1af35d6SDmitri TikhonovIF (EVENT_INCLUDE_DIR)
187b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR})
188b1af35d6SDmitri TikhonovELSE()
189b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "event2/event.h was not found")
190b1af35d6SDmitri TikhonovENDIF()
191b1af35d6SDmitri TikhonovIF (CMAKE_SYSTEM_NAME STREQUAL Windows)
192b1af35d6SDmitri Tikhonov    FIND_LIBRARY(EVENT_LIB event)
193b1af35d6SDmitri TikhonovELSE()
194662de5e1SDmitri Tikhonov    FIND_LIBRARY(EVENT_LIB libevent.a libevent.so)
195b1af35d6SDmitri TikhonovENDIF()
196b1af35d6SDmitri TikhonovIF(EVENT_LIB)
197b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "Found event: ${EVENT_LIB}")
198b1af35d6SDmitri TikhonovELSE()
199b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "libevent not found")
20050aadb33SDmitri TikhonovENDIF()
20150aadb33SDmitri Tikhonov
2025392f7a3SLiteSpeed Techadd_executable(http_server test/http_server.c test/prog.c test/test_common.c test/test_cert.c)
2030adf085aSDmitri Tikhonovadd_executable(md5_server test/md5_server.c test/prog.c test/test_common.c test/test_cert.c)
2040adf085aSDmitri Tikhonovadd_executable(md5_client test/md5_client.c test/prog.c test/test_common.c test/test_cert.c)
2050adf085aSDmitri Tikhonovadd_executable(echo_server test/echo_server.c test/prog.c test/test_common.c test/test_cert.c)
2060adf085aSDmitri Tikhonovadd_executable(echo_client test/echo_client.c test/prog.c test/test_common.c test/test_cert.c)
2075392f7a3SLiteSpeed Tech
2088ca33e0eSDmitri Tikhonov
2098ca33e0eSDmitri TikhonovSET(LIBS lsquic ${EVENT_LIB} ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${ZLIB_LIB} ${LIBS})
210b93f59beSBob Perper
21146a5b669SDmitri TikhonovIF (NOT MSVC)
2125392f7a3SLiteSpeed Tech
213c51ce338SDmitri Tikhonovadd_executable(http_client
214c51ce338SDmitri Tikhonov    test/http_client.c
215c51ce338SDmitri Tikhonov    test/prog.c
216c51ce338SDmitri Tikhonov    test/test_common.c
2175392f7a3SLiteSpeed Tech    test/test_cert.c
218c51ce338SDmitri Tikhonov)
2198ca33e0eSDmitri TikhonovLIST(APPEND LIBS pthread m)
22050aadb33SDmitri Tikhonov
2215d77f141SAmol Deshpande#MSVC
222461e84d8SAmol DeshpandeELSE()
2235392f7a3SLiteSpeed Tech
224461e84d8SAmol Deshpandeadd_executable(http_client
225461e84d8SAmol Deshpande    test/http_client.c
226461e84d8SAmol Deshpande    test/prog.c
227db236e90SDmitri Tikhonov    test/test_common.c
228461e84d8SAmol Deshpande    wincompat/getopt.c
229461e84d8SAmol Deshpande    wincompat/getopt1.c
230461e84d8SAmol Deshpande)
2318ca33e0eSDmitri TikhonovLIST(APPEND LIBS ws2_32)
232461e84d8SAmol Deshpande
233461e84d8SAmol DeshpandeENDIF()
234461e84d8SAmol Deshpande
2358ca33e0eSDmitri TikhonovTARGET_LINK_LIBRARIES(http_client ${LIBS})
2365392f7a3SLiteSpeed TechTARGET_LINK_LIBRARIES(http_server ${LIBS})
2370adf085aSDmitri TikhonovTARGET_LINK_LIBRARIES(md5_server  ${LIBS})
2380adf085aSDmitri TikhonovTARGET_LINK_LIBRARIES(md5_client  ${LIBS})
2390adf085aSDmitri TikhonovTARGET_LINK_LIBRARIES(echo_server ${LIBS})
2400adf085aSDmitri TikhonovTARGET_LINK_LIBRARIES(echo_client ${LIBS})
24150aadb33SDmitri Tikhonov
24250aadb33SDmitri Tikhonovadd_subdirectory(src)
24350aadb33SDmitri Tikhonov
244bfc7bfd8SDmitri Tikhonovadd_subdirectory(test)
24550aadb33SDmitri Tikhonov
2465392f7a3SLiteSpeed TechIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
2477fbe101bSDmitri Tikhonov    # Our test framework relies on assertions, only compile if assertions are
2487fbe101bSDmitri Tikhonov    # enabled.
2497fbe101bSDmitri Tikhonov    #
2507fbe101bSDmitri Tikhonov    enable_testing()
2517fbe101bSDmitri TikhonovENDIF()
2527fbe101bSDmitri Tikhonov
25350aadb33SDmitri Tikhonov
25450aadb33SDmitri TikhonovADD_CUSTOM_TARGET(docs doxygen dox.cfg)
255