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()
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}") list(APPEND TEST_LIBRARIES "${CPP_UNIT_LIB}")
if(NOT CPP_UNIT_CONFIG_${META_PROJECT_NAME}_FOUND) 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}.") 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()
endif()
if(CPP_UNIT_INCLUDE_DIR) if(CPP_UNIT_INCLUDE_DIR)
list(APPEND TEST_INCLUDE_DIRS "${CPP_UNIT_INCLUDE_DIR}") list(APPEND TEST_INCLUDE_DIRS "${CPP_UNIT_INCLUDE_DIR}")
endif() endif()
endif() endif()
if(CPP_UNIT_LIB OR META_NO_CPP_UNIT) # always link test applications against c++utilities
# always link test applications against c++utilities list(APPEND TEST_LIBRARIES ${CPP_UTILITIES_LIB})
list(APPEND TEST_LIBRARIES ${CPP_UTILITIES_LIB})
# set compile definitions # set compile definitions
if(NOT META_PUBLIC_SHARED_LIB_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}) set(META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS ${META_PUBLIC_COMPILE_DEFINITIONS} ${META_ADDITIONAL_PUBLIC_SHARED_COMPILE_DEFINITIONS})
endif() endif()
if(NOT META_PRIVATE_SHARED_LIB_COMPILE_DEFINITIONS) 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}) 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 # add target for test executable, but exclude it from the "all" target when EXCLUDE_TESTS_FROM_ALL is set
if(EXCLUDE_TESTS_FROM_ALL) if(EXCLUDE_TESTS_FROM_ALL)
set(TESTS_EXCLUSION EXCLUDE_FROM_ALL) set(TESTS_EXCLUSION EXCLUDE_FROM_ALL)
else() else()
unset(TESTS_EXCLUSION) unset(TESTS_EXCLUSION)
endif() endif()
add_executable(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests ${TESTS_EXCLUSION} ${TEST_HEADER_FILES} ${TEST_SRC_FILES}) 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 # add top-level target to build all test targets conveniently, also when excluded from "all" target
if(NOT TARGET tests) if(NOT TARGET tests)
add_custom_target(tests DEPENDS ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_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) add_dependencies(tests ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests)
endif() endif()
# handle testing a library (which is default project type) # handle testing a library (which is default project type)
if(NOT META_PROJECT_TYPE OR "${META_PROJECT_TYPE}" STREQUAL "library") if(NOT META_PROJECT_TYPE OR "${META_PROJECT_TYPE}" STREQUAL "library")
# when testing a library, the test application always needs to link against it # when testing a library, the test application always needs to link against it
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
list(APPEND TEST_LIBRARIES ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}) 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) 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}") message(STATUS "Linking test target statically against ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}")
endif() endif()
endif() endif()
# handle testing an application # handle testing an application
if("${META_PROJECT_TYPE}" STREQUAL "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 # using functions directly from the tests might be required -> also create a 'testlib' and link tests against it
if(LINK_TESTS_AGAINST_APP_TARGET) if(LINK_TESTS_AGAINST_APP_TARGET)
# create target for the 'testlib' # 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") # 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) set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_testlib PROPERTIES CXX_VISIBILITY_PRESET default)
endif() endif()
endif() endif()
# actually apply configuration for test target # actually apply configuration for test target
target_link_libraries(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests target_link_libraries(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
PUBLIC ${ACTUAL_ADDITIONAL_LINK_FLAGS} "${PUBLIC_LIBRARIES}" PUBLIC ${ACTUAL_ADDITIONAL_LINK_FLAGS} "${PUBLIC_LIBRARIES}"
PRIVATE "${TEST_LIBRARIES}" "${PRIVATE_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 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${HEADER_INSTALL_DESTINATION}> $<INSTALL_INTERFACE:${HEADER_INSTALL_DESTINATION}>
@ -145,37 +150,37 @@ if(CPP_UNIT_LIB OR META_NO_CPP_UNIT)
PRIVATE PRIVATE
${TEST_INCLUDE_DIRS} ${TEST_INCLUDE_DIRS}
"${PRIVATE_SHARED_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}" PUBLIC "${META_PUBLIC_SHARED_LIB_COMPILE_DEFINITIONS}"
PRIVATE "${META_PRIVATE_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}" PUBLIC "${META_PUBLIC_SHARED_LIB_COMPILE_OPTIONS}"
PRIVATE "${META_PRIVATE_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}" CXX_STANDARD "${META_CXX_STANDARD}"
LINK_SEARCH_START_STATIC ${STATIC_LINKAGE} LINK_SEARCH_START_STATIC ${STATIC_LINKAGE}
LINK_SEARCH_END_STATIC ${STATIC_LINKAGE} LINK_SEARCH_END_STATIC ${STATIC_LINKAGE}
) )
# make the test recognized by ctest # make the test recognized by ctest
unset(RUN_TESTS_APPLICATION_ARG) unset(RUN_TESTS_APPLICATION_ARG)
if(META_PROJECT_TYPE STREQUAL "application") if(META_PROJECT_TYPE STREQUAL "application")
set(RUN_TESTS_APPLICATION_ARGS -a "$<TARGET_FILE:${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}>") set(RUN_TESTS_APPLICATION_ARGS -a "$<TARGET_FILE:${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}>")
endif() endif()
if(NOT META_TEST_TARGET_IS_MANUAL) if(NOT META_TEST_TARGET_IS_MANUAL)
add_test(NAME ${META_PROJECT_NAME}_run_tests COMMAND add_test(NAME ${META_PROJECT_NAME}_run_tests COMMAND
${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests
-p "${CMAKE_CURRENT_SOURCE_DIR}/testfiles" -p "${CMAKE_CURRENT_SOURCE_DIR}/testfiles"
-w "${CMAKE_CURRENT_BINARY_DIR}/testworkingdir" -w "${CMAKE_CURRENT_BINARY_DIR}/testworkingdir"
${RUN_TESTS_APPLICATION_ARGS} ${RUN_TESTS_APPLICATION_ARGS}
) )
endif() endif()
# enable source code based coverage analysis using clang # enable source code based coverage analysis using clang
if(CLANG_SOURCE_BASED_COVERAGE_AVAILABLE) if(CLANG_SOURCE_BASED_COVERAGE_AVAILABLE)
# define path of raw profile data # define path of raw profile data
set(LLVM_PROFILE_RAW_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}_tests.profraw") 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 # 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() else()
message(FATAL_ERROR "Unable to generate target for coverage report because llvm-profdata and llvm-cov are not available.") message(FATAL_ERROR "Unable to generate target for coverage report because llvm-profdata and llvm-cov are not available.")
endif() 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() 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) set(TEST_CONFIG_DONE YES)