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