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
+21 -62
View File
@@ -12,86 +12,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# Macro for benchmarks that don't use GL (same as mujoco_api_test, but uses
# benchmark::benchmark_main).
macro(mujoco_benchmark_test name)
add_executable(${name} ${name}.cc)
target_link_libraries(
${name}
benchmark::benchmark_main
mujoco
absl::core_headers
)
target_include_directories(${name} PRIVATE ${MUJOCO_TEST_INCLUDE})
# TODO(fraromano) Check RPATH settings
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.
gtest_add_tests(
TARGET ${name}
SOURCES ${name}.cc
WORKING_DIRECTORY ${MUJOCO_TEST_WORKING_DIR}
TEST_LIST testList
)
if(WIN32)
set_tests_properties(
${testList} PROPERTIES ENVIRONMENT "PATH=$<TARGET_FILE_DIR:mujoco>;$ENV{PATH}"
)
endif()
endmacro()
mujoco_benchmark_test(ccd_benchmark_test)
target_link_libraries(
mujoco_test(
ccd_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(step_benchmark_test)
target_link_libraries(
mujoco_test(
step_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(thread_performance_test)
target_link_libraries(
mujoco_test(
thread_performance_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(parse_benchmark_test)
target_link_libraries(
mujoco_test(
parse_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(engine_util_spatial_benchmark_test)
target_link_libraries(
mujoco_test(
engine_util_spatial_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(engine_core_smooth_benchmark_test)
target_link_libraries(
mujoco_test(
engine_core_smooth_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_benchmark_test(engine_util_sparse_benchmark_test)
target_link_libraries(
mujoco_test(
engine_util_sparse_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)