Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Tests/CPackComponentsDEB/CMakeLists.txt
3148 views
1
# CPack Example: User-selectable Installation Components
2
#
3
# In this example, we have a simple library (mylib) with an example
4
# application (mylibapp). We create a binary installer (a CPack Generator)
5
# which supports CPack components.
6
7
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
8
# Make sure to properly escape RPATH/RUNPATH entries.
9
if (POLICY CMP0095)
10
cmake_policy(SET CMP0095 NEW)
11
endif()
12
project(CPackComponentsDEB VERSION 1.0.3)
13
14
# Use GNUInstallDirs in order to enforce lib64 if needed
15
include(GNUInstallDirs)
16
17
# Create the mylib library
18
add_library(mylib mylib.cpp)
19
20
# Create the mylibapp application
21
add_executable(mylibapp mylibapp.cpp)
22
target_link_libraries(mylibapp mylib)
23
24
# Duplicate of mylibapp application
25
# which won't be put in any component (?mistake?)
26
add_executable(mylibapp2 mylibapp.cpp)
27
target_link_libraries(mylibapp2 mylib)
28
29
if (CPackDEBConfiguration MATCHES "shlibdeps-with-private-lib")
30
add_subdirectory("shlibdeps-with-private-lib")
31
add_executable(mylibapp3 mylibapp.cpp)
32
target_compile_definitions(mylibapp3 PRIVATE -DSHLIBDEPS_PRIVATE)
33
target_link_libraries(mylibapp3 myprivatelib)
34
endif()
35
36
if (CPackDEBConfiguration MATCHES "shlibdeps-with-ORIGIN-RPATH")
37
add_subdirectory("subdir")
38
add_executable(mylibapp4 mylibapp.cpp)
39
target_compile_definitions(mylibapp4 PRIVATE -DSHLIBDEPS_OTHER)
40
target_link_libraries(mylibapp4 PUBLIC myotherlib)
41
set_target_properties(mylibapp4 PROPERTIES INSTALL_RPATH "\${ORIGIN};$ORIGIN/../lib")
42
endif()
43
44
# Create installation targets. Note that we put each kind of file
45
# into a different component via COMPONENT. These components will
46
# be used to create the installation components.
47
install(TARGETS mylib
48
ARCHIVE
49
DESTINATION ${CMAKE_INSTALL_LIBDIR}
50
COMPONENT libraries)
51
52
install(TARGETS mylibapp
53
RUNTIME
54
DESTINATION ${CMAKE_INSTALL_BINDIR}
55
COMPONENT applications)
56
57
install(FILES mylib.h
58
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
59
COMPONENT headers)
60
61
if (CPackDEBConfiguration MATCHES "shlibdeps-with-private-lib")
62
install(TARGETS mylibapp3
63
RUNTIME
64
DESTINATION ${CMAKE_INSTALL_BINDIR}
65
COMPONENT applications)
66
endif()
67
68
if (CPackDEBConfiguration MATCHES "shlibdeps-with-ORIGIN-RPATH")
69
install(TARGETS myotherlib
70
LIBRARY
71
DESTINATION ${CMAKE_INSTALL_LIBDIR}
72
COMPONENT libraries)
73
install(TARGETS mylibapp4
74
RUNTIME
75
DESTINATION ${CMAKE_INSTALL_BINDIR}
76
COMPONENT applications)
77
endif()
78
79
# CPack boilerplate for this project
80
set(CPACK_PACKAGE_NAME "MyLib")
81
set(CPACK_PACKAGE_CONTACT "None")
82
set(CPACK_PACKAGE_VENDOR "CMake.org")
83
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MyLib - CPack Component Installation Example")
84
set(CPACK_PACKAGE_INSTALL_DIRECTORY "CPack Component Example")
85
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/license.txt)
86
87
# Tell CPack all of the components to install. The "ALL"
88
# refers to the fact that this is the set of components that
89
# will be included when CPack is instructed to put everything
90
# into the binary installer (the default behavior).
91
set(CPACK_COMPONENTS_ALL applications libraries headers)
92
93
# Set the displayed names for each of the components to install.
94
# These will be displayed in the list of components inside the installer.
95
set(CPACK_COMPONENT_APPLICATIONS_DISPLAY_NAME "MyLib Application")
96
set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
97
set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ Headers")
98
99
# Provide descriptions for each of the components to install.
100
# When the user hovers the mouse over the name of a component,
101
# the description will be shown in the "Description" box in the
102
# installer. If no descriptions are provided, the "Description"
103
# box will be removed.
104
set(CPACK_COMPONENT_APPLICATIONS_DESCRIPTION
105
"An extremely useful application that makes use of MyLib")
106
set(CPACK_COMPONENT_LIBRARIES_DESCRIPTION
107
"Static libraries used to build programs with MyLib")
108
set(CPACK_COMPONENT_HEADERS_DESCRIPTION
109
"C/C++ header files for use with MyLib")
110
111
# It doesn't make sense to install the headers without the libraries
112
# (because you could never use the headers!), so make the headers component
113
# depend on the libraries component.
114
set(CPACK_COMPONENT_HEADERS_DEPENDS libraries)
115
116
# creates preinst/prerm scripts with specific permissions. Those permissions
117
# (especially executable) should be in the final archive
118
find_program(CHMOD_PROG chmod)
119
if(CHMOD_PROG)
120
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/preinst "echo default_preinst")
121
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/prerm "echo default_prerm")
122
123
# Those should have 755 permission normally. We mess it up to see if
124
# CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_STRICT_PERMISSION is able to fix
125
# it.
126
execute_process(COMMAND ${CHMOD_PROG} 640 ${CMAKE_CURRENT_BINARY_DIR}/preinst)
127
execute_process(COMMAND ${CHMOD_PROG} 640 ${CMAKE_CURRENT_BINARY_DIR}/prerm)
128
129
set(CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_EXTRA
130
"${CMAKE_CURRENT_BINARY_DIR}/preinst;${CMAKE_CURRENT_BINARY_DIR}/prerm")
131
132
set(CPACK_DEBIAN_APPLICATIONS_PACKAGE_CONTROL_STRICT_PERMISSION TRUE)
133
endif()
134
135
# creates a symbolic link and a directory. Those should not be hashed.
136
# warning: relocation of the symlink is not supported (symlinks with relative
137
# paths)
138
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink mylibapp symtest)
139
install(FILES ${CPackComponentsDEB_BINARY_DIR}/symtest
140
DESTINATION bin
141
COMPONENT applications)
142
143
if(EXISTS "./dirtest")
144
execute_process(COMMAND ${CMAKE_COMMAND} -E rm -rf ./dirtest)
145
endif()
146
# NOTE: directory left empty on purpose
147
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ./dirtest)
148
# NOTE: we should not add the trailing "/" to dirtest
149
install(DIRECTORY ${CPackComponentsDEB_BINARY_DIR}/dirtest
150
DESTINATION bin/
151
COMPONENT applications)
152
153
# We may use the CPack specific config file in order
154
# to tailor CPack behavior on a CPack generator specific way
155
# (Behavior would be different for RPM or TGZ or DEB ...)
156
if (NOT DEFINED CPackDEBConfiguration)
157
message(FATAL_ERROR "CPackDEBConfiguration should be defined")
158
endif()
159
160
# Setup project specific CPack-time CPack Config file.
161
configure_file(${CPackComponentsDEB_SOURCE_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake.in
162
${CPackComponentsDEB_BINARY_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake
163
@ONLY)
164
set(CPACK_PROJECT_CONFIG_FILE ${CPackComponentsDEB_BINARY_DIR}/MyLibCPackConfig-${CPackDEBConfiguration}.cmake)
165
166
# set CPACK_DEBIAN_FILE_NAME to use default package name format
167
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
168
169
# Include CPack to introduce the appropriate targets
170
include(CPack)
171
172