CPack + RPM not working as expected with install(CODE)
I'm seeing a behavior where install(CODE) is not working correctly (or at least, inconsistently) when used with the CPack RPM package generator. I've boiled down the more complex real world case to the attached illustrative example:CMakeLists.txt
The behavior I'm seeing is as follows:
$ mkdir build && cd build && cmake .. && make package
[building]
Run CPack packaging tool...
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: CPT
CPack: - Install project: CPT []
CPack: Create package
CPack: - package: /home/user/cpack_test/build/CPT-0.1.1-Linux.tar.gz generated.
CPack: Create package using TBZ2
CPack: Install projects
CPack: - Run preinstall target for: CPT
CPack: - Install project: CPT []
CPack: Create package
CPack: - package: /home/user/cpack_test/build/CPT-0.1.1-Linux.tar.bz2 generated.
CPack: Create package using RPM
CPack: Install projects
CPack: - Run preinstall target for: CPT
CPack: - Install project: CPT []
Error: file /usr/share/file.txt could not be opened.
CPack: Create package
CPackRPM: Will use GENERATED spec file: /home/cyapp/cpack_test/build/_CPack_Packages/Linux/RPM/SPECS/cpt.spec
CPack: - package: /home/user/cpack_test/build/CPT-0.1.1-Linux.rpm generated.
I would expect all packages to handle the install(CODE) step identically, but RPM looks for the file (incorrectly) in /usr, unlike the TGZ and TBZ2 generators.
I've traced the issue at least part way through the code, and it seems to be related to CPACK_SET_DESTDIR being set to always on (internally with I_ON) for RPM. That ends up causing CMAKE_INSTALL_PREFIX to not get the temporary directory used by CPack - hence the install(CODE) failure. What I'm not sure of yet is how to solve the problem without upsetting the RPM logic expecting this particular behavior for CMAKE_INSTALL_PREFIX. Perhaps one possibility would be to provide a CPACK_CMAKE_INSTALL_PREFIX or something similar for install(CODE) to use that would reliably provide the local CPack target when CPack is being run, and act like a normal CMAKE_INSTALL_PREFIX otherwise?