# Run a CMake script before INSTALL target

**URL:** <https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890>\
**Category:** Code\
**Created:** [March 4, 2021, 7:06pm UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890 "2021-03-04T19:06:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjornblissing](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bjornblissing/32/896_2.png) [@bjornblissing](https://discourse.cmake.org/u/bjornblissing)\
**Post date:** [March 4, 2021, 7:06pm UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890/1 "2021-03-04T19:06:16Z")

</div>

I would like to run a CMake script before the `INSTALL` target (optionally just before building the code).

I have tried using the `install([SCRIPT] <file> [...])` functionality, but that runs during install, which is too late.

The idea is to set the patch part of the `CPACK_PACKAGE_VERSION` to the current Git SHA value, but I want to fetch the latest value just before building the install. (I already have a working CMake script to fetch the latest Git SHA). Currently my script requires the use of CMake generate just before triggering the INSTALL to be sure that the Git SHA is updated correctly. Which could lead to inconsistencies if you have pulled in new code since you generated build files.

---

<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:** [March 4, 2021, 8:57pm UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890/2 "2021-03-04T20:57:23Z")

</div>

I recommend generating the git information as part of the build rather than configure or installation.

I’ve done it before [here](https://github.com/Kitware/sprokit/blob/master/src/sprokit/CMakeLists.txt), but you can probably simplify it for your use case (e.g., not dumping the diff out). Note that it is tied to a configure command which lists a non-existent file in the `OUTPUT` list so that CMake always runs it, so you’ll never have a “do nothing” build with this strategy.

The basic strategy is to make an `add_custom_command` which generates a header (or in your case, a CMake script to include) with the information you need. Just list `some/no/exist/path` in the `OUTPUT` so it always gets rerun since it is never up-to-date.

---

<div class="post-metadata">

**Author:** ![bjornblissing](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bjornblissing/32/896_2.png) [@bjornblissing](https://discourse.cmake.org/u/bjornblissing)\
**Post date:** [March 5, 2021, 11:45am UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890/3 "2021-03-05T11:45:59Z")

</div>

Thank you for you suggestion!

The problem is that the CPack variables in `CPackConfig.cmake` are only written during the generation stage of CMake. So even if I create a (header) file containing the updated SHA value from the Git repository during the build state, I cannot find a way of forwarding these values to CPack.

---

<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:** [March 5, 2021, 2:07pm UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890/4 "2021-03-05T14:07:42Z")

</div>

You can `include()` files from `CPackConfig.cmake`. Just include a CMake script that the git-info retrieval script can write out.

---

<div class="post-metadata">

**Author:** ![bjornblissing](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bjornblissing/32/896_2.png) [@bjornblissing](https://discourse.cmake.org/u/bjornblissing)\
**Post date:** [March 5, 2021, 2:31pm UTC](https://discourse.cmake.org/t/run-a-cmake-script-before-install-target/2890/5 "2021-03-05T14:31:43Z")

</div>

Well, just including a script file inside `CPackConfig.cmake` do not solve the issue, since I need to modify already existing variables inside the CPackConfig.

But I used your original example to parse the `CPackConfig.cmake` file, then do regex replace for the variables I needed to change and then write out the modified file to a new file. And finally replacing the old `CPackConfig.cmake` file with the new one. And this works. A bit hacky solution though…
