Skip to content

Commit

Permalink
Merge pull request #171 from fktn-k/feature/166_organize_test_cmake_s…
Browse files Browse the repository at this point in the history
…cripts

#166 Organize CMake scripts for testing
  • Loading branch information
fktn-k authored Oct 21, 2023
2 parents 5d53c51 + ab82f1b commit 486bdbd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 130 deletions.
55 changes: 28 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
cmake_minimum_required(VERSION 3.8)

#
# Project name and version.
#
################################
# Project name and version #
################################

project(
fkYAML
VERSION 0.1.2
LANGUAGES CXX)

#
# Preparations depending on the specified build options.
#
#############################################################
# Preparations depending on the specified build options #
#############################################################

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# Configure installation of the fkYAML library if this project is the main project.
Expand Down Expand Up @@ -47,9 +47,9 @@ if(FK_YAML_BUILD_TEST OR FK_YAML_BUILD_ALL_TEST)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CATCH2_ROOT_DIR}/contrib")
endif()

#
# Configurations.
#
###########################################
# Configure variables for the library #
###########################################

include(GNUInstallDirs)

Expand All @@ -70,9 +70,9 @@ set(FK_YAML_CMAKE_PROJECT_CONFIG_FILE "${FK_YAML_CMAKE_CONFIG_DIR}/${PROJECT_NAM
set(FK_YAML_CMAKE_PROJECT_TARGETS_FILE "${FK_YAML_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake")
set(FK_YAML_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")

#
# Create target and add include path.
#
##########################################
# Create target and add include path #
##########################################

add_library(${FK_YAML_TARGET_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${FK_YAML_TARGET_NAME} ALIAS ${FK_YAML_TARGET_NAME})
Expand All @@ -82,9 +82,9 @@ target_include_directories(
${FK_YAML_TARGET_NAME} INTERFACE $<BUILD_INTERFACE:${FK_YAML_INCLUDE_BUILD_DIR}>
$<INSTALL_INTERFACE:${FK_YAML_INCLUDE_INSTALL_DIR}>)

#
# Integrate with tools.
#
############################
# Integrate with tools #
############################

# Configure clang-tidy if enabled.
if(FK_YAML_RUN_CLANG_TIDY)
Expand All @@ -96,16 +96,16 @@ if(FK_YAML_RUN_IWYU)
add_subdirectory(tool/iwyu)
endif()

#
# Install a pkg-config file, so other tools can find this library.
#
#################################
# Install a pkg-config file #
#################################

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc")

#
# Create and configure the unit test target.
#
###########################
# Build the test apps #
###########################

if(FK_YAML_BUILD_TEST OR FK_YAML_BUILD_ALL_TEST)
add_subdirectory(${CATCH2_ROOT_DIR})
Expand All @@ -114,17 +114,18 @@ if(FK_YAML_BUILD_TEST OR FK_YAML_BUILD_ALL_TEST)
add_subdirectory(test)
endif()

#
# Generate API documentation if target == doxygen.
#
##################################
# Generate API documentation #
##################################

if(FK_YAML_RUN_DOXYGEN)
set(FK_YAML_DOXYGEN_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
add_subdirectory(docs)
endif()

#
# Install header files, generate and install cmake config files for find_package()
#
#######################
# Install package #
#######################

include(CMakePackageConfigHelpers)

Expand Down
169 changes: 103 additions & 66 deletions test/unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# find lcov tool.
###############################
# Prepare dependent tools #
###############################

# find lcov tool for coverage data.
if(FK_YAML_CODE_COVERAGE)
find_program(LCOV_TOOL NAMES lcov REQUIRED)
execute_process(
Expand All @@ -9,26 +13,16 @@ if(FK_YAML_CODE_COVERAGE)
message(STATUS "lcov ${LCOV_TOOL_VERSION} (${LCOV_TOOL})")
endif()

# set compiler options for unit test app.
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /utf-8 /permissive-")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Od")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wno-terminate")
# necessary to build iterator class test.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-self-move")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-c++98-compat -Wno-c++98-compat-pedantic")
# necessary to build iterator class test.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-self-assign-overloaded -Wno-self-move")
# find Valgrind & prepare to use it as the target memory check tool.
if(FK_YAML_RUN_VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind REQUIRED)
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --error-exitcode=1")
endif()

############################################
# configure C++ standard for unit test #
############################################

# determine C++ standard used in unit test app.
if("${FK_YAML_TEST_TARGET_CXX_STANDARD}" STREQUAL "")
# Apply minimum required C++ standard check by default.
Expand Down Expand Up @@ -56,6 +50,90 @@ if(NOT compiler_supports_cxx_${FK_YAML_TEST_TARGET_CXX_STANDARD})
return()
endif()

#################################
# Configure compile options #
#################################

add_library(unit_test_config INTERFACE)
target_link_libraries(unit_test_config INTERFACE Catch2::Catch2 ${FK_YAML_TARGET_NAME})
target_compile_features(unit_test_config INTERFACE cxx_std_${FK_YAML_TEST_TARGET_CXX_STANDARD})
set_target_properties(unit_test_config PROPERTIES CXX_EXTENTIONS OFF)
target_compile_definitions(unit_test_config INTERFACE $<$<CONFIG:Release>:NDEBUG>)

# Configure compile options in common
target_compile_options(unit_test_config INTERFACE
# MSVC
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /EHsc /utf-8 /permissive-
$<$<CONFIG:Debug>:/Z7>
$<$<CONFIG:Release>:/Od>
>

# GNU
$<$<CXX_COMPILER_ID:GNU>:
-Wall -Wextra -pedantic -Wpedantic --all-warnings --extra-warnings
-Wc++0x-compat
-Wno-self-move # necessary to build iterator class test.
>

# Clang
$<$<CXX_COMPILER_ID:Clang>:
-Wall -pedantic -Wno-c++98-compat -Wno-c++98-compat-pedantic
-Wno-self-assign-overloaded -Wno-self-move # necessary to build iterator class test.
>
)

# additional compile options for Clang Sanitizers.
if(FK_YAML_RUN_CLANG_SANITIZERS)
target_compile_options(
unit_test_config
INTERFACE -O1
-g
-fno-omit-frame-pointer
-fsanitize=address,undefined,bounds,integer,nullability
-fno-sanitize-recover=all
-fno-sanitize=unsigned-integer-overflow,unsigned-shift-base
)

if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
target_link_options(
unit_test_config
INTERFACE -fno-omit-frame-pointer
-fsanitize=address,undefined,bounds,integer,nullability
-fno-sanitize-recover=all
-fno-sanitize=unsigned-integer-overflow,unsigned-shift-base
)
else()
target_link_libraries(
unit_test_config
INTERFACE -fno-omit-frame-pointer
-fsanitize=address,undefined,bounds,integer,nullability
-fno-sanitize-recover=all
-fno-sanitize=unsigned-integer-overflow,unsigned-shift-base
)
endif()
endif()

# additional compile options for coverage.
if(FK_YAML_CODE_COVERAGE)
target_compile_options(
unit_test_config
INTERFACE -O0 # no optimization
-g # generate debug info
--coverage # set all required flags to generate code coverage data
)

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
target_link_options(unit_test_config INTERFACE --coverage)
else()
target_link_libraries(unit_test_config INTERFACE --coverage)
endif()
endif()

###########################
# Build unit test app #
###########################

set(TEST_TARGET "fkYAMLUnitTest")

add_executable(
Expand All @@ -71,60 +149,18 @@ add_executable(
test_serializer_class.cpp
main.cpp)

target_include_directories(${TEST_TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(${TEST_TARGET} Catch2::Catch2)

# set and require the target C++ standard. (defaults to C++11)
target_compile_features(${TEST_TARGET} PRIVATE cxx_std_${FK_YAML_TEST_TARGET_CXX_STANDARD})

set_target_properties(${TEST_TARGET} PROPERTIES CXX_EXTENTIONS OFF)
target_link_libraries(${TEST_TARGET} PRIVATE unit_test_config)

include(Catch)
catch_discover_tests(${TEST_TARGET})

add_dependencies(${TEST_TARGET} ${FK_YAML_TARGET_NAME})

# configure for Valgrind.
if(FK_YAML_RUN_VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind REQUIRED)
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --error-exitcode=1")
endif()

# configure for Clang Sanitizers.
if(FK_YAML_RUN_CLANG_SANITIZERS)
target_compile_options(
${TEST_TARGET}
PRIVATE -O1
-g
-fno-omit-frame-pointer
-fsanitize=address,undefined,bounds,integer,nullability
-fno-sanitize-recover=all
-fno-sanitize=unsigned-integer-overflow,unsigned-shift-base
)
target_link_options(
${TEST_TARGET}
PRIVATE -fno-omit-frame-pointer
-fsanitize=address,undefined,bounds,integer,nullability
-fno-sanitize-recover=all
-fno-sanitize=unsigned-integer-overflow,unsigned-shift-base
)
endif()
############################################
# Configure custom target for coverage #
############################################

if(FK_YAML_CODE_COVERAGE)
target_compile_options(
${TEST_TARGET}
PRIVATE -O0 # no optimization
-g # generate debug info
--coverage # set all required flags to generate code coverage data
)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
target_link_options(${TEST_TARGET} PRIVATE --coverage)
else()
target_link_libraries(${TEST_TARGET} PRIVATE --coverage)
endif()

file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/fkYAML/*.hpp)

add_custom_target(
Expand All @@ -140,5 +176,6 @@ if(FK_YAML_CODE_COVERAGE)
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_NAME}.info.filtered.noexcept
${PROJECT_BINARY_DIR}/coverage/fkYAML.info
DEPENDS ${TEST_TARGET}
COMMENT "Execute unit test app with code coverage.")
COMMENT "Execute unit test app with code coverage."
)
endif()
19 changes: 16 additions & 3 deletions tool/clang_tidy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
###############################
# Prepare dependent tools #
###############################

# find the clang-tidy tool.
find_program(CLANG_TIDY_EXE NAMES clang-tidy REQUIRED)
execute_process(
Expand All @@ -7,7 +11,10 @@ execute_process(
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TIDY_VERSION "${CLANG_TIDY_VERSION}")
message(STATUS "Found clang-tidy. location: ${CLANG_TIDY_EXE} version: ${CLANG_TIDY_VERSION}")

# generate a helper souce file with library source file paths.
####################################
# Generate a helper souce file #
####################################

set(HELPER_FILE_PATH "${CMAKE_CURRENT_BINARY_DIR}/clang_tidy_helper.cpp")
file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/fkYAML/*.hpp)
foreach(SRC_FILE ${SRC_FILES})
Expand All @@ -16,11 +23,17 @@ foreach(SRC_FILE ${SRC_FILES})
endforeach()
file(APPEND ${HELPER_FILE_PATH} "\nint main() {\n return 0;\n}")

# set options for the clang-tidy tool.
###########################################
# set options for the clang-tidy tool #
###########################################

set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# build the helper source file to run the clang-tidy tool
####################################
# build the helper source file #
####################################

add_executable(ClangTidyHelper ${HELPER_FILE_PATH})
target_include_directories(ClangTidyHelper PUBLIC ${FK_YAML_INCLUDE_BUILD_DIR})
target_compile_options(ClangTidyHelper PUBLIC -O0) # no optimization
Expand Down
34 changes: 0 additions & 34 deletions tool/clang_tidy/clang_tidy_helper.cpp

This file was deleted.

Loading

0 comments on commit 486bdbd

Please sign in to comment.