CMake add_custom_target() echo example

The COMMENT field of CMake’s add_custom_target() is intended to print messages during the build. However, this field is a no-op when using CMake Generators like Ninja and GNU Make. To reliably print messages during the build regardless of the Generator used, create a custom echo command using CMake’s built-in cmake -E echo functionality.

set(_out ${PROJECT_BINARY_DIR}/output.txt)

add_custom_target(
    COMMAND ${CMAKE_COMMAND} -E touch ${_out}
    COMMAND ${CMAKE_COMMAND} -E echo "Touch ${_out}"
)