Skip to content

Commit

Permalink
Merge pull request #666 from palerikm/cmake_with_warnings
Browse files Browse the repository at this point in the history
Cleaning up cmake and enabled more warnings.
  • Loading branch information
pabuhler authored Dec 19, 2023
2 parents 8f54f80 + 185837b commit e8059c3
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Configure CMake
working-directory: ${{github.workspace}}/build
shell: bash
run: cmake $GITHUB_WORKSPACE -DBUILD_WITH_SANITIZERS=TRUE ${{ matrix.cmake-crypto-enable}} ${{env.cmake-crypto-dir}}
run: cmake $GITHUB_WORKSPACE -DLIBSRTP_TEST_APPS=ON ${{ matrix.cmake-crypto-enable}} ${{env.cmake-crypto-dir}}

- name: Build
working-directory: ${{github.workspace}}/build
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/stream_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
- name: Configure CMake
working-directory: ${{github.workspace}}/build
shell: bash
run: cmake $GITHUB_WORKSPACE -DBUILD_WITH_SANITIZERS=TRUE -DCMAKE_C_FLAGS:STRING="-DSRTP_NO_STREAM_LIST -DSRTP_USE_TEST_STREAM_LIST"
run: cmake $GITHUB_WORKSPACE -DLIBSRTP_TEST_APPS=ON -DCMAKE_C_FLAGS:STRING="-DSRTP_NO_STREAM_LIST
-DSRTP_USE_TEST_STREAM_LIST"

- name: Build
working-directory: ${{github.workspace}}/build
Expand Down
153 changes: 127 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.21)

project(libsrtp2 VERSION 2.6.0 LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(PACKAGE_VERSION ${PROJECT_VERSION})
set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)


option(ENABLE_WARNINGS "Enable to add warnings to a target." ON)
option(ENABLE_WARNINGS_AS_ERRORS "Enable to treat warnings as errors." ON)

option(ENABLE_SANITIZE_ADDR "Enable address sanitize." OFF)
option(ENABLE_SANITIZE_UNDEF "Enable undefined sanitize." OFF)
option(ENABLE_SANITIZE_LEAK "Enable leak sanitize (Gcc/Clang only)." OFF)
option(ENABLE_SANITIZE_THREAD "Enable thread sanitize (Gcc/Clang only)." OFF)

option(ENABLE_LTO "Enable to add Link Time Optimization." OFF)

option(LIBSRTP_TEST_APPS "Build libSRTP test applications" ON)
option(BUILD_SHARED_LIBS "Build shared library" OFF)

include(TestBigEndian)
include(CheckIncludeFile)
include(CheckFunctionExists)
include(CheckTypeSize)
include(CheckCSourceCompiles)
include(ConfigSafeGuards)

if(ENABLE_WARNINGS)
include(Warnings)
endif()


test_big_endian(WORDS_BIGENDIAN)

Expand Down Expand Up @@ -59,10 +84,6 @@ set(ERR_REPORTING_FILE "" CACHE FILEPATH "Use file for logging")
set(ENABLE_OPENSSL OFF CACHE BOOL "Enable OpenSSL crypto engine")
set(ENABLE_MBEDTLS OFF CACHE BOOL "Enable MbedTLS crypto engine")
set(ENABLE_NSS OFF CACHE BOOL "Enable NSS crypto engine")
option(LIBSRTP_TEST_APPS "Build libSRTP test applications" ON)
set(BUILD_WITH_SANITIZERS OFF CACHE BOOL "Adds sanitizer flags to compiler")
set(BUILD_WITH_WARNINGS ON CACHE BOOL "Adds warnings and warning as error flags to compiler")
option(BUILD_SHARED_LIBS "Build shared library" OFF)

if(ENABLE_OPENSSL OR ENABLE_MBEDTLS OR ENABLE_NSS)
set(USE_EXTERNAL_CRYPTO TRUE)
Expand Down Expand Up @@ -103,29 +124,13 @@ include_directories(${CONFIG_FILE_DIR})
configure_file(config_in_cmake.h ${CONFIG_FILE_DIR}/config.h)
add_definitions(-DHAVE_CONFIG_H)

