-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeMM.cmake
326 lines (289 loc) · 12.7 KB
/
CMakeMM.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
include_guard(GLOBAL)
if(NOT COMMAND colors)
# Colorize
macro(colors)
if(NOT WIN32)
string(ASCII 27 Esc)
set(Reset "${Esc}[m")
set(BoldRed "${Esc}[1;31m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldYellow "${Esc}[1;33m")
set(BoldGreen "${Esc}[1;32m")
endif()
endmacro()
endif()
# Test if raw from github is available
macro(check_accessible)
file(DOWNLOAD "https://raw.githubusercontent.com/TestingRepositories/Ping/master/Ping.cmake" "${CMAKE_CURRENT_BINARY_DIR}/Ping.cmake" STATUS PING_STATUS)
list(GET PING_STATUS 0 PING_RC)
list(GET PING_STATUS 1 PING_MSG)
if(${PING_RC})
set(IS_ONLINE_RAW FALSE)
set(IS_OFFLINE_RAW TRUE)
else()
include(${CMAKE_CURRENT_BINARY_DIR}/Ping.cmake)
if(${PING_RESPONSE} STREQUAL "Pong")
set(IS_ONLINE_RAW TRUE)
set(IS_OFFLINE_RAW FALSE)
else()
set(IS_ONLINE_RAW FALSE)
set(IS_OFFLINE_RAW TRUE)
endif()
unset(PING_RESPONSE)
endif()
endmacro()
# Do the update check.
function(cmmm_changes CHANGELOG_VERSION)
if(${CMMM_VERSION} VERSION_LESS ${CHANGELOG_VERSION})
message("${BoldYellow} - Changes in ${CHANGELOG_VERSION} :${Reset}")
foreach(CMMM_CHANGE IN LISTS ARGN)
message("${BoldYellow} - ${CMMM_CHANGE}${Reset}")
endforeach()
endif()
endfunction()
function(print_changelog)
if(NOT ${CMMM_VERSION} STREQUAL "master" AND NOT ${CMMM_VERSION} STREQUAL "main")
if(NOT ${CMMM_VERSION} STREQUAL ${CMMM_LATEST_VERSION})
message("${BoldYellow}## [CMakeMM] Using CMakeMM version ${CMMM_VERSION}. The latest is ${CMMM_LATEST_VERSION}.${Reset}")
message("${BoldYellow} Changes since ${CMMM_VERSION} include the following :${Reset}")
changelog()
message("${BoldYellow} To update, simply change the value of VERSION in cmmm function.${Reset}")
message("${BoldYellow} You can disable these messages by setting IGNORE_NEW_VERSION in cmmm function. ##${Reset}")
endif()
endif()
endfunction()
function(cmmm_check_updates)
cmake_parse_arguments(CMMM "IGNORE_NEW_VERSION" "REPOSITORY" "" ${ARGN})
if(NOT IS_ONLINE_RAW AND "${CMMM_PROVIDER}" STREQUAL "github")
check_accessible()
message("${BoldYellow}## [CMakeMM] https://raw.githubusercontent.com not accessible; Use https://cdn.jsdelivr.net ##${Reset}")
endif()
if("${IS_ONLINE_RAW}" STREQUAL "TRUE" AND "${CMMM_PROVIDER}" STREQUAL "github")
set(CMMM_GIT_URL "https://raw.githubusercontent.com/${CMMM_REPOSITORY}/master")
elseif("${IS_ONLINE_RAW}" STREQUAL "FALSE" AND "${CMMM_PROVIDER}" STREQUAL "github")
set(CMMM_GIT_URL "https://cdn.jsdelivr.net/gh/${CMMM_REPOSITORY}@master")
elseif("${CMMM_PROVIDER}" STREQUAL "gitlab")
set(CMMM_GIT_URL "https://gitlab.com/${CMMM_REPOSITORY}/-/raw/master")
elseif("${CMMM_PROVIDER}" STREQUAL "gitee")
set(CMMM_GIT_URL "https://gitee.com/${CMMM_REPOSITORY}/raw/master")
endif()
# LatestVersion and Changelog must be up-to-date so must be in master
set(CMMM_CHANGELOG_FILE "${CMMM_DESTINATION}/Changelog.cmake")
set(CMMM_CHANGELOG_URL "${CMMM_GIT_URL}/Changelog.cmake")
file(DOWNLOAD "${CMMM_CHANGELOG_URL}" "${CMMM_CHANGELOG_FILE}" STATUS CMMM_STATUS TIMEOUT ${CMMM_TIMEOUT} INACTIVITY_TIMEOUT ${CMMM_INACTIVITY_TIMEOUT})
list(GET CMMM_STATUS 0 CMMM_RC)
if(${CMMM_RC} EQUAL 0)
include("${CMMM_CHANGELOG_FILE}")
if(${CMMM_VERSION} VERSION_LESS ${CMMM_LATEST_VERSION})
if(NOT ${CMMM_IGNORE_NEW_VERSION})
print_changelog()
endif()
endif()
else()
message("${BoldYellow}** Error while downloading file ${CMMM_CHANGELOG_URL} **${Reset}")
endif()
endfunction()
function(cmcm_module ARG_NAME)
cmake_parse_arguments(ARG "" "REMOTE;LOCAL;VERSION" "ALSO" "${ARGV}")
get_property(CMMM_NO_COLOR GLOBAL PROPERTY CMMM_NO_COLOR)
if(NOT ${CMMM_NO_COLOR})
colors()
endif()
if(NOT ARG_REMOTE AND NOT ARG_LOCAL)
message("${BoldRed}!! [CMakeCM] Either LOCAL or REMOTE is required for cmmm_module !!${Reset}")
message(FATAL_ERROR)
endif()
if(NOT ARG_VERSION)
message("${BoldRed}!! [CMakeCM] Expected a VERSION for cmmm_module !!${Reset}")
message(FATAL_ERROR)
endif()
file(MAKE_DIRECTORY "${CMMM_INSTALL_DESTINATION}")
get_property(CMMM_URL_MODULES GLOBAL PROPERTY CMMM_URL_MODULES)
if(ARG_REMOTE)
file(WRITE "${CMMM_INSTALL_DESTINATION}/${ARG_NAME}" "cmmm_include_module([[${ARG_NAME}]] [[${ARG_REMOTE}]] [[${ARG_VERSION}]] [[${ARG_ALSO}]])\n")
else()
file(WRITE "${CMMM_INSTALL_DESTINATION}/${ARG_NAME}" "cmmm_include_module([[${ARG_NAME}]] [[${CMMM_URL_MODULES}/${ARG_LOCAL}]] [[${ARG_VERSION}]] [[${ARG_ALSO}]])\n")
endif()
endfunction()
macro(cmmm_include_module MODULE_NAME MODULE_URL version also)
get_property(CMMM_NO_COLOR GLOBAL PROPERTY CMMM_NO_COLOR)
if(NOT ${CMMM_NO_COLOR})
colors()
endif()
get_property(CMMM_INSTALL_DESTINATION GLOBAL PROPERTY CMMM_DESTINATION)
get_property(CMMM_DESTINATION_MODULES GLOBAL PROPERTY CMMM_DESTINATION_MODULES)
get_property(CMMM_URL_MODULES GLOBAL PROPERTY CMMM_URL_MODULES)
get_filename_component(CMMM_RESOLVED_DIR "${CMMM_DESTINATION_MODULES}" ABSOLUTE)
get_filename_component(CMMM_RESOLVED "${CMMM_RESOLVED_DIR}/${MODULE_NAME}" ABSOLUTE)
get_filename_component(CMMM_RESOLVED_STAMP "${CMMM_INSTALL_DESTINATION}/${MODULE_NAME}.whence" ABSOLUTE)
set(CMMM_WHENCE_STRING "${CMMM_URL_MODULES}::${MODULE_URL}.${version}")
set(DOWNLOAD_MODULE FALSE)
if(EXISTS "${CMMM_RESOLVED}")
file(READ "${CMMM_RESOLVED_STAMP}" CMMM_STAMP)
if(NOT CMMM_STAMP STREQUAL CMMM_WHENCE_STRING)
set(DOWNLOAD_MODULE TRUE)
endif()
else()
set(DOWNLOAD_MODULE TRUE)
endif()
if(DOWNLOAD_MODULE)
file(MAKE_DIRECTORY "${CMMM_RESOLVED_DIR}")
message("${BoldMagenta}-- [CMakeCM] Downloading new module ${MODULE_NAME} --${Reset}")
file(DOWNLOAD "${MODULE_URL}" "${CMMM_RESOLVED}" STATUS DOWNLOAD_STATUS)
list(GET DOWNLOAD_STATUS 0 CODE)
list(GET DOWNLOAD_STATUS 1 MESSAGE)
if(CODE)
message("${BoldRed}!! [CMakeCM] Error while downloading file from '${MODULE_URL}' to '${CMMM_RESOLVED}' [${CODE}]: ${MESSAGE} !!${Reset}")
message(FATAL_ERROR)
endif()
file(WRITE "${CMMM_RESOLVED_STAMP}" "${CMMM_WHENCE_STRING}")
endif()
include("${CMMM_RESOLVED}")
endmacro()
function(cmmm_modules_list)
cmake_parse_arguments(CMMM "ALWAYS_DOWNLOAD" "URL;GIT_REPOSITORY;BRANCH;FOLDER;FILENAME;DESTINATION" "" "${ARGV}")
get_property(CMMM_NO_COLOR GLOBAL PROPERTY CMMM_NO_COLOR)
if(NOT ${CMMM_NO_COLOR})
colors()
endif()
get_property(CMMM_INACTIVITY_TIMEOUT GLOBAL PROPERTY CMMM_INACTIVITY_TIMEOUT)
get_property(CMMM_TIMEOUT GLOBAL PROPERTY CMMM_TIMEOUT)
# Default modules list name
if(NOT DEFINED CMMM_FILENAME)
set(CMMM_FILENAME "ModulesList")
endif()
# Set default URL
if(DEFINED CMMM_URL AND DEFINED CMMM_GIT_REPOSITORY)
message("${BoldRed}!! [CMakeMM] URL and GIT_REPOSITORY can not appear at the same time !!${Reset}")
message(FATAL_ERROR)
elseif(NOT DEFINED CMMM_URL AND NOT DEFINED CMMM_GIT_REPOSITORY)
message("${BoldRed}!! [CMakeMM] URL or GIT_REPOSITORY must be given !!${Reset}")
message(FATAL_ERROR)
endif()
if(DEFINED CMMM_GIT_REPOSITORY)
check_accessible()
if(${IS_ONLINE_RAW})
set(CMMM_URL "https://raw.githubusercontent.com/${CMMM_GIT_REPOSITORY}")
else()
set(CMMM_URL "https://cdn.jsdelivr.net/gh/${CMMM_GIT_REPOSITORY}")
endif()
endif()
string(FIND ${CMMM_URL} "/" HAS_FLASH REVERSE)
string(LENGTH ${CMMM_URL} CMMM_URL_LENGTH)
math(EXPR HAS_FLASH_PLUS_ONE ${HAS_FLASH}+1)
if(${HAS_FLASH_PLUS_ONE} STREQUAL ${CMMM_URL_LENGTH})
string(SUBSTRING ${CMMM_URL} 0 ${HAS_FLASH} CMMM_URL)
endif()
if(DEFINED CMMM_GIT_REPOSITORY)
if(NOT DEFINED CMMM_BRANCH)
set(CMMM_BRANCH "master")
endif()
if(${IS_ONLINE_RAW})
set(CMMM_URL "${CMMM_URL}/${CMMM_BRANCH}")
else()
set(CMMM_URL "${CMMM_URL}@${CMMM_BRANCH}")
endif()
endif()
set_property(GLOBAL PROPERTY CMMM_URL_MODULES "${CMMM_URL}")
get_property(CMMM_INSTALL_DESTINATION GLOBAL PROPERTY CMMM_DESTINATION)
# Set default modules installation folders
if(NOT DEFINED CMMM_DESTINATION)
set(CMMM_DESTINATION_MODULES "${CMMM_INSTALL_DESTINATION}/Modules")
else()
get_filename_component(CMMM_DESTINATION_MODULES "${CMMM_DESTINATION}" ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
set_property(GLOBAL PROPERTY CMMM_DESTINATION_MODULES ${CMMM_DESTINATION_MODULES})
if(NOT DEFINED CMMM_FOLDER)
set(CMMM_COMPLET_URL "${CMMM_URL}")
else()
set(CMMM_COMPLET_URL "${CMMM_URL}/${CMMM_FOLDER}")
endif()
message("${BoldMagenta}-- [CMakeMM] Downloading ${CMMM_FILENAME}.cmake\n From : ${CMMM_COMPLET_URL}/${CMMM_FILENAME}.cmake\n To : ${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake --${Reset}")
file(DOWNLOAD "${CMMM_COMPLET_URL}/${CMMM_FILENAME}.cmake" "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake" INACTIVITY_TIMEOUT ${CMMM_INACTIVITY_TIMEOUT} STATUS CMAKECM_STATUS TIMEOUT ${CMMM_TIMEOUT})
list(GET CMAKECM_STATUS 0 CMAKECM_CODE)
list(GET CMAKECM_STATUS 1 CMAKECM_MESSAGE)
if(${CMAKECM_CODE})
if(NOT EXISTS "${CMMM_DESTINATION_MODULES}/ModulesList.cmake")
message("${BoldRed}!! [CMakeCM] Error downloading ${CMMM_FILENAME} : ${CMAKECM_MESSAGE} !!${Reset}")
file(REMOVE "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake")
message(FATAL_ERROR)
else()
message("${BoldYellow}## [CMakeCM] Error downloading ${CMMM_FILENAME} : ${CMAKECM_MESSAGE} ##${Reset}")
message("${BoldYellow}## [CMakeCM] Using the one already downloaded ##${Reset}")
file(REMOVE "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake")
include("${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake")
endif()
elseif(${CMMM_ALWAYS_DOWNLOAD} OR NOT EXISTS "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake")
file(RENAME "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake" "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake")
include("${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake")
elseif(NOT ${CMMM_ALWAYS_DOWNLOAD})
file(SHA256 "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake" ModulesListTemp)
file(SHA256 "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake" ModulesList)
if(NOT ${ModulesListTemp} STREQUAL ${ModulesList})
message("${BoldYellow}## [CMakeCM] ModulesList has been uploaded but ALWAYS_DOWNLOAD is set in cmmm_modules_list ##${Reset}")
endif()
file(REMOVE "${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}Temp.cmake")
include("${CMMM_DESTINATION_MODULES}/${CMMM_FILENAME}.cmake")
endif()
message("${BoldGreen}** [CMakeCM] Modules will be installed in \"${CMMM_DESTINATION_MODULES}\" **${Reset}")
endfunction()
macro(cmmm_entry)
cmake_parse_arguments(CMMM "ALWAYS_DOWNLOAD;NO_COLOR" "REPOSITORY;VERSION;DESTINATION;TIMEOUT;INACTIVITY_TIMEOUT;VERBOSITY;PROVIDER" "" ${ARGN})
if(NOT ${CMMM_NO_COLOR})
colors()
endif()
set_property(GLOBAL PROPERTY CMMM_NO_COLOR ${CMMM_NO_COLOR})
list(
INSERT
VERBOSITY
0
"FATAL_ERROR"
"SEND_ERROR"
"WARNING"
"AUTHOR_WARNING"
"DEPRECATION"
"NOTICE"
"STATUS"
"VERBOSE"
"DEBUG"
"TRACE"
)
if(DEFINED CMMM_VERBOSITY)
list(FIND VERBOSITY ${CMMM_VERBOSITY} FOUND)
if(${FOUND} STREQUAL "-1")
message("${BoldYellow}## [CMakeMM] VERBOSITY ${CMMM_VERBOSITY} unknown. VERBOSITY set to STATUS. ##${Reset}")
set(CMMM_VERBOSITY "STATUS")
endif()
elseif(DEFINED CMAKE_MESSAGE_LOG_LEVEL)
list(FIND VERBOSITY ${CMAKE_MESSAGE_LOG_LEVEL} FOUND)
if(${FOUND} STREQUAL "-1")
message("${BoldYellow}## [CMakeMM] VERBOSITY ${CMAKE_MESSAGE_LOG_LEVEL} unknown. VERBOSITY set to STATUS. ##${Reset}")
set(CMMM_VERBOSITY "STATUS")
else()
set(CMMM_VERBOSITY ${CMAKE_MESSAGE_LOG_LEVEL})
endif()
else()
set(CMMM_VERBOSITY "STATUS")
endif()
set_property(GLOBAL PROPERTY CMMM_VERBOSITY ${CMMM_VERBOSITY})
if(NOT DEFINED CMMM_TIMEOUT)
set(CMMM_TIMEOUT 10)
endif()
set_property(GLOBAL PROPERTY CMMM_TIMEOUT ${CMMM_TIMEOUT})
if(NOT DEFINED CMMM_INACTIVITY_TIMEOUT)
set(CMMM_INACTIVITY_TIMEOUT 5)
endif()
set_property(GLOBAL PROPERTY CMMM_INACTIVITY_TIMEOUT ${CMMM_INACTIVITY_TIMEOUT})
if(NOT DEFINED CMMM_DESTINATION)
set(CMMM_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/CMakeMM")
else()
get_filename_component(CMMM_DESTINATION "${CMMM_DESTINATION}" ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
set(CMMM_DESTINATION "${CMMM_DESTINATION}/${CMMM_VERSION}")
set_property(GLOBAL PROPERTY CMMM_DESTINATION ${CMMM_DESTINATION})
# add the CMakeMM installation directory to CMAKE_MODULE_PATH
list(INSERT CMAKE_MODULE_PATH 0 "${CMMM_DESTINATION}")
list(REMOVE_DUPLICATES CMAKE_MODULE_PATH)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE)
cmmm_check_updates(${ARGN})
endmacro()