CMake-CPack Package Installation Path Nightmare

2019-03-18 02:12发布

问题:

I've been frustrated by the the CMake-CPack for almost one week.

The bad thing is the CMake-CPack online documentation does not document this part well.

After googling, I found this variables to use:

CPACK_PACKAGING_PREFIX          # NOT documented
CMAKE_INSTALL_PREFIX            # Documented, but the behavior seems weird
CPACK_INSTALL_PREFIX            # NOT documented
CPACK_PACKAGE_INSTALL_DIRECTORY # Documented, but this variable does NOT work as the online document described
CPACK_PACKAGING_INSTALL_PREFIX  # NOT documented

What I am trying to do is: package a Debian package using fakeroot make package, when the package is installed by sudo dpkg -i MyProgramPackageName, install it to /usr/local, with a subdirectory MyProgramPackageName. That is, all files should be installed under /usr/local/MyProgramPackageName-V.1.2.3.

I've been trying (CMake 2.8.3 and CMake 2.8.5) to tune these variables. I tried so many combinations, but failed.

The only way succeeded is:

Set(CPACK_PACKAGING_INSTALL_PREFIX /usr/local/MyProgramPackageName-V.1.2.3)

But this variable is NOT even documented, and the behavior cannot be guaranteed. If you are confused with my question, please advise me when to use CPACK_PACKAGE_INSTALL_DIRECTORY? because the documentation description about this variable is really attractive, and it is really what I want, but I just could not make it working.

Please advise me.

Peter

回答1:

I didn't find any documentation to support this, but I did find some bug reports and email archives that seem to suggest that the following is what you should be doing:

set(CPACK_SET_DESTDIR true)
set(CPACK_INSTALL_PREFIX /opt/MySuperAwesomePrefix-v.1.2.3)

If CPACK_INSTALL_PREFIX is not set, it will default to CMAKE_INSTALL_PREFIX. Now relative paths from install(... DESTINATION dest) will end up as CPACK_INSTALL_PREFIX/dest inside your package file. This worked when I tried to generate a deb file.



回答2:

The paths used by the CPACK are taken from the INSTALL directives in your CMakeLists.txt files. This allows the result package to mirror what a 'make install' would do. This keeps the CPACK configuration to a minimum.

So, from an example CMakeLists.txt file:

INSTALL(TARGETS ${APPLICATION} DESTINATION bin)

This will install to /usr/bin or /usr/local/bin. If you wanted to place it in a subdirectory you could do it here:

INSTALL(TARGETS ${APPLICATION} DESTINATION bin/myappdir)

Or entirely different directory:

INSTALL(TARGETS ${APPLICATION} DESTINATION /opt/foo/bar)