# Using install(script ... correctly

**URL:** <https://discourse.cmake.org/t/using-install-script-correctly/1836>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [September 14, 2020, 12:31pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836 "2020-09-14T12:31:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [September 14, 2020, 12:31pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/1 "2020-09-14T12:31:09Z")

</div>

I’m trying to use “install(script …” correctly, but it seems to fail silently and I can’t work out why (precisely because its silent - there are no diagnostic or error messages being sent out).

This is the script I want to run (stored as scriptfile.cmake):

```
file(GLOB sofiles ${CMAKE_INSTALL_FULL_BINDIR}/../lib/python2.7/sil/*.so)
foreach (sofile ${sofiles})
    message("Removing RPATH from ${sofile}")
    execute_process(COMMAND chrpath -d ${sofile}
                    COMMAND_ECHO STDOUT)
endforeach() 

```

And I’d like to run it from my main script like this:

` install(SCRIPT ${CMAKE_CURRENT_LIST_DIR}/scriptfile.cmake)`

I know that the script is being found, as I get an error message if I don’t put it there, or if I rename it: but I’m not seeing the messages and the files don’t get changed. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [September 14, 2020, 2:10pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/2 "2020-09-14T14:10:58Z")

</div>

Have you verified that at install time (i.e. when the script runs), `CMAKE_INSTALL_FULL_BINDIR` contains what you expect? Have you tried adding a message (or write to a file) to the script to check both that and `sofiles`?

---

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [September 14, 2020, 2:43pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/3 "2020-09-14T14:43:05Z")

</div>

OK, I am embarrassed now.

I did put a line with:

`install(CODE "execute_process(COMMAND echo ${CMAKE_INSTALL_FULL_BINDIR})")`

immediately before the `install(SCRIPT` line, so that confirmed that I had the correct `CMAKE_INSTALL_FULL_BINDIR`. However, the `message("..")` statement only gets run if the ‘sofiles’ list is non-empty, and it turns out that in my case it was empty.

No idea why; the path is correct, and I’m fairly sure I have the “file(GLOB…” syntax correct, though this is the first time I’ve tried using it. That’s the next thing to investigate…

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [September 14, 2020, 2:58pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/4 "2020-09-14T14:58:40Z")

</div>

Be careful: in the command `install(CODE "execute_process(COMMAND echo ${CMAKE_INSTALL_FULL_BINDIR})")`, `CMAKE_INSTALL_FULL_BINDIR` will be expanded at CMake configure time, that is, when the installation script is _generated._ The reference inside `scriptfile.cmake` will only be expanded at install time (running `make install`), when the installation script is _executed_. Put the `echo` into the script instead of into `install(CODE)`, to verify what the variable value is at _install time._

---

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [September 14, 2020, 10:04pm UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/5 "2020-09-14T22:04:47Z")

</div>

Thank you. Once again your intuition was spot-on, and indeed `CMAKE_INSTALL_FULL_BINDIR` wasn’t defined at install time. I fixed that by making my scriptfile.cmake into scriptfile.cmake.in, then using configure\_file to store the value of `CMAKE_INSTALL_FULL_BINDIR` at configuration time and then run it at install time.

I’m still not out of the woods, though, since I cannot get `file(GLOB` to populate its list properly. In my current configuration I have

```
execute_process(COMMAND ls ${CMAKE_INSTALL_FULL_BINDIR}/../lib/python2.7/sil)
file(GLOB sofiles "${CMAKE_INSTALL_FULL_BINDIR}/../lib/python2.7/sil/*.so")
message("sofiles contains ${sofiles}")
foreach (sofile ${sofiles})
    message("Converting RPATH in ${sofile} to RUNPATH")
    execute_process(COMMAND chrpath --convert ${sofile}
                    COMMAND_ECHO STDOUT)
endforeach()

```

…and in my output I get a listing of all the files in the target directory at install time, including all the .so files I’d expect, but ‘sofiles’ remians stubbornly empty. I’ve tried it with and without the quotes.

---

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [September 15, 2020, 7:52am UTC](https://discourse.cmake.org/t/using-install-script-correctly/1836/6 "2020-09-15T07:52:20Z")

</div>

OK, I’ve solved this.

Because I had used configure\_file to store the value of CMAKE\_INSTALL\_FULL\_BINDIR in my generated file, but ‘sofiles’ had no value at this time, ‘${sofiles}’ was being set to an empty string.

The solution was simply to set the contents of scriptfile.cmake.in to:

```
execute_process(COMMAND ls @CMAKE_INSTALL_FULL_BINDIR@/../lib/python2.7/sil)
file(GLOB sofiles @CMAKE_INSTALL_FULL_BINDIR@/../lib/python2.7/sil/*.so)
message("sofiles contains ${sofiles}")
foreach (sofile ${sofiles})
    message("Converting RPATH in ${sofile} to RUNPATH")
    execute_process(COMMAND chrpath --convert ${sofile}
                    COMMAND_ECHO STDOUT)
endforeach()

```

…and invoke configure\_file using the @ONLY option.
