# How to run script in install phase from install directory

**URL:** <https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043>\
**Category:** Code\
**Created:** [October 22, 2020, 2:20pm UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043 "2020-10-22T14:20:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [October 22, 2020, 2:20pm UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043/1 "2020-10-22T14:20:28Z")

</div>

Dear CMake Gurus,

This is a little convoluted and long, but I’m hoping it’s possible in CMake…with as little change to source code as possible!

To wit, I work on a earth science model and one of the goals with our recent conversion to CMake is to make things “user portable” for the users. What I mean is that userA can build/install the model, and then userB can go to userA’s installation and run an experiment with that install.

Most of this is working, but I’m getting stuck on a new wrinkle. I was mainly focusing on running the model, etc. but there are postprocessing steps I often miss. What we did in old GNU Make days is during a model run when it’s time to make plots, the user ran a script in our (simplified for here) `src/plots/` directory, `src/plots/configure` which set up a bunch of symlinks _in the source tree_ so the plotting package was happy. Of course, this meant only the user that built the experiment could run this because of directory permissions. But, with some ugly GNU Make, I was able to figure out how to run that script at `make install` time so the symlinks got created.

Now, in CMake land, we have a rule that after installation, no script can **ever** refer back to the source directory. So, users now during postprocessing run `install/plots/configure` instead (we didn’t install it to `install/bin` as we wanted to keep all the plotting/postprocessing separate for various reasons). And like before, if userA built the model and userA runs `install/plots/configure` all is well as userA has permission to write to `install/plots`. But userB has an issue doing so.

So, what I’d like to do is:

1. Install/copy `configure` to `install/plots`
2. After copying, _run_ `install/plots/configure` to set up the symlinks in `install/plots`

The first step we are doing with the usual:

```cmake
install (
   PROGRAMS configure
   DESTINATION plots
   )

```

as it’s just “copy this to there”.

It’s that second step I’m having trouble with. The script has to be run from the `install/plots` directory because that’s where the symlinks need to be. Making it even more difficult is that an environment variable needs to be set (essentially to the full path of `install`) because `configure` is a shell script.

I’ve tried various things (making a custom target, ugly `add_custom_command()`), but I seem to be failing at this…

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 22, 2020, 2:43pm UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043/2 "2020-10-22T14:43:37Z")

</div>

Have a look at [install(SCRIPT | CODE …)](https://cmake.org/cmake/help/latest/command/install.html#script).

And to have a `CMake` script knowing various information about your directories, you can use [configure\_file()](https://cmake.org/cmake/help/latest/command/configure_file.html).

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [October 22, 2020, 3:38pm UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043/3 "2020-10-22T15:38:12Z")

</div>

@marc.chevrier Ah. I see. Well, my first attempt was that `install(SCRIPT | CODE)` is expecting _CMake_ code, and I did `install(SCRIPT configure)` and not happy. So as a test I tried this. First, I created a file `bobo.csh`:

```auto
$ cat bobo.csh
#!/bin/csh -f

setenv GEOSUTIL @CMAKE_INSTALL_PREFIX@
echo "GEOSUTIL: ${GEOSUTIL}" > yaya

```

and then added to my `CMakeLists.txt`:

```cmake
configure_file(bobo.csh bobo.csh @ONLY)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bobo.csh DESTINATION plots)
install(CODE "execute_process(COMMAND bobo.csh WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/plots)")

```

However, as far as I can see it wasn’t executed. I then did:

```cmake
install(CODE "execute_process(COMMAND ${CMAKE_INSTALL_PREFIX}/plots/bobo.csh WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/plots)")

```

and that seemed to work! Huzzah. It’s a bit ugly though (so, on a par with my usual CMake code 😄) . Is there a reason the original `execute_process()` didn’t work? Some CMake-ness I’m missing?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [October 23, 2020, 5:51am UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043/4 "2020-10-23T05:51:39Z")

</div>

> [@mathomp4](#):
>
> Is there a reason the original `execute_process()` didn’t work? Some CMake-ness I’m missing?

Your original code didn’t specify a path to `bobo.csh` in the `install(CODE ...)` command. The current directory is not on the `PATH` by default on Unix systems, so the script would not have been found. Since you set the `WORKING_DIRECTORY`, you could have specified the command as `./bobo.csh` instead and it would have worked.

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [October 23, 2020, 10:11pm UTC](https://discourse.cmake.org/t/how-to-run-script-in-install-phase-from-install-directory/2043/5 "2020-10-23T22:11:32Z")

</div>

Ah. Duh. Sometimes I’m just dumb. I guess in my head I was thinking `WORKING_DIRECTORY` would be added to `PATH` as well. But, I guess there’s nothing wrong with specificity.
