blob: dfe5724d7ae6f86132781941bc738118f1bb0f60 [file] [log] [blame]
Felix Weinrank71be0712015-08-26 13:52:37 +02001#
2# Copyright (C) 2015-2015 Oleg Alexeenkov
Felix Weinrank2e5216b2018-08-06 14:03:13 +02003# Copyright (C) 2015-2018 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 Weinrank6c321952017-07-12 01:06:33 +020032project(usrsctplib)
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 Weinrank396ed2a2017-11-15 14:17:00 +010080if (sctp_sanitizer_address AND sctp_sanitizer_memory)
Felix Weinrank9d180962017-11-07 15:46:54 +010081 message(FATAL_ERROR "Can not compile with both sanitizer options")
Felix Weinrank3f31a252017-07-18 09:47:59 +020082endif ()
83
Felix Weinrankcbe162f2018-07-26 18:22:13 +020084if (sctp_link_programs_static OR WIN32)
Felix Weinrank396ed2a2017-11-15 14:17:00 +010085 set(programs_link_library "usrsctp-static")
Felix Weinrank6c321952017-07-12 01:06:33 +020086else ()
Felix Weinrank396ed2a2017-11-15 14:17:00 +010087 set(programs_link_library "usrsctp")
Felix Weinrank6c321952017-07-12 01:06:33 +020088endif ()
89
90
91#################################################
Felix Weinrank5b9ee522017-07-14 15:45:16 +020092# CHECK FOR TYPES AND FUNCTIONS
93#################################################
94
Felix Weinrank396ed2a2017-11-15 14:17:00 +010095check_include_files("sys/queue.h" have_sys_queue_h)
96if (have_sys_queue_h)
Felix Weinrank9d180962017-11-07 15:46:54 +010097 add_definitions(-DHAVE_SYS_QUEUE_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +020098endif ()
99
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100100check_include_files("sys/socket.h;linux/if_addr.h" have_linux_if_addr_h)
101if (have_linux_if_addr_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100102 add_definitions(-DHAVE_LINUX_IF_ADDR_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200103endif ()
104
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100105check_include_files("sys/socket.h;linux/rtnetlink.h" have_linux_rtnetlink_h)
106if (have_linux_rtnetlink_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100107 add_definitions(-DHAVE_LINUX_RTNETLINK_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200108endif ()
109
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100110check_include_files("sys/types.h;netinet/in.h;netinet/ip.h;netinet/ip_icmp.h" have_netinet_ip_icmp_h)
111if (have_netinet_ip_icmp_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100112 add_definitions(-DHAVE_NETINET_IP_ICMP_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200113endif ()
114
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100115check_include_files("stdatomic.h" have_stdatomic_h)
116if (have_stdatomic_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100117 add_definitions(-DHAVE_STDATOMIC_H)
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200118endif ()
119
120
121#################################################
Felix Weinrank6c321952017-07-12 01:06:33 +0200122# CHECK STRUCT MEMBERS
123#################################################
124
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100125set(CMAKE_REQUIRED_INCLUDES "${CMAKE_SOURCE_DIR}/usrsctplib")
126
127check_include_file(usrsctp.h have_usrsctp_h)
128if (NOT have_usrsctp_h)
Felix Weinrank9d180962017-11-07 15:46:54 +0100129 message(FATAL_ERROR "usrsctp.h not found")
Felix Weinrank5b9ee522017-07-14 15:45:16 +0200130endif ()
Felix Weinrankf9f52222017-07-13 01:09:34 +0200131
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100132check_struct_has_member("struct sockaddr" "sa_len" "sys/types.h;sys/socket.h" have_sa_len)
133if (have_sa_len)
134 message(STATUS "have_sa_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100135 add_definitions(-DHAVE_SA_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200136endif ()
137
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100138check_struct_has_member("struct sockaddr_in" "sin_len" "sys/types.h;netinet/in.h" have_sin_len)
139if (have_sin_len)
140 message(STATUS "have_sin_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100141 add_definitions(-DHAVE_SIN_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200142endif ()
143
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100144check_struct_has_member("struct sockaddr_in6" "sin6_len" "sys/types.h;netinet/in.h" have_sin6_len)
145if (have_sin6_len)
146 message(STATUS "have_sin6_len")
Felix Weinrank9d180962017-11-07 15:46:54 +0100147 add_definitions(-DHAVE_SIN6_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200148endif ()
149
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100150check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len)
151if (have_sconn_len)
Felix Weinrank9d180962017-11-07 15:46:54 +0100152 message(STATUS "HAVE_SCONN_LEN")
153 add_definitions(-DHAVE_SCONN_LEN)
Felix Weinrank6c321952017-07-12 01:06:33 +0200154endif ()
155
156
157#################################################
158# COMPILER SETTINGS
159#################################################
160
161# SETTINGS FOR UNIX COMPILER
Felix Weinrank3c3962c2018-08-02 14:00:47 +0200162if (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "AppleClang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
Felix Weinranka0ccbee2017-11-16 15:06:23 +0100163 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Wextra")
Felix Weinrankcbe162f2018-07-26 18:22:13 +0200164 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c99 -pedantic -Wall -Wextra")
Felix Weinrank3f31a252017-07-18 09:47:59 +0200165
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100166 check_c_compiler_flag(-Wfloat-equal has_wfloat_equal)
167 if (has_wfloat_equal)
168 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wfloat-equal")
169 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfloat-equal")
170 endif ()
171
172 check_c_compiler_flag(-Wshadow has_wshadow)
173 if (has_wshadow)
174 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
175 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
176 endif ()
177
178 check_c_compiler_flag(-Wpointer-arith has_wpointer_aritih)
179 if (has_wpointer_aritih)
180 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith")
181 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpointer-arith")
182 endif ()
183
184 check_c_compiler_flag(-Wunreachable-code has_wunreachable_code)
185 if (has_wunreachable_code)
186 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunreachable-code")
187 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunreachable-code")
188 endif ()
189
190 check_c_compiler_flag(-Winit-self has_winit_self)
191 if (has_winit_self)
192 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self")
193 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winit-self")
194 endif ()
195
Felix Weinrank41fbcc12017-11-15 14:48:32 +0100196 check_c_compiler_flag(-Wno-unused-function has_wno_unused_function)
197 if (has_wno_unused_function)
198 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
199 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
200 endif ()
201
202 check_c_compiler_flag(-Wno-unused-parameter has_wno_unused_parameter)
203 if (has_wno_unused_parameter)
204 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
205 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
206 endif ()
207
Felix Weinrank97558662017-11-15 14:52:15 +0100208 check_c_compiler_flag(-Wno-unreachable-code has_wno_unreachable_code)
209 if (has_wno_unreachable_code)
210 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unreachable-code")
211 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unreachable-code")
212 endif ()
213
Felix Weinranka0a5a652018-03-23 20:09:51 +0100214 check_c_compiler_flag(-Wstrict-prototypes has_wstrict_prototypes)
215 if (has_wstrict_prototypes)
216 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
217 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstrict-prototypes")
218 endif ()
219
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100220 if (sctp_werror)
Felix Weinrank9d180962017-11-07 15:46:54 +0100221 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
222 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
223 endif ()
Felix Weinrank3f31a252017-07-18 09:47:59 +0200224
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100225 if (sctp_sanitizer_address)
226 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g")
227 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g")
Felix Weinrank9d180962017-11-07 15:46:54 +0100228 endif ()
Felix Weinrank3f31a252017-07-18 09:47:59 +0200229
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100230 if (sctp_sanitizer_memory)
Felix Weinrank9d180962017-11-07 15:46:54 +0100231 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -g -fsanitize-memory-track-origins")
232 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -g -fsanitize-memory-track-origins")
233 endif ()
Felix Weinrank6c321952017-07-12 01:06:33 +0200234endif ()
235
236# SETTINGS FOR VISUAL STUDIO COMPILER
Felix Weinrank3c3962c2018-08-02 14:00:47 +0200237if (CMAKE_C_COMPILER_ID MATCHES "MSVC")
Felix Weinrank9d180962017-11-07 15:46:54 +0100238 if (CMAKE_C_FLAGS MATCHES "/W[0-4]")
Felix Weinrank2e5216b2018-08-06 14:03:13 +0200239 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
Felix Weinrank9d180962017-11-07 15:46:54 +0100240 else ()
Felix Weinrank2e5216b2018-08-06 14:03:13 +0200241 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
Felix Weinrank9d180962017-11-07 15:46:54 +0100242 endif ()
Felix Weinrank6c321952017-07-12 01:06:33 +0200243
Felix Weinrank2e5216b2018-08-06 14:03:13 +0200244 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100") # 'identifier' : unreferenced formal parameter
245 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127") # conditional expression is constant
246 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4200") # nonstandard extension used : zero-sized array in struct/union
247 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4214") # bit field types other than int
248 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706") # assignment within conditional expression
249 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4245") # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch
250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4389") # 'operator' : signed/unsigned mismatch
251 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702") # unreachable code
252 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701") # Potentially uninitialized local variable 'name' used
253
254 # ToDo
255 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244") # 'conversion' conversion from 'type1' to 'type2', possible loss of data
256
257 if (sctp_werror)
Felix Weinrank9d180962017-11-07 15:46:54 +0100258 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
259 endif ()
Felix Weinrank6c321952017-07-12 01:06:33 +0200260endif ()
261
Felix Weinrank3c3962c2018-08-02 14:00:47 +0200262message(STATUS "Compiler flags (CMAKE_C_FLAGS): ${CMAKE_C_FLAGS}")
263
Felix Weinrank6c321952017-07-12 01:06:33 +0200264
265#################################################
266# INCLUDE SUBDIRS
267#################################################
Felix Weinrank69bbc202016-03-16 11:29:09 +0100268
Felix Weinrank9acfd0f2015-08-26 13:01:12 +0200269add_subdirectory(usrsctplib)
Felix Weinrank6c321952017-07-12 01:06:33 +0200270
Felix Weinrank396ed2a2017-11-15 14:17:00 +0100271if (sctp_build_programs)
Felix Weinrank9d180962017-11-07 15:46:54 +0100272 add_subdirectory(programs)
Felix Weinrank6c321952017-07-12 01:06:33 +0200273endif ()