Debian Codegraph @

coeurl/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.10)
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/cpp-pm/gate/
43 # * https://github.com/cpp-pm/hunter
44
45 option(HUNTER_ENABLED "Enable Hunter package manager support" ON)
46
47 if(HUNTER_ENABLED)
48 if(CMAKE_VERSION VERSION_LESS "3.10")
49 message(
50 FATAL_ERROR
51 "At least CMake version 3.10 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 set(HUNTER_ROOT "" CACHE FILEPATH "Override the HUNTER_ROOT.")
63
64 set(HUNTER_ERROR_PAGE "https://hunter.readthedocs.io/en/latest/reference/errors")
65
66 function(hunter_gate_status_print)
67 if(HUNTER_STATUS_PRINT OR HUNTER_STATUS_DEBUG)
68 foreach(print_message ${ARGV})
69 message(STATUS "[hunter] ${print_message}")
70 endforeach()
71 endif()
72 endfunction()
73
74 function(hunter_gate_status_debug)
75 if(HUNTER_STATUS_DEBUG)
76 foreach(print_message ${ARGV})
77 string(TIMESTAMP timestamp)
78 message(STATUS "[hunter *** DEBUG *** ${timestamp}] ${print_message}")
79 endforeach()
80 endif()
81 endfunction()
82
83 function(hunter_gate_error_page error_page)
84 message("------------------------------ ERROR ------------------------------")
85 message(" ${HUNTER_ERROR_PAGE}/${error_page}.html")
86 message("-------------------------------------------------------------------")
87 message("")
88 message(FATAL_ERROR "")
89 endfunction()
90
91 function(hunter_gate_internal_error)
92 message("")
93 foreach(print_message ${ARGV})
94 message("[hunter ** INTERNAL **] ${print_message}")
95 endforeach()
96 message("[hunter ** INTERNAL **] [Directory:${CMAKE_CURRENT_LIST_DIR}]")
97 message("")
98 hunter_gate_error_page("error.internal")
99 endfunction()
100
101 function(hunter_gate_fatal_error)
102 cmake_parse_arguments(hunter "" "ERROR_PAGE" "" "${ARGV}")
103 if("${hunter_ERROR_PAGE}" STREQUAL "")
104 hunter_gate_internal_error("Expected ERROR_PAGE")
105 endif()
106 message("")
107 foreach(x ${hunter_UNPARSED_ARGUMENTS})
108 message("[hunter ** FATAL ERROR **] ${x}")
109 endforeach()
110 message("[hunter ** FATAL ERROR **] [Directory:${CMAKE_CURRENT_LIST_DIR}]")
111 message("")
112 hunter_gate_error_page("${hunter_ERROR_PAGE}")
113 endfunction()
114
115 function(hunter_gate_user_error)
116 hunter_gate_fatal_error(${ARGV} ERROR_PAGE "error.incorrect.input.data")
117 endfunction()
118
119 function(hunter_gate_self root version sha1 result)
120 string(COMPARE EQUAL "${root}" "" is_bad)
121 if(is_bad)
122 hunter_gate_internal_error("root is empty")
123 endif()
124
125 string(COMPARE EQUAL "${version}" "" is_bad)
126 if(is_bad)
127 hunter_gate_internal_error("version is empty")
128 endif()
129
130 string(COMPARE EQUAL "${sha1}" "" is_bad)
131 if(is_bad)
132 hunter_gate_internal_error("sha1 is empty")
133 endif()
134
135 string(SUBSTRING "${sha1}" 0 7 archive_id)
136
137 if(EXISTS "${root}/cmake/Hunter")
138 set(hunter_self "${root}")
139 else()
140 set(
141 hunter_self
142 "${root}/_Base/Download/Hunter/${version}/${archive_id}/Unpacked"
143 )
144 endif()
145
146 set("${result}" "${hunter_self}" PARENT_SCOPE)
147 endfunction()
148
149 # Set HUNTER_GATE_ROOT cmake variable to suitable value.
150 function(hunter_gate_detect_root)
151 # Check CMake variable
152 if(HUNTER_ROOT)
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 if(DEFINED ENV{HUNTER_ROOT})
160 set(HUNTER_GATE_ROOT "$ENV{HUNTER_ROOT}" PARENT_SCOPE)
161 hunter_gate_status_debug("HUNTER_ROOT detected by environment variable")
162 return()
163 endif()
164
165 # Check HOME environment variable
166 if(DEFINED ENV{HOME})
167 set(HUNTER_GATE_ROOT "$ENV{HOME}/.hunter" PARENT_SCOPE)
168 hunter_gate_status_debug("HUNTER_ROOT set using HOME environment variable")
169 return()
170 endif()
171
172 # Check SYSTEMDRIVE and USERPROFILE environment variable (windows only)
173 if(WIN32)
174 if(DEFINED ENV{SYSTEMDRIVE})
175 set(HUNTER_GATE_ROOT "$ENV{SYSTEMDRIVE}/.hunter" PARENT_SCOPE)
176 hunter_gate_status_debug(
177 "HUNTER_ROOT set using SYSTEMDRIVE environment variable"
178 )
179 return()
180 endif()
181
182 if(DEFINED ENV{USERPROFILE})
183 set(HUNTER_GATE_ROOT "$ENV{USERPROFILE}/.hunter" PARENT_SCOPE)
184 hunter_gate_status_debug(
185 "HUNTER_ROOT set using USERPROFILE environment variable"
186 )
187 return()
188 endif()
189 endif()
190
191 hunter_gate_fatal_error(
192 "Can't detect HUNTER_ROOT"
193 ERROR_PAGE "error.detect.hunter.root"
194 )
195 endfunction()
196
197 function(hunter_gate_download dir)
198 string(
199 COMPARE
200 NOTEQUAL
201 "$ENV{HUNTER_DISABLE_AUTOINSTALL}"
202 ""
203 disable_autoinstall
204 )
205 if(disable_autoinstall AND NOT HUNTER_RUN_INSTALL)
206 hunter_gate_fatal_error(
207 "Hunter not found in '${dir}'"
208 "Set HUNTER_RUN_INSTALL=ON to auto-install it from '${HUNTER_GATE_URL}'"
209 "Settings:"
210 " HUNTER_ROOT: ${HUNTER_GATE_ROOT}"
211 " HUNTER_SHA1: ${HUNTER_GATE_SHA1}"
212 ERROR_PAGE "error.run.install"
213 )
214 endif()
215 string(COMPARE EQUAL "${dir}" "" is_bad)
216 if(is_bad)
217 hunter_gate_internal_error("Empty 'dir' argument")
218 endif()
219
220 string(COMPARE EQUAL "${HUNTER_GATE_SHA1}" "" is_bad)
221 if(is_bad)
222 hunter_gate_internal_error("HUNTER_GATE_SHA1 empty")
223 endif()
224
225 string(COMPARE EQUAL "${HUNTER_GATE_URL}" "" is_bad)
226 if(is_bad)
227 hunter_gate_internal_error("HUNTER_GATE_URL empty")
228 endif()
229
230 set(done_location "${dir}/DONE")
231 set(sha1_location "${dir}/SHA1")
232
233 set(build_dir "${dir}/Build")
234 set(cmakelists "${dir}/CMakeLists.txt")
235
236 hunter_gate_status_debug("Locking directory: ${dir}")
237 file(LOCK "${dir}" DIRECTORY GUARD FUNCTION)
238 hunter_gate_status_debug("Lock done")
239
240 if(EXISTS "${done_location}")
241 # while waiting for lock other instance can do all the job
242 hunter_gate_status_debug("File '${done_location}' found, skip install")
243 return()
244 endif()
245
246 file(REMOVE_RECURSE "${build_dir}")
247 file(REMOVE_RECURSE "${cmakelists}")
248
249 file(MAKE_DIRECTORY "${build_dir}") # check directory permissions
250
251 # Disabling languages speeds up a little bit, reduces noise in the output
252 # and avoids path too long windows error
253 file(
254 WRITE
255 "${cmakelists}"
256 "cmake_minimum_required(VERSION 3.10)\n"
257 "if(POLICY CMP0114)\n"
258 " cmake_policy(SET CMP0114 NEW)\n"
259 "endif()\n"
260 "if(POLICY CMP0135)\n"
261 " cmake_policy(SET CMP0135 NEW)\n"
262 "endif()\n"
263 "project(HunterDownload LANGUAGES NONE)\n"
264 "include(ExternalProject)\n"
265 "ExternalProject_Add(\n"
266 " Hunter\n"
267 " URL\n"
268 " \"${HUNTER_GATE_URL}\"\n"
269 " URL_HASH\n"
270 " SHA1=${HUNTER_GATE_SHA1}\n"
271 " DOWNLOAD_DIR\n"
272 " \"${dir}\"\n"
273 " TLS_VERIFY\n"
274 " ${HUNTER_TLS_VERIFY}\n"
275 " SOURCE_DIR\n"
276 " \"${dir}/Unpacked\"\n"
277 " CONFIGURE_COMMAND\n"
278 " \"\"\n"
279 " BUILD_COMMAND\n"
280 " \"\"\n"
281 " INSTALL_COMMAND\n"
282 " \"\"\n"
283 ")\n"
284 )
285
286 if(HUNTER_STATUS_DEBUG)
287 set(logging_params "")
288 else()
289 set(logging_params OUTPUT_QUIET)
290 endif()
291
292 hunter_gate_status_debug("Run generate")
293
294 # Need to add toolchain file too.
295 # Otherwise on Visual Studio + MDD this will fail with error:
296 # "Could not find an appropriate version of the Windows 10 SDK installed on this machine"
297 if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
298 get_filename_component(absolute_CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" ABSOLUTE)
299 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=${absolute_CMAKE_TOOLCHAIN_FILE}")
300 else()
301 # 'toolchain_arg' can't be empty
302 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=")
303 endif()
304
305 string(COMPARE EQUAL "${CMAKE_MAKE_PROGRAM}" "" no_make)
306 if(no_make)
307 set(make_arg "")
308 else()
309 # Test case: remove Ninja from PATH but set it via CMAKE_MAKE_PROGRAM
310 set(make_arg "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}")
311 endif()
312
313 execute_process(
314 COMMAND
315 "${CMAKE_COMMAND}"
316 "-H${dir}"
317 "-B${build_dir}"
318 "-G${CMAKE_GENERATOR}"
319 "${toolchain_arg}"
320 ${make_arg}
321 WORKING_DIRECTORY "${dir}"
322 RESULT_VARIABLE download_result
323 ${logging_params}
324 )
325
326 if(NOT download_result EQUAL 0)
327 hunter_gate_internal_error(
328 "Configure project failed."
329 "To reproduce the error run: ${CMAKE_COMMAND} -H${dir} -B${build_dir} -G${CMAKE_GENERATOR} ${toolchain_arg} ${make_arg}"
330 "In directory ${dir}"
331 )
332 endif()
333
334 hunter_gate_status_print(
335 "Initializing Hunter workspace (${HUNTER_GATE_SHA1})"
336 " ${HUNTER_GATE_URL}"
337 " -> ${dir}"
338 )
339 execute_process(
340 COMMAND "${CMAKE_COMMAND}" --build "${build_dir}"
341 WORKING_DIRECTORY "${dir}"
342 RESULT_VARIABLE download_result
343 ${logging_params}
344 )
345
346 if(NOT download_result EQUAL 0)
347 hunter_gate_internal_error("Build project failed")
348 endif()
349
350 file(REMOVE_RECURSE "${build_dir}")
351 file(REMOVE_RECURSE "${cmakelists}")
352
353 file(WRITE "${sha1_location}" "${HUNTER_GATE_SHA1}")
354 file(WRITE "${done_location}" "DONE")
355
356 hunter_gate_status_debug("Finished")
357 endfunction()
358
359 # Must be a macro so master file 'cmake/Hunter' can
360 # apply all variables easily just by 'include' command
361 # (otherwise PARENT_SCOPE magic needed)
362 macro(HunterGate)
363 if(HUNTER_GATE_DONE)
364 # variable HUNTER_GATE_DONE set explicitly for external project
365 # (see `hunter_download`)
366 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES)
367 endif()
368
369 # First HunterGate command will init Hunter, others will be ignored
370 get_property(_hunter_gate_done GLOBAL PROPERTY HUNTER_GATE_DONE SET)
371
372 if(NOT HUNTER_ENABLED)
373 # Empty function to avoid error "unknown function"
374 function(hunter_add_package)
375 endfunction()
376
377 set(
378 _hunter_gate_disabled_mode_dir
379 "${CMAKE_CURRENT_LIST_DIR}/cmake/Hunter/disabled-mode"
380 )
381 if(EXISTS "${_hunter_gate_disabled_mode_dir}")
382 hunter_gate_status_debug(
383 "Adding \"disabled-mode\" modules: ${_hunter_gate_disabled_mode_dir}"
384 )
385 list(APPEND CMAKE_PREFIX_PATH "${_hunter_gate_disabled_mode_dir}")
386 endif()
387 elseif(_hunter_gate_done)
388 hunter_gate_status_debug("Secondary HunterGate (use old settings)")
389 hunter_gate_self(
390 "${HUNTER_CACHED_ROOT}"
391 "${HUNTER_VERSION}"
392 "${HUNTER_SHA1}"
393 _hunter_self
394 )
395 include("${_hunter_self}/cmake/Hunter")
396 else()
397 set(HUNTER_GATE_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}")
398
399 string(COMPARE NOTEQUAL "${PROJECT_NAME}" "" _have_project_name)
400 if(_have_project_name)
401 hunter_gate_fatal_error(
402 "Please set HunterGate *before* 'project' command. "
403 "Detected project: ${PROJECT_NAME}"
404 ERROR_PAGE "error.huntergate.before.project"
405 )
406 endif()
407
408 cmake_parse_arguments(
409 HUNTER_GATE "LOCAL" "URL;SHA1;GLOBAL;FILEPATH" "" ${ARGV}
410 )
411
412 string(COMPARE EQUAL "${HUNTER_GATE_SHA1}" "" _empty_sha1)
413 string(COMPARE EQUAL "${HUNTER_GATE_URL}" "" _empty_url)
414 string(
415 COMPARE
416 NOTEQUAL
417 "${HUNTER_GATE_UNPARSED_ARGUMENTS}"
418 ""
419 _have_unparsed
420 )
421 string(COMPARE NOTEQUAL "${HUNTER_GATE_GLOBAL}" "" _have_global)
422 string(COMPARE NOTEQUAL "${HUNTER_GATE_FILEPATH}" "" _have_filepath)
423
424 if(_have_unparsed)
425 hunter_gate_user_error(
426 "HunterGate unparsed arguments: ${HUNTER_GATE_UNPARSED_ARGUMENTS}"
427 )
428 endif()
429 if(_empty_sha1)
430 hunter_gate_user_error("SHA1 suboption of HunterGate is mandatory")
431 endif()
432 if(_empty_url)
433 hunter_gate_user_error("URL suboption of HunterGate is mandatory")
434 endif()
435 if(_have_global)
436 if(HUNTER_GATE_LOCAL)
437 hunter_gate_user_error("Unexpected LOCAL (already has GLOBAL)")
438 endif()
439 if(_have_filepath)
440 hunter_gate_user_error("Unexpected FILEPATH (already has GLOBAL)")
441 endif()
442 endif()
443 if(HUNTER_GATE_LOCAL)
444 if(_have_global)
445 hunter_gate_user_error("Unexpected GLOBAL (already has LOCAL)")
446 endif()
447 if(_have_filepath)
448 hunter_gate_user_error("Unexpected FILEPATH (already has LOCAL)")
449 endif()
450 endif()
451 if(_have_filepath)
452 if(_have_global)
453 hunter_gate_user_error("Unexpected GLOBAL (already has FILEPATH)")
454 endif()
455 if(HUNTER_GATE_LOCAL)
456 hunter_gate_user_error("Unexpected LOCAL (already has FILEPATH)")
457 endif()
458 endif()
459
460 hunter_gate_detect_root() # set HUNTER_GATE_ROOT
461
462 # Beautify path, fix probable problems with windows path slashes
463 get_filename_component(
464 HUNTER_GATE_ROOT "${HUNTER_GATE_ROOT}" ABSOLUTE
465 )
466 hunter_gate_status_debug("HUNTER_ROOT: ${HUNTER_GATE_ROOT}")
467 if(NOT HUNTER_ALLOW_SPACES_IN_PATH)
468 string(FIND "${HUNTER_GATE_ROOT}" " " _contain_spaces)
469 if(NOT _contain_spaces EQUAL -1)
470 hunter_gate_fatal_error(
471 "HUNTER_ROOT (${HUNTER_GATE_ROOT}) contains spaces."
472 "Set HUNTER_ALLOW_SPACES_IN_PATH=ON to skip this error"
473 "(Use at your own risk!)"
474 ERROR_PAGE "error.spaces.in.hunter.root"
475 )
476 endif()
477 endif()
478
479 string(
480 REGEX
481 MATCH
482 "[0-9]+\\.[0-9]+\\.[0-9]+[-_a-z0-9]*"
483 HUNTER_GATE_VERSION
484 "${HUNTER_GATE_URL}"
485 )
486 string(COMPARE EQUAL "${HUNTER_GATE_VERSION}" "" _is_empty)
487 if(_is_empty)
488 set(HUNTER_GATE_VERSION "unknown")
489 endif()
490
491 hunter_gate_self(
492 "${HUNTER_GATE_ROOT}"
493 "${HUNTER_GATE_VERSION}"
494 "${HUNTER_GATE_SHA1}"
495 _hunter_self
496 )
497
498 set(_master_location "${_hunter_self}/cmake/Hunter")
499 if(EXISTS "${HUNTER_GATE_ROOT}/cmake/Hunter")
500 # Hunter downloaded manually (e.g. by 'git clone')
501 set(_unused "xxxxxxxxxx")
502 set(HUNTER_GATE_SHA1 "${_unused}")
503 set(HUNTER_GATE_VERSION "${_unused}")
504 else()
505 get_filename_component(_archive_id_location "${_hunter_self}/.." ABSOLUTE)
506 set(_done_location "${_archive_id_location}/DONE")
507 set(_sha1_location "${_archive_id_location}/SHA1")
508
509 # Check Hunter already downloaded by HunterGate
510 if(NOT EXISTS "${_done_location}")
511 hunter_gate_download("${_archive_id_location}")
512 endif()
513
514 if(NOT EXISTS "${_done_location}")
515 hunter_gate_internal_error("hunter_gate_download failed")
516 endif()
517
518 if(NOT EXISTS "${_sha1_location}")
519 hunter_gate_internal_error("${_sha1_location} not found")
520 endif()
521 file(READ "${_sha1_location}" _sha1_value)
522 string(TOLOWER "${_sha1_value}" _sha1_value_lower)
523 string(TOLOWER "${HUNTER_GATE_SHA1}" _HUNTER_GATE_SHA1_lower)
524 string(COMPARE EQUAL "${_sha1_value_lower}" "${_HUNTER_GATE_SHA1_lower}" _is_equal)
525 if(NOT _is_equal)
526 hunter_gate_internal_error(
527 "Short SHA1 collision:"
528 " ${_sha1_value} (from ${_sha1_location})"
529 " ${HUNTER_GATE_SHA1} (HunterGate)"
530 )
531 endif()
532 if(NOT EXISTS "${_master_location}")
533 hunter_gate_user_error(
534 "Master file not found:"
535 " ${_master_location}"
536 "try to update Hunter/HunterGate"
537 )
538 endif()
539 endif()
540 include("${_master_location}")
541 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES)
542 endif()
543 endmacro()