-
Notifications
You must be signed in to change notification settings - Fork 47
/
header.cmake
326 lines (298 loc) · 10.1 KB
/
header.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Copyright (C) 2008-2019 LAAS-CNRS, JRL AIST-CNRS, INRIA.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# .rst: .. ifmode:: user
#
# .. variable:: ${PROJECT_NAME}_HEADERS
#
# List of C++ header filenames. They will be installed automatically using
# :command:`HEADER_INSTALL`
#
# .rst: .. ifmode:: internal
#
# .. command:: _SETUP_PROJECT_HEADER
#
# This setup CMake to handle headers properly.
#
# 1. The `include` directory in the build and source trees is added to the
# include search path (see INCLUDE_DIRECTORIES). As always, the build
# directory has the priority over the source directory in case of conflict.
#
# However you *should not* have conflicting names for files which are both in
# the build and source trees. Conflicting names are filenames which differ only
# by a prefix:
#
# include/a.h vs _build/include/a.h src/a.h vs src/foo/a.h
#
# ...this files makes a project very fragile as the -I ordering will have a lot
# of importance and may break easily when using tools which may reorder the
# pre-processor flags such as pkg-config.
#
# 1. The headers are installed in the prefix in a way which preserves the
# directory structure.
#
# The directory name for header follows the rule: each non alpha-numeric
# character is replaced by a slash (`/`). In practice, it means that hpp-util
# will put its header in: ${CMAKE_INSTALL_PREFIX}/include/hpp/util
#
# This rule has been decided to homogenize headers location, however some
# packages do not follow this rule (dg-middleware for instance).
#
# In that case, CUSTOM_HEADER_DIR can be set to override this policy.
#
# Reminder: breaking the JRL/LAAS policies shoud be done after discussing the
# issue. You should at least open a ticket or send an e-mail to notify this
# behavior.
#
macro(_SETUP_PROJECT_HEADER)
# Install project headers.
if(DEFINED PROJECT_CUSTOM_HEADER_DIR)
set(HEADER_DIR "${PROJECT_CUSTOM_HEADER_DIR}")
elseif(DEFINED CUSTOM_HEADER_DIR)
set(HEADER_DIR "${CUSTOM_HEADER_DIR}")
else()
string(REGEX REPLACE "[^a-zA-Z0-9]" "/" HEADER_DIR "${PROJECT_NAME}")
endif()
if(NOT DEFINED PROJECT_CUSTOM_HEADER_EXTENSION)
set(PROJECT_CUSTOM_HEADER_EXTENSION "hh")
endif(NOT DEFINED PROJECT_CUSTOM_HEADER_EXTENSION)
if(NOT DEFINED PROJECT_GENERATED_HEADERS_SKIP_CONFIG)
set(PROJECT_GENERATED_HEADERS_SKIP_CONFIG OFF)
endif()
if(NOT DEFINED PROJECT_GENERATED_HEADERS_SKIP_DEPRECATED)
set(PROJECT_GENERATED_HEADERS_SKIP_DEPRECATED OFF)
endif()
if(NOT DEFINED PROJECT_GENERATED_HEADERS_SKIP_WARNING)
set(PROJECT_GENERATED_HEADERS_SKIP_WARNING OFF)
endif()
string(TOLOWER "${HEADER_DIR}" "HEADER_DIR")
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" PACKAGE_CPPNAME "${PROJECT_NAME}")
string(TOLOWER "${PACKAGE_CPPNAME}" "PACKAGE_CPPNAME_LOWER")
string(TOUPPER "${PACKAGE_CPPNAME}" "PACKAGE_CPPNAME")
if(NOT PROJECT_GENERATED_HEADERS_SKIP_CONFIG)
# Generate config.hh header.
generate_configuration_header(
${HEADER_DIR} config.${PROJECT_CUSTOM_HEADER_EXTENSION}
${PACKAGE_CPPNAME} ${PACKAGE_CPPNAME_LOWER}_EXPORTS
)
endif()
if(NOT PROJECT_GENERATED_HEADERS_SKIP_DEPRECATED)
# Generate deprecated.hh header.
configure_file(
${PROJECT_JRL_CMAKE_MODULE_DIR}/deprecated.hh.cmake
${CMAKE_CURRENT_BINARY_DIR}/include/${HEADER_DIR}/deprecated.${PROJECT_CUSTOM_HEADER_EXTENSION}
@ONLY
)
if(INSTALL_GENERATED_HEADERS)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/include/${HEADER_DIR}/deprecated.${PROJECT_CUSTOM_HEADER_EXTENSION}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${HEADER_DIR}
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE
)
endif(INSTALL_GENERATED_HEADERS)
endif()
if(NOT PROJECT_GENERATED_HEADERS_SKIP_WARNING)
# Generate warning.hh header.
configure_file(
${PROJECT_JRL_CMAKE_MODULE_DIR}/warning.hh.cmake
${CMAKE_CURRENT_BINARY_DIR}/include/${HEADER_DIR}/warning.${PROJECT_CUSTOM_HEADER_EXTENSION}
@ONLY
)
if(INSTALL_GENERATED_HEADERS)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/include/${HEADER_DIR}/warning.${PROJECT_CUSTOM_HEADER_EXTENSION}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${HEADER_DIR}
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE
)
endif(INSTALL_GENERATED_HEADERS)
endif()
# Generate config.h header. This header, unlike the previous one is *not*
# installed and is generated in the top-level directory of the build tree.
# Therefore it must not be included by any distributed header.
configure_file(
${PROJECT_JRL_CMAKE_MODULE_DIR}/config.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/config.h
)
# Default include directories: - top-level build directory (for generated
# non-distributed headers). - include directory in the build tree (for
# generated, distributed headers). - include directory in the source tree
# (non-generated, distributed headers).
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/include
)
endmacro(_SETUP_PROJECT_HEADER)
# GENERATE_CONFIGURATION_HEADER
# -----------------------------
#
# This macro generates a configuration header. Macro parameters may be used to
# customize it.
#
# * HEADER_DIR : where to generate the header
# * FILENAME : how the file should be named
# * LIBRARY_NAME : CPP symbol prefix, should match the compiled library name
# * EXPORT_SYMBOL : controls the switch between symbol import/export
function(
GENERATE_CONFIGURATION_HEADER
HEADER_DIR
FILENAME
LIBRARY_NAME
EXPORT_SYMBOL
)
generate_configuration_header_v2(
INCLUDE_DIR
${CMAKE_CURRENT_BINARY_DIR}/include
HEADER_DIR
${HEADER_DIR}
FILENAME
${FILENAME}
LIBRARY_NAME
${LIBRARY_NAME}
EXPORT_SYMBOL
${EXPORT_SYMBOL}
)
endfunction(GENERATE_CONFIGURATION_HEADER)
# ~~~
# .rst: .. command:: GENERATE_CONFIGURATION_HEADER_V2 (
# INCLUDE_DIR <include_dir>
# HEADER_DIR <header_dir>
# FILENAME <filename>
# LIBRARY_NAME <library_name>
# EXPORT_SYBMOL <export_symbol>)
# ~~~
#
# This function generates a configuration header at
# ``<include_dir>/<header_dir>/<filename>``.
#
# If INSTALL_GENERATED_HEADERS is ON, the configuration header will be installed
# in
# ``${CMAKE_INSTALL_INCLUDEDIR}/<header_dir>``.
#
# :param INCLUDE_DIR: Include root directory (absolute).
#
# :param HEADER_DIR: Include sub directory.
#
# :param FILENAME: Configuration header name.
#
# :param LIBRARY_NAME: CPP symbol prefix, should match the compiled library
# name.
#
# :param EXPORT_SYMBOL: Controls the switch between symbol import/export.
function(GENERATE_CONFIGURATION_HEADER_V2)
set(options)
set(
oneValueArgs
INCLUDE_DIR
HEADER_DIR
FILENAME
LIBRARY_NAME
EXPORT_SYMBOL
)
set(multiValueArgs)
cmake_parse_arguments(
ARGS
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(${PROJECT_VERSION_MAJOR} MATCHES UNKNOWN)
set(PROJECT_VERSION_MAJOR_CONFIG ${ARGS_LIBRARY_NAME}_VERSION_UNKNOWN_TAG)
else()
set(PROJECT_VERSION_MAJOR_CONFIG ${PROJECT_VERSION_MAJOR})
endif()
if(${PROJECT_VERSION_MINOR} MATCHES UNKNOWN)
set(PROJECT_VERSION_MINOR_CONFIG ${ARGS_LIBRARY_NAME}_VERSION_UNKNOWN_TAG)
else()
set(PROJECT_VERSION_MINOR_CONFIG ${PROJECT_VERSION_MINOR})
endif()
if(${PROJECT_VERSION_PATCH} MATCHES UNKNOWN)
set(PROJECT_VERSION_PATCH_CONFIG ${ARGS_LIBRARY_NAME}_VERSION_UNKNOWN_TAG)
else()
set(PROJECT_VERSION_PATCH_CONFIG ${PROJECT_VERSION_PATCH})
endif()
# Set variables for configure_file command
set(EXPORT_SYMBOL ${ARGS_EXPORT_SYMBOL})
set(LIBRARY_NAME ${ARGS_LIBRARY_NAME})
# Generate the header.
configure_file(
${PROJECT_JRL_CMAKE_MODULE_DIR}/config.hh.cmake
${ARGS_INCLUDE_DIR}/${ARGS_HEADER_DIR}/${ARGS_FILENAME}
@ONLY
)
# Install it if requested.
if(INSTALL_GENERATED_HEADERS)
install(
FILES ${ARGS_INCLUDE_DIR}/${ARGS_HEADER_DIR}/${ARGS_FILENAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${ARGS_HEADER_DIR}
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE
)
endif()
endfunction(GENERATE_CONFIGURATION_HEADER_V2)
# _SETUP_PROJECT_HEADER_FINALIZE
# ------------------------------
#
# Post-processing of the header management step. Install public headers if
# required.
#
macro(_SETUP_PROJECT_HEADER_FINALIZE)
# If the header list is set, install it.
if(DEFINED ${PROJECT_NAME}_HEADERS)
header_install(${${PROJECT_NAME}_HEADERS})
endif(DEFINED ${PROJECT_NAME}_HEADERS)
endmacro(_SETUP_PROJECT_HEADER_FINALIZE)
# .rst: .. ifmode:: internal
#
# ~~~
# .. command:: HEADER_INSTALL (COMPONENT <component> <files>...)
# ~~~
#
# Install a list of headers.
#
# :param component: Component to forward to install command.
#
# :param files: Files to install.
macro(HEADER_INSTALL)
set(options)
set(oneValueArgs COMPONENT)
set(multiValueArgs)
cmake_parse_arguments(
ARGS
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(ARGS_COMPONENT)
set(_COMPONENT_NAME ${ARGS_COMPONENT})
else()
set(_COMPONENT_NAME ${CMAKE_INSTALL_DEFAULT_COMPONENT_NAME})
endif()
set(FILES ${ARGS_UNPARSED_ARGUMENTS})
foreach(FILE ${FILES})
get_filename_component(DIR "${FILE}" PATH)
string(REGEX REPLACE "${CMAKE_BINARY_DIR}" "" DIR "${DIR}")
string(REGEX REPLACE "${PROJECT_SOURCE_DIR}" "" DIR "${DIR}")
string(REGEX REPLACE "include(/|$)" "" DIR "${DIR}")
install(
FILES ${FILE}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${DIR}"
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE
COMPONENT ${_COMPONENT_NAME}
)
endforeach()
endmacro()