CMake add_custom_target Windows nuisance rebuild

When using CMake’s add_custom_command and add_custom_target on Windows, if the CMAKE_EXECUTABLE_SUFFIX (which is typically .exe) is not included as part of the command or target, it can lead to the custom target is rebuilt every time the project is rebuilt, even if the command is up to date. This happens because CMake does not exactly match the output file of the custom command, since the .exe suffix was missing on Windows, and therefore CMake does not properly track the existence of a target’s dependencies. As a result, CMake assumes that the target is always out of date and needs to be rebuilt. To avoid this issue, ensure that the output of your custom command includes the CMAKE_EXECUTABLE_SUFFIX. For example, if generating an executable with add_custom_command, specify the output file with the correct suffix:

set(myexe ${CMAKE_CURRENT_BINARY_DIR}/my_executable${CMAKE_EXECUTABLE_SUFFIX})

add_custom_command(
    OUTPUT ${myexe}
    COMMAND ...
)

add_custom_target(MyTarget ALL
    DEPENDS ${myexe}
)

By including the CMAKE_EXECUTABLE_SUFFIX in both the OUTPUT and DEPENDS sections, CMake will correctly recognize the target as an executable and will only rebuild it when necessary, thus avoiding unnecessary rebuilds.