Convert test infrastructure targets to static library targets.
authorM. Eric Irrgang <ericirrgang@gmail.com>
Mon, 5 Aug 2019 15:32:20 +0000 (18:32 +0300)
committerPaul Bauer <paul.bauer.q@gmail.com>
Tue, 6 Aug 2019 13:19:11 +0000 (15:19 +0200)
Convert CMake function `gmx_add_unit_test_object_library()` to
`gmx_add_unit_test_library()` and produce STATIC instead of OBJECT
targets. Update `mdrun_test_objlib` to `mdrun_test_infrastructure`
using the helper function.

This allows the target to use `target_link_libraries` to get the
compiler flags, definitions, and include directories necessary for
compatibility with `gmock` and other resources. Resolves some noisiness
from googletest headers under the standard GROMACS `-Wundef` flag.
Relevant to related change that migrate from global GMOCK variables to
modern usage of gtest and gmock CMake targets.

Removed unnecessary TESTUTILS_LIBS variable.

Refs #3033

Change-Id: Id5b47774b679e8e205f899ea80df65153da44516

src/api/cpp/tests/CMakeLists.txt
src/api/cpp/workflow/tests/CMakeLists.txt
src/gromacs/analysisdata/tests/CMakeLists.txt
src/gromacs/commandline/tests/CMakeLists.txt
src/gromacs/gpu_utils/tests/CMakeLists.txt
src/gromacs/mdrunutility/tests/CMakeLists.txt
src/gromacs/onlinehelp/tests/CMakeLists.txt
src/gromacs/trajectoryanalysis/tests/CMakeLists.txt
src/programs/mdrun/tests/CMakeLists.txt
src/testutils/CMakeLists.txt
src/testutils/TestMacros.cmake

index c4c733d253ff8aba4895e088c9b294a0b14716b9..f0ea2ce29b6a5a13917cb68bea7b90478cd9cc5d 100644 (file)
@@ -54,8 +54,6 @@ gmx_add_gtest_executable(
     status.cpp
     system.cpp
     version.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
     )
@@ -71,7 +69,7 @@ target_include_directories(gmxapi-test PRIVATE
                            ${CMAKE_CURRENT_SOURCE_DIR})
 
 # Link against the gmxapi libraries and get access to its public (installed) headers.
-target_link_libraries(gmxapi-test Gromacs::gmxapi)
+target_link_libraries(gmxapi-test PRIVATE Gromacs::gmxapi mdrun_test_infrastructure)
 
 gmx_register_gtest_test(GmxapiExternalInterfaceTests gmxapi-test OPENMP_THREADS 2 INTEGRATION_TEST)
 
@@ -88,8 +86,6 @@ gmx_add_gtest_executable(
     status.cpp
     system.cpp
     version.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
     )
@@ -103,6 +99,6 @@ endif()
 
 target_include_directories(gmxapi-mpi-test PRIVATE
                            ${CMAKE_CURRENT_SOURCE_DIR})
-target_link_libraries(gmxapi-mpi-test Gromacs::gmxapi)
+target_link_libraries(gmxapi-mpi-test PRIVATE Gromacs::gmxapi mdrun_test_infrastructure)
 
 gmx_register_gtest_test(GmxapiMpiTests gmxapi-mpi-test MPI_RANKS 2 OPENMP_THREADS 2 INTEGRATION_TEST)
index b37ecdb7d2aa48b0048d3d5e9a330f890307bd41..e999f58437e9a21c9d4c47e2952a6a74d576dfba 100644 (file)
 gmx_add_gtest_executable(
     workflow-details-test
     workflow.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
 )
 
-target_link_libraries(workflow-details-test Gromacs::gmxapi gmxapi-detail)
+target_link_libraries(workflow-details-test PRIVATE Gromacs::gmxapi gmxapi-detail mdrun_test_infrastructure)
 
 gmx_register_gtest_test(GmxapiInternalInterfaceTests workflow-details-test OPENMP_THREADS 2 INTEGRATION_TEST)
 
