CMakeLists.txt revision b1af35d6
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
2150aadb33SDmitri Tikhonov
221e75f938SDmitri TikhonovMESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
2350aadb33SDmitri Tikhonov
24b93f59beSBob Perper
2546a5b669SDmitri TikhonovIF (NOT MSVC)
26461e84d8SAmol Deshpande
2750aadb33SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter")
2850aadb33SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer")
2950aadb33SDmitri Tikhonov
30e0197994SDmitri TikhonovIF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3)
3150aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers")
3250aadb33SDmitri TikhonovENDIF()
331e75f938SDmitri TikhonovIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
3450aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3")
35db8ac9eeSDmitri Tikhonov    IF(CMAKE_C_COMPILER MATCHES "clang" AND
36db8ac9eeSDmitri Tikhonov                        NOT "$ENV{TRAVIS}" MATCHES "^true$")
3750aadb33SDmitri Tikhonov        SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address")
3850aadb33SDmitri Tikhonov    ENDIF()
3950aadb33SDmitri Tikhonov    # Uncomment to enable fault injection testing via libfiu:
4050aadb33SDmitri Tikhonov    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
41ccd74161SDmitri Tikhonov    #SET(LIBS ${LIBS} fiu)
4250aadb33SDmitri TikhonovELSE()
4350aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0")
4450aadb33SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DNDEBUG")
4550aadb33SDmitri Tikhonov    # Comment out the following line to compile out debug messages:
4650aadb33SDmitri Tikhonov    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
4750aadb33SDmitri TikhonovENDIF()
4850aadb33SDmitri Tikhonov
4967b0dc15SDmitri TikhonovIF(LSQUIC_PROFILE EQUAL 1)
5067b0dc15SDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg")
5167b0dc15SDmitri TikhonovENDIF()
5267b0dc15SDmitri Tikhonov
5316a9b66aSDmitri TikhonovIF(LSQUIC_COVERAGE EQUAL 1)
5416a9b66aSDmitri Tikhonov    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage")
5516a9b66aSDmitri TikhonovENDIF()
5616a9b66aSDmitri Tikhonov
5750aadb33SDmitri TikhonovIF(MY_CMAKE_FLAGS MATCHES "fsanitize=address")
5850aadb33SDmitri Tikhonov    MESSAGE(STATUS "AddressSanitizer is ON")
5950aadb33SDmitri TikhonovELSE()
6050aadb33SDmitri Tikhonov    MESSAGE(STATUS "AddressSanitizer is OFF")
6150aadb33SDmitri TikhonovENDIF()
6250aadb33SDmitri Tikhonov
63461e84d8SAmol Deshpande#MSVC
64461e84d8SAmol DeshpandeELSE()
652d296031SDmitri TikhonovSET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214")	# nonstandard extension used: bit field types other than int
66461e84d8SAmol 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")
671e75f938SDmitri TikhonovIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
68b5d685fcSBob Perper    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od")
69461e84d8SAmol Deshpande    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
70ccd74161SDmitri Tikhonov    #SET(LIBS ${LIBS} fiu)
71461e84d8SAmol DeshpandeELSE()
72b5d685fcSBob Perper    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox")
73461e84d8SAmol Deshpande    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DNDEBUG")
74461e84d8SAmol Deshpande    # Comment out the following line to compile out debug messages:
75461e84d8SAmol Deshpande    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
76461e84d8SAmol DeshpandeENDIF()
77461e84d8SAmol Deshpande
78461e84d8SAmol DeshpandeENDIF() #MSVC
79461e84d8SAmol Deshpande
8050aadb33SDmitri Tikhonovset(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}")
8150aadb33SDmitri Tikhonov
8250aadb33SDmitri TikhonovMESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}")
8350aadb33SDmitri Tikhonov
84b1af35d6SDmitri Tikhonov# This must be done before adding other include directories to take
85b1af35d6SDmitri Tikhonov# precedence over header files from other SSL installs.
86b1af35d6SDmitri TikhonovFIND_PATH(BORINGSSL_INCLUDE_DIR NAMES openssl/ssl.h
87b1af35d6SDmitri Tikhonov            PATHS ${BORINGSSL_DIR}/include
88b1af35d6SDmitri Tikhonov            NO_DEFAULT_PATH)
89b1af35d6SDmitri TikhonovIF (BORINGSSL_INCLUDE_DIR)
90b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE_DIR}")
91b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE_DIR})
92b1af35d6SDmitri TikhonovELSE()
93b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "BoringSSL headers not found")
94e0197994SDmitri TikhonovENDIF()
9550aadb33SDmitri Tikhonov
96b1af35d6SDmitri TikhonovFOREACH(BORINGSSL_LIB ssl crypto decrepit)
97b1af35d6SDmitri Tikhonov    IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
98b1af35d6SDmitri Tikhonov        FIND_LIBRARY(BORINGSSL_LIB_${BORINGSSL_LIB}
99b1af35d6SDmitri Tikhonov            NAMES ${BORINGSSL_LIB}
100b1af35d6SDmitri Tikhonov            PATHS ${BORINGSSL_DIR}/${BORINGSSL_LIB}
101b1af35d6SDmitri Tikhonov            PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
102b1af35d6SDmitri Tikhonov            NO_DEFAULT_PATH)
103b1af35d6SDmitri Tikhonov    ELSE()
104b1af35d6SDmitri Tikhonov        FIND_LIBRARY(BORINGSSL_LIB_${BORINGSSL_LIB}
105b1af35d6SDmitri Tikhonov            NAMES lib${BORINGSSL_LIB}.a
106b1af35d6SDmitri Tikhonov            PATHS ${BORINGSSL_DIR}/${BORINGSSL_LIB}
107b1af35d6SDmitri Tikhonov            NO_DEFAULT_PATH)
108b1af35d6SDmitri Tikhonov    ENDIF()
109b1af35d6SDmitri Tikhonov    IF(BORINGSSL_LIB_${BORINGSSL_LIB})
110b1af35d6SDmitri Tikhonov        MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${BORINGSSL_LIB}}")
111b1af35d6SDmitri Tikhonov    ELSE()
112b1af35d6SDmitri Tikhonov        MESSAGE(STATUS "${BORINGSSL_LIB} library not found")
113b1af35d6SDmitri Tikhonov    ENDIF()
114b1af35d6SDmitri TikhonovENDFOREACH()
115461e84d8SAmol Deshpande
116b1af35d6SDmitri TikhonovIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
11750aadb33SDmitri Tikhonov    # Find libevent on FreeBSD:
118b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES( /usr/local/include )
119b1af35d6SDmitri Tikhonov    LINK_DIRECTORIES( /usr/local/lib )
120b1af35d6SDmitri TikhonovENDIF()
121b1af35d6SDmitri Tikhonov
122b1af35d6SDmitri Tikhonov# Find zlib and libevent header files and library files
123b1af35d6SDmitri Tikhonov# TODO: libevent is not strictly necessary to build the library.
124b1af35d6SDmitri TikhonovFIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h)
125b1af35d6SDmitri TikhonovIF (ZLIB_INCLUDE_DIR)
126b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
127b1af35d6SDmitri TikhonovELSE()
128b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "zlib.h was not found")
129b1af35d6SDmitri TikhonovENDIF()
130b1af35d6SDmitri TikhonovIF (CMAKE_SYSTEM_NAME STREQUAL Windows)
131b1af35d6SDmitri Tikhonov    FIND_LIBRARY(ZLIB_LIB zlib)
132b1af35d6SDmitri TikhonovELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin)
133b1af35d6SDmitri Tikhonov    # XXX somehow FIND_LIBRARY() does not find zlib on Travis?
134b1af35d6SDmitri Tikhonov    SET(ZLIB_LIB z)
135b1af35d6SDmitri TikhonovELSE()
136b1af35d6SDmitri Tikhonov    FIND_LIBRARY(ZLIB_LIB libz.a)
137b1af35d6SDmitri TikhonovENDIF()
138b1af35d6SDmitri TikhonovIF(ZLIB_LIB)
139b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}")
140b1af35d6SDmitri TikhonovELSE()
141b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "zlib not found")
142b1af35d6SDmitri TikhonovENDIF()
143b1af35d6SDmitri TikhonovFIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h)
144b1af35d6SDmitri TikhonovIF (EVENT_INCLUDE_DIR)
145b1af35d6SDmitri Tikhonov    INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR})
146b1af35d6SDmitri TikhonovELSE()
147b1af35d6SDmitri Tikhonov    MESSAGE(FATAL_ERROR "event2/event.h was not found")
148b1af35d6SDmitri TikhonovENDIF()
149b1af35d6SDmitri TikhonovIF (CMAKE_SYSTEM_NAME STREQUAL Windows)
150b1af35d6SDmitri Tikhonov    FIND_LIBRARY(EVENT_LIB event)
151b1af35d6SDmitri TikhonovELSE()
152b1af35d6SDmitri Tikhonov    FIND_LIBRARY(EVENT_LIB libevent.a)
153b1af35d6SDmitri TikhonovENDIF()
154b1af35d6SDmitri TikhonovIF(EVENT_LIB)
155b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "Found event: ${EVENT_LIB}")
156b1af35d6SDmitri TikhonovELSE()
157b1af35d6SDmitri Tikhonov    MESSAGE(STATUS "libevent not found")
15850aadb33SDmitri TikhonovENDIF()
15950aadb33SDmitri Tikhonov
160b1af35d6SDmitri TikhonovSET(CMAKE_INCLUDE_CURRENT_DIR ON)
161b1af35d6SDmitri Tikhonovinclude_directories( include src/lshpack)
162b93f59beSBob Perper
16346a5b669SDmitri TikhonovIF (NOT MSVC)
164c51ce338SDmitri Tikhonovadd_executable(http_client
165c51ce338SDmitri Tikhonov    test/http_client.c
166c51ce338SDmitri Tikhonov    test/prog.c
167c51ce338SDmitri Tikhonov    test/test_common.c
168c51ce338SDmitri Tikhonov)
169b1af35d6SDmitri TikhonovTARGET_LINK_LIBRARIES(http_client lsquic ${EVENT_LIB} pthread
170b1af35d6SDmitri Tikhonov    ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${LIBS} ${ZLIB_LIB} m)
17150aadb33SDmitri Tikhonov
1725d77f141SAmol Deshpande#MSVC
173461e84d8SAmol DeshpandeELSE()
174461e84d8SAmol Deshpandeadd_executable(http_client
175461e84d8SAmol Deshpande    test/http_client.c
176461e84d8SAmol Deshpande    test/prog.c
177db236e90SDmitri Tikhonov    test/test_common.c
178461e84d8SAmol Deshpande    wincompat/getopt.c
179461e84d8SAmol Deshpande    wincompat/getopt1.c
180461e84d8SAmol Deshpande)
181461e84d8SAmol Deshpande
182b93f59beSBob Perpertarget_link_libraries(http_client
1832d296031SDmitri Tikhonov        lsquic
184b1af35d6SDmitri Tikhonov        ${EVENT_LIB}
185b1af35d6SDmitri Tikhonov        ${ZLIB_LIB}
186b1af35d6SDmitri Tikhonov		${BORINGSSL_LIB_ssl}
187b1af35d6SDmitri Tikhonov		${BORINGSSL_LIB_crypto}
1885d77f141SAmol Deshpande		ws2_32
189ccd74161SDmitri Tikhonov		${LIBS} )
190461e84d8SAmol Deshpande
191461e84d8SAmol DeshpandeENDIF()
192461e84d8SAmol Deshpande
193ccd74161SDmitri Tikhonov#target_link_libraries(http_client lsquic event pthread libssl.a libcrypto.a ${LIBS} z m)
19450aadb33SDmitri Tikhonov
19550aadb33SDmitri Tikhonovadd_subdirectory(src)
19650aadb33SDmitri Tikhonov
197bfc7bfd8SDmitri Tikhonovadd_subdirectory(test)
19850aadb33SDmitri Tikhonov
1991e75f938SDmitri TikhonovIF(NOT (CMAKE_C_FLAGS MATCHES "-DNDEBUG"))
2007fbe101bSDmitri Tikhonov    # Our test framework relies on assertions, only compile if assertions are
2017fbe101bSDmitri Tikhonov    # enabled.
2027fbe101bSDmitri Tikhonov    #
2037fbe101bSDmitri Tikhonov    enable_testing()
2047fbe101bSDmitri TikhonovENDIF()
2057fbe101bSDmitri Tikhonov
20650aadb33SDmitri Tikhonov
20750aadb33SDmitri TikhonovADD_CUSTOM_TARGET(docs doxygen dox.cfg)
208