# how to set variables for install(SCRIPT...)?

**URL:** <https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185>\
**Category:** Code\
**Created:** [April 21, 2021, 3:31pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185 "2021-04-21T15:31:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [April 21, 2021, 3:31pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/1 "2021-04-21T15:31:37Z")

</div>

is it possible to pass a variable for the cmake script when using install(SCRIPT) mode?

the following is invalid:

```cmake
install(SCRIPT -DFOO=bar -P foo.cmake)

```

and this generates without error but doesn’t pass `FOO` to `foo.cmake`:

```cmake
install(SCRIPT foo.cmake -DFOO=bar)

```

one option would be to use `install(CODE ...)`, instead.

---

<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:** [April 21, 2021, 3:44pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/2 "2021-04-21T15:44:09Z")

</div>

Yeah, you’ll need to inject variable settings into the install step somehow. I have written a set of `set()` calls to a file then added an inclusion of that file via `install(CODE)` to work around this before.

---

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [April 21, 2021, 4:27pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/3 "2021-04-21T16:27:44Z")

</div>

That’s one way to do it. I would have loved to see something like this working:

```cmake
# no options here for simplicity
install(CODE [[
	execute_process(
		COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/foo.cmake
	)
]])

```

but this doesn’t fly because source dir is _that_ source dir not this one.

```cmake
execute_process(
	COMMAND ${CMAKE_COMMAND} -E echo src dir: ${CMAKE_CURRENT_SOURCE_DIR}
)
install(CODE "message(\"this src dir: ${CMAKE_CURRENT_SOURCE_DIR}\")")
install(CODE [[
	execute_process(
		COMMAND ${CMAKE_COMMAND} -E echo that src dir: ${CMAKE_CURRENT_SOURCE_DIR}
	)
]])

```

this gives me

```auto
src dir: /tmp/test/extern/foo
this src dir: /tmp/test/extern/foo
that src dir: /tmp/test/build/extern/foo-prefix/src/foo-build

```

why is this? The first 2 commands know about my source dir but the last one does not. It looks like source dir is equal to binary dir in the last command.

---

<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:** [April 21, 2021, 6:05pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/4 "2021-04-21T18:05:04Z")

</div>

I think that the `[[` / `]]` quoting in the latter prevents expansion until the install time.

---

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [April 22, 2021, 8:13am UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/5 "2021-04-22T08:13:52Z")

</div>

this is it, thank you!

---

<div class="post-metadata">

**Author:** ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)\
**Post date:** [April 27, 2021, 8:09pm UTC](https://discourse.cmake.org/t/how-to-set-variables-for-install-script/3185/6 "2021-04-27T20:09:11Z")

</div>

Note that you may not _want_ (and/or need) to pass directory paths in to the install script. If you’re doing it because you need to reference, say, the location of a build file, you may be able to reference it via a generator expression like `$<TARGET_FILE:output_target>`, `$<TARGET_OBJECTS:object_target>`, or `$<TARGET_PROPERTY:imported_target,LOCATION>`1 — and another bonus, those _can_ be used inside `[[` `]]` blocks. Unlike variable references, generator expressions are always expanded.

#### Notes

1. With that last one, unless the target was imported in the current scope you might have to resort to passing in a variable:

```cmake
get_target_property(DEPENDENCY_PATH imported_target LOCATION)
install(CODE "set(DEPENDENCY_PATH \"${DEPENDENCY_PATH}\")")
install(CODE [[
  something_using(${DEPENDENCY_PATH})
]])

```
