]> git.zndr.dk Git - unittest.git/commitdiff
Add new files
authorJannik ZANDER <jzander@grundfos.com>
Fri, 10 Feb 2017 18:09:09 +0000 (19:09 +0100)
committerJannik ZANDER <jzander@grundfos.com>
Fri, 10 Feb 2017 18:09:09 +0000 (19:09 +0100)
AddUnitTest.cmake [new file with mode: 0644]
CMakeLists.txt.in [new file with mode: 0644]

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