# Variables available to code in install(SCRIPT) and install(CODE)

**URL:** https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484
**Category:** Development
**Created:** [November 15, 2021, 11:48am UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484 "2021-11-15T11:48:49Z")
**Posts on this page:** 5
**Page:** 1

<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: [November 15, 2021, 11:48am UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484/1 "2021-11-15T11:48:49Z")

</div>

I was looking into what variables we document as being available to `install(SCRIPT)` and `install(CODE)`. I was surprised that we don’t seem to document any. I expect that at the very least, we would guarantee that `CMAKE_INSTALL_PREFIX` would be defined,. We also don’t make any guarantees about the working directory (that I could find). Without `CMAKE_INSTALL_PREFIX` being documented as available or the working directory guaranteed, you would have no documented way to install anything or access things that have already been installed.

I suspect anyone using these `install()` subcommands already assumes `CMAKE_INSTALL_PREFIX` is defined. It seems to be one of the first things defined in the `cmake_install.cmake` script we generate. Is there any reason we can’t formally document it as always being defined and available? Given that it seems this variable has always been defined and available (from what I can tell, but would be good to confirm), we could add it now (even for 3.22) and not need a `versionadded` directive.

---

<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: [November 15, 2021, 12:03pm UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484/2 "2021-11-15T12:03:40Z")

</div>

While we’re at it, `$ENV{DESTDIR}` should also be documented. Its interactions with `${CMAKE_INSTALL_PREFIX}` on Windows is [not trivial](https://gitlab.kitware.com/paraview/paraview/-/blob/3feca92883e52fb5d5816a2885e48e654bed8ccb/CMake/ParaViewTestInstall.cmake#L45-51). I’m sure someone can end up configuring things in a way that such code breaks.

---

<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: [November 15, 2021, 12:13pm UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484/3 "2021-11-15T12:13:54Z")

</div>

Ouch, yes I had forgotten about that. Looking at a sample `cmake_install.cmake`, to be robust, yes you would also need to know about `$ENV{DESTDIR}` and that the full path is expressed as `$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}`.

Do we really need to worry about interactions on Windows in CMake’s own docs though? I think even within CMake itself, we just use the above concatenation and it is the user’s responsibility to ensure that makes sense and works. At most, I think we’d just have to say that if you’re on Windows and you set the `DESTDIR` environment variable, you must ensure that `CMAKE_INSTALL_PREFIX` does not include a drive letter.

---

<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: [November 15, 2021, 12:16pm UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484/4 "2021-11-15T12:16:56Z")

</div>

Actually, we already have a warning about this in the docs for the `DESTDIR` environment variable:

> WARNING: `DESTDIR` may not be used on Windows because installation prefix usually contains a drive letter like in `C:/Program Files` which cannot be prepended with some other prefix.

---

<div class="post-metadata">

### Author: ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)
#### Post date: [November 15, 2021, 2:11pm UTC](https://discourse.cmake.org/t/variables-available-to-code-in-install-script-and-install-code/4484/5 "2021-11-15T14:11:50Z")

</div>

Just FYI, Qt’s CI still abuses and sets DESTDIR on Windows and it worked mostly fine over the past 2 years, it just doesn’t prepend the drive letter. e.g. set DESTDIR=/Users/pc/Documents/bla
