Skip to content
Snippets Groups Projects
Commit 516ea062 authored by Maxence Naud's avatar Maxence Naud
Browse files

UPD: unit-tests/CmakeLists.txt format and add minimum version for Catch2

parent 54414eee
No related branches found
No related tags found
1 merge request!318[Upd] release verision 0.5.0
Pipeline #64164 waiting for manual action
find_package(Catch2 QUIET) # Catch2 configuration
set(CATCH2_MIN_VERSION 3.3.0)
# Try to find system installed Catch2
find_package(Catch2 ${CATCH2_MIN_VERSION} QUIET)
if(NOT Catch2_FOUND) if(NOT Catch2_FOUND)
message(STATUS "Catch2 not found in system, retrieving from git") message(STATUS "Catch2 not found in system, retrieving from git")
...@@ -9,62 +13,93 @@ if(NOT Catch2_FOUND) ...@@ -9,62 +13,93 @@ if(NOT Catch2_FOUND)
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG devel # or a later release GIT_TAG devel # or a later release
) )
FetchContent_MakeAvailable(Catch2) FetchContent_MakeAvailable(Catch2)
message(STATUS "Fetched Catch2 version ${Catch2_VERSION}")
else() else()
message(STATUS "Found system Catch2 version ${Catch2_VERSION}") message(STATUS "Using system Catch2 version ${Catch2_VERSION}")
endif() endif()
# Get all source files
file(GLOB_RECURSE src_files "*.cpp") file(GLOB_RECURSE src_files "*.cpp")
# Create test executable
add_executable(tests${module_name} ${src_files}) add_executable(tests${module_name} ${src_files})
# Set C++14 standard
target_compile_features(tests${module_name} PRIVATE cxx_std_14) target_compile_features(tests${module_name} PRIVATE cxx_std_14)
set(FORCE_CI TRUE) set(FORCE_CI TRUE)
if (NOT(FORCE_CI)) # Compiler flags and options
if(NOT(FORCE_CI))
if (DOSANITIZE STREQUAL "ON") # Sanitization configuration
set(SANITIZE_FLAGS -fsanitize=address,leak,undefined,float-divide-by-zero -fno-omit-frame-pointer) if(DOSANITIZE STREQUAL "ON")
#TODO sanitizer seems buggy in some situations with msvc, leading to linker errors, temporarily inactivating it set(SANITIZE_FLAGS
#set(SANITIZE_MSVC_FLAGS) -fsanitize=address,leak,undefined,float-divide-by-zero
set(SANITIZE_MSVC_FLAGS /fsanitize=address) -fno-omit-frame-pointer
target_compile_definitions(tests${module_name} PUBLIC _DISABLE_VECTOR_ANNOTATION) )
else() set(SANITIZE_MSVC_FLAGS /fsanitize=address)
set(SANITIZE_FLAGS)
set(SANITIZE_MSVC_FLAGS)
endif()
set(STRICT_ALIASING_FLAGS -fstrict-aliasing -Wstrict-aliasing=2) # Temporary workaround for MSVC sanitizer issues
target_compile_definitions(tests${module_name} PUBLIC _DISABLE_VECTOR_ANNOTATION)
# -fvisibility=hidden required by pybind11 else()
target_compile_options(tests${module_name} PUBLIC set(SANITIZE_FLAGS "")
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: set(SANITIZE_MSVC_FLAGS "")
-fvisibility=hidden>) endif()
target_compile_options(tests${module_name} PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -Wold-style-cast -pedantic -Werror=narrowing -Wshadow $<$<BOOL:${WERROR}>:-Werror> ${SANITIZE_FLAGS}>)
target_compile_options(tests${module_name} PRIVATE
$<$<CXX_COMPILER_ID:GNU>:${STRICT_ALIASING_FLAGS}>)
target_compile_options(${module_name} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /DWIN32 /D_WINDOWS /GR /EHsc /MP /Zc:__cplusplus /Zc:preprocessor /permissive- ${SANITIZE_MSVC_FLAGS}>)
if (DOSANITIZE STREQUAL "ON")
target_compile_options(${module_name} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/MDd>)
endif()
# TODO FIXME: I'm not sure it's a good idea to propagate this option but, at this point, it was the only way that worked to silence C4477
target_compile_options(${module_name} PUBLIC $<$<CXX_COMPILER_ID:MSVC>: /wd4477>)
target_link_options(tests${module_name} PUBLIC $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:${SANITIZE_FLAGS}>) # Strict aliasing for better type safety
#target_link_options(tests${module_name} PUBLIC $<$<CXX_COMPILER_ID:MSVC>:${SANITIZE_MSVC_FLAGS}>) set(STRICT_ALIASING_FLAGS -fstrict-aliasing -Wstrict-aliasing=2)
endif() # Compiler options for different toolchains
target_compile_options(tests${module_name} PUBLIC
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-fvisibility=hidden> # Hide symbols by default
) # -fvisibility=hidden required by Pybind11
target_link_libraries(tests${module_name} PRIVATE ${module_name}) # Common warning and error flags
target_compile_options(tests${module_name} PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -Wold-style-cast -pedantic -Werror=narrowing -Wshadow
$<$<BOOL:${WERROR}>:-Werror>
${SANITIZE_FLAGS}
>
)
# Strict aliasing for GCC
target_compile_options(tests${module_name} PRIVATE
$<$<CXX_COMPILER_ID:GNU>:${STRICT_ALIASING_FLAGS}>
)
target_link_libraries(tests${module_name} PRIVATE Catch2::Catch2WithMain) # MSVC-specific settings
target_compile_options(tests${module_name} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /DWIN32 /D_WINDOWS /GR /EHsc /MP
/Zc:__cplusplus /Zc:preprocessor /permissive-
${SANITIZE_MSVC_FLAGS}
>
)
# Workaround for C4477 warning in MSVC
if(DOSANITIZE STREQUAL "ON")
target_compile_options(tests${module_name} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/MDd>)
endif()
# TODO: Fix this once proper configuration is available
# only way to silence C4477
target_compile_options(tests${module_name} PUBLIC $<$<CXX_COMPILER_ID:MSVC>: /wd4477>)
endif()
# Link libraries
target_link_libraries(tests${module_name} PRIVATE
${module_name}
Catch2::Catch2WithMain
)
# Setup testing
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest) include(CTest)
include(Catch) include(Catch)
# Discover and add tests
catch_discover_tests(tests${module_name}) catch_discover_tests(tests${module_name})
# Set test configuration for CTest
set(CTEST_CONFIGURATION_TYPE ${CMAKE_BUILD_TYPE})
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment