Running commands in spec file install section using CPackRPM

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

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

It looks like 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 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

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.

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.

You can generate the spec file using 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

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

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

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

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.