Objetivo personalizado global en CMake
Quería escribir un pequeño sistema de prueba de unidad con CMake que pueda copiar y pegar fácilmente en todos mis proyectos, así que se me ocurrió esto:
include(CMakeParseArguments)
set(UNIT_TEST "unit_tests")
add_custom_target(${UNIT_TEST} ALL VERBATIM)
function(add_unit_test dependency)
cmake_parse_arguments(UT_ "" "NAME" "" ${ARGN})
if(NOT ${UT_NAME})
set(${UT_NAME} ${ARG0})
endif()
add_test(${ARGN})
add_dependencies(${UNIT_TEST} ${dependency})
add_custom_command(TARGET ${UNIT_TEST}
COMMENT "Run tests"
POST_BUILD COMMAND ctest
ARGS -R ${UT_NAME} --output-on-failures
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM)
endfunction(add_unit_test)
Esperaba que funcionara bien así y ejecutara todas las pruebas unitarias que agregaría en mi proyecto llamandoadd_unit_test(dep ...)
con una dependencia para compilar antes y luego los mismos argumentos que paraadd_test(...)
. En realidad, este error aparece:
CMake Warning (dev) at cmake/testing.cmake:13 (add_custom_command):
Policy CMP0040 is not set: The target in the TARGET signature of
add_custom_command() must exist. Run "cmake --help-policy CMP0040" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
The target name "unit_tests" is unknown in this context.
Call Stack (most recent call first):
source/test/CMakeLists.txt:10 (add_unit_test)
This warning is for project developers. Use -Wno-dev to suppress it.
¿Por qué es exactamente el objetivo desconocido en este momento?include(cmake/testing.cmake)
es lo primero que llamo despuéscmake_minimum_required
en mi script de compilación del proyecto, por lo que no puede ser porqueadd_custom_target(${UNIT_TEST} ALL VERBATIM)
no ha sido llamado todavía.
¿Hay alguna manera de agregar un comando personalizado al objetivo UNIT_TEST?