0

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?

goodman
  • 424
  • 8
  • 24
  • 1
    The problem of your code is not about DEPENDS clause of `add_custom_command`. The problem is that none target depends on the file you stated as OUTPUT. You could add `add_custom_target` which depends on your checksum file, as in [that answer](https://stackoverflow.com/a/2940153/3440745). – Tsyvarev May 06 '22 at 08:33
  • Also, instead of depending on executable file - `DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}` - it is better to depend on the executable **target**: `DEPENDS ${EXE_NAME}`. That way CMake will adjust not only the file-level dependency, but also the target-level one. – Tsyvarev May 06 '22 at 08:35
  • ya If I add dependency on checksum file using `add_custom_target` its working. – goodman May 06 '22 at 09:11

0 Answers0