-
Notifications
You must be signed in to change notification settings - Fork 1
/
CDeploy
330 lines (286 loc) · 13.1 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
function(deploy_package package version)
include(CMakeParseArguments)
set(options NO_OS NO_ARCH NO_COMPILER NO_CACHE)
set(oneValueArgs)
set(multiValueArgs)
cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(MSVC)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
if(NOT "${config}" STREQUAL "Debug")
string(TOUPPER "${config}" config_upper)
set(CMAKE_MAP_IMPORTED_CONFIG_${config_upper} Release)
endif()
endforeach()
endif()
if(NOT __NO_CACHE)
find_package(${package} ${version} EXACT QUIET CONFIG NO_DEFAULT_PATH)
endif()
if(NOT ${package}_FOUND)
string(TOLOWER "${package}" filename)
set(filename "${filename}-${version}")
if(NOT __NO_OS)
set(filename "${filename}-${CDEPLOY_OS}")
endif()
if(NOT __NO_ARCH)
set(filename "${filename}-${CDEPLOY_ARCH}")
endif()
if(NOT __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}/.cmake/downloadcache")
endif()
endif()
endif()
set(cache_file "${CDEPLOY_CACHE_DIR}/${filename}")
if(NOT __NO_CACHE AND EXISTS "${cache_file}")
message("-- Found ${filename} in cache")
else()
set(repository "${__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(package_folder "${CMAKE_BINARY_DIR}/${_extract_dir}")
find_package(${package} ${version} EXACT QUIET CONFIG REQUIRED
PATHS "${package_folder}" NO_DEFAULT_PATH
)
endif()
endfunction()
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_COMPILER_IS_GNUCC) # CMAKE_CXX_COMPILER_ARCHITECTURE_ID does not provide anything with MinGW compilers
execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -v OUTPUT_VARIABLE GCC_VERSION_OUTPUT ERROR_VARIABLE GCC_VERSION_OUTPUT)
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_COMPILER_IS_GNUCC)
string(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "\\1" gcc_version "${CMAKE_CXX_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")
string(REGEX REPLACE "([0-9]+).*" "clang\\1" ${var} "${CMAKE_CXX_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})
else()
set(${var} ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION})
endif()
set(${var} ${${var}} PARENT_SCOPE)
endfunction()
function(get_target_os var)
if(WIN32)
set(${var} windows)
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(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
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
)
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 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(__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()