-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDeploy
390 lines (341 loc) · 16.7 KB
/
CDeploy
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
macro(deploy_package package version)
include(CMakeParseArguments)
cmake_parse_arguments(_deploy_package "NO_OS;NO_ARCH;NO_COMPILER;NO_CACHE;NO_CONFIG_MAPPING" "" "COMPONENTS" ${ARGN})
if(MSVC)
if(_deploy_package_NO_CONFIG_MAPPING)
foreach(_deploy_package_config ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
if(NOT "${_deploy_package_config}" STREQUAL "Debug")
string(TOUPPER "${_deploy_package_config}" _deploy_package_config)
unset(CMAKE_MAP_IMPORTED_CONFIG_${_deploy_package_config})
endif()
endforeach()
else()
foreach(_deploy_package_config ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
if(NOT "${_deploy_package_config}" STREQUAL "Debug")
string(TOUPPER "${_deploy_package_config}" _deploy_package_config)
set(CMAKE_MAP_IMPORTED_CONFIG_${_deploy_package_config} Release)
endif()
endforeach()
endif()
endif()
set(_deploy_package_find_package_args)
if(_deploy_package_COMPONENTS)
list(APPEND _deploy_package_find_package_args COMPONENTS ${_deploy_package_COMPONENTS})
endif()
if("${version}" MATCHES "[^\\-]+-.+")
string(REGEX REPLACE "([^\\-]+)-.+" "\\1" _deploy_package_find_version "${version}")
else()
set(_deploy_package_find_version "${version}")
endif()
if(NOT _deploy_package_NO_CACHE)
find_package(${package} ${_deploy_package_find_version} EXACT QUIET CONFIG ${_deploy_package_find_package_args} NO_DEFAULT_PATH)
endif()
if(NOT ${package}_FOUND)
function(_deploy_package_download)
foreach(component ${_deploy_package_COMPONENTS})
set(${package}${component}_DIR "${package}${component}_DIR-NOTFOUND" PARENT_SCOPE)
endforeach()
string(TOLOWER "${package}" filename)
set(filename "${filename}-${version}")
if(NOT _deploy_package_NO_OS)
set(filename "${filename}-${CDEPLOY_OS}")
endif()
if(NOT _deploy_package_NO_ARCH)
set(filename "${filename}-${CDEPLOY_ARCH}")
endif()
if(NOT _deploy_package_NO_COMPILER)
set(filename "${filename}-${CDEPLOY_COMPILER}")
endif()
set(filename "${filename}.zip")
if(NOT CDEPLOY_CACHE_DIR)
set(CDEPLOY_CACHE_DIR "$ENV{CDEPLOY_CACHE_DIR}")
if(NOT CDEPLOY_CACHE_DIR)
if(WIN32)
set(CDEPLOY_CACHE_DIR "$ENV{USERPROFILE}/.cmake/downloadcache")
else()
set(CDEPLOY_CACHE_DIR "$ENV{HOME}")
if(NOT CDEPLOY_CACHE_DIR OR "${CDEPLOY_CACHE_DIR}" STREQUAL "/")
set(CDEPLOY_CACHE_DIR "/tmp")
else()
set(CDEPLOY_CACHE_DIR "${CDEPLOY_CACHE_DIR}/.cmake/downloadcache")
endif()
endif()
endif()
endif()
set(cache_file "${CDEPLOY_CACHE_DIR}/${filename}")
if(NOT _deploy_package_NO_CACHE AND EXISTS "${cache_file}")
message("-- Found ${filename} in cache")
else()
set(repository "${_deploy_package_UNPARSED_ARGUMENTS}")
if(NOT repository)
if(CDEPLOY_REPOSITORY)
set(repository "${CDEPLOY_REPOSITORY}")
else()
set(repository "$ENV{CDEPLOY_REPOSITORY}")
endif()
endif()
set(url "${repository}/${filename}")
message("-- Downloading ${url}")
file(DOWNLOAD "${url}" "${cache_file}.part" STATUS download_status)
list(GET download_status 0 _download_result)
if(NOT ${_download_result} EQUAL 0)
list(GET download_status 1 _download_error)
message(FATAL_ERROR "Could not download: ${_download_error} (${_download_result})")
return()
endif()
file(RENAME "${cache_file}.part" "${cache_file}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E tar tf "${cache_file}"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
OUTPUT_VARIABLE _unzip_output
RESULT_VARIABLE _unzip_result)
if(NOT ${_unzip_result} EQUAL 0)
message(FATAL_ERROR "Could not extract file")
return()
endif()
string(REGEX REPLACE "([^/]+).*" "\\1" _extract_dir "${_unzip_output}")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${cache_file}"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
RESULT_VARIABLE _unzip_result)
if(NOT ${_unzip_result} EQUAL 0)
message(FATAL_ERROR "Could not extract file")
return()
endif()
set(_deploy_package_package_folder "${CMAKE_BINARY_DIR}/${_extract_dir}" PARENT_SCOPE)
endfunction()
_deploy_package_download()
find_package(${package} ${_deploy_package_find_version} EXACT QUIET CONFIG REQUIRED
${_deploy_package_find_package_args}
PATHS "${_deploy_package_package_folder}" NO_DEFAULT_PATH
)
endif()
endmacro()
function(get_target_arch var)
set(${var} ${CMAKE_SYSTEM_PROCESSOR})
if(WIN32)
if(MSVC)
if(CMAKE_CL_64)
set(${var} x64)
else()
set(${var} x86)
endif()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") # CMAKE_CXX_COMPILER_ARCHITECTURE_ID does not provide the target arch with MinGW compilers
if(CMAKE_CXX_COMPILER)
execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -v OUTPUT_VARIABLE GCC_VERSION_OUTPUT ERROR_VARIABLE GCC_VERSION_OUTPUT)
else()
execute_process(COMMAND "${CMAKE_C_COMPILER}" -v OUTPUT_VARIABLE GCC_VERSION_OUTPUT ERROR_VARIABLE GCC_VERSION_OUTPUT)
endif()
if(GCC_VERSION_OUTPUT MATCHES ".*x86_64.*")
set(${var} x64)
else()
set(${var} x86)
endif()
endif()
endif()
set(${var} ${${var}} PARENT_SCOPE)
endfunction()
function(get_target_compiler var)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
string(REGEX REPLACE "[ ]?([0-9]+\\.[0-9]+).*" "\\1" gcc_version "${CMAKE_CXX_COMPILER_VERSION} ${CMAKE_C_COMPILER_VERSION}")
if(NOT gcc_version VERSION_LESS "5.0")
string(REGEX REPLACE "([0-9]+).*" "\\1" gcc_version "${gcc_version}")
endif()
set(${var} "gcc${gcc_version}")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
string(REGEX REPLACE "[ ]?([0-9]+).*" "clang\\1" ${var} "${CMAKE_CXX_COMPILER_VERSION} ${CMAKE_C_COMPILER_VERSION}")
elseif(MSVC)
set(MSVC_YEARS 2008 2010 2012 2013 2015 2017 2019)
set(MSVC_MSC_VERS 150 160 170 180 190 191 192)
string(SUBSTRING "${MSVC_VERSION}" 0 3 MSVC_VER_SHORT)
list(FIND MSVC_MSC_VERS ${MSVC_VER_SHORT} MSVC_INDEX)
list(GET MSVC_YEARS ${MSVC_INDEX} MSVS_YEAR)
set(${var} vs${MSVS_YEAR})
elseif(CMAKE_CXX_COMPILER_ID)
string(TOLOWER "${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}" ${var})
else()
string(TOLOWER "${CMAKE_C_COMPILER_ID}${CMAKE_C_COMPILER_VERSION}" ${var})
endif()
set(${var} ${${var}} PARENT_SCOPE)
endfunction()
function(get_target_os var)
if(WIN32)
set(${var} windows)
elseif(APPLE)
set(${var} macos)
else()
file(GLOB ETC_RELEASE_FILES /etc/*-release)
list(REMOVE_ITEM ETC_RELEASE_FILES /etc/lsb-release)
list(GET ETC_RELEASE_FILES 0 ETC_RELEASE_FILE)
file(READ "${ETC_RELEASE_FILE}" ETC_RELEASE_FILE_CONTENT)
string(REGEX REPLACE "=|\n" ";" ETC_RELEASE_FILE_CONTENT "${ETC_RELEASE_FILE_CONTENT}")
string(REGEX REPLACE "; +| +;" ";" ETC_RELEASE_FILE_CONTENT "${ETC_RELEASE_FILE_CONTENT}")
list(FIND ETC_RELEASE_FILE_CONTENT "NAME" NAME_INDEX)
if(NAME_INDEX GREATER -1)
math(EXPR NAME_INDEX "${NAME_INDEX} + 1")
list(GET ETC_RELEASE_FILE_CONTENT "${NAME_INDEX}" ETC_RELEASE_NAME)
else()
list(GET ETC_RELEASE_FILE_CONTENT 0 ETC_RELEASE_NAME)
endif()
string(REGEX MATCH "[^ \"]+" ETC_RELEASE_NAME "${ETC_RELEASE_NAME}")
list(FIND ETC_RELEASE_FILE_CONTENT "VERSION_ID" VERSION_INDEX)
if(NOT VERSION_INDEX GREATER -1)
list(FIND ETC_RELEASE_FILE_CONTENT "VERSION" VERSION_INDEX)
endif()
if(VERSION_INDEX GREATER -1)
math(EXPR VERSION_INDEX "${VERSION_INDEX} + 1")
list(GET ETC_RELEASE_FILE_CONTENT "${VERSION_INDEX}" ETC_RELEASE_VERSION)
string(REGEX MATCH "[^ \"]+" ETC_RELEASE_VERSION "${ETC_RELEASE_VERSION}")
else()
string(REGEX MATCH "[0-9]+" ETC_RELEASE_VERSION "${ETC_RELEASE_FILE_CONTENT}")
endif()
string(TOLOWER "${ETC_RELEASE_NAME}${ETC_RELEASE_VERSION}" ${var})
endif()
set(${var} ${${var}} PARENT_SCOPE)
endfunction()
get_target_arch(CDEPLOY_ARCH)
get_target_compiler(CDEPLOY_COMPILER)
get_target_os(CDEPLOY_OS)
set(CPACK_GENERATOR "ZIP")
string(TOLOWER "${PROJECT_NAME}" CPACK_PACKAGE_FILE_NAME)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${PROJECT_VERSION}")
if(CDEPLOY_PACKAGE_REVISION)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CDEPLOY_PACKAGE_REVISION}")
endif()
if(NOT CDEPLOY_NO_OS)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CDEPLOY_OS}")
endif()
if(NOT CDEPLOY_NO_ARCH)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CDEPLOY_ARCH}")
endif()
if(NOT CDEPLOY_NO_COMPILER)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CDEPLOY_COMPILER}")
endif()
if(MSVC AND NOT CDEPLOY_NO_DEBUG_BUILD)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER ".cmake")
include(ExternalProject)
ExternalProject_Add(DEBUG_BUILD
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build-debug"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-debug" -DCDEPLOY_NO_DEBUG_BUILD=True -DCMAKE_BUILD_TYPE=Debug
BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-debug" --config Debug
INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-debug" --config Debug --target install
BUILD_ALWAYS True
)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug/" DESTINATION . USE_SOURCE_PERMISSIONS)
set_property(TARGET DEBUG_BUILD PROPERTY FOLDER ".cmake")
if(NOT CDEPLOY_DEBUG_BUILD)
set_property(TARGET DEBUG_BUILD PROPERTY EXCLUDE_FROM_DEFAULT_BUILD True)
set_property(TARGET DEBUG_BUILD PROPERTY EXCLUDE_FROM_ALL True)
endif()
endif()
if(CDEPLOY_DEBUG_BUILD)
endif()
if(MSVC)
set(CMAKE_DEBUG_POSTFIX d)
endif()
file(REMOVE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in")
function(deploy_export_init)
if(NOT EXISTS "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in")
file(WRITE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "@PACKAGE_INIT@\n\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "set_and_check(INSTALL_DIR \"@PACKAGE_INSTALL_DIR@\")\n\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "include(CMakeFindDependencyMacro)\n\n")
endif()
endfunction()
function(deploy_export_dependency name)
deploy_export_init()
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "find_dependency(${name} ${ARGN})\n\n")
endfunction()
function(deploy_export name)
include(CMakeParseArguments)
set(options INTERFACE LIBRARY STATIC SHARED EXECUTABLE)
set(oneValueArgs CONFIGURATION IMPORTED_LOCATION IMPORTED_IMPLIB)
set(multiValueArgs INTERFACE_INCLUDE_DIRECTORIES INTERFACE_SOURCES INTERFACE_COMPILE_DEFINITIONS INTERFACE_COMPILE_FEATURES INTERFACE_COMPILE_OPTIONS INTERFACE_LINK_LIBRARIES PROPERTIES)
cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
deploy_export_init()
set(target_flags)
if(__STATIC)
set(target_flags "${target_flags} STATIC")
endif()
if(__SHARED)
set(target_flags "${target_flags} SHARED")
endif()
if(__INTERFACE)
set(target_flags "${target_flags} INTERFACE")
endif()
set(target_config)
if(__CONFIGURATION)
string(TOUPPER "${__CONFIGURATION}" __CONFIGURATION)
set(target_config "_${__CONFIGURATION}")
endif()
set(target_properties)
if(__IMPORTED_LOCATION)
set(target_properties "${target_properties} IMPORTED_LOCATION${target_config} \"\${INSTALL_DIR}/${__IMPORTED_LOCATION}\"")
endif()
if(__IMPORTED_IMPLIB)
set(target_properties "${target_properties} IMPORTED_IMPLIB${target_config} \"\${INSTALL_DIR}/${__IMPORTED_IMPLIB}\"")
endif()
if(__INTERFACE_INCLUDE_DIRECTORIES)
set(target_properties "${target_properties} INTERFACE_INCLUDE_DIRECTORIES \"")
foreach(dir ${__INTERFACE_INCLUDE_DIRECTORIES})
set(target_properties "${target_properties}\${INSTALL_DIR}/${dir};")
endforeach()
set(target_properties "${target_properties}\"")
endif()
if(__INTERFACE_SOURCES)
set(target_properties "${target_properties} INTERFACE_SOURCES \"")
foreach(file ${__INTERFACE_SOURCES})
set(target_properties "${target_properties}\${INSTALL_DIR}/${file};")
endforeach()
set(target_properties "${target_properties}\"")
endif()
foreach(property INTERFACE_COMPILE_DEFINITIONS INTERFACE_COMPILE_FEATURES INTERFACE_COMPILE_OPTIONS INTERFACE_LINK_LIBRARIES)
if(__${property})
set(target_properties "${target_properties} ${property} \"")
foreach(arg ${__${property}})
set(target_properties "${target_properties}${arg};")
endforeach()
set(target_properties "${target_properties}\"")
endif()
endforeach()
if(__PROPERTIES)
foreach(arg ${__PROPERTIES})
set(target_properties "${target_properties} ${arg}")
endforeach()
endif()
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "if(NOT TARGET ${PROJECT_NAME}::${name})\n")
if(__LIBRARY OR __INTERFACE)
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" " add_library(${PROJECT_NAME}::${name} ${target_flags} IMPORTED GLOBAL)\n")
else()
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" " add_executable(${PROJECT_NAME}::${name} IMPORTED GLOBAL)\n")
endif()
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "endif()\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "set_target_properties(${PROJECT_NAME}::${name}\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" " PROPERTIES\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" " ${target_properties}\n")
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" ")\n")
if(__CONFIGURATION)
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "set_property(TARGET ${PROJECT_NAME}::${name} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${__CONFIGURATION})\n")
endif()
file(APPEND "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "\n")
endfunction()
function(install_deploy_export)
include(CMakePackageConfigHelpers)
if(EXISTS "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in")
set(INSTALL_DIR .)
configure_package_config_file("${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake.in" "${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}"
PATH_VARS
INSTALL_DIR
)
install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "lib/cmake/${PROJECT_NAME}")
else()
install(EXPORT ${PROJECT_NAME}Config
DESTINATION "lib/cmake/${PROJECT_NAME}"
NAMESPACE ${PROJECT_NAME}::
)
endif()
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" COMPATIBILITY ExactVersion)
install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "lib/cmake/${PROJECT_NAME}")
endfunction()