Copy (changed) scripts as part of build

In my Make architecture, I had the following:

Directory/

  • Makefile
  • src
  • bin (output goes here)
    ** my_exe
  • run/
    ** run1.sh
    ** run2.sh
    ** run3.sh

The run*.sh scripts all run "…/bin/my_exe ".

my_exe itself is a dummy server that I use for testing other programs that I am developing. I’m happy to run it directly from the build directory - there’s no particular need to “install” it. The run scripts are not “tests” per-se, they are just shortcuts so I can run it with different configurations of the server without manually typing in a half dozen or more command line options.

Moving the architecture to CMake poses a slight problem. my_exe now gets built into some directory in another part of the tree. Once it’s built, I’d like to be able to run it via scripts without needing to install it into a standard path.

My proposed solution is to “build” the run scripts in CMake so that they end up in a known location relative to the binary, at which point I can just run the scripts from the CMake output directory.

I can see a bunch of ways in CMake to copy files. But am not sure about targets. What’s the easiest way to create a target that says “if any of files have changed, then copy them into the CMake build destination directory”?

This is roughly equivalent to the Xcode “copy files” build phase.

I would make the contents of these scripts something like:

#!/bin/sh

readonly tool="$<TARGET_FILE:my_exe>"

"$tool" …

You can then have CMake put these into the build tree with:

file(GENERATE
  INPUT  "${CMAKE_CURRENT_SOURCE_DIR}/run/run1.sh.in"
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/run/run1.sh")

That makes sense. How do I attach these commands to a target?

Something like:

add_custom_target(run1
  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/run/run1.sh"
  WORKING_DIRECTORY ...)