# CMAKE\_INSTALL\_PREFIX in CPACK\_PRE\_BUILD\_SCRIPTS

**URL:** https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631
**Category:** Usage
**Created:** [January 26, 2021, 10:23am UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631 "2021-01-26T10:23:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![floofy\_kh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@floofy\_kh](https://discourse.cmake.org/u/floofy_kh)
#### Post date: [January 26, 2021, 10:23am UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631/1 "2021-01-26T10:23:52Z")

</div>

Hello,

I’m trying to use CPACK\_PRE\_BUILD\_SCRIPTS to filter out certain files from the staging directory before CPack begins packaging. The use case for this is that we call on an external package manager executable to copy binaries into our install directory. That’s done through install(CODE …).

Unfortunately, some of the binaries being copied into the install directory aren’t required or desired in the final package, such as test executables and other CI tools. I was hoping to use CPACK\_PRE\_BUILD\_SCRIPTS to remove any unwanted files from the staging directory before CPack does it’s thing.

This is where my issue lies, the script I have that’s run for CPACK\_PRE\_BUILD\_SCRIPTS doesn’t have it’s CMAKE\_ variables initialised to what I’d expect. If I have the following pre-build script:

> message(“CMAKE\_INSTALL\_PREFIX = ${CMAKE\_INSTALL\_PREFIX}”)  
> message(“CMAKE\_SOURCE\_DIR = ${CMAKE\_SOURCE\_DIR}”)  
> message(“CMAKE\_CURRENT\_SOURCE\_DIR = ${CMAKE\_CURRENT\_SOURCE\_DIR}”)  
> message(“CMAKE\_BINARY\_DIR = ${CMAKE\_BINARY\_DIR}”)  
> message(“CMAKE\_CURRENT\_BINARY\_DIR = ${CMAKE\_CURRENT\_BINARY\_DIR}”)  
> message(“CPACK\_PACKAGE\_FILES = ${CPACK\_PACKAGE\_FILES}”)

Then I get the following output during CPack:

> CMAKE\_INSTALL\_PREFIX = C:/Program Files (x86)  
> CMAKE\_SOURCE\_DIR =  
> CMAKE\_CURRENT\_SOURCE\_DIR =  
> CMAKE\_BINARY\_DIR =  
> CMAKE\_CURRENT\_BINARY\_DIR =  
> CPACK\_PACKAGE\_FILES =

At the very least I was hoping that CMAKE\_INSTALL\_PREFIX would be initialised to the staging directory. Currently it’s not even set to what’s in my CMakeCache.

Is this expected behaviour for CPACK\_PRE\_BUILD\_SCRIPTS?

Currently using CMake 3.19.3  
Windows 10  
Tried Visual Studio 2019 and Ninja CMake generators  
Tried ZIP and NSIS CPack generators

---

<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: [January 26, 2021, 2:23pm UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631/2 "2021-01-26T14:23:53Z")

</div>

You’re probably looking for `$ENV{DESTDIR}` which is what CPack uses to “inject” itself into the installation procedure. That said, not all of the variables are available.

> [@floofy\_kh](#):
>
> Unfortunately, some of the binaries being copied into the install directory aren’t required or desired in the final package

I think you probably want to use components for the bits you want and then install only those components rather than trying to undo installation of some bits.

---

<div class="post-metadata">

### Author: ![floofy\_kh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@floofy\_kh](https://discourse.cmake.org/u/floofy_kh)
#### Post date: [January 26, 2021, 3:48pm UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631/3 "2021-01-26T15:48:42Z")

</div>

> [@ben.boeckel](#):
>
> You’re probably looking for `$ENV{DESTDIR}` which is what CPack uses to “inject” itself into the installation procedure. That said, not all of the variables are available.

I had a look at the docs for DESTDIR and it doesn’t seem like I would be able to use it in this case as it wouldn’t work on Windows.

> [@ben.boeckel](#):
>
> I think you probably want to use components for the bits you want and then install only those components rather than trying to undo installation of some bits.

Unfortunately I don’t have the granular control to be able to use components.  
Effectively what I’m doing is the following

```
install(CODE "execute_process(COMMAND an-executable-that-copies-binaries-into-install-folder)")

```

And that executable doesn’t provide the API to be able to filter what’s going to be copied.

The docs for [CPACK\_INSTALL\_SCRIPTS](https://cmake.org/cmake/help/latest/module/CPack.html#variable:CPACK_INSTALL_SCRIPTS) state the following

> For every script, the following variables will be set: `CMAKE_CURRENT_SOURCE_DIR`, `CMAKE_CURRENT_BINARY_DIR` and `CMAKE_INSTALL_PREFIX` (which is set to the staging install directory)

I was hoping the same would apply to `CPACK_PRE_BUILD_SCRIPTS` since they seem to be related, but unfortunately not.

I have an alternative solution which doesn’t make use of `CPACK_PRE_BUILD_SCRIPTS` and instead just extends the code in the install(CODE …) mentioned above to include the same script as I would assign to `CPACK_PRE_BUILD_SCRIPTS`. In that case `CMAKE_INSTALL_PREFIX` will be initialised correctly to the staging directory.

---

<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: [January 26, 2021, 8:20pm UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631/4 "2021-01-26T20:20:42Z")

</div>

This has already been [raised](https://gitlab.kitware.com/cmake/cmake/-/issues/21520) in the CMake issue tracker (at least in part, for setting some of the variables).

---

<div class="post-metadata">

### Author: ![floofy\_kh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@floofy\_kh](https://discourse.cmake.org/u/floofy_kh)
#### Post date: [January 27, 2021, 8:42am UTC](https://discourse.cmake.org/t/cmake-install-prefix-in-cpack-pre-build-scripts/2631/5 "2021-01-27T08:42:59Z")

</div>

Thank you. I did have a quick search for existing bugs, but I obviously didn’t do a very good job of it. I’ll keep an eye on the bug. Thanks again.
