I am using CMake to build my project and now I want to use CPack to create Linux packages (starting with a DEB package). I have things set up such that I can generated a DEB package now, but there is one problem that I am not entirely sure how I should solve it.
During compilation, I am also generating a systemd service file for my application that necessarily contains the absolute path to where my application is installed to. No big problem as we have CMAKE_INSTALL_PREFIX
which enables us to get the needed path that is in agreement to where things are installed to when one executes make install
.
However, cpack
seems to use a different install prefix (/usr/
instead of /usr/local/
) and therefore the path in the service file inside my DEB package, doesn't match to where the generated package actually installs the executable to.
After a bit of research, I found CPACK_PACKAGING_INSTALL_PREFIX which apparently allows me to override cpack's default, but that is not quite what I want. I would like CPack to do its thing, but I need a way to essentially anticipate where CPack wants to install things to in order to configure my service file correctly. The linked docs also state that
Each CPack generator has a default value
Indicating that the chosen path is generator-dependent.
Now, it would be possible to figure out where e.g. the DEB generator installs things to and then manually set CMAKE_INSTALL_PREFIX
to that same value so that the service file configuration works as expected, but I would like to avoid having such a manual step in the process.
Therefore my question: Is there a way to determine at configure time what path a given CPack generator will use as the install prefix? Or even better: have a way to automatically reconfigure certain files when CPack runs and then adapt the respective paths on-the-fly without having to modify the files configured during a regular CMake invocation.
I think the answer is- there's no super easy way from a CPack package-author's perspective. I did a grep through CMake's Modules/
folder and didn't find the default/fallback being set there. It turns out it's set as a default/fallback value in the CPack binary's source code. You can see for yourself: https://github.com/Kitware/CMake/search?q=SetOptionIfNotSet+CPACK_PACKAGING_INSTALL_PREFIX, where you'll see (at the time of this writing):
/
/usr
/usr
/usr/local
/Applications
/usr
So if you want to mirror that logic in your own CPack config scripts, you'll have to do so yourself. You could look at using the value of CPACK_GENERATOR
in a CPACK_PROJECT_CONFIG_FILE
(note that CPACK_GENERATOR
means different things in different script contexts).