Use gmock and gtest targets from googletest-release-1.8.0
authorM. Eric Irrgang <ericirrgang@gmail.com>
Tue, 23 Jul 2019 19:05:53 +0000 (22:05 +0300)
committerMark Abraham <mark.j.abraham@gmail.com>
Wed, 7 Aug 2019 20:13:10 +0000 (22:13 +0200)
Import a couple of missing files from the googletest-release-1.8.0 and
switch to the upstream definition of the `gmock` and `gtest` targets.

Removed some outdated content from README.Gromacs

Refs #3033

Change-Id: Iaefe4d11b01a3d37e162d7d00bae7ba34a0b5b4e

python_packaging/sample_restraint/tests/CMakeLists.txt
src/external/CMakeLists.txt
src/external/googletest/.gitignore [new file with mode: 0644]
src/external/googletest/CMakeLists.txt [new file with mode: 0644]
src/external/googletest/README.Gromacs
src/external/googletest/googlemock/CMakeLists.txt
src/external/googletest/googletest/CMakeLists.txt
src/external/googletest/googletest/cmake/internal_utils.cmake [new file with mode: 0644]
src/external/googletest/googletest/docs/XcodeGuide.md
src/testutils/CMakeLists.txt
src/testutils/TestMacros.cmake

index fbc43563ded912f241fd8d2e1c4aeb6c18e302d8..fef4361d19336db0c8eaa964b86d5c07c8a1539a 100644 (file)
@@ -61,10 +61,6 @@ gtest_add_tests(TARGET library-test
 target_include_directories(library-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
 target_link_libraries(library-test Gromacs::gmxapi gtest_main)
 
-set_tests_properties(${BasicPlugin} PROPERTIES
-                     COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}"
-                     COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
-
 # Test the C++ force evaluation for the restrained-ensemble biasing potential.
 add_executable(histogram-test test_histogram.cpp)
 gtest_add_tests(TARGET histogram-test
@@ -76,10 +72,6 @@ set_target_properties(histogram-test PROPERTIES SKIP_BUILD_RPATH FALSE)
 target_link_libraries(histogram-test ensemblepotential Gromacs::gmxapi
                       gtest_main)
 
-set_tests_properties(${EnsembleHistogramPotentialPlugin} PROPERTIES
-                     COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}"
-                     COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
-
 # Test the flat-bottom bounding potential built in to the ensemble restraint.
 add_executable(bounding-test test_bounding_restraint.cpp)
 gtest_add_tests(TARGET bounding-test
@@ -90,7 +82,3 @@ target_include_directories(bounding-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
 set_target_properties(bounding-test PROPERTIES SKIP_BUILD_RPATH FALSE)
 target_link_libraries(bounding-test ensemblepotential Gromacs::gmxapi
                       GTest::Main)
-
-set_tests_properties(${EnsembleBoundingPotentialPlugin} PROPERTIES
-                     COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}"
-                     COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
index 1c3d081cd084289ad816a7bf5d34f4d07fc60672..c0953266099ef83d1431a88e87ba21c643769150 100644 (file)
 # the research papers on the package. Check out http://www.gromacs.org.
 
 if(BUILD_TESTING AND GMX_BUILD_UNITTESTS)
-    include(CheckCXXCompilerFlag)
-    check_cxx_compiler_flag("-Wno-unused-variable" HAS_NO_UNUSED_VARIABLE)
-
     # GTest/GMock suggest linking with pthreads when available for thread safety
     set(CMAKE_THREAD_PREFER_PTHREAD 1)
     find_package(Threads)
     set(PTHREADS_LIBRARIES)
     if (CMAKE_USE_PTHREADS_INIT)
         set(PTHREADS_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
-        list(APPEND GMOCK_COMPILE_DEFINITIONS "GTEST_HAS_PTHREAD=1")
+        # Input to googletest CMake scripts
+        set(GTEST_HAS_PTHREAD 1)
+        # Output for consumption in GROMACS build tree
         set(GTEST_IS_THREADSAFE 1)
     else()
-        list(APPEND GMOCK_COMPILE_DEFINITIONS "GTEST_HAS_PTHREAD=0")
+        # Input to googletest CMake scripts
+        set(GTEST_HAS_PTHREAD 0)
+        # Output for consumption in GROMACS build tree
         set(GTEST_IS_THREADSAFE 0)
     endif()
-    if (WIN32)
-        list(APPEND GMOCK_COMPILE_DEFINITIONS _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
-    endif()
-
-    set(GMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest/googlemock)
-    set(GTEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest/googletest)
-    set(GTEST_SOURCES ${GTEST_DIR}/src/gtest-all.cc)
-    set(GMOCK_SOURCES ${GMOCK_DIR}/src/gmock-all.cc)
 
-    set(GTEST_INCLUDE_DIRS ${GTEST_DIR}/include)
-    set(GMOCK_INCLUDE_DIRS ${GMOCK_DIR}/include ${GTEST_INCLUDE_DIRS})
-
-    include_directories(BEFORE SYSTEM ${GTEST_INCLUDE_DIRS})
-    include_directories(BEFORE SYSTEM ${GTEST_DIR})
-    include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
-    include_directories(BEFORE SYSTEM ${GMOCK_DIR})
-
-    if (HAS_NO_UNUSED_VARIABLE)
-        set_source_files_properties(${GTEST_SOURCES} PROPERTIES COMPILE_FLAGS "-Wno-unused-variable")
+    add_subdirectory(googletest)
+    target_compile_definitions(gmock PRIVATE
+                               _GNU_SOURCE=1
+                               GTEST_CAN_STREAM_RESULTS=0)
+    if (WIN32)
+        target_compile_definitions(gmock PRIVATE _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
+        target_compile_definitions(gtest PRIVATE _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
     endif()
 
-    add_library(gmock STATIC ${UNITTEST_TARGET_OPTIONS} ${GMOCK_SOURCES} ${GTEST_SOURCES})
-    set_property(TARGET gmock APPEND PROPERTY COMPILE_DEFINITIONS
-        "_GNU_SOURCE=1;${GMOCK_COMPILE_DEFINITIONS};GTEST_CAN_STREAM_RESULTS=0")
-    set_property(TARGET gmock APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
-
-    set(GMOCK_LIBRARIES gmock ${PTHREADS_LIBRARIES} PARENT_SCOPE)
-    set(GTEST_LIBRARIES ${GMOCK_LIBRARIES} PARENT_SCOPE)
-    set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIRS} PARENT_SCOPE)
-    set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} PARENT_SCOPE)
-    set(GMOCK_COMPILE_DEFINITIONS ${GMOCK_COMPILE_DEFINITIONS} PARENT_SCOPE)
-    set(GMOCK_COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}" PARENT_SCOPE)
     set(GTEST_IS_THREADSAFE "${GTEST_IS_THREADSAFE}" PARENT_SCOPE)
 endif()
diff --git a/src/external/googletest/.gitignore b/src/external/googletest/.gitignore
new file mode 100644 (file)
index 0000000..ce310bc
--- /dev/null
@@ -0,0 +1,2 @@
+# Ignore CI build directory
+build/
diff --git a/src/external/googletest/CMakeLists.txt b/src/external/googletest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..695d057
--- /dev/null
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 2.6.2)
+
+project( googletest-distribution )
+
+#enable_testing()
+
+option(BUILD_GTEST "Builds the googletest subproject" OFF)
+
+#Note that googlemock target already builds googletest
+option(BUILD_GMOCK "Builds the googlemock subproject" ON)
+
+if(BUILD_GMOCK)
+  add_subdirectory( googlemock )
+elseif(BUILD_GTEST)
+  add_subdirectory( googletest )
+endif()
index d82223b9fcae0d145e23320871c821475a5dce45..3261ee3a511fa0b104588f01a7ed864f086f97e1 100644 (file)
@@ -15,7 +15,6 @@ are unneeded for GROMACS:
 travis.h
 appveyor.yml
 build-aux
-cmake
 codegear
 configure.ac
 Makefile.am
@@ -24,21 +23,29 @@ m4
 xcode
 test
 scripts
-googletest/docs/V*md
+googletest/docs
 googletest/samples
-googlemock/docs/v*/*md
-
-The file CMakeLists.txt in the parent directory is part of the GROMACS build
-system and has been written from scratch.
+googlemock/docs
 
 This README.Gromacs file is new, of course.
 
-In the default version, googletest-1.8.0 assumes wcscasecmp() to be
-present on Linux.  This is not the case on all platforms, so in
-gtest/src/gtest.cc we have added a define to only use this if
-GTEST_HAS_WCSCASECMP is set. Since we never set this define, we fall
-back on the alternative internal implementation which should be
-perfectly fine for now.
+The top-level CMakeLists.txt has enable_testing() commented out, as we
+do not intend to test GoogleTest as part of a GROMACS build.
+
+The CMakeLists.txt files in googletest and googlemock have declared
+their include directories as SYSTEM INTERFACE (not just INTERFACE) so
+that compilers will include such headers with e.g. -isystem, so that
+they will not generate warnings from such headers included in GROMACS
+test code.
+
+The googlemock/CMakeLists.txt no longer builds
+googletest/src/gtest-all.cc as part of the gmock target, because it is
+already built as part of the gtest target, and this leads to duplicate
+definitions of extern symbols declared in gtest headers. Added the
+link-time dependency of gmock upon gtest that is now required.
+
+Both the gtest_main and gmock_main executables and libraries are not
+created, because GROMACS does not use them.
 
 googletest-1.8.0 also assumes that streaming results to a socket is
 something you can always do on Linux, but the implementation uses
@@ -48,7 +55,3 @@ use this feature, we'd just want to turn it off, but there's no
 interface for that. So, GTEST_CAN_STREAM_RESULTS is added in
 googletest/include/gtest/internal/gtest-port.h to allow the default
 behaviour to be applied only if the user hasn't gotten involved.
-
-Two helper functions for implementing INSTANTIATE_TEST_CASE_P in
-gtest-param-test.h have had the static specifer added to their
-declarations, which silences -Wmissing-prototype warnings.
\ No newline at end of file
index beb259a2e9dbb5dcb50f61c7a779f563528983f6..681cccf33d2f3ee5cbace264e46333040cfbc3e8 100644 (file)
@@ -83,28 +83,29 @@ endif()
 # a user aggressive about warnings.
 cxx_library(gmock
             "${cxx_strict}"
-            "${gtest_dir}/src/gtest-all.cc"
+#            "${gtest_dir}/src/gtest-all.cc"
             src/gmock-all.cc)
+target_link_libraries(gmock gtest)
 
-cxx_library(gmock_main
-            "${cxx_strict}"
-            "${gtest_dir}/src/gtest-all.cc"
-            src/gmock-all.cc
-            src/gmock_main.cc)
+#cxx_library(gmock_main
+#            "${cxx_strict}"
+##            "${gtest_dir}/src/gtest-all.cc"
+#            src/gmock-all.cc
+#            src/gmock_main.cc)
 
 # If the CMake version supports it, attach header directory information
 # to the targets for when we are part of a parent build (ie being pulled
 # in via add_subdirectory() rather than being a standalone build).
 if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
-  target_include_directories(gmock      INTERFACE "${gmock_SOURCE_DIR}/include")
-  target_include_directories(gmock_main INTERFACE "${gmock_SOURCE_DIR}/include")
+  target_include_directories(gmock      SYSTEM INTERFACE "${gmock_SOURCE_DIR}/include")
+#  target_include_directories(gmock_main SYSTEM INTERFACE "${gmock_SOURCE_DIR}/include")
 endif()
 
 ########################################################################
 #
 # Install rules
-install(TARGETS gmock gmock_main
-  DESTINATION lib)
+#install(TARGETS gmock gmock_main
+#  DESTINATION lib)
 install(DIRECTORY ${gmock_SOURCE_DIR}/include/gmock
   DESTINATION include)
 
index 621d0f0421543eba41e1d69f497814104bb9c6a7..c1aea52db60dc8a6b2b70a470cf7bbe30b87374e 100644 (file)
@@ -88,22 +88,22 @@ endif()
 # are used for other targets, to ensure that gtest can be compiled by a user
 # aggressive about warnings.
 cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
-cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
-target_link_libraries(gtest_main gtest)
+#cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
+#target_link_libraries(gtest_main gtest)
 
 # If the CMake version supports it, attach header directory information
 # to the targets for when we are part of a parent build (ie being pulled
 # in via add_subdirectory() rather than being a standalone build).
 if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
-  target_include_directories(gtest      INTERFACE "${gtest_SOURCE_DIR}/include")
-  target_include_directories(gtest_main INTERFACE "${gtest_SOURCE_DIR}/include")
+  target_include_directories(gtest      SYSTEM INTERFACE "${gtest_SOURCE_DIR}/include")
+#  target_include_directories(gtest_main SYSTEM INTERFACE "${gtest_SOURCE_DIR}/include")
 endif()
 
 ########################################################################
 #
 # Install rules
-install(TARGETS gtest gtest_main
-  DESTINATION lib)
+#install(TARGETS gtest gtest_main
+#  DESTINATION lib)
 install(DIRECTORY ${gtest_SOURCE_DIR}/include/gtest
   DESTINATION include)
 
diff --git a/src/external/googletest/googletest/cmake/internal_utils.cmake b/src/external/googletest/googletest/cmake/internal_utils.cmake
new file mode 100644 (file)
index 0000000..777b91e
--- /dev/null
@@ -0,0 +1,254 @@
+# Defines functions and macros useful for building Google Test and
+# Google Mock.
+#
+# Note:
+#
+# - This file will be run twice when building Google Mock (once via
+#   Google Test's CMakeLists.txt, and once via Google Mock's).
+#   Therefore it shouldn't have any side effects other than defining
+#   the functions and macros.
+#
+# - The functions/macros defined in this file may depend on Google
+#   Test and Google Mock's option() definitions, and thus must be
+#   called *after* the options have been defined.
+
+# Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
+#
+# This must be a macro(), as inside a function string() can only
+# update variables in the function scope.
+macro(fix_default_compiler_settings_)
+  if (MSVC)
+    # For MSVC, CMake sets certain flags to defaults we want to override.
+    # This replacement code is taken from sample in the CMake Wiki at
+    # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
+    foreach (flag_var
+             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
+             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
+      if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
+        # When Google Test is built as a shared library, it should also use
+        # shared runtime libraries.  Otherwise, it may end up with multiple
+        # copies of runtime library data in different modules, resulting in
+        # hard-to-find crashes. When it is built as a static library, it is
+        # preferable to use CRT as static libraries, as we don't have to rely
+        # on CRT DLLs being available. CMake always defaults to using shared
+        # CRT libraries, so we override that default here.
+        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
+      endif()
+
+      # We prefer more strict warning checking for building Google Test.
+      # Replaces /W3 with /W4 in defaults.
+      string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}")
+    endforeach()
+  endif()
+endmacro()
+
+# Defines the compiler/linker flags used to build Google Test and
+# Google Mock.  You can tweak these definitions to suit your need.  A
+# variable's value is empty before it's explicitly assigned to.
+macro(config_compiler_and_linker)
+  if (NOT gtest_disable_pthreads)
+    # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
+    find_package(Threads)
+  endif()
+
+  fix_default_compiler_settings_()
+  if (MSVC)
+    # Newlines inside flags variables break CMake's NMake generator.
+    # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
+    set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi")
+    if (MSVC_VERSION LESS 1400)  # 1400 is Visual Studio 2005
+      # Suppress spurious warnings MSVC 7.1 sometimes issues.
+      # Forcing value to bool.
+      set(cxx_base_flags "${cxx_base_flags} -wd4800")
+      # Copy constructor and assignment operator could not be generated.
+      set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512")
+      # Compatibility warnings not applicable to Google Test.
+      # Resolved overload was found by argument-dependent lookup.
+      set(cxx_base_flags "${cxx_base_flags} -wd4675")
+    endif()
+    if (MSVC_VERSION LESS 1500)  # 1500 is Visual Studio 2008
+      # Conditional expression is constant.
+      # When compiling with /W4, we get several instances of C4127
+      # (Conditional expression is constant). In our code, we disable that
+      # warning on a case-by-case basis. However, on Visual Studio 2005,
+      # the warning fires on std::list. Therefore on that compiler and earlier,
+      # we disable the warning project-wide.
+      set(cxx_base_flags "${cxx_base_flags} -wd4127")
+    endif()
+    if (NOT (MSVC_VERSION LESS 1700))  # 1700 is Visual Studio 2012.
+      # Suppress "unreachable code" warning on VS 2012 and later.
+      # http://stackoverflow.com/questions/3232669 explains the issue.
+      set(cxx_base_flags "${cxx_base_flags} -wd4702")
+    endif()
+    if (NOT (MSVC_VERSION GREATER 1900))  # 1900 is Visual Studio 2015
+      # BigObj required for tests.
+      set(cxx_base_flags "${cxx_base_flags} -bigobj")
+    endif()
+
+    set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
+    set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
+    set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
+    set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
+    set(cxx_no_rtti_flags "-GR-")
+  elseif (CMAKE_COMPILER_IS_GNUCXX)
+    set(cxx_base_flags "-Wall -Wshadow")
+    set(cxx_exception_flags "-fexceptions")
+    set(cxx_no_exception_flags "-fno-exceptions")
+    # Until version 4.3.2, GCC doesn't define a macro to indicate
+    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
+    # explicitly.
+    set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
+    set(cxx_strict_flags
+      "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
+  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
+    set(cxx_exception_flags "-features=except")
+    # Sun Pro doesn't provide macros to indicate whether exceptions and
+    # RTTI are enabled, so we define GTEST_HAS_* explicitly.
+    set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
+    set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
+  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
+      CMAKE_CXX_COMPILER_ID STREQUAL "XL")
+    # CMake 2.8 changes Visual Age's compiler ID to "XL".
+    set(cxx_exception_flags "-qeh")
+    set(cxx_no_exception_flags "-qnoeh")
+    # Until version 9.0, Visual Age doesn't define a macro to indicate
+    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
+    # explicitly.
+    set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
+  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
+    set(cxx_base_flags "-AA -mt")
+    set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
+    set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
+    # RTTI can not be disabled in HP aCC compiler.
+    set(cxx_no_rtti_flags "")
+  endif()
+
+  if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available and allowed.
+    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
+  else()
+    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")
+  endif()
+
+  # For building gtest's own tests and samples.
+  set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
+  set(cxx_no_exception
+    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
+  set(cxx_default "${cxx_exception}")
+  set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
+  set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
+
+  # For building the gtest libraries.
+  set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
+endmacro()
+
+# Defines the gtest & gtest_main libraries.  User tests should link
+# with one of them.
+function(cxx_library_with_type name type cxx_flags)
+  # type can be either STATIC or SHARED to denote a static or shared library.
+  # ARGN refers to additional arguments after 'cxx_flags'.
+  add_library(${name} ${type} ${ARGN})
+  set_target_properties(${name}
+    PROPERTIES
+    COMPILE_FLAGS "${cxx_flags}")
+  if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
+    set_target_properties(${name}
+      PROPERTIES
+      COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
+  endif()
+  if (CMAKE_USE_PTHREADS_INIT)
+    target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
+  endif()
+endfunction()
+
+########################################################################
+#
+# Helper functions for creating build targets.
+
+function(cxx_shared_library name cxx_flags)
+  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
+endfunction()
+
+function(cxx_library name cxx_flags)
+  cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
+endfunction()
+
+# cxx_executable_with_flags(name cxx_flags libs srcs...)
+#
+# creates a named C++ executable that depends on the given libraries and
+# is built from the given source files with the given compiler flags.
+function(cxx_executable_with_flags name cxx_flags libs)
+  add_executable(${name} ${ARGN})
+  if (cxx_flags)
+    set_target_properties(${name}
+      PROPERTIES
+      COMPILE_FLAGS "${cxx_flags}")
+  endif()
+  if (BUILD_SHARED_LIBS)
+    set_target_properties(${name}
+      PROPERTIES
+      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
+  endif()
+  # To support mixing linking in static and dynamic libraries, link each
+  # library in with an extra call to target_link_libraries.
+  foreach (lib "${libs}")
+    target_link_libraries(${name} ${lib})
+  endforeach()
+endfunction()
+
+# cxx_executable(name dir lib srcs...)
+#
+# creates a named target that depends on the given libs and is built
+# from the given source files.  dir/name.cc is implicitly included in
+# the source file list.
+function(cxx_executable name dir libs)
+  cxx_executable_with_flags(
+    ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
+endfunction()
+
+# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
+find_package(PythonInterp)
+
+# cxx_test_with_flags(name cxx_flags libs srcs...)
+#
+# creates a named C++ test that depends on the given libs and is built
+# from the given source files with the given compiler flags.
+function(cxx_test_with_flags name cxx_flags libs)
+  cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
+  add_test(${name} ${name})
+endfunction()
+
+# cxx_test(name libs srcs...)
+#
+# creates a named test target that depends on the given libs and is
+# built from the given source files.  Unlike cxx_test_with_flags,
+# test/name.cc is already implicitly included in the source file list.
+function(cxx_test name libs)
+  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
+    "test/${name}.cc" ${ARGN})
+endfunction()
+
+# py_test(name)
+#
+# creates a Python test with the given name whose main module is in
+# test/name.py.  It does nothing if Python is not installed.
+function(py_test name)
+  # We are not supporting Python tests on Linux yet as they consider
+  # all Linux environments to be google3 and try to use google3 features.
+  if (PYTHONINTERP_FOUND)
+    # ${CMAKE_BINARY_DIR} is known at configuration time, so we can
+    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
+    # only at ctest runtime (by calling ctest -c <Configuration>), so
+    # we have to escape $ to delay variable substitution here.
+    if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
+      add_test(
+        NAME ${name}
+        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
+            --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>)
+    else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
+      add_test(
+        ${name}
+        ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
+          --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
+    endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
+  endif()
+endfunction()
index bf24bf51bfc45412f658bddd53a332e149d77309..21d7f5c05f6d5e34d1ca0bf881dd67df3747eac4 100644 (file)
@@ -90,4 +90,4 @@ The Debugger has exited with status 0.
 
 # Summary #
 
-Unit testing is a valuable way to ensure your data model stays valid even during rapid development or refactoring. The Google Testing Framework is a great unit testing framework for C and C++ which integrates well with an Xcode development environment.
\ No newline at end of file
+Unit testing is a valuable way to ensure your data model stays valid even during rapid development or refactoring. The Google Testing Framework is a great unit testing framework for C and C++ which integrates well with an Xcode development environment.
index c2bc95f76fafce8cd14bb9d0cce78557f4ac52e8..52d8d392abf1f9175b5b84c97750cd1f62fd956a 100644 (file)
@@ -37,7 +37,6 @@ if (NOT GMX_BUILD_UNITTESTS)
     return()
 endif()
 
-include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
 set(TESTUTILS_SOURCES
     cmdlinetest.cpp
     conftest.cpp
@@ -69,9 +68,7 @@ add_library(testutils STATIC ${UNITTEST_TARGET_OPTIONS} ${TESTUTILS_SOURCES})
 gmx_target_compile_options(testutils)
 target_compile_definitions(testutils PRIVATE HAVE_CONFIG_H)
 target_include_directories(testutils SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
-set_property(TARGET testutils APPEND PROPERTY COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}")
-set_property(TARGET testutils APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
-target_link_libraries(testutils libgromacs ${GMX_COMMON_LIBRARIES} ${GMOCK_LIBRARIES})
+target_link_libraries(testutils PRIVATE libgromacs ${GMX_COMMON_LIBRARIES} gmock)
 
 if(HAVE_TINYXML2)
     include_directories(SYSTEM ${TinyXML2_INCLUDE_DIR})
index 9a0a2e05aad253ea2a38902a75b910ad39a0565c..ca52ca973ded2e198d087869550a92a96c538249 100644 (file)
@@ -38,11 +38,9 @@ function (gmx_add_unit_test_library NAME)
     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
         add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
         gmx_target_compile_options(${NAME})
-        target_compile_options(${NAME} PUBLIC "${GMOCK_COMPILE_FLAGS}")
         target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
-        target_compile_definitions(${NAME} PUBLIC "${GMOCK_COMPILE_DEFINITIONS}")
-        target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include ${GMOCK_INCLUDE_DIRS})
-        target_link_libraries(${NAME} PUBLIC testutils)
+        target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
+        target_link_libraries(${NAME} PRIVATE testutils gmock)
     endif()
 endfunction ()
 
@@ -74,12 +72,10 @@ function (gmx_add_gtest_executable EXENAME)
                  TEST_USES_HARDWARE_DETECTION=true)
         endif()
 
-        include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
         add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
             ${_source_files} ${TESTUTILS_DIR}/unittest_main.cpp)
         gmx_target_compile_options(${EXENAME})
-        target_compile_options(${EXENAME} PRIVATE "${GMOCK_COMPILE_FLAGS}")
-        target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H "${GMOCK_COMPILE_DEFINITIONS}" "${EXTRA_COMPILE_DEFINITIONS}")
+        target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
         # Permit GROMACS code to include externally developed headers,
         # such as the functionality from the nonstd project that we
@@ -88,7 +84,7 @@ function (gmx_add_gtest_executable EXENAME)
         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
 
         target_link_libraries(${EXENAME} PRIVATE
-            testutils libgromacs ${GMOCK_LIBRARIES}
+            testutils libgromacs gmock
             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
 
         if(GMX_CLANG_TIDY)