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