Search code examples
cmakecpack

CPack / CMake: Different installation prefixes per CPACK_GENERATOR


How can I specify different installation prefixes for the different CPACK_GENERATORs?

For example:

  • the DEB package should be installed to /opt/project
  • the TGZ archive should consist only of the project directory

From the documentation I understood that I would have to use the CPACK_PROJECT_CONFIG_FILE variable. Using that, it should be possible to achieve the desired goal. However, it did not work for me.

This is my CPack configuration:

set(CPACK_GENERATOR "DEB;TGZ")
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_SOURCE_DIR}/cmake/package.linux.txt)

And this is my ${CMAKE_SOURCE_DIR}/cmake/package.linux.txt file:

IF (CPACK_GENERATOR MATCHES "DEB")
    set(CPACK_NATIVE_INSTALL_DIRECTORY "/opt")
    set(CMAKE_INSTALL_PREFIX "/opt")
    set(CPACK_PACKAGING_INSTALL_PREFIX "/opt")
    set(CPACK_INSTALL_DIRECTORY "/opt")
ELSEIF(CPACK_GENERATOR MATCHES "TGZ")
    set(CPACK_NATIVE_INSTALL_DIRECTORY "")
    set(CMAKE_INSTALL_PREFIX "")
    set(CPACK_PACKAGING_INSTALL_PREFIX "")
    set(CPACK_INSTALL_DIRECTORY "")
ENDIF()

I have made sure that all files get parsed using MESSAGE() directives, but the prefix of my packages is always /usr/local.


Solution

  • Finally, I found out what was wrong. I had the CPACK_SET_DESTDIR flag set. This makes CPACK append usr/local to the directory containing the binaries of the compilation. After removing the corresponding SET() directive, everything worked like a charm.

    For the record I will provide you with my minimal working example.

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.2)
    project(stripped-down C)
    
    add_executable(main main.c)
    
    install(TARGETS main
        RUNTIME DESTINATION bin
        )
    
    set(CPACK_GENERATOR "DEB;TGZ")
    set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_SOURCE_DIR}/package.txt)
    
    set(CPACK_PACKAGE_CONTACT "Some One <some.one@somewhere.com>")
    
    include(CPack)
    

    ${CMAKE_SOURCE_DIR}/package.txt:

    IF (CPACK_GENERATOR MATCHES "DEB")
        set(CPACK_PACKAGING_INSTALL_PREFIX "/opt")
    ELSEIF(CPACK_GENERATOR MATCHES "TGZ")
        set(CPACK_PACKAGING_INSTALL_PREFIX "")
    ENDIF()