# Running commands in spec file install section using CPackRPM

**URL:** https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993
**Category:** Usage
**Created:** [April 12, 2020, 1:39am UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993 "2020-04-12T01:39:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![praetorian](https://discourse.cmake.org/user_avatar/discourse.cmake.org/praetorian/32/512_2.png) [@praetorian](https://discourse.cmake.org/u/praetorian)
#### Post date: [April 12, 2020, 1:39am UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/1 "2020-04-12T01:39:18Z")

</div>

Hello,  
I’m trying to package a project using CPack RPM. During packaging, I need to post process some of the installed files after they’ve been copied to `RPM_BUILD_ROOT` (or whatever temp location `install` copies them to). If I were writing the spec file by hand, I’d put these commands in the `%install` section, but I can’t figure out a way to do so with CPackRPM.

This is my install command

```auto
install(
    DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/
    USE_SOURCE_PERMISSIONS
    DESTINATION "test"
)

```

Now I need to execute commands on the files that were copied to `<something>/${CPACK_PACKAGING_INSTALL_PREFIX}/test`.

`install(CODE ...)` or `install(SCRIPT ...)` might be what I want, but how can I set the `WORKING_DIRECTORY` of the commands within to the destination where the `install` command copied the files to (i.e. `<something>`).

I’m using CMake 3.6.3

Thanks,  
Ashish

---

<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 12, 2020, 11:37am UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/2 "2020-04-12T11:37:30Z")

</div>

It looks like [`CPACK_RPM_SPEC_MORE_DEFINE`](https://cmake.org/cmake/help/latest/cpack_gen/rpm.html#variable:CPACK_RPM_SPEC_MORE_DEFINE) is what you want. You’ll have to inspect the generated `.spec` file to see what variables you’d want to override. [`CMAKE_RPM_SPEC_INSTALL_POST`](https://cmake.org/cmake/help/latest/cpack_gen/rpm.html#variable:CPACK_RPM_SPEC_INSTALL_POST) looks more direct, but has been replaced with the former. Docs for what variables are available would be a nice-to-have though…

Cc: @kyle.edwards

---

<div class="post-metadata">

### Author: ![praetorian](https://discourse.cmake.org/user_avatar/discourse.cmake.org/praetorian/32/512_2.png) [@praetorian](https://discourse.cmake.org/u/praetorian)
#### Post date: [April 13, 2020, 5:18am UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/3 "2020-04-13T05:18:56Z")

</div>

Thanks, but I don’t see how `CPACK_RPM_SPEC_MORE_DEFINE` can be used for what I want to do. That allows me to define something global to the spec file, while I want to add lines within the `%install` section. Also, in my use case, I have `CPACK_RPM_COMPONENT_INSTALL` enabled, so the solution would have to be specific to one component.

I can use `CMAKE_RPM_SPEC_INSTALL_POST` to do what I want, but I’d rather avoid modifying the files after installation because that breaks `rpm -V`. It’d be a lot more preferable to process them prior to packaging.

---

<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 13, 2020, 1:25pm UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/4 "2020-04-13T13:25:11Z")

</div>

> [@praetorian](#):
>
> That allows me to define something global to the spec file, while I want to add lines within the `%install` section.

There may be a variable which expands to code inside of `%install`. I can’t see the `.spec` template right now, but something should exist in the `.spec` file CPack ends up using. If not, we should add a variable in such a location.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [April 13, 2020, 3:48pm UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/5 "2020-04-13T15:48:55Z")

</div>

You can generate the spec file using [CPACK\_RPM\_GENERATE\_USER\_BINARY\_SPECFILE\_TEMPLATE](https://cmake.org/cmake/help/v3.17/cpack_gen/rpm.html#CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE)

as from the code: [https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Internal/CPack/CPackRPM.cmake#L1599](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Internal/CPack/CPackRPM.cmake#L1599)

you cannot currently inject what you want in the `%install` section.  
All that said may be the `install(CODE ...)` or `install(SCRIPT ...)` should work if you handle `DESTDIR` properly (since CPack does internally use `DESTDIR` mechanism to install things before packaging)

see e.g.: [https://cmake.org/pipermail/cmake-developers/2013-January/017811.html](https://cmake.org/pipermail/cmake-developers/2013-January/017811.html)

---

<div class="post-metadata">

### Author: ![praetorian](https://discourse.cmake.org/user_avatar/discourse.cmake.org/praetorian/32/512_2.png) [@praetorian](https://discourse.cmake.org/u/praetorian)
#### Post date: [April 13, 2020, 8:33pm UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/6 "2020-04-13T20:33:26Z")

</div>

@erk, thanks so much, that worked! For reference, this is what I did

```auto
install(
    CODE "
        execute_process(
            COMMAND bash -c \"
                <call post processing commands>
            \"
            WORKING_DIRECTORY \$ENV{DESTDIR}
        )
    "
    COMPONENT my_component
)

```

---

<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 14, 2020, 11:51am UTC](https://discourse.cmake.org/t/running-commands-in-spec-file-install-section-using-cpackrpm/993/7 "2020-04-14T11:51:38Z")

</div>

That looks good. I might recommend putting the commands into a file and running that file rather than directly embedding the commands to avoid having to deal with CMake/bash quoting nesting.
