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