0

Here's a custom command we find in the CMake documentation:

add_custom_command(
  TARGET foo POST_BUILD
  COMMAND someHasher -i "$<TARGET_FILE:myExe>"
                     -o "$<TARGET_FILE:myExe>.hash"
  VERBATIM)

suppose that, instead of adding .hash, I want to replace any existing extension with .hash. I would need some way to strip the extension from $<TARGET_FILE:myExe>... how should I do this? Can I do better than full-blown regexp matching?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html -> `TARGET_FILE_BASE_NAME` – KamilCuk Dec 29 '21 at 20:49
  • Yup, that will do it. Now for the harder problem of OBJECT libraries not being usable as targets for POST_BUILD... – einpoklum Dec 29 '21 at 20:51
  • Yes, but that is odd - OBJECT is just a collection of object files. If you want to generate something, do not use `POST_BUILD` - depend on files, depend on object files. I would also refactor that custom command to `OUTPUT "$.hash"` instead of `POST_BUILD`. I.e. build dependencies on _files_. `POST_BUILD` is great like for showing stats `size $`, if you want to generate files I recommend just doing `OUTPUT` and another `add_custom_target`. – KamilCuk Dec 29 '21 at 20:53
  • @KamilCuk : Please have a look at [this question](https://stackoverflow.com/q/70525167/1593077). – einpoklum Dec 29 '21 at 21:25

1 Answers1

2

(due to @KamilCuk)

Use TARGET_FILE_BASE_NAME, like so:

add_custom_command(
  TARGET foo POST_BUILD
  COMMAND someHasher -i "$<TARGET_FILE:myExe>"
                     -o "$<TARGET_FILE_BASE_NAME:myExe>.hash"
  VERBATIM)
einpoklum
  • 118,144
  • 57
  • 340
  • 684