Debian Codegraph @

mtxclient/cmake/HunterGate.cmake

raw
1 # Copyright (c) 2013-2019, Ruslan Baratov
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice, this
8 # list of conditions and the following disclaimer.
9 #
10 # * Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25 # This is a gate file to Hunter package manager.
26 # Include this file using `include` command and add package you need, example:
27 #
28 # cmake_minimum_required(VERSION 3.2)
29 #
30 # include("cmake/HunterGate.cmake")
31 # HunterGate(
32 # URL "https://github.com/path/to/hunter/archive.tar.gz"
33 # SHA1 "798501e983f14b28b10cda16afa4de69eee1da1d"
34 # )
35 #
36 # project(MyProject)
37 #
38 # hunter_add_package(Foo)
39 # hunter_add_package(Boo COMPONENTS Bar Baz)
40 #
41 # Projects:
42 # * https://github.com/hunter-packages/gate/
43 # * https://github.com/ruslo/hunter
44
45 option(HUNTER_ENABLED "Enable Hunter package manager support" ON)
46
47 if(HUNTER_ENABLED)
48 if(CMAKE_VERSION VERSION_LESS "3.2")
49 message(
50 FATAL_ERROR
51 "At least CMake version 3.2 required for Hunter dependency management."
52 " Update CMake or set HUNTER_ENABLED to OFF."
53 )
54 endif()
55 endif()
56
57 include(CMakeParseArguments) # cmake_parse_arguments
58
59 option(HUNTER_STATUS_PRINT "Print working status" ON)
60 option(HUNTER_STATUS_DEBUG "Print a lot info" OFF)
61 option(HUNTER_TLS_VERIFY "Enable/disable TLS certificate checking on downloads" ON)
62
63 set(HUNTER_ERROR_PAGE "https://docs.hunter.sh/en/latest/reference/errors")
64
65 function(hunter_gate_status_print)
66 if(HUNTER_STATUS_PRINT OR HUNTER_STATUS_DEBUG)
67 foreach(print_message ${ARGV})
68 message(STATUS "[hunter] ${print_message}")
69 endforeach()
70 endif()
71 endfunction()
72
73 function(hunter_gate_status_debug)
74 if(HUNTER_STATUS_DEBUG)
75 foreach(print_message ${ARGV})
76 string(TIMESTAMP timestamp)
77 message(STATUS "[hunter *** DEBUG *** ${timestamp}] ${print_message}")
78 endforeach()
79 endif()
80 endfunction()
81
82 function(hunter_gate_error_page error_page)
83 message("------------------------------ ERROR ------------------------------")
84 message(" ${HUNTER_ERROR_PAGE}/${error_page}.html")
85 message("-------------------------------------------------------------------")
86 message("")
87 message(FATAL_ERROR "")
88 endfunction()
89
90 function(hunter_gate_internal_error)
91 message("")
92 foreach(print_message ${ARGV})
93 message("[hunter ** INTERNAL **] ${print_message}")
94 endforeach()
95 message("[hunter ** INTERNAL **] [Directory:${CMAKE_CURRENT_LIST_DIR}]")
96 message("")
97 hunter_gate_error_page("error.internal")
98 endfunction()
99
100 function(hunter_gate_fatal_error)
101 cmake_parse_arguments(hunter "" "ERROR_PAGE" "" "${ARGV}")
102 if("${hunter_ERROR_PAGE}" STREQUAL "")
103 hunter_gate_internal_error("Expected ERROR_PAGE")
104 endif()
105 message("")
106 foreach(x ${hunter_UNPARSED_ARGUMENTS})
107 message("[hunter ** FATAL ERROR **] ${x}")
108 endforeach()
109 message("[hunter ** FATAL ERROR **] [Directory:${CMAKE_CURRENT_LIST_DIR}]")
110 message("")
111 hunter_gate_error_page("${hunter_ERROR_PAGE}")
112 endfunction()
113
114 function(hunter_gate_user_error)
115 hunter_gate_fatal_error(${ARGV} ERROR_PAGE "error.incorrect.input.data")
116 endfunction()
117
118 function(hunter_gate_self root version sha1 result)
119 string(COMPARE EQUAL "${root}" "" is_bad)
120 if(is_bad)
121 hunter_gate_internal_error("root is empty")
122 endif()
123
124 string(COMPARE EQUAL "${version}" "" is_bad)
125 if(is_bad)
126 hunter_gate_internal_error("version is empty")
127 endif()
128
129 string(COMPARE EQUAL "${sha1}" "" is_bad)
130 if(is_bad)
131 hunter_gate_internal_error("sha1 is empty")
132 endif()
133
134 string(SUBSTRING "${sha1}" 0 7 archive_id)
135
136 if(EXISTS "${root}/cmake/Hunter")
137 set(hunter_self "${root}")
138 else()
139 set(
140 hunter_self
141 "${root}/_Base/Download/Hunter/${version}/${archive_id}/Unpacked"
142 )
143 endif()
144
145 set("${result}" "${hunter_self}" PARENT_SCOPE)
146 endfunction()
147
148 # Set HUNTER_GATE_ROOT cmake variable to suitable value.
149 function(hunter_gate_detect_root)
150 # Check CMake variable
151 string(COMPARE NOTEQUAL "${HUNTER_ROOT}" "" not_empty)
152 if(not_empty)
153 set(HUNTER_GATE_ROOT "${HUNTER_ROOT}" PARENT_SCOPE)
154 hunter_gate_status_debug("HUNTER_ROOT detected by cmake variable")
155 return()
156 endif()
157
158 # Check environment variable
159 string(COMPARE NOTEQUAL "$ENV{HUNTER_ROOT}" "" not_empty)
160 if(not_empty)
161 set(HUNTER_GATE_ROOT "$ENV{HUNTER_ROOT}" PARENT_SCOPE)
162 hunter_gate_status_debug("HUNTER_ROOT detected by environment variable")
163 return()
164 endif()
165
166 # Check HOME environment variable
167 string(COMPARE NOTEQUAL "$ENV{HOME}" "" result)
168 if(result)
169 set(HUNTER_GATE_ROOT "$ENV{HOME}/.hunter" PARENT_SCOPE)
170 hunter_gate_status_debug("HUNTER_ROOT set using HOME environment variable")
171 return()
172 endif()
173
174 # Check SYSTEMDRIVE and USERPROFILE environment variable (windows only)
175 if(WIN32)
176 string(COMPARE NOTEQUAL "$ENV{SYSTEMDRIVE}" "" result)
177 if(result)
178 set(HUNTER_GATE_ROOT "$ENV{SYSTEMDRIVE}/.hunter" PARENT_SCOPE)
179 hunter_gate_status_debug(
180 "HUNTER_ROOT set using SYSTEMDRIVE environment variable"
181 )
182 return()
183 endif()
184
185 string(COMPARE NOTEQUAL "$ENV{USERPROFILE}" "" result)
186 if(result)
187 set(HUNTER_GATE_ROOT "$ENV{USERPROFILE}/.hunter" PARENT_SCOPE)
188 hunter_gate_status_debug(
189 "HUNTER_ROOT set using USERPROFILE environment variable"
190 )
191 return()
192 endif()
193 endif()
194
195 hunter_gate_fatal_error(
196 "Can't detect HUNTER_ROOT"
197 ERROR_PAGE "error.detect.hunter.root"
198 )
199 endfunction()
200
201 function(hunter_gate_download dir)
202 string(
203 COMPARE
204 NOTEQUAL
205 "$ENV{HUNTER_DISABLE_AUTOINSTALL}"
206 ""
207 disable_autoinstall
208 )
209 if(disable_autoinstall AND NOT HUNTER_RUN_INSTALL)
210 hunter_gate_fatal_error(
211 "Hunter not found in '${dir}'"
212 "Set HUNTER_RUN_INSTALL=ON to auto-install it from '${HUNTER_GATE_URL}'"
213 "Settings:"
214 " HUNTER_ROOT: ${HUNTER_GATE_ROOT}"
215 " HUNTER_SHA1: ${HUNTER_GATE_SHA1}"
216 ERROR_PAGE "error.run.install"
217 )
218 endif()
219 string(COMPARE EQUAL "${dir}" "" is_bad)
220 if(is_bad)
221 hunter_gate_internal_error("Empty 'dir' argument")
222 endif()
223
224 string(COMPARE EQUAL "${HUNTER_GATE_SHA1}" "" is_bad)
225 if(is_bad)
226 hunter_gate_internal_error("HUNTER_GATE_SHA1 empty")
227 endif()
228
229 string(COMPARE EQUAL "${HUNTER_GATE_URL}" "" is_bad)
230 if(is_bad)
231 hunter_gate_internal_error("HUNTER_GATE_URL empty")
232 endif()
233
234 set(done_location "${dir}/DONE")
235 set(sha1_location "${dir}/SHA1")
236
237 set(build_dir "${dir}/Build")
238 set(cmakelists "${dir}/CMakeLists.txt")
239
240 hunter_gate_status_debug("Locking directory: ${dir}")
241 file(LOCK "${dir}" DIRECTORY GUARD FUNCTION)
242 hunter_gate_status_debug("Lock done")
243
244 if(EXISTS "${done_location}")
245 # while waiting for lock other instance can do all the job
246 hunter_gate_status_debug("File '${done_location}' found, skip install")
247 return()
248 endif()
249
250 file(REMOVE_RECURSE "${build_dir}")
251 file(REMOVE_RECURSE "${cmakelists}")
252
253 file(MAKE_DIRECTORY "${build_dir}") # check directory permissions
254
255 # Disabling languages speeds up a little bit, reduces noise in the output
256 # and avoids path too long windows error
257 file(
258 WRITE
259 "${cmakelists}"
260 "cmake_minimum_required(VERSION 3.2)\n"
261 "project(HunterDownload LANGUAGES NONE)\n"
262 "include(ExternalProject)\n"
263 "ExternalProject_Add(\n"
264 " Hunter\n"
265 " URL\n"
266 " \"${HUNTER_GATE_URL}\"\n"
267 " URL_HASH\n"
268 " SHA1=${HUNTER_GATE_SHA1}\n"
269 " DOWNLOAD_DIR\n"
270 " \"${dir}\"\n"
271 " TLS_VERIFY\n"
272 " ${HUNTER_TLS_VERIFY}\n"
273 " SOURCE_DIR\n"
274 " \"${dir}/Unpacked\"\n"
275 " CONFIGURE_COMMAND\n"
276 " \"\"\n"
277 " BUILD_COMMAND\n"
278 " \"\"\n"
279 " INSTALL_COMMAND\n"
280 " \"\"\n"
281 ")\n"
282 )
283
284 if(HUNTER_STATUS_DEBUG)
285 set(logging_params "")
286 else()
287 set(logging_params OUTPUT_QUIET)
288 endif()
289
290 hunter_gate_status_debug("Run generate")
291
292 # Need to add toolchain file too.
293 # Otherwise on Visual Studio + MDD this will fail with error:
294 # "Could not find an appropriate version of the Windows 10 SDK installed on this machine"
295 if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
296 get_filename_component(absolute_CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" ABSOLUTE)
297 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=${absolute_CMAKE_TOOLCHAIN_FILE}")
298 else()
299 # 'toolchain_arg' can't be empty
300 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=")
301 endif()
302
303 string(COMPARE EQUAL "${CMAKE_MAKE_PROGRAM}" "" no_make)
304 if(no_make)
305 set(make_arg "")
306 else()
307 # Test case: remove Ninja from PATH but set it via CMAKE_MAKE_PROGRAM
308 set(make_arg "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}")
309 endif()
310
311 execute_process(
312 COMMAND
313 "${CMAKE_COMMAND}"
314 "-H${dir}"
315 "-B${build_dir}"
316 "-G${CMAKE_GENERATOR}"
317 "${toolchain_arg}"
318 ${make_arg}
319 WORKING_DIRECTORY "${dir}"
320 RESULT_VARIABLE download_result
321 ${logging_params}
322 )
323
324 if(NOT download_result EQUAL 0)
325 hunter_gate_internal_error(
326 "Configure project failed."
327 "To reproduce the error run: ${CMAKE_COMMAND} -H${dir} -B${build_dir} -G${CMAKE_GENERATOR} ${toolchain_arg} ${make_arg}"
328 "In directory ${dir}"
329 )
330 endif()
331
332 hunter_gate_status_print(
333 "Initializing Hunter workspace (${HUNTER_GATE_SHA1})"
334 " ${HUNTER_GATE_URL}"
335 " -> ${dir}"
336 )
337 execute_process(
338 COMMAND "${CMAKE_COMMAND}" --build "${build_dir}"
339 WORKING_DIRECTORY "${dir}"
340 RESULT_VARIABLE download_result
341 ${logging_params}
342 )
343
344 if(NOT download_result EQUAL 0)
345 hunter_gate_internal_error("Build project failed")
346 endif()
347
348 file(REMOVE_RECURSE "${build_dir}")
349 file(REMOVE_RECURSE "${cmakelists}")
350
351 file(WRITE "${sha1_location}" "${HUNTER_GATE_SHA1}")
352 file(WRITE "${done_location}" "DONE")
353
354 hunter_gate_status_debug("Finished")
355 endfunction()
356
357 # Must be a macro so master file 'cmake/Hunter' can
358 # apply all variables easily just by 'include' command
359 # (otherwise PARENT_SCOPE magic needed)
360 macro(HunterGate)
361 if(HUNTER_GATE_DONE)
362 # variable HUNTER_GATE_DONE set explicitly for external project
363 # (see `hunter_download`)
364 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES)
365 endif()
366
367 # First HunterGate command will init Hunter, others will be ignored
368 get_property(_hunter_gate_done GLOBAL PROPERTY HUNTER_GATE_DONE SET)
369
370 if(NOT HUNTER_ENABLED)
371 # Empty function to avoid error "unknown function"
372 function(hunter_add_package)
373 endfunction()
374
375 set(
376 _hunter_gate_disabled_mode_dir
377 "${CMAKE_CURRENT_LIST_DIR}/cmake/Hunter/disabled-mode"
378 )
379 if(EXISTS "${_hunter_gate_disabled_mode_dir}")
380 hunter_gate_status_debug(
381 "Adding \"disabled-mode\" modules: ${_hunter_gate_disabled_mode_dir}"
382 )
383 list(APPEND CMAKE_PREFIX_PATH "${_hunter_gate_disabled_mode_dir}")
384 endif()
385 elseif(_hunter_gate_done)
386 hunter_gate_status_debug("Secondary HunterGate (use old settings)")
387 hunter_gate_self(
388 "${HUNTER_CACHED_ROOT}"
389 "${HUNTER_VERSION}"
390 "${HUNTER_SHA1}"
391 _hunter_self
392 )
393 include("${_hunter_self}/cmake/Hunter")
394 else()
395 set(HUNTER_GATE_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}")
396
397 string(COMPARE NOTEQUAL "${PROJECT_NAME}" "" _have_project_name)
398 if(_have_project_name)
399 hunter_gate_fatal_error(
400 "Please set HunterGate *before* 'project' command. "
401 "Detected project: ${PROJECT_NAME}"
402 ERROR_PAGE "error.huntergate.before.project"
403 )
404 endif()
405
406 cmake_parse_arguments(
407 HUNTER_GATE "LOCAL" "URL;SHA1;GLOBAL;FILEPATH" "" ${ARGV}
408 )
409
410 string(COMPARE EQUAL "${HUNTER_GATE_SHA1}" "" _empty_sha1)
411 string(COMPARE EQUAL "${HUNTER_GATE_URL}" "" _empty_url)
412 string(
413 COMPARE
414 NOTEQUAL
415 "${HUNTER_GATE_UNPARSED_ARGUMENTS}"
416 ""
417 _have_unparsed
418 )
419 string(COMPARE NOTEQUAL "${HUNTER_GATE_GLOBAL}" "" _have_global)
420 string(COMPARE NOTEQUAL "${HUNTER_GATE_FILEPATH}" "" _have_filepath)
421
422 if(_have_unparsed)
423 hunter_gate_user_error(
424 "HunterGate unparsed arguments: ${HUNTER_GATE_UNPARSED_ARGUMENTS}"
425 )
426 endif()
427 if(_empty_sha1)
428 hunter_gate_user_error("SHA1 suboption of HunterGate is mandatory")
429 endif()
430 if(_empty_url)
431 hunter_gate_user_error("URL suboption of HunterGate is mandatory")
432 endif()
433 if(_have_global)
434 if(HUNTER_GATE_LOCAL)
435 hunter_gate_user_error("Unexpected LOCAL (already has GLOBAL)")
436 endif()
437 if(_have_filepath)
438 hunter_gate_user_error("Unexpected FILEPATH (already has GLOBAL)")
439 endif()
440 endif()
441 if(HUNTER_GATE_LOCAL)
442 if(_have_global)
443 hunter_gate_user_error("Unexpected GLOBAL (already has LOCAL)")
444 endif()
445 if(_have_filepath)
446 hunter_gate_user_error("Unexpected FILEPATH (already has LOCAL)")
447 endif()
448 endif()
449 if(_have_filepath)
450 if(_have_global)
451 hunter_gate_user_error("Unexpected GLOBAL (already has FILEPATH)")
452 endif()
453 if(HUNTER_GATE_LOCAL)
454 hunter_gate_user_error("Unexpected LOCAL (already has FILEPATH)")
455 endif()
456 endif()
457
458 hunter_gate_detect_root() # set HUNTER_GATE_ROOT
459
460 # Beautify path, fix probable problems with windows path slashes
461 get_filename_component(
462 HUNTER_GATE_ROOT "${HUNTER_GATE_ROOT}" ABSOLUTE
463 )
464 hunter_gate_status_debug("HUNTER_ROOT: ${HUNTER_GATE_ROOT}")
465 if(NOT HUNTER_ALLOW_SPACES_IN_PATH)
466 string(FIND "${HUNTER_GATE_ROOT}" " " _contain_spaces)
467 if(NOT _contain_spaces EQUAL -1)
468 hunter_gate_fatal_error(
469 "HUNTER_ROOT (${HUNTER_GATE_ROOT}) contains spaces."
470 "Set HUNTER_ALLOW_SPACES_IN_PATH=ON to skip this error"
471 "(Use at your own risk!)"
472 ERROR_PAGE "error.spaces.in.hunter.root"
473 )
474 endif()
475 endif()
476
477 string(
478 REGEX
479 MATCH
480 "[0-9]+\\.[0-9]+\\.[0-9]+[-_a-z0-9]*"
481 HUNTER_GATE_VERSION
482 "${HUNTER_GATE_URL}"
483 )
484 string(COMPARE EQUAL "${HUNTER_GATE_VERSION}" "" _is_empty)
485 if(_is_empty)
486 set(HUNTER_GATE_VERSION "unknown")
487 endif()
488
489 hunter_gate_self(
490 "${HUNTER_GATE_ROOT}"
491 "${HUNTER_GATE_VERSION}"
492 "${HUNTER_GATE_SHA1}"
493 _hunter_self
494 )
495
496 set(_master_location "${_hunter_self}/cmake/Hunter")
497 if(EXISTS "${HUNTER_GATE_ROOT}/cmake/Hunter")
498 # Hunter downloaded manually (e.g. by 'git clone')
499 set(_unused "xxxxxxxxxx")
500 set(HUNTER_GATE_SHA1 "${_unused}")
501 set(HUNTER_GATE_VERSION "${_unused}")
502 else()
503 get_filename_component(_archive_id_location "${_hunter_self}/.." ABSOLUTE)
504 set(_done_location "${_archive_id_location}/DONE")
505 set(_sha1_location "${_archive_id_location}/SHA1")
506
507 # Check Hunter already downloaded by HunterGate
508 if(NOT EXISTS "${_done_location}")
509 hunter_gate_download("${_archive_id_location}")
510 endif()
511
512 if(NOT EXISTS "${_done_location}")
513 hunter_gate_internal_error("hunter_gate_download failed")
514 endif()
515
516 if(NOT EXISTS "${_sha1_location}")
517 hunter_gate_internal_error("${_sha1_location} not found")
518 endif()
519 file(READ "${_sha1_location}" _sha1_value)
520 string(COMPARE EQUAL "${_sha1_value}" "${HUNTER_GATE_SHA1}" _is_equal)
521 if(NOT _is_equal)
522 hunter_gate_internal_error(
523 "Short SHA1 collision:"
524 " ${_sha1_value} (from ${_sha1_location})"
525 " ${HUNTER_GATE_SHA1} (HunterGate)"
526 )
527 endif()
528 if(NOT EXISTS "${_master_location}")
529 hunter_gate_user_error(
530 "Master file not found:"
531 " ${_master_location}"
532 "try to update Hunter/HunterGate"
533 )
534 endif()
535 endif()
536 include("${_master_location}")
537 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES)
538 endif()
539 endmacro()