blob: 37270d1d3795b2a53b59473004af6c5ddb945dc0 [file] [log] [blame]
Felix Weinrank71be0712015-08-26 13:52:37 +02001#
2# Copyright (C) 2015-2015 Oleg Alexeenkov
Felix Weinrank2718a592019-07-30 11:44:35 +02003# Copyright (C) 2015-2019 Felix Weinrank
Felix Weinrank71be0712015-08-26 13:52:37 +02004#
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# 3. Neither the name of the project nor the names of its contributors
16# may be used to endorse or promote products derived from this software
17# without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30#
31
Felix Weinrank14827002019-08-06 16:14:48 +020032project(usrsctplib C)
Felix Weinranka0ccbee2017-11-16 15:06:23 +010033cmake_minimum_required(VERSION 3.0)
Felix Weinrank69bbc202016-03-16 11:29:09 +010034
35# Debug build type as default
36if (NOT CMAKE_BUILD_TYPE)
Felix Weinrank9d180962017-11-07 15:46:54 +010037 message(STATUS "No build type selected, using DEBUG")
38 set(CMAKE_BUILD_TYPE "DEBUG")
Felix Weinrank6c321952017-07-12 01:06:33 +020039endif ()
40
41include(CheckStructHasMember)
Felix Weinrank5b9ee522017-07-14 15:45:16 +020042include(CheckIncludeFile)
43include(CheckIncludeFiles)
Felix Weinrank396ed2a2017-11-15 14:17:00 +010044include(CheckCCompilerFlag)
Felix Weinrank6c321952017-07-12 01:06:33 +020045
46#################################################
47# CHECK OPTIONS
48#################################################
49
Felix Weinrank396ed2a2017-11-15 14:17:00 +010050option(sctp_invariants "Add runtime checks" 0)
51if (sctp_invariants)
Felix Weinrank9d180962017-11-07 15:46:54 +010052 add_definitions(-DINVARIANTS)
Felix Weinrank6c321952017-07-12 01:06:33 +020053endif ()
54
Felix Weinrank396ed2a2017-11-15 14:17:00 +010055option(sctp_debug "Provide debug information" 1)
56if (sctp_debug)
Felix Weinrank9d180962017-11-07 15:46:54 +010057 add_definitions(-DSCTP_DEBUG)
Felix Weinrank6c321952017-07-12 01:06:33 +020058endif ()
59
Felix Weinrank396ed2a2017-11-15 14:17:00 +010060option(sctp_inet "Support IPv4" 1)
61if (sctp_inet)
Felix Weinrank9d180962017-11-07 15:46:54 +010062 add_definitions(-DINET)
Felix Weinrank6c321952017-07-12 01:06:33 +020063endif ()
64
Felix Weinrank396ed2a2017-11-15 14:17:00 +010065option(sctp_inet6 "Support IPv6" 1)
Felix Weinrank22306052017-11-15 14:33:14 +010066if (sctp_inet6)
Felix Weinrank9d180962017-11-07 15:46:54 +010067 add_definitions(-DINET6)
Felix Weinrank6c321952017-07-12 01:06:33 +020068endif ()
69
Michael Tuexendaaaf942018-05-21 18:19:52 +020070option(sctp_werror "Treat warning as error" 1)
Felix Weinrank6c321952017-07-12 01:06:33 +020071
Felix Weinrank396ed2a2017-11-15 14:17:00 +010072option(sctp_link_programs_static "Link example programs static" 0)
Felix Weinrank6c321952017-07-12 01:06:33 +020073
Felix Weinrank396ed2a2017-11-15 14:17:00 +010074option(sctp_build_programs "Build example programs" 1)
Felix Weinrank6c321952017-07-12 01:06:33 +020075
Felix Weinrank396ed2a2017-11-15 14:17:00 +010076option(sctp_sanitizer_address "Compile with address sanitizer" 0)
Felix Weinrank3f31a252017-07-18 09:47:59 +020077
Felix Weinrank396ed2a2017-11-15 14:17:00 +010078option(sctp_sanitizer_memory "Compile with memory sanitizer" 0)
Felix Weinrank3f31a252017-07-18 09:47:59 +020079
Felix Weinrank04d617c2019-08-25 17:56:12 +020080option(sctp_build_fuzzer "Compile in clang fuzzing mode" 0)
Felix Weinrank04d617c2019-08-25 17:56:12 +020081
Felix Weinrank396ed2a2017-11-15 14:17:00 +010082if (sctp_sanitizer_address AND sctp_sanitizer_memory)
Felix Weinrank9d180962017-11-07 15:46:54 +010083 message(FATAL_ERROR "Can not compile with both sanitizer options")
Felix Weinrank3f31a252017-07-18 09:47:59 +020084endif ()
85
Felix Weinrankcbe162f2018-07-26 18:22:13 +020086if (sctp_link_programs_static OR WIN32)
Felix Weinrank396ed2a2017-11-15 14:17:00 +010087 set(programs_link_library "usrsctp-static")
Felix Weinrank6c321952017-07-12 01:06:33 +020088else ()
Felix Weinrank396ed2a2017-11-15 14:17:00 +010089 set(programs_link_library "usrsctp")
Felix Weinrank6c321952017-07-12 01:06:33 +020090endif ()
91
92
93#################################################
Felix Weinrank5b9ee522017-07-14 15:45:16 +020094# CHECK FOR TYPES AND FUNCTIONS
95#################################################
96
Felix Weinrank396ed2a2017-11-15 14:17:00 +010097check_include_files("sys/queue.h" have_sys_queue_h)
98if (have_sys_queue_h)
Felix Weinrank9d180962017-11-07 15:46:54 +010099 add_definitions(-DHAVE_SYS_QUEUE_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200100endif ()
101
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100102check_include_files("sys/socket.h;linux/if_addr.h" have_linux_if_addr_h)
103if (have_linux_if_addr_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100104 add_definitions(-DHAVE_LINUX_IF_ADDR_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200105endif ()
106
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100107check_include_files("sys/socket.h;linux/rtnetlink.h" have_linux_rtnetlink_h)
108if (have_linux_rtnetlink_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100109 add_definitions(-DHAVE_LINUX_RTNETLINK_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200110endif ()
111
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100112check_include_files("sys/types.h;netinet/in.h;netinet/ip.h;netinet/ip_icmp.h" have_netinet_ip_icmp_h)
113if (have_netinet_ip_icmp_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100114 add_definitions(-DHAVE_NETINET_IP_ICMP_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200115endif ()
116
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100117check_include_files("stdatomic.h" have_stdatomic_h)
118if (have_stdatomic_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100119 add_definitions(-DHAVE_STDATOMIC_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200120endif ()
121
122
123#################################################
Felix Weinrank6c321952017-07-12 01:06:33 +0200124# CHECK STRUCT MEMBERS
125#################################################
126
Paul-Louis Ageneau17983b02020-01-22 14:10:27 +0000127set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/usrsctplib")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100128
129check_include_file(usrsctp.h have_usrsctp_h)
130if (NOT have_usrsctp_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100131 message(FATAL_ERROR "usrsctp.h not found")
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200132endif ()
Felix Weinrankf9f52222017-07-13 01:09:34 +0200133
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100134check_struct_has_member("struct sockaddr" "sa_len" "sys/types.h;sys/socket.h" have_sa_len)
135if (have_sa_len)
136 message(STATUS "have_sa_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100137 add_definitions(-DHAVE_SA_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200138endif ()
139
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100140check_struct_has_member("struct sockaddr_in" "sin_len" "sys/types.h;netinet/in.h" have_sin_len)
141if (have_sin_len)
142 message(STATUS "have_sin_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100143 add_definitions(-DHAVE_SIN_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200144endif ()
145
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100146check_struct_has_member("struct sockaddr_in6" "sin6_len" "sys/types.h;netinet/in.h" have_sin6_len)
147if (have_sin6_len)
148 message(STATUS "have_sin6_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100149 add_definitions(-DHAVE_SIN6_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200150endif ()
151
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100152check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len)
153if (have_sconn_len)
Felix Weinrank9d180962017-11-07 15:46:54 +0100154 message(STATUS "HAVE_SCONN_LEN")
155 add_definitions(-DHAVE_SCONN_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200156endif ()
157
158
159#################################################
160# COMPILER SETTINGS
161#################################################
162
Yury Yaroshevichef8a35a2019-10-10 15:00:14 +0300163# Determine if compiler is Visual Studio compiler or Clang in MSVC compatible mode
164if (CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_C_SIMULATE_ID MATCHES "MSVC")
165 set(C_COMPILER_IS_MSVC_LIKE TRUE)
166endif()
167
168# SETTINGS FOR VISUAL STUDIO COMPILER
169if (C_COMPILER_IS_MSVC_LIKE)
170 if (CMAKE_C_FLAGS MATCHES "/W[0-4]")
171 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
172 else ()
173 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
174 endif ()
175
176 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100") # 'identifier' : unreferenced formal parameter
177 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127") # conditional expression is constant
178 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4200") # nonstandard extension used : zero-sized array in struct/union
179 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4214") # bit field types other than int
180 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706") # assignment within conditional expression
181 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4245") # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch
182 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4389") # 'operator' : signed/unsigned mismatch
183 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702") # unreachable code
184 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701") # Potentially uninitialized local variable 'name' used
185
186 # ToDo
187 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244") # 'conversion' conversion from 'type1' to 'type2', possible loss of data
188
189 if (sctp_werror)
190 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
191
192 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
193 # temporary disable exta clang warnings preventing compilation
194 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-unused-function")
195 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-missing-field-initializers")
196 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-sign-compare")
197 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-format")
198 endif()
199 endif ()
Felix Weinrank6c321952017-07-12 01:06:33 +0200200# SETTINGS FOR UNIX COMPILER
Yury Yaroshevichef8a35a2019-10-10 15:00:14 +0300201elseif (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "AppleClang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
Felix Weinrank14827002019-08-06 16:14:48 +0200202 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Wextra")
Felix Weinrank3f31a252017-07-18 09:47:59 +0200203
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100204 check_c_compiler_flag(-Wfloat-equal has_wfloat_equal)
205 if (has_wfloat_equal)
Felix Weinrank14827002019-08-06 16:14:48 +0200206 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wfloat-equal")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100207 endif ()
208
209 check_c_compiler_flag(-Wshadow has_wshadow)
210 if (has_wshadow)
Felix Weinrank14827002019-08-06 16:14:48 +0200211 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100212 endif ()
213
214 check_c_compiler_flag(-Wpointer-arith has_wpointer_aritih)
215 if (has_wpointer_aritih)
Felix Weinrank14827002019-08-06 16:14:48 +0200216 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100217 endif ()
218
219 check_c_compiler_flag(-Wunreachable-code has_wunreachable_code)
220 if (has_wunreachable_code)
Felix Weinrank14827002019-08-06 16:14:48 +0200221 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunreachable-code")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100222 endif ()
223
224 check_c_compiler_flag(-Winit-self has_winit_self)
225 if (has_winit_self)
Felix Weinrank14827002019-08-06 16:14:48 +0200226 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self")
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100227 endif ()
228
Felix Weinrank41fbcc12017-11-15 14:48:32 +0100229 check_c_compiler_flag(-Wno-unused-function has_wno_unused_function)
230 if (has_wno_unused_function)
Felix Weinrank14827002019-08-06 16:14:48 +0200231 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
Felix Weinrank41fbcc12017-11-15 14:48:32 +0100232 endif ()
233
234 check_c_compiler_flag(-Wno-unused-parameter has_wno_unused_parameter)
235 if (has_wno_unused_parameter)
Felix Weinrank14827002019-08-06 16:14:48 +0200236 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
Felix Weinrank41fbcc12017-11-15 14:48:32 +0100237 endif ()
238
Felix Weinrank97558662017-11-15 14:52:15 +0100239 check_c_compiler_flag(-Wno-unreachable-code has_wno_unreachable_code)
240 if (has_wno_unreachable_code)
Felix Weinrank14827002019-08-06 16:14:48 +0200241 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unreachable-code")
Felix Weinrank97558662017-11-15 14:52:15 +0100242 endif ()
243
Felix Weinranka0a5a652018-03-23 20:09:51 +0100244 check_c_compiler_flag(-Wstrict-prototypes has_wstrict_prototypes)
245 if (has_wstrict_prototypes)
Felix Weinrank14827002019-08-06 16:14:48 +0200246 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
Felix Weinranka0a5a652018-03-23 20:09:51 +0100247 endif ()
248
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100249 if (sctp_werror)
Felix Weinrank14827002019-08-06 16:14:48 +0200250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
Felix Weinrank9d180962017-11-07 15:46:54 +0100251 endif ()
Felix Weinrank3f31a252017-07-18 09:47:59 +0200252
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100253 if (sctp_sanitizer_address)
Felix Weinrank248221f2019-09-26 16:53:56 +0200254 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined,signed-integer-overflow -fno-omit-frame-pointer -fno-sanitize-recover=all -fsanitize-address-use-after-scope ")
Felix Weinrank9d180962017-11-07 15:46:54 +0100255 endif ()
Felix Weinrank3f31a252017-07-18 09:47:59 +0200256
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100257 if (sctp_sanitizer_memory)
Felix Weinrank248221f2019-09-26 16:53:56 +0200258 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins -fPIE")
Felix Weinrank9d180962017-11-07 15:46:54 +0100259 endif ()
Felix Weinrank04d617c2019-08-25 17:56:12 +0200260
Felix Weinrank248221f2019-09-26 16:53:56 +0200261 if (sctp_build_fuzzer)
Felix Weinrank7b87cd42020-03-18 00:38:11 +0100262 set(CMAKE_BUILD_TYPE "DEBUG")
Felix Weinrank248221f2019-09-26 16:53:56 +0200263 add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
Felix Weinrank7b87cd42020-03-18 00:38:11 +0100264 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fsanitize=fuzzer-no-link")
Felix Weinrank04d617c2019-08-25 17:56:12 +0200265 endif ()
Felix Weinrank6c321952017-07-12 01:06:33 +0200266endif ()
267
Felix Weinrank3c3962c2018-08-02 14:00:47 +0200268message(STATUS "Compiler flags (CMAKE_C_FLAGS): ${CMAKE_C_FLAGS}")
269
Felix Weinrank6c321952017-07-12 01:06:33 +0200270
271#################################################
272# INCLUDE SUBDIRS
273#################################################
Felix Weinrank69bbc202016-03-16 11:29:09 +0100274
Felix Weinrank9acfd0f2015-08-26 13:01:12 +0200275add_subdirectory(usrsctplib)
Felix Weinrank6c321952017-07-12 01:06:33 +0200276
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100277if (sctp_build_programs)
Felix Weinrank9d180962017-11-07 15:46:54 +0100278 add_subdirectory(programs)
Felix Weinrank6c321952017-07-12 01:06:33 +0200279endif ()
Felix Weinrank04d617c2019-08-25 17:56:12 +0200280
281if (sctp_build_fuzzer)
282 add_subdirectory(fuzzer)
283endif ()