Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
446 lines (386 loc) · 15.1 KB
/
Copy pathCMakeLists.txt
File metadata and controls
446 lines (386 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
cmake_minimum_required(VERSION 3.10)
# set the project name
project(SIPp)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pedantic -Wno-deprecated-copy -Wno-array-bounds")
endif()
# Include binary dir first, where we add generated config.h and
# version.h. If nothing is found there (release tar) use the version.h
# from the source.
include_directories(
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include)
enable_testing()
if(BUILD_STATIC)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif(BUILD_STATIC)
function(optional_library LIB_NAME DESCRIPTION)
set(USE_${LIB_NAME} "DEFAULT" CACHE STRING "${DESCRIPTION}")
string(TOLOWER "${LIB_NAME}" LIB_LOWER)
if(USE_${LIB_NAME} STREQUAL "ON" OR USE_${LIB_NAME} STREQUAL "1")
find_library(${LIB_NAME}_LIBRARY ${LIB_LOWER})
if(NOT ${LIB_NAME}_LIBRARY)
message(FATAL_ERROR "USE_${LIB_NAME} is ON, but lib${LIB_LOWER} is missing: install its development package")
endif()
set(USE_${LIB_NAME} 1)
elseif(USE_${LIB_NAME} STREQUAL "OFF" OR USE_${LIB_NAME} STREQUAL "0")
set(USE_${LIB_NAME} 0)
else()
find_library(${LIB_NAME}_LIBRARY ${LIB_LOWER})
if(${LIB_NAME}_LIBRARY)
message(STATUS "${LIB_NAME} is enabled")
set(USE_${LIB_NAME} 1 PARENT_SCOPE)
else()
message(STATUS "${LIB_NAME} is disabled")
set(USE_${LIB_NAME} 0 PARENT_SCOPE)
endif()
endif()
endfunction()
# A three-way cache option: AUTO, ON or OFF. ON and OFF may be given in any
# spelling CMake takes for a boolean (1, TRUE, YES, 0, FALSE, NO, ...), as
# packagers often pass -DOPTION=1. Sets <VAR>_MODE to AUTO, ON or OFF.
function(tristate_option VAR DESCRIPTION)
set(${VAR} "AUTO" CACHE STRING "${DESCRIPTION}")
set_property(CACHE ${VAR} PROPERTY STRINGS AUTO ON OFF)
string(TOUPPER "${${VAR}}" value)
if(value STREQUAL "AUTO")
set(${VAR}_MODE AUTO PARENT_SCOPE)
elseif(value MATCHES "^(ON|1|TRUE|YES|Y)$")
set(${VAR}_MODE ON PARENT_SCOPE)
elseif(value MATCHES "^(OFF|0|FALSE|NO|N)$")
set(${VAR}_MODE OFF PARENT_SCOPE)
else()
message(FATAL_ERROR "${VAR} must be AUTO, ON or OFF, not '${${VAR}}'")
endif()
endfunction()
function(add_sipp_target TARGET_NAME)
cmake_parse_arguments(SIPP "EXCLUDE_FROM_ALL" "" "SOURCES;EXTRA_LIBRARIES;EXTRA_INCLUDES;COMPILE_DEFINITIONS" ${ARGN})
if(SIPP_EXCLUDE_FROM_ALL)
add_executable(${TARGET_NAME} EXCLUDE_FROM_ALL ${all_SRCS} ${SIPP_SOURCES})
else()
add_executable(${TARGET_NAME} ${all_SRCS} ${SIPP_SOURCES})
endif()
if(SIPP_COMPILE_DEFINITIONS)
target_compile_definitions(${TARGET_NAME} PUBLIC ${SIPP_COMPILE_DEFINITIONS})
endif()
if(SIPP_EXTRA_INCLUDES)
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC ${SIPP_EXTRA_INCLUDES})
endif()
apply_common_sipp_dependencies(${TARGET_NAME})
if(SIPP_EXTRA_LIBRARIES)
target_link_libraries(${TARGET_NAME} ${SIPP_EXTRA_LIBRARIES})
endif()
endfunction()
function(apply_common_sipp_dependencies TARGET_NAME)
# Basic libraries that all targets need
target_link_libraries(${TARGET_NAME} dl pthread ${PUGIXML_LIBRARIES})
# Enable symbol export for plugins
set_target_properties(${TARGET_NAME} PROPERTIES ENABLE_EXPORTS ON)
# Conditional libraries based on configuration
if(CURSES_LIBRARY)
target_link_libraries(${TARGET_NAME} ${CURSES_LIBRARY})
if(CURSES_LIBRARY_INCLUDE_DIRS OR TINFO_LIBRARY_INCLUDE_DIRS)
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC ${CURSES_LIBRARY_INCLUDE_DIRS} ${TINFO_LIBRARY_INCLUDE_DIRS})
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND TINFO_LIBRARY)
target_link_libraries(${TARGET_NAME} ${TINFO_LIBRARY})
endif()
if(RT_LIBRARY)
target_link_libraries(${TARGET_NAME} ${RT_LIBRARY})
endif()
if(RESOLV_LIBRARY)
target_link_libraries(${TARGET_NAME} ${RESOLV_LIBRARY})
endif()
if(USE_GSL AND GSL_LIBRARY AND GSLCBLAS_LIBRARY)
target_link_libraries(${TARGET_NAME} ${GSL_LIBRARY} ${GSLCBLAS_LIBRARY})
endif()
if(BUILD_STATIC AND SSL_STATIC_LIBRARIES)
set(SSL_LIBRARIES ${SSL_STATIC_LIBRARIES})
endif()
target_link_libraries(${TARGET_NAME} ${SSL_LIBRARIES})
if(SSL_LIBRARY_DIRS)
target_link_directories(${TARGET_NAME} PRIVATE ${SSL_LIBRARY_DIRS})
endif()
if(SSL_INCLUDE_DIRS)
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC ${SSL_INCLUDE_DIRS})
endif()
if(USE_PCAP AND PCAP_LIBRARIES)
target_link_libraries(${TARGET_NAME} ${PCAP_LIBRARIES})
if(CAP_LIBRARY)
target_link_libraries(${TARGET_NAME} ${CAP_LIBRARY})
endif()
endif()
if(USE_SCTP AND SCTP_LIBRARY)
target_link_libraries(${TARGET_NAME} ${SCTP_LIBRARY})
endif()
if(LUA_FOUND)
if(BUILD_STATIC AND LUA_STATIC_LIBRARIES)
target_link_libraries(${TARGET_NAME} ${LUA_STATIC_LIBRARIES})
else()
target_link_libraries(${TARGET_NAME} ${LUA_LIBRARIES})
endif()
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC ${LUA_INCLUDE_DIRS})
if(LUA_LIBRARY_DIRS)
target_link_directories(${TARGET_NAME} PRIVATE ${LUA_LIBRARY_DIRS})
endif()
endif()
if(BUILD_STATIC AND MSYS)
target_link_libraries(${TARGET_NAME} crypt32)
endif()
endfunction()
optional_library(SCTP "Build with SCTP support")
optional_library(PCAP "Build with PCAP playback support")
optional_library(GSL "Build with improved statistical support")
option(USE_WOLFSSL "Use WolfSSL instead of OpenSSL" OFF)
option(TLS_KEY_LOGGING "Enable TLS session key logging (OpenSSL only)" OFF)
find_package(PkgConfig QUIET) # import pkg_check_modules() and friends
# AUTO builds with Lua if pkg-config finds it.
tristate_option(USE_LUA "Build with Lua scripting: AUTO, ON, or OFF")
if(NOT USE_LUA_MODE STREQUAL "OFF")
if(PKG_CONFIG_FOUND)
pkg_search_module(LUA lua5.4 lua-5.4 lua5.3 lua-5.3 lua)
endif()
if(LUA_FOUND)
message(STATUS "Lua is enabled")
add_definitions("-DUSE_LUA")
elseif(USE_LUA_MODE STREQUAL "ON")
message(FATAL_ERROR "USE_LUA is ON, but pkg-config cannot find Lua: install its development package")
else()
message(STATUS "Lua is disabled")
endif()
endif()
# AUTO takes the bundled copy if its submodule is there, else the system one.
tristate_option(USE_SYSTEM_PUGIXML "Use system pugixml: AUTO, ON, or OFF")
set(BUNDLED_PUGIXML ${PROJECT_SOURCE_DIR}/third_party/pugixml)
if(USE_SYSTEM_PUGIXML_MODE STREQUAL "OFF" OR
(USE_SYSTEM_PUGIXML_MODE STREQUAL "AUTO" AND EXISTS ${BUNDLED_PUGIXML}/CMakeLists.txt))
if(NOT EXISTS ${BUNDLED_PUGIXML}/CMakeLists.txt)
message(FATAL_ERROR "USE_SYSTEM_PUGIXML is OFF, but the bundled pugixml is missing: run 'git submodule update --init'")
endif()
message(STATUS "Using bundled pugixml")
set(PUGIXML_SOURCES ${BUNDLED_PUGIXML}/src/pugixml.cpp)
include_directories(${BUNDLED_PUGIXML}/src)
set(PUGIXML_LIBRARIES "")
else()
pkg_check_modules(PUGIXML pugixml)
if(PUGIXML_FOUND)
message(STATUS "Using system pugixml")
include_directories(SYSTEM ${PUGIXML_INCLUDE_DIRS})
elseif(USE_SYSTEM_PUGIXML_MODE STREQUAL "ON")
message(FATAL_ERROR "USE_SYSTEM_PUGIXML is ON, but pkg-config cannot find pugixml: install its development package")
else()
message(FATAL_ERROR "pugixml is required. Either install the library or run 'git submodule update --init'")
endif()
endif()
if(USE_WOLFSSL)
pkg_search_module(SSL REQUIRED wolfssl>=3.15.0)
if(TLS_KEY_LOGGING)
message(WARNING "TLS key logging is only supported with OpenSSL, ignoring")
set(TLS_KEY_LOGGING OFF)
endif()
add_definitions("-DUSE_WOLFSSL" "-DOPENSSL_ALL")
else()
pkg_search_module(SSL REQUIRED openssl>=1.1.1)
add_definitions("-DUSE_OPENSSL")
if(TLS_KEY_LOGGING)
add_definitions("-DUSE_OPENSSL_KL")
endif()
endif()
option(BUILD_STATIC "Build a statically-linked binary" OFF)
option(USE_LOCAL_IP_HINTS "Build with local ip hints priority" OFF)
option(USE_ASAN "Build with AddressSanitizer support" OFF)
if(USE_ASAN)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
message(STATUS "AddressSanitizer enabled")
else()
message(WARNING "AddressSanitizer is only supported with GCC or Clang")
set(USE_ASAN OFF)
endif()
endif(USE_ASAN)
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
)
include(${CMAKE_ROOT}/Modules/CheckCXXSourceCompiles.cmake)
include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
include(${CMAKE_ROOT}/Modules/CheckSymbolExists.cmake)
include(${CMAKE_ROOT}/Modules/CheckStructHasMember.cmake)
CHECK_INCLUDE_FILE("endian.h" HAVE_ENDIAN_H)
CHECK_INCLUDE_FILE("sys/endian.h" HAVE_SYS_ENDIAN_H)
CHECK_INCLUDE_FILE("libkern/OSByteOrder.h" HAVE_OSBYTEORDER_H)
CHECK_INCLUDE_FILE("sys/epoll.h" HAVE_EPOLL)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
CHECK_SYMBOL_EXISTS(recvmmsg "sys/socket.h" HAVE_RECVMMSG)
unset(CMAKE_REQUIRED_DEFINITIONS)
CHECK_STRUCT_HAS_MEMBER("struct udphdr" uh_sport "sys/types.h;netinet/udp.h" HAVE_UDP_UH_PREFIX)
configure_file("${PROJECT_SOURCE_DIR}/include/config.h.in"
"${PROJECT_BINARY_DIR}/config.h" )
# The built-in scenarios (-sn, -sd) are docs/*.xml, each made into a raw
# string literal for src/scenario.cpp to include.
file(GLOB builtin_scenarios "${PROJECT_SOURCE_DIR}/docs/*.xml")
foreach(xml ${builtin_scenarios})
get_filename_component(name "${xml}" NAME)
file(READ "${xml}" content)
file(WRITE "${PROJECT_BINARY_DIR}/docs/${name}.inc.tmp" "R\"SIPP(${content})SIPP\",\n")
# Rewritten only when it changed, so scenario.cpp isn't rebuilt each time.
configure_file("${PROJECT_BINARY_DIR}/docs/${name}.inc.tmp"
"${PROJECT_BINARY_DIR}/docs/${name}.inc" COPYONLY)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${xml}")
endforeach()
list(REMOVE_ITEM all_SRCS
"${PROJECT_SOURCE_DIR}/src/sipp_unittest.cpp")
list(REMOVE_ITEM all_SRCS
"${PROJECT_SOURCE_DIR}/src/sipp.cpp")
if(NOT USE_PCAP)
list(REMOVE_ITEM all_SRCS
"${PROJECT_SOURCE_DIR}/src/prepare_pcap.c"
"${PROJECT_SOURCE_DIR}/src/send_packets.c")
endif(NOT USE_PCAP)
if(USE_PCAP)
add_definitions("-DPCAPPLAY")
endif(USE_PCAP)
if(USE_LOCAL_IP_HINTS)
add_definitions("-DUSE_LOCAL_IP_HINTS")
endif(USE_LOCAL_IP_HINTS)
if(USE_GSL)
add_definitions("-DHAVE_GSL")
endif(USE_GSL)
if(USE_SCTP)
add_definitions("-DUSE_SCTP")
endif(USE_SCTP)
# Find all libraries before creating targets
if(PKG_CONFIG_FOUND)
pkg_search_module(CURSES_LIBRARY ncursesw cursesw ncurses curses)
if(CURSES_LIBRARY_FOUND)
set(CURSES_LIBRARY ${CURSES_LIBRARY_LIBRARIES})
endif()
pkg_search_module(TINFO_LIBRARY tinfo)
if(TINFO_LIBRARY_FOUND)
set(TINFO_LIBRARY ${TINFO_LIBRARY_LIBRARIES})
endif()
endif()
if(NOT CURSES_LIBRARY)
find_library(CURSES_LIBRARY NAMES ncursesw cursesw ncurses curses)
endif()
if(NOT TINFO_LIBRARY)
find_library(TINFO_LIBRARY NAMES tinfo)
endif()
if(NOT CURSES_LIBRARY)
message(FATAL_ERROR "libcurses / libncurses was not found; please install devel package")
endif()
if(BUILD_STATIC)
add_definitions("-DNCURSES_STATIC")
endif(BUILD_STATIC)
find_library(RT_LIBRARY NAMES rt)
# res_query() for DNS NAPTR and SRV: in libresolv on macOS and glibc before 2.34,
# in libc on musl.
find_library(RESOLV_LIBRARY NAMES resolv)
if(USE_GSL)
find_library(GSLCBLAS_LIBRARY gslcblas)
if(NOT GSLCBLAS_LIBRARY)
message(FATAL_ERROR "USE_GSL is on, but libgslcblas is missing: install the GSL development package")
endif()
endif(USE_GSL)
if(USE_PCAP)
set(PCAP_LIBRARIES ${PCAP_LIBRARY})
if(PKG_CONFIG_FOUND)
pkg_search_module(PCAP libpcap)
if(BUILD_STATIC AND PCAP_STATIC_LIBRARIES)
set(PCAP_LIBRARIES ${PCAP_STATIC_LIBRARIES})
find_library(CAP_LIBRARY cap)
endif()
endif()
endif(USE_PCAP)
if(USE_SCTP)
find_library(SCTP_LIBRARY sctp)
endif()
# add the main executable
add_sipp_target(sipp
SOURCES "${PROJECT_SOURCE_DIR}/src/sipp.cpp" ${PUGIXML_SOURCES}
)
target_compile_features(sipp PUBLIC cxx_auto_type cxx_range_for)
# AUTO takes the bundled copy if its submodule is there, else the system one,
# and builds no unit tests if there is neither.
tristate_option(USE_SYSTEM_GTEST "Use system GTest: AUTO, ON, or OFF")
set(BUNDLED_GTEST ${PROJECT_SOURCE_DIR}/gtest/googletest)
if(USE_SYSTEM_GTEST_MODE STREQUAL "OFF" OR
(USE_SYSTEM_GTEST_MODE STREQUAL "AUTO" AND EXISTS ${BUNDLED_GTEST}))
if(NOT EXISTS ${BUNDLED_GTEST})
message(FATAL_ERROR "USE_SYSTEM_GTEST is OFF, but the bundled GTest is missing: run 'git submodule update --init'")
endif()
message(STATUS "Using bundled GTest")
include(GoogleTest)
add_sipp_target(sipp_unittest
EXCLUDE_FROM_ALL
SOURCES "${PROJECT_SOURCE_DIR}/src/sipp_unittest.cpp" ${PUGIXML_SOURCES}
"${BUNDLED_GTEST}/src/gtest-all.cc"
COMPILE_DEFINITIONS "-DGTEST"
EXTRA_INCLUDES ${BUNDLED_GTEST}
${BUNDLED_GTEST}/include
)
gtest_discover_tests(sipp_unittest TEST_PREFIX sipp_unittests:)
else()
find_package(GTest QUIET)
if(GTest_FOUND)
message(STATUS "Using system GTest")
include(GoogleTest)
add_sipp_target(sipp_unittest
EXCLUDE_FROM_ALL
SOURCES "${PROJECT_SOURCE_DIR}/src/sipp_unittest.cpp" ${PUGIXML_SOURCES}
COMPILE_DEFINITIONS "-DGTEST"
)
target_link_libraries(sipp_unittest GTest::gtest GTest::gtest_main)
gtest_discover_tests(sipp_unittest TEST_PREFIX sipp_unittests:)
elseif(USE_SYSTEM_GTEST_MODE STREQUAL "ON")
message(FATAL_ERROR "USE_SYSTEM_GTEST is ON, but GTest was not found: install its development package")
else()
message(WARNING "GTest not found. Run 'git submodule update --init' or install libgtest-dev")
endif()
endif()
# add version
find_package(Git)
file(WRITE ${PROJECT_SOURCE_DIR}/include/version.cmake
"execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --always --first-parent
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
configure_file(\${SRC} \${DST} @ONLY)
")
if(EXISTS ${PROJECT_SOURCE_DIR}/.git)
add_custom_target(
version
${CMAKE_COMMAND} -D SRC=${PROJECT_SOURCE_DIR}/include/version.h.in
-D DST=${PROJECT_BINARY_DIR}/version.h
-P ${PROJECT_SOURCE_DIR}/include/version.cmake
)
add_dependencies(sipp version)
if(TARGET sipp_unittest)
add_dependencies(sipp_unittest version)
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT TINFO_LIBRARY)
message(WARNING "libtinfo was not found -- please install package if linking fails")
endif()
if(DEBUG)
add_definitions("-g -O0")
endif(DEBUG)
if(SIPP_MAX_MSG_SIZE)
add_definitions("-DSIPP_MAX_MSG_SIZE=${SIPP_MAX_MSG_SIZE}")
endif(SIPP_MAX_MSG_SIZE)
if(CYGWIN)
add_definitions("-D_GNU_SOURCE")
endif(CYGWIN)
install(TARGETS sipp DESTINATION bin)
option(INSTALL_SIPP_MULTI "Install tools/sipp_multi.py next to sipp" OFF)
if(INSTALL_SIPP_MULTI)
install(PROGRAMS "${PROJECT_SOURCE_DIR}/tools/sipp_multi.py" DESTINATION bin)
endif()