Path: blob/master/Tests/CPackComponentsDEB/CMakeLists.txt
3148 views
# CPack Example: User-selectable Installation Components1#2# In this example, we have a simple library (mylib) with an example3# application (mylibapp). We create a binary installer (a CPack Generator)4# which supports CPack components.56cmake_minimum_required(VERSION 3.10 FATAL_ERROR)7# Make sure to properly escape RPATH/RUNPATH entries.8if (POLICY CMP0095)9cmake_policy(SET CMP0095 NEW)10endif()11project(CPackComponentsDEB VERSION 1.0.3)1213# Use GNUInstallDirs in order to enforce lib64 if needed14include(GNUInstallDirs)1516# Create the mylib library17add_library(mylib mylib.cpp)1819# Create the mylibapp application20add_executable(mylibapp mylibapp.cpp)21target_link_libraries(mylibapp mylib)2223# Duplicate of mylibapp application24# which won't be put in any component (?mistake?)25add_executable(mylibapp2 mylibapp.cpp)26target_link_libraries(mylibapp2 mylib)2728if (CPackDEBConfiguration MATCHES "shlibdeps-with-private-lib")29add_subdirectory("shlibdeps-with-private-lib")30add_executable(mylibapp3 mylibapp.cpp)31target_compile_definitions(mylibapp3 PRIVATE -DSHLIBDEPS_PRIVATE)32target_link_libraries(mylibapp3 myprivatelib)33endif()3435if (CPackDEBConfiguration MATCHES "shlibdeps-with-ORIGIN-RPATH")36add_subdirectory("subdir")37add_executable(mylibapp4 mylibapp.cpp)38target_compile_definitions(mylibapp4 PRIVATE -DSHLIBDEPS_OTHER)39target_link_libraries(mylibapp4 PUBLIC myotherlib)40set_target_properties(mylibapp4 PROPERTIES INSTALL_RPATH "\${ORIGIN};$ORIGIN/../lib")41endif()4243# Create installation targets. Note that we put each kind of file44# into a different component via COMPONENT. These components will45# be used to create the installation components.46install(TARGETS mylib47ARCHIVE48DESTINATION ${CMAKE_INSTALL_LIBDIR}49COMPONENT libraries)5051install(TARGETS mylibapp52RUNTIME53DESTINATION ${CMAKE_INSTALL_BINDIR}54COMPONENT applications)5556install(FILES mylib.h57DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}58COMPONENT headers)5960if (CPackDEBConfiguration MATCHES "shlibdeps-with-private-lib")61install(TARGETS mylibapp362RUNTIME63DESTINATION ${CMAKE_INSTALL_BINDIR}64COMPONENT applications)65endif()6667if (CPackDEBConfiguration MATCHES "shlibdeps-with-ORIGIN-RPATH")68install(TARGETS myotherlib69LIBRARY70DESTINATION ${CMAKE_INSTALL_LIBDIR}71COMPONENT libraries)72install(TARGETS mylibapp473RUNTIME74DESTINATION ${CMAKE_INSTALL_BINDIR}75COMPONENT applications)76endif()7778# CPack boilerplate for this project79set(CPACK_PACKAGE_NAME "MyLib")80set(CPACK_PACKAGE_CONTACT "None")81set(CPACK_PACKAGE_VENDOR "CMake.org")82set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MyLib - CPack Component Installation Example")83set(CPACK_PACKAGE_INSTALL_DIRECTORY "CPack Component Example")84set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/license.txt)8586# Tell CPack all of the components to install. The "ALL"87# refers to the fact that this is the set of components that88# will be included when CPack is instructed to put everything89# into the binary installer (the default behavior).90set(CPACK_COMPONENTS_ALL applications libraries headers)9192# Set the displayed names for each of the components to install.93# These will be displayed in the list of components inside the installer.94set(CPACK_COMPONENT_APPLICATIONS_DISPLAY_NAME "MyLib Application")95set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")96set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ Headers")9798# Provide descriptions for each of the components to install.99# When the user hovers the mouse over the name of a component,100# the description will be shown in the "Description" box in the101# installer. If no descriptions are provided, the "Description"102# box will be removed.103set(CPACK_COMPONENT_APPLICATIONS_DESCRIPTION104"An extremely useful application that makes use of MyLib")105set(CPACK_COMPONENT_LIBRARIES_DESCRIPTION106"Static libraries used to build programs with MyLib")107set(CPACK_COMPONENT_HEADERS_DESCRIPTION108"C/C++ header files for use with MyLib")109110# It doesn't make sense to install the headers without the libraries111# (because you could never use the headers!), so make the headers component112# depend on the libraries component.113set(CPACK_COMPONENT_HEADERS_DEPENDS libraries)114115# creates preinst/prerm scripts with specific permissions. Those permissions116# (especially executable) should be in the final archive117find_program(CHMOD_PROG chmod)118if(CHMOD_PROG)119file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/preinst "echo default_preinst")120file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/prerm "echo default_prerm")121122# Those should have 755 permission normally. We mess it up to see if123# CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_STRICT_PERMISSION is able to fix124# it.125execute_process(COMMAND ${CHMOD_PROG} 640 ${CMAKE_CURRENT_BINARY_DIR}/preinst)126execute_process(COMMAND ${CHMOD_PROG} 640 ${CMAKE_CURRENT_BINARY_DIR}/prerm)127128set(CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_EXTRA129"${CMAKE_CURRENT_BINARY_DIR}/preinst;${CMAKE_CURRENT_BINARY_DIR}/prerm")130131set(CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_STRICT_PERMISSION TRUE)132endif()133134# creates a symbolic link and a directory. Those should not be hashed.135# warning: relocation of the symlink is not supported (symlinks with relative136# paths)137execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink mylibapp symtest)138install(FILES ${CPackComponentsDEB_BINARY_DIR}/symtest139DESTINATION bin140COMPONENT applications)141142if(EXISTS "./dirtest")143execute_process(COMMAND ${CMAKE_COMMAND} -E rm -rf ./dirtest)144endif()145# NOTE: directory left empty on purpose146execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ./dirtest)147# NOTE: we should not add the trailing "/" to dirtest148install(DIRECTORY ${CPackComponentsDEB_BINARY_DIR}/dirtest149DESTINATION bin/150COMPONENT applications)151152# We may use the CPack specific config file in order153# to tailor CPack behavior on a CPack generator specific way154# (Behavior would be different for RPM or TGZ or DEB ...)155if (NOT DEFINED CPackDEBConfiguration)156message(FATAL_ERROR "CPackDEBConfiguration should be defined")157endif()158159# Setup project specific CPack-time CPack Config file.160configure_file(${CPackComponentsDEB_SOURCE_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake.in161${CPackComponentsDEB_BINARY_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake162@ONLY)163set(CPACK_PROJECT_CONFIG_FILE ${CPackComponentsDEB_BINARY_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake)164165# set CPACK_DEBIAN_FILE_NAME to use default package name format166set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")167168# Include CPack to introduce the appropriate targets169include(CPack)170171172