From: Jannik ZANDER Date: Fri, 10 Feb 2017 18:09:09 +0000 (+0100) Subject: Add new files X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;h=1e6df3e47ea8ef7fa6f74274ae1436cf88444f98;p=unittest.git Add new files --- diff --git a/AddUnitTest.cmake b/AddUnitTest.cmake new file mode 100644 index 0000000..da74bf9 --- /dev/null +++ b/AddUnitTest.cmake @@ -0,0 +1,125 @@ +cmake_minimum_required (VERSION 3.2) + +enable_testing() +set_property( GLOBAL PROPERTY USE_FOLDERS ON) + +function(add_unit) + cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN}) + print_target("Component" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}") + if ("${my_SRCS}" MATCHES ".*\\.(cpp|c)") + # Add public library + add_library(${my_TARGET} "${my_SRCS}") + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}") + target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS}) + add_unitfolder(${my_TARGET} "${my_SRCS}") + else() + # Add interface library + add_library(${my_TARGET} INTERFACE) + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + target_link_libraries(${my_TARGET} INTERFACE "${my_DEPS}") + target_compile_definitions(${my_TARGET} INTERFACE ${my_DEFS}) + + # Add header files to IDE view (this has no effect on build) + foreach(src ${my_SRCS}) + target_sources(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${src}") + endforeach() + endif() +endfunction() + +function(add_unittest) + cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN}) + print_target("UnitTest" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}") + + # Add test executable + add_executable(${my_TARGET} "${my_SRCS}") + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + target_include_directories(${my_TARGET} PUBLIC "${GTEST_INCLUDE_DIRS}") + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" ${GTEST_BOTH_LIBRARIES}) + + # Add test to test suite + add_test(${my_TARGET} ${my_TARGET}) + add_unitfolder(${my_TARGET} "${my_SRCS}") + + # Run test after build + add_custom_command(TARGET ${my_TARGET} POST_BUILD COMMAND ${my_TARGET}) +endfunction() + +function(add_unitfolder mytarget mysrcs) + # source_group("" FILES "${mysrcs}") + string(REGEX REPLACE "_stub*$" "" myfolder ${mytarget}) + string(REGEX REPLACE "_stubs*$" "" myfolder ${mytarget}) + string(REGEX REPLACE "_test*$" "" myfolder ${myfolder}) + string(REGEX REPLACE "_if*$" "" myfolder ${myfolder}) + string(TOUPPER ${myfolder} myfolder) + set_property(TARGET ${mytarget} PROPERTY FOLDER "${myfolder}") +endfunction() + +# Print target function +function(print_target type target srcs incs deps defs) + message("${type}:") + message(" ${target}:") + message(" Sources:") + foreach(src ${srcs}) + message(" - ${src}") + endforeach() + message(" Includes:") + foreach(inc ${incs}) + message(" - ${inc}") + endforeach() + message(" Dependencies:") + foreach(dep ${deps}) + message(" - ${dep}") + endforeach() + message(" Definitions:") + foreach(def ${defs}) + message(" - ${def}") + endforeach() + message("===================================================") +endfunction() + +# Compiler options +if(MSVC) + add_definitions(/FC /D_CRT_SECURE_NO_WARNINGS) +endif() + +if(CMAKE_COMPILER_IS_GNUCXX) + add_definitions(-Wall -Wno-deprecated -pthread -D__PC__) +endif() + +# Download and unpack googletest at configure time +configure_file("CMakeLists.txt.in" googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") +endif() +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") +endif() + +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +# Add googletest directly to our build. This defines +# the gtest and gtest_main targets. +add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src + ${CMAKE_BINARY_DIR}/googletest-build) + +# The gtest/gtest_main targets carry header search path +# dependencies automatically when using CMake 2.8.11 or +# later. Otherwise we have to add them here ourselves. +if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") +endif() diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in new file mode 100644 index 0000000..73ea4a6 --- /dev/null +++ b/CMakeLists.txt.in @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) +