CMake: How to set dependency from executable to custom command

I am trying to generate checksum for the executable using add_custom_command.

project(demo)
cmake_minimum_required(VERSION 3.12)
set(FILE_NAME Demo.cpp)
set(GEN_FILE_NAME ExeHashsha256)
set(EXE_NAME demoExe)
add_executable(${EXE_NAME} ${FILE_NAME})

add_custom_command(
    OUTPUT
        ${CMAKE_CURRENT_BINARY_DIR}/${GEN_FILE_NAME}
    COMMAND
        COMMAND ${CMAKE_COMMAND} -E sha256sum demoExe > ${CMAKE_CURRENT_BINARY_DIR}/${GEN_FILE_NAME}
    COMMENT
        "(Re)Running the custom command"
    DEPENDS
        ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}
)

I added the executable dependency for add_custom_command , however the checksum file is not being generated (ExeHashsha256). When I use add_custom_target, a checksum file is generated. As per my knowledge executable is one of the targets, thus the dependence can be included in ‘add custom command.’ Is it possible to generate without using add_custom_target?

CMake logic is based on targets, so no…

But you can attach a command to a target using add_custom_command(TARGET <target> PRE_BUILD | PRE_LINK | POST_BUILD ...).