if(BUILD_WITH_SANITIZERS AND NOT WIN32)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(SANITIZERS address,undefined)
else()
set(SANITIZERS leak,address,undefined)
endif()
message(STATUS "Using sanitizers: ${SANITIZERS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
if(ENABLE_SANITIZE_ADDR OR ENABLE_SANITIZE_UNDEF)
include(Sanitizer)
add_sanitizer_flags()
endif()


if(BUILD_WITH_WARNINGS)
if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
set(WARNINGS -Wall -pedantic -Wextra -Wcast-qual -Werror)
elseif(MSVC)
set(WARNINGS /W4 /WX)
endif()
message(STATUS "Using warnings: ${WARNINGS}")
add_compile_options(${WARNINGS})
if(ENABLE_LTO)
include(LTO)
endif()

set(SOURCES_C
Expand Down Expand Up @@ -252,6 +257,25 @@ add_library(srtp2
)
add_library(libSRTP::srtp2 ALIAS srtp2)

if (${ENABLE_WARNINGS})
target_set_warnings(
TARGET
"srtp2"
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
endif()

if(${ENABLE_LTO})
target_enable_lto(
TARGET
"srtp2"
ENABLE
ON)
endif()


set_target_properties(srtp2 PROPERTIES VERSION ${PROJECT_VERSION})

target_include_directories(srtp2 PUBLIC
Expand Down Expand Up @@ -297,6 +321,13 @@ if(LIBSRTP_TEST_APPS)
add_executable(aes_calc crypto/test/aes_calc.c test/getopt_s.c test/util.c)
target_include_directories(aes_calc PRIVATE test)
target_link_libraries(aes_calc srtp2)
target_set_warnings(
TARGET
aes_calc
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
add_test(aes_calc_128 aes_calc 000102030405060708090a0b0c0d0e0f
00112233445566778899aabbccddeeff
69c4e0d86a7b0430d8cdb78070b4c55a)
Expand All @@ -305,49 +336,112 @@ if(LIBSRTP_TEST_APPS)
8ea2b7ca516745bfeafc49904b496089)

add_executable(sha1_driver crypto/test/sha1_driver.c test/util.c)
target_set_warnings(
TARGET
sha1_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(sha1_driver PRIVATE test)
target_link_libraries(sha1_driver srtp2)
add_test(sha1_driver sha1_driver -v)
endif()

add_executable(datatypes_driver crypto/test/datatypes_driver.c test/util.c)
target_set_warnings(
TARGET
datatypes_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(datatypes_driver PRIVATE test)
target_link_libraries(datatypes_driver srtp2)
add_test(datatypes_driver datatypes_driver -v)

add_executable(cipher_driver crypto/test/cipher_driver.c test/getopt_s.c)
target_set_warnings(
TARGET
cipher_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(cipher_driver PRIVATE test)
target_link_libraries(cipher_driver srtp2)
add_test(cipher_driver cipher_driver -v)

add_executable(kernel_driver crypto/test/kernel_driver.c test/getopt_s.c)
target_set_warnings(
TARGET
kernel_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(kernel_driver PRIVATE test)
target_link_libraries(kernel_driver srtp2)
add_test(kernel_driver kernel_driver -v)

add_executable(rdbx_driver test/rdbx_driver.c test/getopt_s.c test/ut_sim.c)
target_set_warnings(
TARGET
rdbx_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(rdbx_driver PRIVATE test)
target_link_libraries(rdbx_driver srtp2)
add_test(rdbx_driver rdbx_driver -v)

add_executable(replay_driver test/replay_driver.c test/ut_sim.c)
target_set_warnings(
TARGET
replay_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(replay_driver PRIVATE test)
target_link_libraries(replay_driver srtp2)
add_test(replay_driver replay_driver -v)

add_executable(roc_driver test/roc_driver.c test/ut_sim.c)
target_set_warnings(
TARGET
roc_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_include_directories(roc_driver PRIVATE test)
target_link_libraries(roc_driver srtp2)
add_test(roc_driver roc_driver -v)
endif()

add_executable(srtp_driver test/srtp_driver.c
test/util.c test/getopt_s.c)
target_set_warnings(
TARGET
srtp_driver
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_link_libraries(srtp_driver srtp2)
add_test(srtp_driver srtp_driver -v)

if(NOT (BUILD_SHARED_LIBS AND WIN32))
add_executable(test_srtp test/test_srtp.c)
target_set_warnings(
TARGET
test_srtp
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
if(ENABLE_OPENSSL)
target_include_directories(test_srtp PRIVATE ${OPENSSL_INCLUDE_DIR})
elseif(ENABLE_MBEDTLS)
Expand All @@ -362,6 +456,13 @@ if(LIBSRTP_TEST_APPS)
find_program(BASH_PROGRAM bash)
if(BASH_PROGRAM AND NOT WIN32)
add_executable(rtpw test/rtpw.c test/rtp.c test/util.c test/getopt_s.c)
target_set_warnings(
TARGET
rtpw
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_link_libraries(rtpw srtp2)
add_test(NAME rtpw_test
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/rtpw_test.sh -w ${CMAKE_CURRENT_SOURCE_DIR}/test/words.txt
Expand Down
10 changes: 10 additions & 0 deletions cmake/ConfigSafeGuards.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(
FATAL_ERROR
"In-source builds not allowed. Please make a build directory.")
endif()

if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()
19 changes: 19 additions & 0 deletions cmake/LTO.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function(target_enable_lto)
set(oneValueArgs TARGET ENABLE)
cmake_parse_arguments(
LTO
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN})

include(CheckIPOSupported)
check_ipo_supported(RESULT result OUTPUT output)
if(result)
message(STATUS "IPO/LTO is supported: ${LTO_TARGET}")
set_property(TARGET ${LTO_TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION
${LTO_ENABLE})
else()
message(WARNING "IPO/LTO is not supported: ${LTO_TARGET}")
endif()
endfunction()
52 changes: 52 additions & 0 deletions cmake/Sanitizer.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function(add_sanitizer_flags)
if(NOT ENABLE_SANITIZE_ADDR AND NOT ENABLE_SANITIZE_UNDEF)
return()
endif()

if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
add_compile_options("-fno-omit-frame-pointer")
add_link_options("-fno-omit-frame-pointer")

if(ENABLE_SANITIZE_ADDR)
add_compile_options("-fsanitize=address")
add_link_options("-fsanitize=address")
endif()

if(ENABLE_SANITIZE_UNDEF)
add_compile_options("-fsanitize=undefined")
add_link_options("-fsanitize=undefined")
endif()

if(ENABLE_SANITIZE_LEAK)
add_compile_options("-fsanitize=leak")
add_link_options("-fsanitize=leak")
endif()

if(ENABLE_SANITIZE_THREAD)
if(ENABLE_SANITIZE_ADDR OR ENABLE_SANITIZE_LEAK)
message(WARNING "thread does not work with: address and leak")
endif()
add_compile_options("-fsanitize=thread")
add_link_options("-fsanitize=thread")
endif()
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
if(ENABLE_SANITIZE_ADDR)
add_compile_options("/fsanitize=address")
endif()

if(ENABLE_SANITIZE_UNDEF)
message(STATUS "sanitize=undefined not avail. for MSVC")
endif()

if(ENABLE_SANITIZE_LEAK)
message(STATUS "sanitize=leak not avail. for MSVC")
endif()

if(ENABLE_SANITIZE_THREAD)
message(STATUS "sanitize=thread not avail. for MSVC")
endif()
else()
message(WARNING "This sanitizer not supported in this environment (${CMAKE_C_COMPILER_ID})")
return()
endif()
endfunction(add_sanitizer_flags)
Loading

0 comments on commit e8059c3

Please sign in to comment.