1# Copyright (c) 2013-2017, 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 get_filename_component(absolute_CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" ABSOLUTE) 302 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=${absolute_CMAKE_TOOLCHAIN_FILE}") 303 else() 304 # 'toolchain_arg' can't be empty 305 set(toolchain_arg "-DCMAKE_TOOLCHAIN_FILE=") 306 endif() 307 308 string(COMPARE EQUAL "${CMAKE_MAKE_PROGRAM}" "" no_make) 309 if(no_make) 310 set(make_arg "") 311 else() 312 # Test case: remove Ninja from PATH but set it via CMAKE_MAKE_PROGRAM 313 set(make_arg "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}") 314 endif() 315 316 execute_process( 317 COMMAND 318 "${CMAKE_COMMAND}" 319 "-H${dir}" 320 "-B${build_dir}" 321 "-G${CMAKE_GENERATOR}" 322 "${toolchain_arg}" 323 ${make_arg} 324 WORKING_DIRECTORY "${dir}" 325 RESULT_VARIABLE download_result 326 ${logging_params} 327 ) 328 329 if(NOT download_result EQUAL 0) 330 hunter_gate_internal_error("Configure project failed") 331 endif() 332 333 hunter_gate_status_print( 334 "Initializing Hunter workspace (${HUNTER_GATE_SHA1})" 335 " ${HUNTER_GATE_URL}" 336 " -> ${dir}" 337 ) 338 execute_process( 339 COMMAND "${CMAKE_COMMAND}" --build "${build_dir}" 340 WORKING_DIRECTORY "${dir}" 341 RESULT_VARIABLE download_result 342 ${logging_params} 343 ) 344 345 if(NOT download_result EQUAL 0) 346 hunter_gate_internal_error("Build project failed") 347 endif() 348 349 file(REMOVE_RECURSE "${build_dir}") 350 file(REMOVE_RECURSE "${cmakelists}") 351 352 file(WRITE "${sha1_location}" "${HUNTER_GATE_SHA1}") 353 file(WRITE "${done_location}" "DONE") 354 355 hunter_gate_status_debug("Finished") 356endfunction() 357 358# Must be a macro so master file 'cmake/Hunter' can 359# apply all variables easily just by 'include' command 360# (otherwise PARENT_SCOPE magic needed) 361macro(HunterGate) 362 if(HUNTER_GATE_DONE) 363 # variable HUNTER_GATE_DONE set explicitly for external project 364 # (see `hunter_download`) 365 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES) 366 endif() 367 368 # First HunterGate command will init Hunter, others will be ignored 369 get_property(_hunter_gate_done GLOBAL PROPERTY HUNTER_GATE_DONE SET) 370 371 if(NOT HUNTER_ENABLED) 372 # Empty function to avoid error "unknown function" 373 function(hunter_add_package) 374 endfunction() 375 376 set( 377 _hunter_gate_disabled_mode_dir 378 "${CMAKE_CURRENT_LIST_DIR}/cmake/Hunter/disabled-mode" 379 ) 380 if(EXISTS "${_hunter_gate_disabled_mode_dir}") 381 hunter_gate_status_debug( 382 "Adding \"disabled-mode\" modules: ${_hunter_gate_disabled_mode_dir}" 383 ) 384 list(APPEND CMAKE_PREFIX_PATH "${_hunter_gate_disabled_mode_dir}") 385 endif() 386 elseif(_hunter_gate_done) 387 hunter_gate_status_debug("Secondary HunterGate (use old settings)") 388 hunter_gate_self( 389 "${HUNTER_CACHED_ROOT}" 390 "${HUNTER_VERSION}" 391 "${HUNTER_SHA1}" 392 _hunter_self 393 ) 394 include("${_hunter_self}/cmake/Hunter") 395 else() 396 set(HUNTER_GATE_LOCATION "${CMAKE_CURRENT_LIST_DIR}") 397 398 string(COMPARE NOTEQUAL "${PROJECT_NAME}" "" _have_project_name) 399 if(_have_project_name) 400 hunter_gate_fatal_error( 401 "Please set HunterGate *before* 'project' command. " 402 "Detected project: ${PROJECT_NAME}" 403 WIKI "error.huntergate.before.project" 404 ) 405 endif() 406 407 cmake_parse_arguments( 408 HUNTER_GATE "LOCAL" "URL;SHA1;GLOBAL;FILEPATH" "" ${ARGV} 409 ) 410 411 string(COMPARE EQUAL "${HUNTER_GATE_SHA1}" "" _empty_sha1) 412 string(COMPARE EQUAL "${HUNTER_GATE_URL}" "" _empty_url) 413 string( 414 COMPARE 415 NOTEQUAL 416 "${HUNTER_GATE_UNPARSED_ARGUMENTS}" 417 "" 418 _have_unparsed 419 ) 420 string(COMPARE NOTEQUAL "${HUNTER_GATE_GLOBAL}" "" _have_global) 421 string(COMPARE NOTEQUAL "${HUNTER_GATE_FILEPATH}" "" _have_filepath) 422 423 if(_have_unparsed) 424 hunter_gate_user_error( 425 "HunterGate unparsed arguments: ${HUNTER_GATE_UNPARSED_ARGUMENTS}" 426 ) 427 endif() 428 if(_empty_sha1) 429 hunter_gate_user_error("SHA1 suboption of HunterGate is mandatory") 430 endif() 431 if(_empty_url) 432 hunter_gate_user_error("URL suboption of HunterGate is mandatory") 433 endif() 434 if(_have_global) 435 if(HUNTER_GATE_LOCAL) 436 hunter_gate_user_error("Unexpected LOCAL (already has GLOBAL)") 437 endif() 438 if(_have_filepath) 439 hunter_gate_user_error("Unexpected FILEPATH (already has GLOBAL)") 440 endif() 441 endif() 442 if(HUNTER_GATE_LOCAL) 443 if(_have_global) 444 hunter_gate_user_error("Unexpected GLOBAL (already has LOCAL)") 445 endif() 446 if(_have_filepath) 447 hunter_gate_user_error("Unexpected FILEPATH (already has LOCAL)") 448 endif() 449 endif() 450 if(_have_filepath) 451 if(_have_global) 452 hunter_gate_user_error("Unexpected GLOBAL (already has FILEPATH)") 453 endif() 454 if(HUNTER_GATE_LOCAL) 455 hunter_gate_user_error("Unexpected LOCAL (already has FILEPATH)") 456 endif() 457 endif() 458 459 hunter_gate_detect_root() # set HUNTER_GATE_ROOT 460 461 # Beautify path, fix probable problems with windows path slashes 462 get_filename_component( 463 HUNTER_GATE_ROOT "${HUNTER_GATE_ROOT}" ABSOLUTE 464 ) 465 hunter_gate_status_debug("HUNTER_ROOT: ${HUNTER_GATE_ROOT}") 466 if(NOT HUNTER_ALLOW_SPACES_IN_PATH) 467 string(FIND "${HUNTER_GATE_ROOT}" " " _contain_spaces) 468 if(NOT _contain_spaces EQUAL -1) 469 hunter_gate_fatal_error( 470 "HUNTER_ROOT (${HUNTER_GATE_ROOT}) contains spaces." 471 "Set HUNTER_ALLOW_SPACES_IN_PATH=ON to skip this error" 472 "(Use at your own risk!)" 473 WIKI "error.spaces.in.hunter.root" 474 ) 475 endif() 476 endif() 477 478 string( 479 REGEX 480 MATCH 481 "[0-9]+\\.[0-9]+\\.[0-9]+[-_a-z0-9]*" 482 HUNTER_GATE_VERSION 483 "${HUNTER_GATE_URL}" 484 ) 485 string(COMPARE EQUAL "${HUNTER_GATE_VERSION}" "" _is_empty) 486 if(_is_empty) 487 set(HUNTER_GATE_VERSION "unknown") 488 endif() 489 490 hunter_gate_self( 491 "${HUNTER_GATE_ROOT}" 492 "${HUNTER_GATE_VERSION}" 493 "${HUNTER_GATE_SHA1}" 494 _hunter_self 495 ) 496 497 set(_master_location "${_hunter_self}/cmake/Hunter") 498 if(EXISTS "${HUNTER_GATE_ROOT}/cmake/Hunter") 499 # Hunter downloaded manually (e.g. by 'git clone') 500 set(_unused "xxxxxxxxxx") 501 set(HUNTER_GATE_SHA1 "${_unused}") 502 set(HUNTER_GATE_VERSION "${_unused}") 503 else() 504 get_filename_component(_archive_id_location "${_hunter_self}/.." ABSOLUTE) 505 set(_done_location "${_archive_id_location}/DONE") 506 set(_sha1_location "${_archive_id_location}/SHA1") 507 508 # Check Hunter already downloaded by HunterGate 509 if(NOT EXISTS "${_done_location}") 510 hunter_gate_download("${_archive_id_location}") 511 endif() 512 513 if(NOT EXISTS "${_done_location}") 514 hunter_gate_internal_error("hunter_gate_download failed") 515 endif() 516 517 if(NOT EXISTS "${_sha1_location}") 518 hunter_gate_internal_error("${_sha1_location} not found") 519 endif() 520 file(READ "${_sha1_location}" _sha1_value) 521 string(COMPARE EQUAL "${_sha1_value}" "${HUNTER_GATE_SHA1}" _is_equal) 522 if(NOT _is_equal) 523 hunter_gate_internal_error( 524 "Short SHA1 collision:" 525 " ${_sha1_value} (from ${_sha1_location})" 526 " ${HUNTER_GATE_SHA1} (HunterGate)" 527 ) 528 endif() 529 if(NOT EXISTS "${_master_location}") 530 hunter_gate_user_error( 531 "Master file not found:" 532 " ${_master_location}" 533 "try to update Hunter/HunterGate" 534 ) 535 endif() 536 endif() 537 include("${_master_location}") 538 set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES) 539 endif() 540endmacro()