-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_install.cmake
158 lines (136 loc) · 5.46 KB
/
lib_install.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
cmake_minimum_required(VERSION 3.20)
# lib_install(
# # The name of the project
# TARGET_PROJECT <str>
#
# # List of targets to install.
# # If the argumnt is omitted then the default value is "${TARGET_PROJECT}".
# [TARGETS_LIST <target1>[;<target2>...]]
#
# # Define the version of the library/package.
# VERSION <path>
#
# # Template for lib config file.
# LIB_CONFIG <path>
#
# # Compatibility mode (optional, default: SameMinorVersion).
# [LIB_COMPATIBILITY <path>]
# )
function(lib_install)
set(one_value_args
TARGET_PROJECT
LIB_CONFIG
VERSION
LIB_COMPATIBILITY
)
set(multi_value_args TARGETS_LIST)
cmake_parse_arguments(lib_install "${options}"
"${one_value_args}"
"${multi_value_args}"
${ARGN}
)
message(STATUS "====================================")
message(STATUS "PREPARING INSTALL OF LIBRARY PROJECT")
message(STATUS "TARGET_PROJECT=${lib_install_TARGET_PROJECT}")
message(STATUS "LIB_CONFIG=${lib_install_LIB_CONFIG}")
message(STATUS "VERSION=${lib_install_VERSION}")
message(STATUS "LIB_COMPATIBILITY=${lib_install_LIB_COMPATIBILITY}")
if (NOT lib_install_TARGET_PROJECT)
message(FATAL_ERROR "TARGET_PROJECT parameter must be defined")
endif ()
if (NOT lib_install_TARGETS_LIST)
set(lib_install_TARGETS_LIST ${lib_install_TARGET_PROJECT})
message(STATUS "TARGETS_LIST=${lib_install_TARGETS_LIST} (auto deduced)")
else ()
message(STATUS "TARGETS_LIST=${lib_install_TARGETS_LIST}")
endif ()
if (NOT lib_install_LIB_CONFIG)
message(FATAL_ERROR "LIB_CONFIG parameter must be defined")
endif ()
if (NOT lib_install_LIB_COMPATIBILITY)
set(lib_install_LIB_COMPATIBILITY SameMinorVersion)
endif ()
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(cmake_destination ${CMAKE_INSTALL_LIBDIR}/cmake/${lib_install_TARGET_PROJECT})
install(TARGETS ${lib_install_TARGETS_LIST}
EXPORT ${lib_install_TARGET_PROJECT}_project_targets
)
install(EXPORT ${lib_install_TARGET_PROJECT}_project_targets
FILE ${lib_install_TARGET_PROJECT}_targets.cmake
NAMESPACE ${lib_install_TARGET_PROJECT}::
DESTINATION ${cmake_destination}
)
set(tmp_version_config ${PROJECT_BINARY_DIR}/${lib_install_TARGET_PROJECT}-config-version.cmake)
set(tmp_project_config ${PROJECT_BINARY_DIR}/${lib_install_TARGET_PROJECT}-config.cmake)
configure_package_config_file(${lib_install_LIB_CONFIG}
${tmp_project_config}
INSTALL_DESTINATION ${cmake_destination}
)
write_basic_package_version_file(
${tmp_version_config}
VERSION ${lib_install_VERSION}
COMPATIBILITY ${lib_install_LIB_COMPATIBILITY}
)
# Install version, config and target files.
install(FILES ${tmp_version_config} ${tmp_project_config}
DESTINATION ${cmake_destination}
)
endfunction()
# lib_install_headers(
# # The path-prefix that must be removed from all headers to install.
# REMOVE_INCLUDE_DIR_PREFIX <str>
#
# # List of targets to install.
# TARGETS_LIST <target1>[;<target2>...]
#
# # Define the version of the library/package.
# VERSION <path>
#
# # Template for lib config file.
# LIB_CONFIG <path>
#
# # Compatibility mode (optional, default: SameMinorVersion).
# [LIB_COMPATIBILITY <path>]
# )
function(lib_install_headers)
set(one_value_args REMOVE_INCLUDE_DIR_PREFIX)
set(multi_value_args HEADERS)
cmake_parse_arguments(lib_install_headers "${options}"
"${one_value_args}"
"${multi_value_args}"
${ARGN})
message(STATUS "=====================================")
message(STATUS "PREPARING INSTALL FOR HEADERS")
message(STATUS "REMOVE_INCLUDE_DIR_PREFIX=${lib_install_headers_REMOVE_INCLUDE_DIR_PREFIX}")
message(STATUS "HEADERS=${lib_install_headers_HEADERS}")
foreach (header ${lib_install_headers_HEADERS} )
if(IS_ABSOLUTE "${header}")
if(NOT IS_ABSOLUTE "${lib_install_headers_REMOVE_INCLUDE_DIR_PREFIX}")
message(FATAL_ERROR "HEADERS values and REMOVE_INCLUDE_DIR_PREFIX "
"must both be either absolute or relative")
endif()
file(RELATIVE_PATH
header_path
${lib_install_headers_REMOVE_INCLUDE_DIR_PREFIX}
${header}
)
else ()
if(IS_ABSOLUTE "${lib_install_headers_REMOVE_INCLUDE_DIR_PREFIX}")
message(FATAL_ERROR "HEADERS values and REMOVE_INCLUDE_DIR_PREFIX "
"must both be either absolute or relative")
endif()
# We must provide absolute path for `file(RELATIVE_PATH ...)`
# to work, so we just take CMAKE_CURRENT_SOURCE_DIR to construct absolute
# path values.
file(RELATIVE_PATH
header_path
${CMAKE_CURRENT_SOURCE_DIR}/${lib_install_headers_REMOVE_INCLUDE_DIR_PREFIX}
${CMAKE_CURRENT_SOURCE_DIR}/${header}
)
endif ()
get_filename_component(relative_to_include_dir ${header_path} PATH )
install( FILES ${header}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${relative_to_include_dir} )
endforeach ()
endfunction()