Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake Targets carrying compiler flags #275

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 3 additions & 163 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# SUCH DAMAGE.
#

project(usrsctplib)
project(usrsctplib C)
cmake_minimum_required(VERSION 3.0)

# Debug build type as default
Expand All @@ -43,30 +43,6 @@ include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckCCompilerFlag)

#################################################
# CHECK OPTIONS
#################################################

option(sctp_invariants "Add runtime checks" 0)
if (sctp_invariants)
add_definitions(-DINVARIANTS)
endif ()

option(sctp_debug "Provide debug information" 1)
if (sctp_debug)
add_definitions(-DSCTP_DEBUG)
endif ()

option(sctp_inet "Support IPv4" 1)
if (sctp_inet)
add_definitions(-DINET)
endif ()

option(sctp_inet6 "Support IPv6" 1)
if (sctp_inet6)
add_definitions(-DINET6)
endif ()

option(sctp_werror "Treat warning as error" 1)

option(sctp_link_programs_static "Link example programs static" 0)
Expand All @@ -81,158 +57,23 @@ if (sctp_sanitizer_address AND sctp_sanitizer_memory)
message(FATAL_ERROR "Can not compile with both sanitizer options")
endif ()

if (sctp_link_programs_static OR WIN32)
set(programs_link_library "usrsctp-static")
else ()
set(programs_link_library "usrsctp")
endif ()


#################################################
# CHECK FOR TYPES AND FUNCTIONS
#################################################

check_include_files("sys/queue.h" have_sys_queue_h)
if (have_sys_queue_h)
add_definitions(-DHAVE_SYS_QUEUE_H)
endif ()

check_include_files("sys/socket.h;linux/if_addr.h" have_linux_if_addr_h)
if (have_linux_if_addr_h)
add_definitions(-DHAVE_LINUX_IF_ADDR_H)
endif ()

check_include_files("sys/socket.h;linux/rtnetlink.h" have_linux_rtnetlink_h)
if (have_linux_rtnetlink_h)
add_definitions(-DHAVE_LINUX_RTNETLINK_H)
endif ()

check_include_files("sys/types.h;netinet/in.h;netinet/ip.h;netinet/ip_icmp.h" have_netinet_ip_icmp_h)
if (have_netinet_ip_icmp_h)
add_definitions(-DHAVE_NETINET_IP_ICMP_H)
endif ()

check_include_files("stdatomic.h" have_stdatomic_h)
if (have_stdatomic_h)
add_definitions(-DHAVE_STDATOMIC_H)
endif ()


#################################################
# CHECK STRUCT MEMBERS
#################################################

set(CMAKE_REQUIRED_INCLUDES "${CMAKE_SOURCE_DIR}/usrsctplib")
#set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/usrsctplib")

check_include_file(usrsctp.h have_usrsctp_h)
check_include_file(${CMAKE_CURRENT_LIST_DIR}/usrsctplib/usrsctp.h have_usrsctp_h)
if (NOT have_usrsctp_h)
message(FATAL_ERROR "usrsctp.h not found")
endif ()

check_struct_has_member("struct sockaddr" "sa_len" "sys/types.h;sys/socket.h" have_sa_len)
if (have_sa_len)
message(STATUS "have_sa_len")
add_definitions(-DHAVE_SA_LEN)
endif ()

check_struct_has_member("struct sockaddr_in" "sin_len" "sys/types.h;netinet/in.h" have_sin_len)
if (have_sin_len)
message(STATUS "have_sin_len")
add_definitions(-DHAVE_SIN_LEN)
endif ()

check_struct_has_member("struct sockaddr_in6" "sin6_len" "sys/types.h;netinet/in.h" have_sin6_len)
if (have_sin6_len)
message(STATUS "have_sin6_len")
add_definitions(-DHAVE_SIN6_LEN)
endif ()

check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len)
if (have_sconn_len)
message(STATUS "HAVE_SCONN_LEN")
add_definitions(-DHAVE_SCONN_LEN)
endif ()


#################################################
# COMPILER SETTINGS
#################################################

# SETTINGS FOR UNIX COMPILER
if (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "AppleClang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c99 -pedantic -Wall -Wextra")

check_c_compiler_flag(-Wfloat-equal has_wfloat_equal)
if (has_wfloat_equal)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wfloat-equal")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfloat-equal")
endif ()

check_c_compiler_flag(-Wshadow has_wshadow)
if (has_wshadow)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
endif ()

check_c_compiler_flag(-Wpointer-arith has_wpointer_aritih)
if (has_wpointer_aritih)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpointer-arith")
endif ()

check_c_compiler_flag(-Wunreachable-code has_wunreachable_code)
if (has_wunreachable_code)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunreachable-code")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunreachable-code")
endif ()

check_c_compiler_flag(-Winit-self has_winit_self)
if (has_winit_self)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winit-self")
endif ()

check_c_compiler_flag(-Wno-unused-function has_wno_unused_function)
if (has_wno_unused_function)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
endif ()

check_c_compiler_flag(-Wno-unused-parameter has_wno_unused_parameter)
if (has_wno_unused_parameter)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
endif ()

check_c_compiler_flag(-Wno-unreachable-code has_wno_unreachable_code)
if (has_wno_unreachable_code)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unreachable-code")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unreachable-code")
endif ()

check_c_compiler_flag(-Wstrict-prototypes has_wstrict_prototypes)
if (has_wstrict_prototypes)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstrict-prototypes")
endif ()

if (sctp_werror)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif ()

if (sctp_sanitizer_address)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g")
endif ()

if (sctp_sanitizer_memory)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -g -fsanitize-memory-track-origins")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fno-omit-frame-pointer -g -fsanitize-memory-track-origins")
endif ()
endif ()

# SETTINGS FOR VISUAL STUDIO COMPILER
if (CMAKE_C_COMPILER_ID MATCHES "MSVC")
if (CMAKE_C_FLAGS MATCHES "/W[0-4]")
Expand All @@ -259,7 +100,6 @@ if (CMAKE_C_COMPILER_ID MATCHES "MSVC")
endif ()
endif ()

message(STATUS "Compiler flags (CMAKE_C_FLAGS): ${CMAKE_C_FLAGS}")


#################################################
Expand Down
5 changes: 2 additions & 3 deletions programs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ set(check_programs
test_timer.c
tsctp.c
)

foreach (source_file ${check_programs})
get_filename_component(source_file_we ${source_file} NAME_WE)
add_executable(
Expand All @@ -101,7 +100,7 @@ foreach (source_file ${check_programs})

target_link_libraries(
${source_file_we}
${programs_link_library}
${CMAKE_THREAD_LIBS_INIT}
usrsctp
Threads::Threads
)
endforeach ()
Loading