0

I have shell scripts in some sub projects. These projects do not compile anything. What I want is to copy the scripts and / or pre-built binaries to the same location as it would if it compiled a local app.

#configure_file(test.sh test.sh COPYONLY)
add_custom_target(run ALL DEPENDS test.sh)
#add_custom_target(foo
#   COMMAND cp test.sh
#   ${CMAKE_CURRENT_BINARY_DIR}/test.sh)
add_custom_command(
    OUTPUT test.sh
    COMMAND cp test.sh ${CMAKE_CURRENT_BINARY_DIR}/test.sh
    DEPENDS test.sh)
#   COMMAND ${CMAKE_COMMAND} -E copy
#   ${CMAKE_SOURCE_DIR}/test.sh
#   ${CMAKE_CURRENT_BINARY_DIR}/test.sh)

This shows everything I tried.

configure_file works only if I do a clean build and never again. I need it to work every time I do a diff build.

preetam
  • 1,451
  • 1
  • 17
  • 43
  • Doesn't it work? What happens when you run it? You want to copy it during compilation or during cmake cofiguration? Maybe you want `execute_process`? – KamilCuk Aug 22 '19 at 23:21
  • It doesn't work. No copy happens. tried execute process. same result. – preetam Aug 22 '19 at 23:52
  • What did you do? Did you insert "execute_process" into the file, saved the file, aaaand...? Did you run `cmake ..` ? Did you run `make`? – KamilCuk Aug 23 '19 at 00:11
  • Its doesnt use make. It uses ninja build. So yes, It automatically runs the build system once cmake is run. – preetam Aug 23 '19 at 00:30
  • Since you have the file with the same name - `test.sh` - in both source and build directories, it is better to use **absolute path** whenever you want to refer to that file. This would prevent confusion (both for CMake and for you) which actual file is referred. E.g. the `COMMAND` part of the `add_custom_command` is run from the *build* directory, so in your current code `cp` copies the file from the build directory(as current one) to the same place. – Tsyvarev Aug 23 '19 at 07:34
  • "configure_file works only if I do a clean build and never again. I need it to work every time I do a diff build." - I hardly imagine the reason why do you need to copy the **same** file **every build**, not just once. With proper `OUTPUT` and `DEPENDS` parameters, `add_custom_command` would also copy the file only once, until content of original file (in the source directory) is changed. But the **same** is about `configure_file`: Whenever you run `ninja` and original file is detected to be changed since the last run, CMake *automatically* re-runs and updates the resulted file. – Tsyvarev Aug 23 '19 at 07:42
  • @Tsyvarev : Because the build creates an embedded image and this file has to be copied each time i build an image. The actual image is tared after final file system is built with build directory. As such each built will clean this and rebuild. – preetam Sep 03 '19 at 22:52
  • OK, I think I understand your situation. With proper OUTPUT clause of `add_custom_command` it should trigger rebuilding the output file once it is found to be absent. I would expect the same from `configure_file` but I in that case I am not so sure. So, have you tried to provide **absolute paths** in `add_custom_command` and `add_custom_target`? – Tsyvarev Sep 03 '19 at 22:59

0 Answers0