Stop clean from removing custom file?


we have a custom command/target that EDITS a file. The file is in git and may be changed by the build. The developer can then modify and commit it. The problem is, since that file is marked as GENERATED, the file is removed by clean (or Rebuild in some guis like VSCode). How can I stop cmake from cleaning it?

Here is my simplified example:


cmake_minimum_required(VERSION 3.0.0)
project(test_inout VERSION 0.1.0 LANGUAGES C)

# Problem: inout.txt is removed by clean (or rebuild via vscode)
add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/inout.txt
    # WORKAROUND: after clean, checkout the file via git.
    COMMAND test -e ${CMAKE_CURRENT_SOURCE_DIR}/inout.txt || git checkout -- ${CMAKE_CURRENT_SOURCE_DIR}/inout.txt

add_custom_target(inout ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/inout.txt)

# Does not work (at least for ninja)
# set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM TRUE)

# Does not work
# cmake_policy(SET CMP0118 OLD)
# set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/inout.txt PROPERTIES GENERATED "")


Hello World


This file will be appended

As you can see, I tried something, my workaround works. Do you know better/simpler solutions?

Use a stamp file as output that you create (touch) after editing the file.