How to create MD5 file within add_custom_target

I would like to create a file containing the MD5 computed over another file, within the implementation of a custom target (so, not using file(MD5…)). Once I have done cmake -E md5sum filename, how to store the result in a file with an arbitrary name?
Thanks in advance for any help

What is wrong with using file(MD5 ...)?

How/where are you executing cmake -E md5sum filename?
Are you using CMake’s execute_process command? If yes, then you can use the OUTPUT_FILE argument. See https://cmake.org/cmake/help/latest/command/execute_process.html for more details.

Sorry, my description was awful, i modified it

In that case I would use an intermediate CMake script. For instance:

  • CMakeLists.txt:
add_custom_command(
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/myfile.md5"
  COMMAND
    "${CMAKE_COMMAND}"
    "-P"
    "${CMAKE_CURRENT_SOURCE_DIR}/make_md5.cmake"
    "${input_file}"
    "${CMAKE_CURRENT_BINARY_DIR}/myfile.md5"
)
  • make_md5.cmake:
file(MD5 "${CMAKE_ARGV3}" md5_sum)
file(WRITE "${CMAKE_ARGV4}" "${md5_sum}")

This is what I was thinking about, and what I was trying to avoid. Doing operations at build time in our cmake code is leading to rewriting a lot of configure time operations within CMake scripts to be executed at build time…if there are no alternatives I think this is a great improvement area for CMake…

The script is run at build time. It’s just that the add_custom_command also runs CMake. This makes your build way more cross-platform than trying to suss out what the shell syntax for it is on whatever platform (while this is probably fine, once it gets complicated, you’ll likely prefer a script anyways).