Reduce indentation in TestTarget.cmake

This commit is contained in:
Martchus 2019-01-13 22:01:52 +01:00
parent a0dff23c26
commit f66aa33d61
1 changed files with 274 additions and 275 deletions

View File

@ -33,46 +33,51 @@ if(NOT META_NO_CPP_UNIT)
endif()
endif()
if(CPP_UNIT_LIB)
if(NOT CPP_UNIT_LIB)
message(WARNING "Unable to add test target because cppunit could not be located.")
set(META_HAVE_TESTS NO)
set(TEST_CONFIG_DONE YES)
return()
endif()
list(APPEND TEST_LIBRARIES "${CPP_UNIT_LIB}")
if(NOT CPP_UNIT_CONFIG_${META_PROJECT_NAME}_FOUND)
message(WARNING "Cppunit not detected via pkg-config so the version couldn't be checked. Required version for ${META_PROJECT_NAME} is ${META_REQUIRED_CPP_UNIT_VERSION}.")
endif()
endif()
if(CPP_UNIT_INCLUDE_DIR)
list(APPEND TEST_INCLUDE_DIRS "${CPP_UNIT_INCLUDE_DIR}")
endif()
endif()
if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
# always link test applications against c++utilities
list(APPEND TEST_LIBRARIES ${CPP_UTILITIES_LIB})
# always link test applications against c++utilities
list(APPEND TEST_LIBRARIES ${CPP_UTILITIES_LIB})
# set compile definitions
if(NOT META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS)
# set compile definitions
if(NOT META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS)
set(META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS ${META_PUBLIC_COMPILE_DEFINITIONS} ${META_ADDITIONAL_PUBLIC_SHARED_COMPILE_DEFINITIONS})
endif()
if(NOT META_PRIVATE_SHARED_LIB_COMPILE_DEFINITIONS)
endif()
if(NOT META_PRIVATE_SHARED_LIB_COMPILE_DEFINITIONS)
set(META_PRIVATE_SHARED_LIB_COMPILE_DEFINITIONS ${META_PRIVATE_COMPILE_DEFINITIONS} ${META_ADDITIONAL_PRIVATE_SHARED_COMPILE_DEFINITIONS})
endif()
endif()
# add target for test executable, but exclude it from the "all" target when EXCLUDE_TESTS_FROM_ALL is set
if(EXCLUDE_TESTS_FROM_ALL)
# add target for test executable, but exclude it from the "all" target when EXCLUDE_TESTS_FROM_ALL is set
if(EXCLUDE_TESTS_FROM_ALL)
set(TESTS_EXCLUSION EXCLUDE_FROM_ALL)
else()
else()
unset(TESTS_EXCLUSION)
endif()
add_executable(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests ${TESTS_EXCLUSION} ${TEST_HEADER_FILES} ${TEST_SRC_FILES})
endif()
add_executable(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests ${TESTS_EXCLUSION} ${TEST_HEADER_FILES} ${TEST_SRC_FILES})
# add top-level target to build all test targets conveniently, also when excluded from "all" target
if(NOT TARGET tests)
# add top-level target to build all test targets conveniently, also when excluded from "all" target
if(NOT TARGET tests)
add_custom_target(tests DEPENDS ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests)
else()
else()
add_dependencies(tests ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests)
endif()
endif()
# handle testing a library (which is default project type)
if(NOT META_PROJECT_TYPE OR "${META_PROJECT_TYPE}" STREQUAL "library")
# handle testing a library (which is default project type)
if(NOT META_PROJECT_TYPE OR "${META_PROJECT_TYPE}" STREQUAL "library")
# when testing a library, the test application always needs to link against it
if(BUILD_SHARED_LIBS)
list(APPEND TEST_LIBRARIES ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX})
@ -81,10 +86,10 @@ if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
list(APPEND TEST_LIBRARIES ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_static)
message(STATUS "Linking test target statically against ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}")
endif()
endif()
endif()
# handle testing an application
if("${META_PROJECT_TYPE}" STREQUAL "application")
# handle testing an application
if("${META_PROJECT_TYPE}" STREQUAL "application")
# using functions directly from the tests might be required -> also create a 'testlib' and link tests against it
if(LINK_TESTS_AGAINST_APP_TARGET)
# create target for the 'testlib'
@ -130,14 +135,14 @@ if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
# ensure all symbols are visible (man gcc: "Despite the nomenclature, default always means public")
set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_testlib PROPERTIES CXX_VISIBILITY_PRESET default)
endif()
endif()
endif()
# actually apply configuration for test target
target_link_libraries(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
# actually apply configuration for test target
target_link_libraries(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
PUBLIC ${ACTUAL_ADDITIONAL_LINK_FLAGS} "${PUBLIC_LIBRARIES}"
PRIVATE "${TEST_LIBRARIES}" "${PRIVATE_LIBRARIES}"
)
target_include_directories(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
)
target_include_directories(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${HEADER_INSTALL_DESTINATION}>
@ -145,37 +150,37 @@ if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
PRIVATE
${TEST_INCLUDE_DIRS}
"${PRIVATE_SHARED_INCLUDE_DIRS}"
)
target_compile_definitions(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
)
target_compile_definitions(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
PUBLIC "${META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS}"
PRIVATE "${META_PRIVATE_SHARED_LIB_COMPILE_DEFINITIONS}"
)
target_compile_options(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
)
target_compile_options(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
PUBLIC "${META_PUBLIC_SHARED_LIB_COMPILE_OPTIONS}"
PRIVATE "${META_PRIVATE_SHARED_LIB_COMPILE_OPTIONS}"
)
set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests PROPERTIES
)
set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests PROPERTIES
CXX_STANDARD "${META_CXX_STANDARD}"
LINK_SEARCH_START_STATIC ${STATIC_LINKAGE}
LINK_SEARCH_END_STATIC ${STATIC_LINKAGE}
)
)
# make the test recognized by ctest
unset(RUN_TESTS_APPLICATION_ARG)
if(META_PROJECT_TYPE STREQUAL "application")
# make the test recognized by ctest
unset(RUN_TESTS_APPLICATION_ARG)
if(META_PROJECT_TYPE STREQUAL "application")
set(RUN_TESTS_APPLICATION_ARGS -a "$<TARGET_FILE:${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}>")
endif()
if(NOT META_TEST_TARGET_IS_MANUAL)
endif()
if(NOT META_TEST_TARGET_IS_MANUAL)
add_test(NAME ${META_PROJECT_NAME}_run_tests COMMAND
${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
-p "${CMAKE_CURRENT_SOURCE_DIR}/testfiles"
-w "${CMAKE_CURRENT_BINARY_DIR}/testworkingdir"
${RUN_TESTS_APPLICATION_ARGS}
)
endif()
endif()
# enable source code based coverage analysis using clang
if(CLANG_SOURCE_BASED_COVERAGE_AVAILABLE)
# enable source code based coverage analysis using clang
if(CLANG_SOURCE_BASED_COVERAGE_AVAILABLE)
# define path of raw profile data
set(LLVM_PROFILE_RAW_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests.profraw")
# define path of list with additional raw profile data from fork processes spawned during tests
@ -333,18 +338,12 @@ if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
else()
message(FATAL_ERROR "Unable to generate target for coverage report because llvm-profdata and llvm-cov are not available.")
endif()
endif()
# add the test executable to the dependencies of the check target
if(NOT META_TEST_TARGET_IS_MANUAL)
add_dependencies(check ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests)
endif()
set(META_HAVE_TESTS YES)
else()
message(WARNING "Unable to add test target because cppunit could not be located.")
set(META_HAVE_TESTS NO)
endif()
# add the test executable to the dependencies of the check target
if(NOT META_TEST_TARGET_IS_MANUAL)
add_dependencies(check ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests)
endif()
set(META_HAVE_TESTS YES)
set(TEST_CONFIG_DONE YES)