Search code examples
c++cmakecpack

Why should include(CPack) go after the per project package configuration?


I'm experimenting with CPack module of CMake and got somewhat confusing behavior. I have CpackMylib.cmake that is included into a root CMakeLists.txt. It looks as follows:

include(CPack)             #included on top
install (TARGETS mylib
          LIBRARY
          DESTINATION /usr/lib
          COMPONENT mylib-all
)

install (DIRECTORY include/
          DESTINATION /usr/include/mylib 
          COMPONENT mylib-all)
set(CPACK_PACKAGE_NAME "mylib")
set(CPACK_GENERATOR "DEB")

And when running make package it fails to create the package with the following trace:

Run CPack packaging tool...
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: mylib
CPack: - Install project: mylib
CMake Error at /home/krjoff/mylib/cmake_install.cmake:55 (file):
  file INSTALL cannot copy file "/home/krjoff/mylib/libmylib.so" to
  "/usr/lib/mylib.so".


CMake Error at /home/krjoff/mylib/cmake_install.cmake:73 (file):
  file INSTALL cannot set permissions on "/usr/include/mylib"


CPack Error: Error when generating package: mylib
Makefile:129: recipe for target 'package' failed
make: *** [package] Error 1

It looks like it simply ignores all variables I put after the include(CPack) and trying to build some STGZ package and install it immediately. But if I put the include(CPack) in the end of the CpackMylib.cmake after all configuration's been made it works perfectly fine.

Can someone explain why is it necessary to put the include(CPack) after all the configuration settings?


Solution

  • This is how is supposed to work CPack. When you include it in your CMakeLists.txt, it reads all the variables listed in its documentation like CPACK_GENERATOR or CPACK_PACKAGE_NAME and creates the packagetarget you then call with make package.

    If you include it before setting those variables, their value will be ignored.

    Before including this CPack module in your CMakeLists.txt file, there are a variety of variables that can be set to customize the resulting installers. The most commonly-used variables are:

    • CPACK_PACKAGE_NAME
      The name of the package (or application). If not specified, it defaults to the project name.

    • CPACK_PACKAGE_VENDOR
      The name of the package vendor. (e.g., “Kitware”). The default is “Humanity”.

    • CPACK_PACKAGE_DIRECTORY
      The directory in which CPack is doing its packaging. If it is not set then this will default (internally) to the build dir. This variable may be defined in a CPack config file or from the cpack command line option -B. If set, the command line option overrides the value found in the config file.

    • ...

    Source : CPack --- CMake