@@ -59,12 +57,10 @@ set_tests_properties(GmxapiInternalInterfaceTests PROPERTIES
 gmx_add_gtest_executable(
     workflow-details-mpi-test MPI
     workflow.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
 )
 
-target_link_libraries(workflow-details-mpi-test Gromacs::gmxapi gmxapi-detail)
+target_link_libraries(workflow-details-mpi-test PRIVATE Gromacs::gmxapi gmxapi-detail mdrun_test_infrastructure)
 
 gmx_register_gtest_test(GmxapiInternalsMpiTests workflow-details-mpi-test MPI_RANKS 2 OPENMP_THREADS 2 INTEGRATION_TEST)
index 524cafc40d01508b61499b91b9d0d82fb17870d0..e0fc5e1595f7f43e1508dc16b054a1380311388b 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2011,2012,2013,2014, by the GROMACS development team, led by
+# Copyright (c) 2011,2012,2013,2014,2019, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-gmx_add_unit_test_object_library(analysisdata-test-shared
-                                 datatest.cpp
-                                 mock_datamodule.cpp)
+gmx_add_unit_test_library(analysisdata-test-shared
+                          datatest.cpp
+                          mock_datamodule.cpp)
 
 gmx_add_unit_test(AnalysisDataUnitTests analysisdata-test
                   analysisdata.cpp
                   arraydata.cpp
                   average.cpp
                   histogram.cpp
-                  lifetime.cpp
-                  $<TARGET_OBJECTS:analysisdata-test-shared>)
+                  lifetime.cpp)
+target_link_libraries(analysisdata-test PRIVATE analysisdata-test-shared)
index 4aecc092ec499bc4d800cba8e0e6f98d35ce348c..5ff5ff97cf6a87540a43877abf0063f4148d0138 100644 (file)
@@ -59,5 +59,5 @@ gmx_add_unit_test(CommandLineUnitTests commandline-test
                   cmdlineparser.cpp
                   cmdlineprogramcontext.cpp
                   filenm.cpp
-                  pargs.cpp
-                  $<TARGET_OBJECTS:onlinehelp-test-shared>)
+                  pargs.cpp)
+target_link_libraries(commandline-test PRIVATE onlinehelp-test-shared)
index e9ce017eafa8a8e77cf5f2162d14904932c7c2ae..5a61beea2ad4a76e9795c154602be849b98cc6b2 100644 (file)
@@ -90,8 +90,8 @@ gmx_add_unit_test(GpuUtilsUnitTests gpu_utils-test
     )
 
 if(GMX_USE_CUDA)
-    target_link_libraries(gpu_utils-test gpu_utilstest_cuda)
+    target_link_libraries(gpu_utils-test PRIVATE gpu_utilstest_cuda)
 endif()
 if(GMX_USE_OPENCL)
-    target_link_libraries(gpu_utils-test ${OpenCL_LIBRARIES})
+    target_link_libraries(gpu_utils-test PRIVATE ${OpenCL_LIBRARIES})
 endif()
index 97b3f485134c600bc722c12496de4e24c38edf39..7dc3d14faaaab930298b52bd4c7cd2a354e9aeda 100644 (file)
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-gmx_add_unit_test_object_library(mdrunutility-test-shared
-                                 threadaffinitytest.cpp)
+gmx_add_unit_test_library(mdrunutility-test-shared
+                          threadaffinitytest.cpp)
 
 gmx_add_unit_test(MdrunUtilityUnitTests mdrunutility-test
-                  threadaffinity.cpp
-                  $<TARGET_OBJECTS:mdrunutility-test-shared>)
+                  threadaffinity.cpp)
+target_link_libraries(mdrunutility-test PRIVATE mdrunutility-test-shared)
 
 gmx_add_mpi_unit_test(MdrunUtilityMpiUnitTests mdrunutility-mpi-test 4
-                      threadaffinity_mpi.cpp
-                      $<TARGET_OBJECTS:mdrunutility-test-shared>)
+                      threadaffinity_mpi.cpp)
+# TODO This target does not exist in a non-MPI build. It would be preferable
+# to change functions like gmx_add_mpi_unit_test to add link-time dependencies
+# as well as source files.
+if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
+    target_link_libraries(mdrunutility-mpi-test PRIVATE mdrunutility-test-shared)
+endif()
index 1fb607a319ef12dbc849fa2a6b71a1a95844d14d..41dfd64f0473023bfa45cf12f68d9142701edfde 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2012,2014,2015,2016, by the GROMACS development team, led by
+# Copyright (c) 2012,2014,2015,2016,2019, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-gmx_add_unit_test_object_library(onlinehelp-test-shared
-                                 mock_helptopic.cpp)
+gmx_add_unit_test_library(onlinehelp-test-shared
+                          mock_helptopic.cpp)
 
 gmx_add_unit_test(OnlineHelpUnitTests onlinehelp-test
                   helpformat.cpp
                   helpmanager.cpp
-                  helpwritercontext.cpp
-                  $<TARGET_OBJECTS:onlinehelp-test-shared>)
+                  helpwritercontext.cpp)
+target_link_libraries(onlinehelp-test PRIVATE onlinehelp-test-shared)
index 48c4c9d57ffd08689bcf6ef8ece945726e429a21..bdc04b8875ab25074adaec9f5fdf8a89b0a286c8 100644 (file)
@@ -46,8 +46,8 @@ gmx_add_unit_test(TrajectoryAnalysisUnitTests trajectoryanalysis-test
                   surfacearea.cpp
                   topologyinformation.cpp
                   trajectory.cpp
-                  unionfind.cpp
-                  $<TARGET_OBJECTS:analysisdata-test-shared>)
+                  unionfind.cpp)
+target_link_libraries(trajectoryanalysis-test PRIVATE analysisdata-test-shared)
 
 add_executable(test_selection ${UNITTEST_TARGET_OPTIONS} test_selection.cpp)
