# Copy (changed) scripts as part of build

**URL:** https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320
**Category:** Code
**Created:** [June 4, 2020, 6:17am UTC](https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320 "2020-06-04T06:17:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andrew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrew/32/2478_2.png) [@Andrew](https://discourse.cmake.org/u/Andrew)
#### Post date: [June 4, 2020, 6:17am UTC](https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320/1 "2020-06-04T06:17:09Z")

</div>

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.

---

<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: [June 4, 2020, 12:43pm UTC](https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320/2 "2020-06-04T12:43:43Z")

</div>

> [@Andrew](#):
>
> 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 would make the contents of these scripts something like:

```sh
#!/bin/sh

readonly tool="$<TARGET_FILE:my_exe>"

"$tool" …

```

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

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

```

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrew/32/2478_2.png) [@Andrew](https://discourse.cmake.org/u/Andrew)
#### Post date: [June 5, 2020, 2:32am UTC](https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320/3 "2020-06-05T02:32:18Z")

</div>

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

---

<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: [June 5, 2020, 1:30pm UTC](https://discourse.cmake.org/t/copy-changed-scripts-as-part-of-build/1320/4 "2020-06-05T13:30:21Z")

</div>

Something like:

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

```
