1# 2# Copyright (C) 2015-2015 Oleg Alexeenkov 3# Copyright (C) 2015-2019 Felix Weinrank 4# 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 32project(usrsctplib C) 33cmake_minimum_required(VERSION 3.0) 34 35# Debug build type as default 36if (NOT CMAKE_BUILD_TYPE) 37 message(STATUS "No build type selected, using DEBUG") 38 set(CMAKE_BUILD_TYPE "DEBUG") 39endif () 40 41include(CheckStructHasMember) 42include(CheckIncludeFile) 43include(CheckIncludeFiles) 44include(CheckCCompilerFlag) 45 46################################################# 47# CHECK OPTIONS 48################################################# 49 50option(sctp_invariants "Add runtime checks" 0) 51if (sctp_invariants) 52 add_definitions(-DINVARIANTS) 53endif () 54 55option(sctp_debug "Provide debug information" 1) 56if (sctp_debug) 57 add_definitions(-DSCTP_DEBUG) 58endif () 59 60option(sctp_inet "Support IPv4" 1) 61if (sctp_inet) 62 add_definitions(-DINET) 63endif () 64 65option(sctp_inet6 "Support IPv6" 1) 66if (sctp_inet6) 67 add_definitions(-DINET6) 68endif () 69 70option(sctp_werror "Treat warning as error" 1) 71 72option(sctp_link_programs_static "Link example programs static" 0) 73 74option(sctp_build_programs "Build example programs" 1) 75 76option(sctp_sanitizer_address "Compile with address sanitizer" 0) 77 78option(sctp_sanitizer_memory "Compile with memory sanitizer" 0) 79 80option(sctp_build_fuzzer "Compile in clang fuzzing mode" 0) 81 82if (sctp_sanitizer_address AND sctp_sanitizer_memory) 83 message(FATAL_ERROR "Can not compile with both sanitizer options") 84endif () 85 86if (sctp_link_programs_static OR WIN32) 87 set(programs_link_library "usrsctp-static") 88else () 89 set(programs_link_library "usrsctp") 90endif () 91 92 93################################################# 94# CHECK FOR TYPES AND FUNCTIONS 95################################################# 96 97check_include_files("sys/queue.h" have_sys_queue_h) 98if (have_sys_queue_h) 99 add_definitions(-DHAVE_SYS_QUEUE_H) 100endif () 101 102check_include_files("sys/socket.h;linux/if_addr.h" have_linux_if_addr_h) 103if (have_linux_if_addr_h) 104 add_definitions(-DHAVE_LINUX_IF_ADDR_H) 105endif () 106 107check_include_files("sys/socket.h;linux/rtnetlink.h" have_linux_rtnetlink_h) 108if (have_linux_rtnetlink_h) 109 add_definitions(-DHAVE_LINUX_RTNETLINK_H) 110endif () 111 112check_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) 114 add_definitions(-DHAVE_NETINET_IP_ICMP_H) 115endif () 116 117check_include_files("stdatomic.h" have_stdatomic_h) 118if (have_stdatomic_h) 119 add_definitions(-DHAVE_STDATOMIC_H) 120endif () 121 122 123################################################# 124# CHECK STRUCT MEMBERS 125################################################# 126 127set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/usrsctplib") 128 129check_include_file(usrsctp.h have_usrsctp_h) 130if (NOT have_usrsctp_h) 131 message(FATAL_ERROR "usrsctp.h not found") 132endif () 133 134check_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") 137 add_definitions(-DHAVE_SA_LEN) 138endif () 139 140check_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") 143 add_definitions(-DHAVE_SIN_LEN) 144endif () 145 146check_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") 149 add_definitions(-DHAVE_SIN6_LEN) 150endif () 151 152check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len) 153if (have_sconn_len) 154 message(STATUS "HAVE_SCONN_LEN") 155 add_definitions(-DHAVE_SCONN_LEN) 156endif () 157 158 159################################################# 160# COMPILER SETTINGS 161################################################# 162 163# 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 () 200# SETTINGS FOR UNIX COMPILER 201elseif (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "AppleClang" OR CMAKE_C_COMPILER_ID MATCHES "GNU") 202 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Wextra") 203 204 check_c_compiler_flag(-Wfloat-equal has_wfloat_equal) 205 if (has_wfloat_equal) 206 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wfloat-equal") 207 endif () 208 209 check_c_compiler_flag(-Wshadow has_wshadow) 210 if (has_wshadow) 211 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow") 212 endif () 213 214 check_c_compiler_flag(-Wpointer-arith has_wpointer_aritih) 215 if (has_wpointer_aritih) 216 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith") 217 endif () 218 219 check_c_compiler_flag(-Wunreachable-code has_wunreachable_code) 220 if (has_wunreachable_code) 221 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunreachable-code") 222 endif () 223 224 check_c_compiler_flag(-Winit-self has_winit_self) 225 if (has_winit_self) 226 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self") 227 endif () 228 229 check_c_compiler_flag(-Wno-unused-function has_wno_unused_function) 230 if (has_wno_unused_function) 231 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") 232 endif () 233 234 check_c_compiler_flag(-Wno-unused-parameter has_wno_unused_parameter) 235 if (has_wno_unused_parameter) 236 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter") 237 endif () 238 239 check_c_compiler_flag(-Wno-unreachable-code has_wno_unreachable_code) 240 if (has_wno_unreachable_code) 241 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unreachable-code") 242 endif () 243 244 check_c_compiler_flag(-Wstrict-prototypes has_wstrict_prototypes) 245 if (has_wstrict_prototypes) 246 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes") 247 endif () 248 249 if (sctp_werror) 250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 251 endif () 252 253 if (sctp_sanitizer_address) 254 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 ") 255 endif () 256 257 if (sctp_sanitizer_memory) 258 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins -fPIE") 259 endif () 260 261 if (sctp_build_fuzzer) 262 set(CMAKE_BUILD_TYPE "DEBUG") 263 add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) 264 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fsanitize=fuzzer-no-link") 265 endif () 266endif () 267 268message(STATUS "Compiler flags (CMAKE_C_FLAGS): ${CMAKE_C_FLAGS}") 269 270 271################################################# 272# INCLUDE SUBDIRS 273################################################# 274 275add_subdirectory(usrsctplib) 276 277if (sctp_build_programs) 278 add_subdirectory(programs) 279endif () 280 281if (sctp_build_fuzzer) 282 add_subdirectory(fuzzer) 283endif () 284