-target_link_libraries(test_selection libgromacs ${GMX_EXE_LINKER_FLAGS})
+target_link_libraries(test_selection PRIVATE libgromacs ${GMX_EXE_LINKER_FLAGS})
index 849070d63f232f7f9dfac693661fb26e4c96fb43..c7179463e2f342ccfd7720fe0293cdf181dce050 100644 (file)
@@ -32,9 +32,9 @@
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-# Make an "object library" for test infrastructure code that we re-use
-# in multiple test executables.
-add_library(mdrun_test_objlib OBJECT
+# Make a static library for test infrastructure code that we re-use
+# in multiple test executables across the repository.
+gmx_add_unit_test_library(mdrun_test_infrastructure
     energyreader.cpp
     energycomparison.cpp
     moduletest.cpp
@@ -42,9 +42,6 @@ add_library(mdrun_test_objlib OBJECT
     trajectorycomparison.cpp
     trajectoryreader.cpp
     )
-gmx_target_compile_options(mdrun_test_objlib)
-target_compile_definitions(mdrun_test_objlib PRIVATE HAVE_CONFIG_H)
-target_include_directories(mdrun_test_objlib SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
 
 set(testname "MdrunTests")
 set(exename "mdrun-test")
@@ -64,11 +61,10 @@ gmx_add_gtest_executable(
     termination.cpp
     trajectory_writing.cpp
     mimic.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
     )
+target_link_libraries(${exename} PRIVATE mdrun_test_infrastructure)
 gmx_register_gtest_test(${testname} ${exename} OPENMP_THREADS 2 INTEGRATION_TEST)
 
 # To avoid running into test timeouts, some end-to-end tests of mdrun
@@ -85,11 +81,10 @@ gmx_add_gtest_executable(
     rerun.cpp
     simple_mdrun.cpp
     tpitest.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
     )
+target_link_libraries(${exename} PRIVATE mdrun_test_infrastructure)
 gmx_register_gtest_test(${testname} ${exename} OPENMP_THREADS 2 INTEGRATION_TEST)
 
 # Tests that only make sense to run with multiple ranks and/or real
@@ -107,9 +102,8 @@ gmx_add_gtest_executable(
     multisimtest.cpp
     pmetest.cpp
     replicaexchange.cpp
-    # pseudo-library for code for testing mdrun
-    $<TARGET_OBJECTS:mdrun_test_objlib>
     # pseudo-library for code for mdrun
     $<TARGET_OBJECTS:mdrun_objlib>
     )
+target_link_libraries(${exename} PRIVATE mdrun_test_infrastructure)
 gmx_register_gtest_test(${testname} ${exename} OPENMP_THREADS 2 INTEGRATION_TEST)
index 5b461d48a74971d5384a68b09fe9b7011773ed19..c2bc95f76fafce8cd14bb9d0cce78557f4ac52e8 100644 (file)
@@ -69,7 +69,6 @@ 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(TESTUTILS_LIBS testutils)
 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})
@@ -109,7 +108,6 @@ endif()
 
 set(TESTUTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(TESTUTILS_DIR ${TESTUTILS_DIR} PARENT_SCOPE)
-set(TESTUTILS_LIBS ${TESTUTILS_LIBS} PARENT_SCOPE)
 set(GMX_CAN_RUN_MPI_TESTS ${GMX_CAN_RUN_MPI_TESTS} PARENT_SCOPE)
 
 add_subdirectory(tests)
index 0af9f820b1e35f2c3fb640c212f162767d371395..9a0a2e05aad253ea2a38902a75b910ad39a0565c 100644 (file)
 
 include(CMakeParseArguments)
 
-function (gmx_add_unit_test_object_library NAME)
+function (gmx_add_unit_test_library NAME)
     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
-        add_library(${NAME} OBJECT ${UNITTEST_TARGET_OPTIONS} ${ARGN})
+        add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
         gmx_target_compile_options(${NAME})
-        target_compile_options(${NAME} PRIVATE "${GMOCK_COMPILE_FLAGS}")
-        target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H "${GMOCK_COMPILE_DEFINITIONS}")
+        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)
     endif()
 endfunction ()
 
@@ -85,8 +87,8 @@ function (gmx_add_gtest_executable EXENAME)
         # headers so that no warnings are issued from them.
         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
 
-        target_link_libraries(${EXENAME}
-            ${TESTUTILS_LIBS} libgromacs ${GMOCK_LIBRARIES}
+        target_link_libraries(${EXENAME} PRIVATE
+            testutils libgromacs ${GMOCK_LIBRARIES}
             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
 
         if(GMX_CLANG_TIDY)