From: Jannik ZANDER Date: Fri, 10 Feb 2017 19:34:16 +0000 (+0100) Subject: Add cmake folder X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;ds=inline;p=codility.git Add cmake folder --- diff --git a/AddUnitTest.cmake b/AddUnitTest.cmake deleted file mode 100644 index 05f52ff..0000000 --- a/AddUnitTest.cmake +++ /dev/null @@ -1,124 +0,0 @@ -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_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" gtest gtest_main) - - # 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 b/CMakeLists.txt index 383368d..d26d427 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(project_test) -include(AddUnitTest.cmake) +include(cmake/AddUnitTest.cmake) add_subdirectory( solution1 ) add_subdirectory( solution2 ) diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in deleted file mode 100644 index 73ea4a6..0000000 --- a/CMakeLists.txt.in +++ /dev/null @@ -1,16 +0,0 @@ -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 "" -) - diff --git a/cmake/AddUnitTest.cmake b/cmake/AddUnitTest.cmake new file mode 100644 index 0000000..722109e --- /dev/null +++ b/cmake/AddUnitTest.cmake @@ -0,0 +1,124 @@ +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_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" gtest gtest_main) + + # 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("${CMAKE_CURRENT_SOURCE_DIR}/cmake/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/cmake/CMakeLists.txt.in b/cmake/CMakeLists.txt.in new file mode 100644 index 0000000..73ea4a6 --- /dev/null +++ b/cmake/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 "" +) +