Reduce cmake test boilerplate

All instances of mujoco_test in the code base were followed by
a call to  target_link_libraries(<testname> fixture gmock).

As anyhow mujoco_test already was calling target_link_libraries
to some predefined list of targets (mujoco and gtest_main),
this PR adds to the list of default linked targets also fixture
and gmock, to reduce the boilerplate.

Furthermore, to completly remove the need for calling
target_link_libraries after a call to mujoco_test, this PR also
add to the mujoco_test macro the ADDITIONAL_LINK_LIBRARIES
argument, that can be used if a given test needs to link some
additional targets beside the default ones.

To permit to use these new features also for mujoco benchmarks,
this PR adds a MAIN_TARGET parameter to mujoco_test, to select
if gtest_main or another target is used to provide the main
entry point to the test executable.
This commit is contained in:
Silvio
2024-12-07 16:01:45 +01:00
parent c1d97d4a22
commit 756a8d716a
9 changed files with 58 additions and 134 deletions
+24 -6
View File
@@ -17,10 +17,22 @@ set(MUJOCO_TEST_WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR})
include(GoogleTest)
# This macro is used to add a C++ test to MuJoCo CMake build system
#
# The CMake targets linked by default with target_link_libraries are:
# mujoco fixture gmock
#
# The macro supports the following parameters:
# * PROPERTIES: multiple value parameter, its value is set as test properties
# via the set_tests_properties(<test> PROPERTIES <argument>) function
# * ADDITIONAL_LINK_LIBRARIES: multiple value parameter, additional libraries linked
# to the test via target_link_libraries
# * MAIN_TARGET: single value parameter, used to specify the target linked to the tests
# that define the main entry poiny. If not indicated, gest_main is used
macro(mujoco_test name)
set(options)
set(oneValueArgs)
set(multiValueArgs PROPERTIES)
set(oneValueArgs MAIN_TARGET)
set(multiValueArgs PROPERTIES ADDITIONAL_LINK_LIBRARIES)
cmake_parse_arguments(
_ARGS
"${options}"
@@ -30,7 +42,15 @@ macro(mujoco_test name)
)
add_executable(${name} ${name}.cc)
target_link_libraries(${name} gtest_main mujoco)
target_link_libraries(${name} mujoco fixture gmock)
if(_ARGS_MAIN_TARGET)
target_link_libraries(${name} ${_ARGS_MAIN_TARGET})
else()
target_link_libraries(${name} gtest_main)
endif()
if(_ARGS_ADDITIONAL_LINK_LIBRARIES)
target_link_libraries(${name} ${_ARGS_ADDITIONAL_LINK_LIBRARIES})
endif()
target_include_directories(${name} PRIVATE ${MUJOCO_TEST_INCLUDE})
set_target_properties(${name} PROPERTIES BUILD_RPATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
# gtest_discover_tests is recommended over gtest_add_tests, but has some issues in Windows.
@@ -48,6 +68,7 @@ macro(mujoco_test name)
if(_ARGS_PROPERTIES)
set_tests_properties(${testList} PROPERTIES ${_ARGS_PROPERTIES})
endif()
endmacro()
add_library(fixture STATIC fixture.h fixture.cc)
@@ -67,13 +88,10 @@ target_link_libraries(
target_include_directories(fixture PRIVATE ${mujoco_SOURCE_DIR}/include gmock)
mujoco_test(fixture_test)
target_link_libraries(fixture_test fixture gmock)
mujoco_test(header_test)
target_link_libraries(header_test fixture gmock)
mujoco_test(pipeline_test)
target_link_libraries(pipeline_test fixture gmock)
add_subdirectory(benchmark)
add_subdirectory(engine)