CMakeLists.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. project(usrsctplib C)
  32. cmake_minimum_required(VERSION 3.0)
  33. # Debug build type as default
  34. if (NOT CMAKE_BUILD_TYPE)
  35. message(STATUS "No build type selected, using DEBUG")
  36. set(CMAKE_BUILD_TYPE "DEBUG")
  37. endif ()
  38. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  39. include(CheckStructHasMember)
  40. include(CheckIncludeFile)
  41. include(CheckIncludeFiles)
  42. include(CheckCCompilerFlag)
  43. #################################################
  44. # CHECK OPTIONS
  45. #################################################
  46. option(sctp_invariants "Add runtime checks" 0)
  47. if (sctp_invariants)
  48. add_definitions(-DINVARIANTS)
  49. endif ()
  50. option(sctp_debug "Provide debug information" 1)
  51. if (sctp_debug)
  52. add_definitions(-DSCTP_DEBUG)
  53. endif ()
  54. option(sctp_inet "Support IPv4" 1)
  55. if (sctp_inet)
  56. add_definitions(-DINET)
  57. endif ()
  58. option(sctp_inet6 "Support IPv6" 1)
  59. if (sctp_inet6)
  60. add_definitions(-DINET6)
  61. endif ()
  62. option(sctp_werror "Treat warning as error" 1)
  63. option(sctp_build_shared_lib "Build USRSCTP as shared library" 0)
  64. option(sctp_build_programs "Build example programs" 1)
  65. option(sctp_sanitizer_address "Compile with address sanitizer" 0)
  66. option(sctp_sanitizer_memory "Compile with memory sanitizer" 0)
  67. option(sctp_build_fuzzer "Compile in clang fuzzing mode" 0)
  68. if (sctp_sanitizer_address AND sctp_sanitizer_memory)
  69. message(FATAL_ERROR "Can not compile with both sanitizer options")
  70. endif ()
  71. #################################################
  72. # CHECK FOR TYPES AND FUNCTIONS
  73. #################################################
  74. check_include_files("sys/queue.h" have_sys_queue_h)
  75. if (have_sys_queue_h)
  76. add_definitions(-DHAVE_SYS_QUEUE_H)
  77. endif ()
  78. check_include_files("sys/socket.h;linux/if_addr.h" have_linux_if_addr_h)
  79. if (have_linux_if_addr_h)
  80. add_definitions(-DHAVE_LINUX_IF_ADDR_H)
  81. endif ()
  82. check_include_files("sys/socket.h;linux/rtnetlink.h" have_linux_rtnetlink_h)
  83. if (have_linux_rtnetlink_h)
  84. add_definitions(-DHAVE_LINUX_RTNETLINK_H)
  85. endif ()
  86. check_include_files("sys/types.h;netinet/in.h;netinet/ip.h;netinet/ip_icmp.h" have_netinet_ip_icmp_h)
  87. if (have_netinet_ip_icmp_h)
  88. add_definitions(-DHAVE_NETINET_IP_ICMP_H)
  89. endif ()
  90. check_include_files("sys/types.h;sys/socket.h;net/route.h" have_net_route_h)
  91. if (have_net_route_h)
  92. add_definitions(-DHAVE_NET_ROUTE_H)
  93. endif ()
  94. check_include_files("stdatomic.h" have_stdatomic_h)
  95. if (have_stdatomic_h)
  96. add_definitions(-DHAVE_STDATOMIC_H)
  97. endif ()
  98. #################################################
  99. # CHECK STRUCT MEMBERS
  100. #################################################
  101. set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/usrsctplib")
  102. check_include_file(usrsctp.h have_usrsctp_h)
  103. if (NOT have_usrsctp_h)
  104. message(FATAL_ERROR "usrsctp.h not found")
  105. endif ()
  106. check_struct_has_member("struct sockaddr" "sa_len" "sys/types.h;sys/socket.h" have_sa_len)
  107. if (have_sa_len)
  108. message(STATUS "have_sa_len")
  109. add_definitions(-DHAVE_SA_LEN)
  110. endif ()
  111. check_struct_has_member("struct sockaddr_in" "sin_len" "sys/types.h;netinet/in.h" have_sin_len)
  112. if (have_sin_len)
  113. message(STATUS "have_sin_len")
  114. add_definitions(-DHAVE_SIN_LEN)
  115. endif ()
  116. check_struct_has_member("struct sockaddr_in6" "sin6_len" "sys/types.h;netinet/in.h" have_sin6_len)
  117. if (have_sin6_len)
  118. message(STATUS "have_sin6_len")
  119. add_definitions(-DHAVE_SIN6_LEN)
  120. endif ()
  121. check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len)
  122. if (have_sconn_len)
  123. message(STATUS "HAVE_SCONN_LEN")
  124. add_definitions(-DHAVE_SCONN_LEN)
  125. endif ()
  126. #################################################
  127. # COMPILER SETTINGS
  128. #################################################
  129. # Determine if compiler is Visual Studio compiler or Clang in MSVC compatible mode
  130. if (CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_C_SIMULATE_ID MATCHES "MSVC")
  131. set(C_COMPILER_IS_MSVC_LIKE TRUE)
  132. endif()
  133. # SETTINGS FOR VISUAL STUDIO COMPILER
  134. if (C_COMPILER_IS_MSVC_LIKE)
  135. if (CMAKE_C_FLAGS MATCHES "/W[0-4]")
  136. string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  137. else ()
  138. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
  139. endif ()
  140. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100") # 'identifier' : unreferenced formal parameter
  141. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127") # conditional expression is constant
  142. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4200") # nonstandard extension used : zero-sized array in struct/union
  143. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4214") # bit field types other than int
  144. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706") # assignment within conditional expression
  145. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4245") # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch
  146. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4389") # 'operator' : signed/unsigned mismatch
  147. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702") # unreachable code
  148. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701") # Potentially uninitialized local variable 'name' used
  149. # ToDo
  150. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244") # 'conversion' conversion from 'type1' to 'type2', possible loss of data
  151. if (sctp_werror)
  152. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  153. if (CMAKE_C_COMPILER_ID MATCHES "Clang")
  154. # temporary disable exta clang warnings preventing compilation
  155. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-unused-function")
  156. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-missing-field-initializers")
  157. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-sign-compare")
  158. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /clang:-Wno-format")
  159. endif()
  160. endif ()
  161. # SETTINGS FOR UNIX COMPILER
  162. elseif (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "AppleClang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
  163. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Wextra")
  164. check_c_compiler_flag(-Wfloat-equal has_wfloat_equal)
  165. if (has_wfloat_equal)
  166. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wfloat-equal")
  167. endif ()
  168. check_c_compiler_flag(-Wshadow has_wshadow)
  169. if (has_wshadow)
  170. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  171. endif ()
  172. check_c_compiler_flag(-Wpointer-arith has_wpointer_aritih)
  173. if (has_wpointer_aritih)
  174. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith")
  175. endif ()
  176. check_c_compiler_flag(-Wunreachable-code has_wunreachable_code)
  177. if (has_wunreachable_code)
  178. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunreachable-code")
  179. endif ()
  180. check_c_compiler_flag(-Winit-self has_winit_self)
  181. if (has_winit_self)
  182. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self")
  183. endif ()
  184. check_c_compiler_flag(-Wno-unused-function has_wno_unused_function)
  185. if (has_wno_unused_function)
  186. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
  187. endif ()
  188. check_c_compiler_flag(-Wno-unused-parameter has_wno_unused_parameter)
  189. if (has_wno_unused_parameter)
  190. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
  191. endif ()
  192. check_c_compiler_flag(-Wno-unreachable-code has_wno_unreachable_code)
  193. if (has_wno_unreachable_code)
  194. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unreachable-code")
  195. endif ()
  196. check_c_compiler_flag(-Wstrict-prototypes has_wstrict_prototypes)
  197. if (has_wstrict_prototypes)
  198. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  199. endif ()
  200. if (sctp_werror)
  201. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  202. endif ()
  203. if (sctp_sanitizer_address)
  204. 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 ")
  205. endif ()
  206. if (sctp_sanitizer_memory)
  207. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins -fPIE")
  208. endif ()
  209. if (sctp_build_fuzzer)
  210. set(CMAKE_BUILD_TYPE "RelWithDebInfo")
  211. add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  212. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=fuzzer-no-link")
  213. endif ()
  214. endif ()
  215. message(STATUS "Compiler flags (CMAKE_C_FLAGS): ${CMAKE_C_FLAGS}")
  216. #################################################
  217. # INCLUDE SUBDIRS
  218. #################################################
  219. add_subdirectory(usrsctplib)
  220. if (sctp_build_programs)
  221. add_subdirectory(programs)
  222. endif ()
  223. if (sctp_build_fuzzer)
  224. add_subdirectory(fuzzer)
  225. endif ()