# How to create MD5 file within add\_custom\_target

**URL:** https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156
**Category:** Usage
**Created:** [March 4, 2022, 8:27am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156 "2022-03-04T08:27:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fabiop](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@fabiop](https://discourse.cmake.org/u/fabiop)
#### Post date: [March 4, 2022, 8:27am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/1 "2022-03-04T08:27:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 4, 2022, 8:30am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/2 "2022-03-04T08:30:21Z")

</div>

> [@fabiop](#):
>
> so, not using file(MD5…)

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

> [@fabiop](#):
>
> Once I have done cmake -E md5sum filename, how to store the result in a file with an arbitrary name?

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](https://cmake.org/cmake/help/latest/command/execute_process.html) for more details.

---

<div class="post-metadata">

### Author: ![fabiop](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@fabiop](https://discourse.cmake.org/u/fabiop)
#### Post date: [March 4, 2022, 8:36am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/3 "2022-03-04T08:36:34Z")

</div>

Sorry, my description was awful, i modified it

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 4, 2022, 8:50am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/4 "2022-03-04T08:50:50Z")

</div>

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

- `CMakeLists.txt`:

```cmake
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`:

```cmake
file(MD5 "${CMAKE_ARGV3}" md5_sum)
file(WRITE "${CMAKE_ARGV4}" "${md5_sum}")

```

---

<div class="post-metadata">

### Author: ![fabiop](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@fabiop](https://discourse.cmake.org/u/fabiop)
#### Post date: [March 4, 2022, 8:54am UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/5 "2022-03-04T08:54:56Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 4, 2022, 8:38pm UTC](https://discourse.cmake.org/t/how-to-create-md5-file-within-add-custom-target/5156/6 "2022-03-04T20:38:27Z")

</div>

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).
