-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThing.cmake
437 lines (402 loc) · 15.8 KB
/
Thing.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#
# Copyright (C) Cybernetica
#
# Research/Commercial License Usage
# Licensees holding a valid Research License or Commercial License
# for the Software may use this file according to the written
# agreement between you and Cybernetica.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3.0 as published by the Free Software
# Foundation and appearing in the file LICENSE.GPL included in the
# packaging of this file. Please review the following information to
# ensure the GNU General Public License version 3.0 requirements will be
# met: http://www.gnu.org/copyleft/gpl-3.0.html.
#
# For further information, please contact us at [email protected].
#
IF(CMAKE_VERSION VERSION_LESS 2.8.12)
MESSAGE(FATAL_ERROR "Thing: CMake 2.8.12 or later required, but version ${CMAKE_VERSION} found!")
ENDIF()
INCLUDE(ExternalProject)
INCLUDE(Thing_gitDownload.cmake)
SET(Thing_VoidCommand ${CMAKE_COMMAND} -E echo_append)
MACRO(ThingNewEmptyList name)
IF(DEFINED "${name}")
MESSAGE(FATAL_ERROR "CMake variable ${name} is already defined!")
ENDIF()
SET("${name}" tmp)
LIST(REMOVE_ITEM "${name}" tmp)
ENDMACRO()
# Joins all items of a list to a string using the given glue:
FUNCTION(Thing_join output glue)
STRING(REGEX REPLACE "([^\\]|^);" "\\1${glue}" tmp "${ARGN}")
STRING(REGEX REPLACE "[\\](.)" "\\1" tmp "${tmp}")
SET(${output} "${tmp}" PARENT_SCOPE)
ENDFUNCTION()
FUNCTION(Thing_pathJoin output)
IF(WIN32)
Thing_join(out ";" ${ARGN})
ELSE()
Thing_join(out ":" ${ARGN})
ENDIF()
SET(${output} "${out}" PARENT_SCOPE)
ENDFUNCTION()
IF(NOT DEFINED Thing_EXTRA_KEYWORDS)
SET(Thing_EXTRA_KEYWORDS "")
ENDIF()
SET(Thing__stoppers
# General options:
"DEPENDS" "PREFIX" "LIST_SEPARATOR" "TMP_DIR" "STAMP_DIR" "EXCLUDE_FROM_ALL"
# Download step options:
"DOWNLOAD_NAME" "DOWNLOAD_DIR" "DOWNLOAD_COMMAND" "DOWNLOAD_NO_PROGRESS"
"CVS_REPOSITORY" "CVS_MODULE" "CVS_TAG"
"SVN_REPOSITORY" "SVN_REVISION" "SVN_USERNAME" "SVN_PASSWORD" "SVN_TRUST_CERT"
"GIT_REPOSITORY" "GIT_TAG" "GIT_SUBMODULES"
"HG_REPOSITORY" "HG_TAG"
"URL" "URL_HASH" "URL_MD5"
"TLS_VERIFY" "TLS_CAINFO"
"TIMEOUT"
# Update/patch step options:
"UPDATE_COMMAND" "UPDATE_DISCONNECTED" "PATCH_COMMAND"
# Configure step options:
"SOURCE_DIR" "CONFIGURE_COMMAND" "CMAKE_COMMAND" "CMAKE_GENERATOR"
"CMAKE_GENERATOR_PLATFORM" "CMAKE_GENERATOR_TOOLSET" "CMAKE_ARGS"
"CMAKE_CACHE_ARGS" "CMAKE_CACHE_DEFAULT_ARGS"
# Build step options:
"BINARY_DIR" "BUILD_COMMAND" "BUILD_IN_SOURCE" "BUILD_ALWAYS"
"BUILD_BYPRODUCTS"
# Install step options:
"INSTALL_DIR" "INSTALL_COMMAND"
# Test step options:
"TEST_BEFORE_INSTALL" "TEST_AFTER_INSTALL" "TEST_EXCLUDE_FROM_MAIN"
"TEST_COMMAND"
# Output logging options:
"LOG_DOWNLOAD" "LOG_UPDATE" "LOG_CONFIGURE" "LOG_BUILD" "LOG_TEST"
"LOG_INSTALL"
# Terminal access options:
"USES_TERMINAL_DOWNLOAD" "USES_TERMINAL_UPDATE" "USES_TERMINAL_CONFIGURE"
"USES_TERMINAL_BUILD" "USES_TERMINAL_TEST" "USES_TERMINAL_INSTALL"
# Other options:
"STEP_TARGETS" "INDEPENDENT_STEP_TARGETS"
# Thing options:
"SHALLOW_CLONE"
)
MACRO(Thing_addKeywords)
LIST(APPEND Thing_EXTRA_KEYWORDS ${ARGN})
LIST(REMOVE_DUPLICATES Thing_EXTRA_KEYWORDS)
ENDMACRO()
FUNCTION(Thing_getKeywords out)
SET(allKeywords ${Thing__stoppers})
LIST(APPEND allKeywords ${Thing_EXTRA_KEYWORDS})
SET(${out} ${allKeywords} PARENT_SCOPE)
ENDFUNCTION()
# Returns all the values from a given ExternalProject_Add arguments group under
# the given argument group name, concatenated:
FUNCTION(Thing_getArgs_concat inArgs argName outArgs)
Thing_getKeywords(stoppers)
LIST(FIND stoppers "${argName}" isStopper)
IF(${isStopper} EQUAL -1)
MESSAGE(WARNING "Thing: Unknown argument name given, but continuing anyway: ${argName}")
ENDIF()
SET(SKIP 1)
ThingNewEmptyList(tmp)
FOREACH(a IN LISTS inArgs)
IF("${a}$" STREQUAL "${argName}$")
SET(SKIP 0)
ELSEIF(NOT SKIP)
LIST(FIND stoppers "${a}" isStopper)
IF(NOT (${isStopper} EQUAL -1))
SET(SKIP 1)
ELSE()
LIST(APPEND tmp "${a}")
ENDIF()
ENDIF()
ENDFOREACH()
SET(${outArgs} "${tmp}" PARENT_SCOPE)
ENDFUNCTION()
# Removes all given argument groups from a list of ExternalProject_Add arguments:
FUNCTION(Thing_removeArgs inArgs argName outArgs)
Thing_getKeywords(stoppers)
LIST(FIND stoppers "${argName}" isStopper)
IF(${isStopper} EQUAL -1)
MESSAGE(WARNING "Thing: Unknown argument name given, but continuing anyway: ${argName}")
ENDIF()
SET(SKIP 0)
SET(tmp "")
FOREACH(a IN LISTS inArgs)
IF("${a}$" STREQUAL "${argName}$")
SET(SKIP 1)
ELSEIF(SKIP)
LIST(FIND stoppers "${a}" isStopper)
IF(NOT (${isStopper} EQUAL -1))
SET(SKIP 0)
LIST(APPEND tmp "${a}")
ENDIF()
ELSE()
LIST(APPEND tmp "${a}")
ENDIF()
ENDFOREACH()
SET(${outArgs} "${tmp}" PARENT_SCOPE)
ENDFUNCTION()
# Resets a given argument in a list of ExternalProject_Add arguments:
FUNCTION(Thing_resetArgs inArgs argName newValues outArgs)
Thing_removeArgs("${inArgs}" "${argName}" args)
LIST(APPEND args "${argName}" "${newValues}")
SET(${outArgs} "${args}" PARENT_SCOPE)
ENDFUNCTION()
# Returns whether the given argument list contains the given argument:
FUNCTION(Thing_hasArg args argName out)
Thing_getKeywords(stoppers)
LIST(FIND stoppers "${argName}" isStopper)
IF(${isStopper} EQUAL -1)
MESSAGE(WARNING "Thing: Unknown argument name given, but continuing anyway: ${argName}")
ENDIF()
LIST(FIND args "${argName}" hasArg)
IF(${hasArg} EQUAL -1)
SET("${out}" FALSE PARENT_SCOPE)
ELSE()
SET("${out}" TRUE PARENT_SCOPE)
ENDIF()
ENDFUNCTION()
# Sets an argument if it is not already set:
FUNCTION(Thing_setDefaultArg out args arg)
Thing_hasArg("${args}" "${arg}" hasArg)
IF("${hasArg}")
SET(${out} "${args}" PARENT_SCOPE)
ELSE()
Thing_resetArgs("${args}" "${arg}" "${ARGN}" o)
SET("${out}" "${o}" PARENT_SCOPE)
ENDIF()
ENDFUNCTION()
# Constructs a new ExternalProject_Add argument list for an out-of-tree
# configure and build, e.g configure/cmake && make && make install:
FUNCTION(Thing_remoteConfigure name origArgs buildDir sourceDir outArgs)
SET(argsToRemove DOWNLOAD_NAME DOWNLOAD_DIR DOWNLOAD_COMMAND CVS_REPOSITORY
CVS_MODULE CVS_TAG SVN_REPOSITORY SVN_REVISION SVN_USERNAME
SVN_PASSWORD SVN_TRUST_CERT GIT_REPOSITORY GIT_TAG
HG_REPOSITORY HG_TAG URL URL_HASH URL_MD5 TLS_VERIFY
TLS_CAINFO TIMEOUT UPDATE_COMMAND PATCH_COMMAND SOURCE_DIR
BINARY_DIR SHALLOW_CLONE)
FOREACH(argToRemove IN LISTS argsToRemove)
Thing_removeArgs("${origArgs}" "${argToRemove}" origArgs)
ENDFOREACH()
LIST(APPEND origArgs
"DOWNLOAD_COMMAND" "${Thing_VoidCommand}"
"UPDATE_COMMAND" "${Thing_VoidCommand}"
"PATCH_COMMAND" "${Thing_VoidCommand}"
"BINARY_DIR" "${buildDir}"
"SOURCE_DIR" "${sourceDir}")
SET(${outArgs} "${origArgs}" PARENT_SCOPE)
ENDFUNCTION()
# Constructs a new ExternalProject_Add argument list for an out-of-tree build,
# e.g. make && make install:
FUNCTION(Thing_remoteBuild name origArgs buildDir sourceDir outArgs)
SET(argsToRemove DOWNLOAD_NAME DOWNLOAD_DIR DOWNLOAD_COMMAND CVS_REPOSITORY
CVS_MODULE CVS_TAG SVN_REPOSITORY SVN_REVISION SVN_USERNAME
SVN_PASSWORD SVN_TRUST_CERT GIT_REPOSITORY GIT_TAG
HG_REPOSITORY HG_TAG URL URL_HASH URL_MD5 TLS_VERIFY
TLS_CAINFO TIMEOUT UPDATE_COMMAND PATCH_COMMAND SOURCE_DIR
CONFIGURE_COMMAND CMAKE_COMMAND CMAKE_GENERATOR
CMAKE_GENERATOR_TOOLSET CMAKE_ARGS CMAKE_CACHE_ARGS
BINARY_DIR SHALLOW_CLONE)
FOREACH(argToRemove IN LISTS argsToRemove)
Thing_removeArgs("${origArgs}" "${argToRemove}" origArgs)
ENDFOREACH()
LIST(APPEND origArgs
"DOWNLOAD_COMMAND" "${Thing_VoidCommand}"
"UPDATE_COMMAND" "${Thing_VoidCommand}"
"PATCH_COMMAND" "${Thing_VoidCommand}"
"CONFIGURE_COMMAND" "${Thing_VoidCommand}"
"BINARY_DIR" "${buildDir}"
"SOURCE_DIR" "${sourceDir}")
SET(${outArgs} "${origArgs}" PARENT_SCOPE)
ENDFUNCTION()
FUNCTION(Thing_addFromOverride name)
IF(TARGET "${name}")
SET(m "Thing_addFromOverride() called multiple times for target")
MESSAGE(FATAL_ERROR "${m} \"${name}\"!!! Aborting.")
ENDIF()
Thing_getArgs_concat("${ARGN}" DEPENDS deps)
Thing_removeArgs("${ARGN}" DEPENDS newArgs)
IF(deps)
LIST(APPEND newArgs DEPENDS)
FOREACH(dep IN LISTS deps)
LIST(FIND Thing_SKIP "${dep}" depFound)
IF(${depFound} EQUAL -1)
LIST(APPEND newArgs "${dep}")
ENDIF()
ENDFOREACH()
ENDIF()
IF("${CMAKE_VERSION}" VERSION_LESS "2.8.10")
Thing_removeArgs("${newArgs}" URL_HASH newArgs)
ELSE()
Thing_removeArgs("${newArgs}" URL_MD5 newArgs)
ENDIF()
Thing_getArgs_concat("${newArgs}" GIT_REPOSITORY git_repository)
Thing_getArgs_concat("${newArgs}" SHALLOW_CLONE shallow_clone)
# Remove all non-ExternalProject keywords:
Thing_removeArgs("${newArgs}" SHALLOW_CLONE newArgs)
FOREACH(keyword IN LISTS Thing_EXTRA_KEYWORDS)
Thing_removeArgs("${newArgs}" "${keyword}" newArgs)
ENDFOREACH()
IF(NOT git_repository)
# Add a non git external dependency using ExternalProject_Add with a dummy UPDATE_COMMAND
# Otherwise UPDATE_COMMAND for http dependencies defaults to none causing changes to installed files not to trigger a reinstall
ExternalProject_Add("${name}" "${newArgs}" UPDATE_COMMAND ${CMAKE_COMMAND} -E echo Update)
ELSE()
Thing_removeArgs("${newArgs}" UPDATE_COMMAND newArgs)
Thing_removeArgs("${newArgs}" DOWNLOAD_COMMAND newArgs)
ExternalProject_Add("${name}" "${newArgs}" UPDATE_COMMAND ${Thing_VoidCommand} DOWNLOAD_COMMAND ${Thing_VoidCommand})
ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
GET_FILENAME_COMPONENT(src_name "${source_dir}" NAME)
GET_FILENAME_COMPONENT(work_dir "${source_dir}" PATH)
Thing_getArgs_concat("${newArgs}" DOWNLOAD_COMMAND download_command)
SET(download_comment)
IF(download_command)
SET(Thing_GitDownloadCommand ${download_command})
ELSEIF(shallow_clone)
SET(Thing_GitDownloadCommand "git clone --depth 1 ${git_repository} ${src_name}")
SET(download_comment "Performing newGitDownload step (git clone --depth 1) for '${name}'")
ELSE()
SET(Thing_GitDownloadCommand "git clone ${git_repository} ${src_name}")
SET(download_comment "Performing newGitDownload step (git clone) for '${name}'")
ENDIF()
Thing_getArgs_concat("${newArgs}" GIT_TAG git_tag)
IF(NOT git_tag)
SET(git_tag "master")
ENDIF()
Thing_getArgs_concat("${newArgs}" UPDATE_COMMAND update_command)
IF(update_command)
SET(Thing_GitUpdateCommand ${update_command})
ELSE()
SET(Thing_GitUpdateCommand "git show-ref --heads ${git_tag} && git fetch && git checkout ${git_tag} && git merge --ff-only --no-verify-signatures origin/${git_tag} || true")
ENDIF()
Thing_getArgs_concat("${newArgs}" LOG_DOWNLOAD log_download)
SET(depends)
# For the download step, and the git clone operation, only the repository
# should be recorded in a configured RepositoryInfo file. If the repo
# changes, the clone script should be run again. But if only the tag
# changes, avoid running the clone script again. Let the 'always' running
# update step checkout the new tag.
#
SET(module)
SET(tag)
IF("${CMAKE_VERSION}" VERSION_LESS "3.23.0")
CONFIGURE_FILE(
"${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
"${stamp_dir}/${name}-gitinfo.txt"
@ONLY
)
ELSE()
CONFIGURE_FILE(
"${CMAKE_ROOT}/Modules/ExternalProject/RepositoryInfo.txt.in"
"${stamp_dir}/${name}-gitinfo.txt"
@ONLY
)
ENDIF()
Thing_writeGitDownloadScript(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
git ${git_repository} ${git_tag} ${src_name} ${work_dir}
${stamp_dir}/${name}-gitinfo.txt ${stamp_dir}/${name}-gitclone-lastrun.txt
${Thing_GitDownloadCommand}
)
SET(download_cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
LIST(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
IF(log_download)
SET(log LOG log_download)
ELSE()
SET(log "")
ENDIF()
ExternalProject_Add_Step(
"${name}" newGitDownload
COMMENT ${download_comment}
COMMAND ${download_cmd}
WORKING_DIRECTORY ${work_dir}
DEPENDEES mkdir
DEPENDERS download
${log})
Thing_getArgs_concat("${newArgs}" LOG_UPDATE log_update)
IF(log_update)
SET(log LOG log_update)
ELSE()
SET(log "")
ENDIF()
ExternalProject_Add_Step(
"${name}" newGitUpdate
COMMAND sh -c ${Thing_GitUpdateCommand}
DEPENDEES download
DEPENDERS update
ALWAYS 1
WORKING_DIRECTORY "${source_dir}"
${log})
ENDIF()
ENDFUNCTION()
SET(Thing_Polymorphisms_DIR
"${CMAKE_CURRENT_BINARY_DIR}/ThingPolymorphisms")
FILE(MAKE_DIRECTORY "${Thing_Polymorphisms_DIR}")
MACRO(Thing_call cmdName expr)
FILE(WRITE "${Thing_Polymorphisms_DIR}/${Thing__cmdName}.cmake"
"${cmdName}(${expr})")
INCLUDE("${Thing_Polymorphisms_DIR}/${Thing__cmdName}.cmake")
ENDMACRO()
SET(Thing_ListSeparator "~~...~~")
FUNCTION(Thing_add name)
IF(DEFINED Thing_add_HELP_I_am_trapped_in_a_recursive_loop)
SET(m "Unexpected Thing_add() recursion detected!!! Did you call")
SET(m "${m} Thing_add() from inside of Thing_OVERRIDE()? - Call")
MESSAGE(FATAL_ERROR "${m} Thing_addFromOverride() instead. Aborting.")
ENDIF()
SET(Thing_add_HELP_I_am_trapped_in_a_recursive_loop "round-round-round we go")
IF(TARGET "${name}")
SET(m "Thing_add() called multiple times for target")
MESSAGE(FATAL_ERROR "${m} \"${name}\"!!! Aborting.")
ENDIF()
LIST(FIND Thing_SKIP "${name}" isSkipped)
IF(${isSkipped} EQUAL -1)
IF(NOT CMAKE_BUILD_TYPE)
SET(bType "Debug")
ELSE()
SET(bType "${CMAKE_BUILD_TYPE}")
ENDIF()
SET(args "${ARGN}")
Thing_setDefaultArg(args "${args}" "LIST_SEPARATOR"
"${Thing_ListSeparator}")
Thing_getArgs_concat("${args}" "LIST_SEPARATOR" separator)
Thing_setDefaultArg(args "${args}" "PREFIX"
"${CMAKE_CURRENT_BINARY_DIR}/${name}")
Thing_setDefaultArg(args "${args}" "INSTALL_DIR"
"${CMAKE_INSTALL_PREFIX}")
Thing_setDefaultArg(args "${args}" "SOURCE_DIR"
"${CMAKE_CURRENT_SOURCE_DIR}/${name}")
Thing_join(prefixPaths "${separator}" ${CMAKE_PREFIX_PATH})
Thing_setDefaultArg(args "${args}" "CMAKE_ARGS"
"-DCMAKE_PREFIX_PATH=${prefixPaths}"
"-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
"-DCMAKE_BUILD_TYPE=${bType}")
FOREACH(override IN LISTS Thing_ARGUMENT_OVERRIDES)
IF(COMMAND "${override}")
Thing_call("${override}" "\"${name}\" \"${args}\" args")
ELSE()
MESSAGE(WARNING "Thing: \"${override}\" in argument overrides is not a command!")
ENDIF()
ENDFOREACH()
IF(COMMAND "Thing_OVERRIDE")
Thing_OVERRIDE("${name}" "${args}")
ELSE()
Thing_addFromOverride("${name}" "${args}")
ENDIF()
ExternalProject_Get_Property("${name}" install_dir)
LIST(FIND CMAKE_PREFIX_PATH "${install_dir}" isInPrefixPath)
IF(${isInPrefixPath} EQUAL -1)
LIST(APPEND CMAKE_PREFIX_PATH "${install_dir}")
ENDIF()
SET(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE)
SET(Thing_installDir_${name} "${install_dir}" PARENT_SCOPE)
ENDIF()
UNSET(Thing_add_HELP_I_am_trapped_in_a_recursive_loop)
ENDFUNCTION()
FUNCTION(Thing_getProperty name property out)
ExternalProject_Get_Property("${name}" "${property}")
SET("${out}" "${${property}}" PARENT_SCOPE)
ENDFUNCTION()