CMakeLists.txt revision 966c52fc
1# Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE.
2cmake_minimum_required(VERSION 2.8)
3
4
5PROJECT(lsquic C)
6
7OPTION(LSQUIC_FIU "Use Fault Injection in Userspace (FIU)" OFF)
8OPTION(LSQUIC_BIN "Compile example binaries that use the library" ON)
9OPTION(LSQUIC_TESTS "Compile library unit tests" ON)
10OPTION(LSQUIC_SHARED_LIB "Compile as shared librarry" OFF)
11
12INCLUDE(GNUInstallDirs)
13
14IF (CMAKE_SYSTEM_NAME STREQUAL "Linux")
15    # If using older glibc, need to link with -lrt.  See clock_getres(2).
16    EXECUTE_PROCESS(
17        COMMAND ${PROJECT_SOURCE_DIR}/print-glibc-version.sh ${CMAKE_C_COMPILER}
18        OUTPUT_VARIABLE GLIBC_VERSION)
19    IF(NOT GLIBC_VERSION EQUAL "" AND GLIBC_VERSION VERSION_LESS 2.17)
20        SET(LIBS ${LIBS} rt)
21    ENDIF()
22ELSEIF (CMAKE_SYSTEM_NAME STREQUAL "Android")
23    # for android-ndk >= r19b
24    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "BOTH")
25    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE "BOTH")
26    set(CMAKE_FIND_ROOT_PATH_MODE_PATH "BOTH")
27ENDIF()
28
29IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
30    SET(CMAKE_BUILD_TYPE Debug)
31ENDIF()
32
33MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
34
35IF (NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-DLSQUIC_DEBUG_NEXT_ADV_TICK")
36    SET(MY_CMAKE_FLAGS "-DLSQUIC_DEBUG_NEXT_ADV_TICK=1")
37ENDIF()
38
39IF (NOT MSVC)
40
41SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter")
42SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer")
43INCLUDE(CheckCCompilerFlag)
44CHECK_C_COMPILER_FLAG(-Wno-implicit-fallthrough HAS_NO_IMPLICIT_FALLTHROUGH)
45IF (HAS_NO_IMPLICIT_FALLTHROUGH)
46    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-implicit-fallthrough")
47ENDIF()
48
49IF(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.3)
50    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-missing-field-initializers")
51ENDIF()
52
53IF(LSQUIC_FIU)
54    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
55    SET(LIBS ${LIBS} fiu)
56ENDIF()
57
58IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
59    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3")
60    IF(CMAKE_C_COMPILER MATCHES "clang" AND
61                        NOT "$ENV{TRAVIS}" MATCHES "^true$" AND
62                        NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-fsanitize")
63        SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address")
64        SET(LIBS ${LIBS} -fsanitize=address)
65    ENDIF()
66    # Uncomment to enable cleartext protocol mode (no crypto):
67    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_ENABLE_HANDSHAKE_DISABLE=1")
68ELSE()
69    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O3 -g0")
70    # Comment out the following line to compile out debug messages:
71    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
72ENDIF()
73
74IF(LSQUIC_PROFILE EQUAL 1)
75    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg")
76ENDIF()
77
78IF(LSQUIC_COVERAGE EQUAL 1)
79    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fprofile-arcs -ftest-coverage")
80ENDIF()
81
82IF(MY_CMAKE_FLAGS MATCHES "fsanitize=address")
83    MESSAGE(STATUS "AddressSanitizer is ON")
84ELSE()
85    MESSAGE(STATUS "AddressSanitizer is OFF")
86ENDIF()
87
88#MSVC
89ELSE()
90SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4100")	# unreferenced formal parameter
91SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4115")	# unnamed type definition in parentheses
92SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4116")	# named type definition in parentheses
93SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4146")	# unary minus operator applied to unsigned type, result still unsigned
94SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4132")	# const initialization
95SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4200")	# zero-sized array
96SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4204")	# non-constant aggregate initializer
97SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4244")	# integer conversion
98SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4245")	# conversion from 'int' to 'unsigned int', signed/unsigned mismatch
99SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4267")	# integer conversion
100SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4214")	# nonstandard extension used: bit field types other than int
101SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4295")	# array is too small to include a terminating null character
102SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4334")	# result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
103SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4456")	# hide previous local declaration
104SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4459")	# hide global declaration
105SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} /wd4706")	# assignment within conditional expression
106SET(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")
107IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
108    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od")
109    #SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
110    #SET(LIBS ${LIBS} fiu)
111ELSE()
112    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox")
113    # Comment out the following line to compile out debug messages:
114    #SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
115ENDIF()
116
117ENDIF() #MSVC
118
119set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}")
120
121MESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}")
122
123find_package(Perl)
124IF(NOT PERL_FOUND)
125    MESSAGE(FATAL_ERROR "Perl not found -- need it to generate source code")
126ENDIF()
127
128IF(LSQUIC_SHARED_LIB)
129    SET(LIB_SUFFIX .so)
130ELSE()
131    SET(LIB_SUFFIX .a)
132ENDIF()
133
134IF (NOT DEFINED BORINGSSL_INCLUDE AND DEFINED BORINGSSL_DIR)
135    FIND_PATH(BORINGSSL_INCLUDE NAMES openssl/ssl.h
136                PATHS ${BORINGSSL_DIR}/include
137                NO_DEFAULT_PATH)
138ENDIF()
139# This must be done before adding other include directories to take
140# precedence over header files from other SSL installs.
141
142IF (BORINGSSL_INCLUDE)
143    MESSAGE(STATUS "BoringSSL include directory ${BORINGSSL_INCLUDE}")
144    INCLUDE_DIRECTORIES(${BORINGSSL_INCLUDE})
145ELSE()
146    MESSAGE(FATAL_ERROR "BoringSSL headers not found")
147ENDIF()
148
149IF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR)
150    FOREACH(LIB_NAME ssl crypto decrepit)
151        IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
152            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
153                NAMES ${LIB_NAME}
154                PATHS ${BORINGSSL_DIR}/${LIB_NAME}
155		PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
156                NO_DEFAULT_PATH)
157        ELSE()
158            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
159                NAMES lib${LIB_NAME}${LIB_SUFFIX}
160                PATHS ${BORINGSSL_DIR}/${LIB_NAME}
161                NO_DEFAULT_PATH)
162        ENDIF()
163        IF(BORINGSSL_LIB_${LIB_NAME})
164            MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}")
165        ELSE()
166            MESSAGE(STATUS "${LIB_NAME} library not found")
167        ENDIF()
168    ENDFOREACH()
169
170ELSE()
171
172
173    FOREACH(LIB_NAME ssl crypto decrepit)
174        IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
175            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
176                NAMES ${LIB_NAME}
177                PATHS ${BORINGSSL_LIB}
178                PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
179                NO_DEFAULT_PATH)
180        ELSE()
181            FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
182                NAMES lib${LIB_NAME}${LIB_SUFFIX}
183                PATHS ${BORINGSSL_LIB}
184                PATH_SUFFIXES ${LIB_NAME}
185                NO_DEFAULT_PATH)
186        ENDIF()
187        IF(BORINGSSL_LIB_${LIB_NAME})
188            MESSAGE(STATUS "Found ${BORINGSSL_LIB} library: ${BORINGSSL_LIB_${LIB_NAME}}")
189        ELSE()
190            MESSAGE(STATUS "${BORINGSSL_LIB} library not found")
191        ENDIF()
192    ENDFOREACH()
193
194ENDIF()
195
196SET(CMAKE_INCLUDE_CURRENT_DIR ON)
197INCLUDE_DIRECTORIES(include)
198IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
199    # Find libevent on FreeBSD:
200    include_directories( /usr/local/include )
201    link_directories( /usr/local/lib )
202ENDIF()
203
204# Find zlib and libevent header files and library files
205# TODO: libevent is not strictly necessary to build the library.
206FIND_PATH(ZLIB_INCLUDE_DIR NAMES zlib.h)
207IF (ZLIB_INCLUDE_DIR)
208    INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
209ELSE()
210    MESSAGE(FATAL_ERROR "zlib.h was not found")
211ENDIF()
212IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
213    FIND_LIBRARY(ZLIB_LIB zlib)
214ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin)
215    # XXX somehow FIND_LIBRARY() does not find zlib on Travis?
216    SET(ZLIB_LIB z)
217ELSE()
218    FIND_LIBRARY(ZLIB_LIB libz${LIB_SUFFIX})
219ENDIF()
220IF(ZLIB_LIB)
221    MESSAGE(STATUS "Found zlib: ${ZLIB_LIB}")
222ELSE()
223    MESSAGE(STATUS "zlib not found")
224ENDIF()
225
226IF (LSQUIC_BIN OR LSQUIC_TESTS)
227FIND_PATH(EVENT_INCLUDE_DIR NAMES event2/event.h)
228IF (EVENT_INCLUDE_DIR)
229    INCLUDE_DIRECTORIES(${EVENT_INCLUDE_DIR})
230ELSE()
231    MESSAGE(FATAL_ERROR "event2/event.h was not found")
232ENDIF()
233IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
234    FIND_LIBRARY(EVENT_LIB event)
235ELSE()
236    FIND_LIBRARY(EVENT_LIB libevent${LIB_SUFFIX})
237    IF(NOT EVENT_LIB)
238        FIND_LIBRARY(EVENT_LIB libevent.so)
239    ENDIF()
240ENDIF()
241IF(EVENT_LIB)
242    MESSAGE(STATUS "Found event: ${EVENT_LIB}")
243ELSE()
244    MESSAGE(STATUS "libevent not found")
245ENDIF()
246SET(LIBS lsquic ${EVENT_LIB} ${BORINGSSL_LIB_ssl} ${BORINGSSL_LIB_crypto} ${ZLIB_LIB} ${LIBS})
247IF(MSVC)
248FIND_LIBRARY(PCRE_LIB pcre)
249IF(PCRE_LIB)
250    MESSAGE(STATUS "Found pcre: ${PCRE_LIB}")
251    LIST(APPEND LIBS ${PCRE_LIB})
252ELSE()
253    MESSAGE(STATUS "pcre not found: http_server won't work")
254ENDIF()
255FIND_LIBRARY(PCREPOSIX_LIB pcreposix)
256IF(PCREPOSIX_LIB)
257    MESSAGE(STATUS "Found pcreposix: ${PCREPOSIX_LIB}")
258    LIST(APPEND LIBS ${PCREPOSIX_LIB})
259ELSE()
260    MESSAGE(STATUS "pcreposix not found: http_server won't work")
261ENDIF()
262LIST(APPEND LIBS ws2_32)
263ENDIF()
264ENDIF()	# LSQUIC_BIN OR LSQUIC_TESTS
265
266IF(LSQUIC_BIN)
267IF(MSVC)
268    SET(GETOPT_C wincompat/getopt.c)
269ENDIF()
270add_executable(http_server bin/http_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
271IF(NOT MSVC)   #   TODO: port MD5 server and client to Windows
272add_executable(md5_server bin/md5_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
273add_executable(md5_client bin/md5_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
274ENDIF()
275add_executable(echo_server bin/echo_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
276add_executable(echo_client bin/echo_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
277add_executable(duck_server bin/duck_server.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
278add_executable(duck_client bin/duck_client.c bin/prog.c bin/test_common.c bin/test_cert.c ${GETOPT_C})
279
280
281IF (NOT MSVC)
282
283add_executable(http_client
284    bin/http_client.c
285    bin/prog.c
286    bin/test_common.c
287    bin/test_cert.c
288)
289LIST(APPEND LIBS pthread m)
290
291#MSVC
292ELSE()
293
294add_executable(http_client
295    bin/http_client.c
296    bin/prog.c
297    bin/test_common.c
298    bin/test_cert.c
299    wincompat/getopt.c
300    wincompat/getopt1.c
301)
302LIST(APPEND LIBS ws2_32)
303
304ENDIF()
305
306TARGET_LINK_LIBRARIES(http_client ${LIBS})
307TARGET_LINK_LIBRARIES(http_server ${LIBS})
308IF(NOT MSVC)
309TARGET_LINK_LIBRARIES(md5_server  ${LIBS})
310TARGET_LINK_LIBRARIES(md5_client  ${LIBS})
311ENDIF()
312TARGET_LINK_LIBRARIES(echo_server ${LIBS})
313TARGET_LINK_LIBRARIES(echo_client ${LIBS})
314TARGET_LINK_LIBRARIES(duck_server ${LIBS})
315TARGET_LINK_LIBRARIES(duck_client ${LIBS})
316
317add_subdirectory(bin)
318ENDIF() # LSQUIC_BIN
319
320add_subdirectory(src)
321
322IF(LSQUIC_TESTS AND CMAKE_BUILD_TYPE STREQUAL "Debug")
323    # Our test framework relies on assertions, only compile if assertions are
324    # enabled.
325    #
326    enable_testing()
327    add_subdirectory(tests)
328ENDIF()
329
330
331FIND_PROGRAM(SPHINX NAMES sphinx-build)
332IF(SPHINX)
333    ADD_CUSTOM_TARGET(docs
334        ${SPHINX} -b html
335        docs
336        docs/_build
337    )
338ELSE()
339    MESSAGE(STATUS "sphinx-build not found: docs won't be made")
340ENDIF()
341
342INSTALL(FILES
343    include/lsquic.h
344    include/lsquic_types.h
345    include/lsxpack_header.h
346    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lsquic
347)
348