|
| 1 | +# 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 | + |
| 45 | +cmake_minimum_required(VERSION 3.0) # Minimum for Hunter |
| 46 | +include(CMakeParseArguments) # cmake_parse_arguments |
| 47 | + |
| 48 | +option(HUNTER_ENABLED "Enable Hunter package manager support" ON) |
| 49 | +option(HUNTER_STATUS_PRINT "Print working status" ON) |
| 50 | +option(HUNTER_STATUS_DEBUG "Print a lot info" OFF) |
| 51 | + |
| 52 | +set(HUNTER_WIKI "https://github.com/ruslo/hunter/wiki") |
| 53 | + |
| 54 | +function(hunter_gate_wiki wiki_page) |
| 55 | + message("------------------------------ WIKI -------------------------------") |
| 56 | + message(" ${HUNTER_WIKI}/${wiki_page}") |
| 57 | + message("-------------------------------------------------------------------") |
| 58 | + message("") |
| 59 | + message(FATAL_ERROR "") |
| 60 | +endfunction() |
| 61 | + |
| 62 | +function(hunter_gate_internal_error) |
| 63 | + message("") |
| 64 | + foreach(print_message ${ARGV}) |
| 65 | + message("[hunter ** INTERNAL **] ${print_message}") |
| 66 | + endforeach() |
| 67 | + message("[hunter ** INTERNAL **] [Directory:${CMAKE_CURRENT_LIST_DIR}]") |
| 68 | + message("") |
| 69 | + hunter_gate_wiki("error.internal") |
| 70 | +endfunction() |
| 71 | + |
| 72 | +function(hunter_gate_fatal_error) |
| 73 | + cmake_parse_arguments(hunter "" "WIKI" "" "${ARGV}") |
| 74 | + if(NOT hunter_WIKI) |
| 75 | + hunter_gate_internal_error("Expected wiki") |
| 76 | + endif() |
| 77 | + message("") |
| 78 | + foreach(x ${hunter_UNPARSED_ARGUMENTS}) |
| 79 | + message("[hunter ** FATAL ERROR **] ${x}") |
| 80 | + endforeach() |
| 81 | + message("[hunter ** FATAL ERROR **] [Directory:${CMAKE_CURRENT_LIST_DIR}]") |
| 82 | + message("") |
| 83 | + hunter_gate_wiki("${hunter_WIKI}") |
| 84 | +endfunction() |
| 85 | + |
| 86 | +function(hunter_gate_user_error) |
| 87 | + hunter_gate_fatal_error(${ARGV} WIKI "error.incorrect.input.data") |
| 88 | +endfunction() |
| 89 | + |
| 90 | +function(hunter_gate_self root version sha1 result) |
| 91 | + string(COMPARE EQUAL "${root}" "" is_bad) |
| 92 | + if(is_bad) |
| 93 | + hunter_gate_internal_error("root is empty") |
| 94 | + endif() |
| 95 | + |
| 96 | + string(COMPARE EQUAL "${version}" "" is_bad) |
| 97 | + if(is_bad) |
| 98 | + hunter_gate_internal_error("version is empty") |
| 99 | + endif() |
| 100 | + |
| 101 | + string(COMPARE EQUAL "${sha1}" "" is_bad) |
| 102 | + if(is_bad) |
| 103 | + hunter_gate_internal_error("sha1 is empty") |
| 104 | + endif() |
| 105 | + |
| 106 | + string(SUBSTRING "${sha1}" 0 7 archive_id) |
| 107 | + |
| 108 | + if(EXISTS "${root}/cmake/Hunter") |
| 109 | + set(hunter_self "${root}") |
| 110 | + else() |
| 111 | + set( |
| 112 | + hunter_self |
| 113 | + "${root}/_Base/Download/Hunter/${version}/${archive_id}/Unpacked" |
| 114 | + ) |
| 115 | + endif() |
| 116 | + |
| 117 | + set("${result}" "${hunter_self}" PARENT_SCOPE) |
| 118 | +endfunction() |
| 119 | + |
| 120 | +# Set HUNTER_GATE_ROOT cmake variable to suitable value. |
| 121 | +function(hunter_gate_detect_root) |
| 122 | + # Check CMake variable |
| 123 | + if(HUNTER_ROOT) |
| 124 | + set(HUNTER_GATE_ROOT "${HUNTER_ROOT}" PARENT_SCOPE) |
| 125 | + return() |
| 126 | + endif() |
| 127 | + |
| 128 | + # Check environment variable |
| 129 | + string(COMPARE NOTEQUAL "$ENV{HUNTER_ROOT}" "" not_empty) |
| 130 | + if(not_empty) |
| 131 | + set(HUNTER_GATE_ROOT "$ENV{HUNTER_ROOT}" PARENT_SCOPE) |
| 132 | + return() |
| 133 | + endif() |
| 134 | + |
| 135 | + hunter_gate_fatal_error( |
| 136 | + "Can't detect HUNTER_ROOT" |
| 137 | + WIKI "error.detect.hunter.root" |
| 138 | + ) |
| 139 | +endfunction() |
| 140 | + |
| 141 | +# Must be a macro so master file 'cmake/Hunter' can |
| 142 | +# apply all variables easily just by 'include' command |
| 143 | +# (otherwise PARENT_SCOPE magic needed) |
| 144 | +macro(HunterGate) |
| 145 | + if(HUNTER_GATE_DONE) |
| 146 | + # variable HUNTER_GATE_DONE set explicitly for external project |
| 147 | + # (see `hunter_download`) |
| 148 | + set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES) |
| 149 | + endif() |
| 150 | + |
| 151 | + # First HunterGate command will init Hunter, others will be ignored |
| 152 | + get_property(_hunter_gate_done GLOBAL PROPERTY HUNTER_GATE_DONE SET) |
| 153 | + |
| 154 | + if(_hunter_gate_done) |
| 155 | + hunter_gate_self( |
| 156 | + "${HUNTER_CACHED_ROOT}" |
| 157 | + "${HUNTER_VERSION}" |
| 158 | + "${HUNTER_SHA1}" |
| 159 | + _hunter_self |
| 160 | + ) |
| 161 | + include("${_hunter_self}/cmake/Hunter") |
| 162 | + else() |
| 163 | + set(HUNTER_GATE_LOCATION "${CMAKE_CURRENT_LIST_DIR}") |
| 164 | + |
| 165 | + if(PROJECT_NAME) |
| 166 | + |
| 167 | + message("----------${PROJECT_NAME}") |
| 168 | + hunter_gate_fatal_error( |
| 169 | + "Please set HunterGate *before* 'project' command" |
| 170 | + WIKI "error.huntergate.before.project" |
| 171 | + ) |
| 172 | + endif() |
| 173 | + |
| 174 | + cmake_parse_arguments( |
| 175 | + HUNTER_GATE "LOCAL" "URL;SHA1;GLOBAL;FILEPATH" "" ${ARGV} |
| 176 | + ) |
| 177 | + if(HUNTER_GATE_UNPARSED_ARGUMENTS) |
| 178 | + hunter_gate_user_error( |
| 179 | + "HunterGate unparsed arguments: ${HUNTER_GATE_UNPARSED_ARGUMENTS}" |
| 180 | + ) |
| 181 | + endif() |
| 182 | + if(HUNTER_GATE_GLOBAL) |
| 183 | + if(HUNTER_GATE_LOCAL) |
| 184 | + hunter_gate_user_error("Unexpected LOCAL (already has GLOBAL)") |
| 185 | + endif() |
| 186 | + if(HUNTER_GATE_FILEPATH) |
| 187 | + hunter_gate_user_error("Unexpected FILEPATH (already has GLOBAL)") |
| 188 | + endif() |
| 189 | + endif() |
| 190 | + if(HUNTER_GATE_LOCAL) |
| 191 | + if(HUNTER_GATE_GLOBAL) |
| 192 | + hunter_gate_user_error("Unexpected GLOBAL (already has LOCAL)") |
| 193 | + endif() |
| 194 | + if(HUNTER_GATE_FILEPATH) |
| 195 | + hunter_gate_user_error("Unexpected FILEPATH (already has LOCAL)") |
| 196 | + endif() |
| 197 | + endif() |
| 198 | + if(HUNTER_GATE_FILEPATH) |
| 199 | + if(HUNTER_GATE_GLOBAL) |
| 200 | + hunter_gate_user_error("Unexpected GLOBAL (already has FILEPATH)") |
| 201 | + endif() |
| 202 | + if(HUNTER_GATE_LOCAL) |
| 203 | + hunter_gate_user_error("Unexpected LOCAL (already has FILEPATH)") |
| 204 | + endif() |
| 205 | + endif() |
| 206 | + |
| 207 | + hunter_gate_detect_root() # set HUNTER_GATE_ROOT |
| 208 | + |
| 209 | + # Beautify path, fix probable problems with windows path slashes |
| 210 | + get_filename_component( |
| 211 | + HUNTER_GATE_ROOT "${HUNTER_GATE_ROOT}" ABSOLUTE |
| 212 | + ) |
| 213 | + string(FIND "${HUNTER_GATE_ROOT}" " " _contain_spaces) |
| 214 | + if(NOT _contain_spaces EQUAL -1) |
| 215 | + hunter_gate_fatal_error( |
| 216 | + "HUNTER_ROOT (${HUNTER_GATE_ROOT}) contains spaces" |
| 217 | + WIKI "error.spaces.in.hunter.root" |
| 218 | + ) |
| 219 | + endif() |
| 220 | + |
| 221 | + string( |
| 222 | + REGEX |
| 223 | + MATCH |
| 224 | + "[0-9]+\\.[0-9]+\\.[0-9]+[-_a-z0-9]*" |
| 225 | + HUNTER_GATE_VERSION |
| 226 | + "${HUNTER_GATE_URL}" |
| 227 | + ) |
| 228 | + string(COMPARE EQUAL "${HUNTER_GATE_VERSION}" "" _is_empty) |
| 229 | + if(_is_empty) |
| 230 | + set(HUNTER_GATE_VERSION "unknown") |
| 231 | + endif() |
| 232 | + |
| 233 | + hunter_gate_self( |
| 234 | + "${HUNTER_GATE_ROOT}" |
| 235 | + "${HUNTER_GATE_VERSION}" |
| 236 | + "${HUNTER_GATE_SHA1}" |
| 237 | + hunter_self_ |
| 238 | + ) |
| 239 | + |
| 240 | + set(_master_location "${hunter_self_}/cmake/Hunter") |
| 241 | + if(EXISTS "${HUNTER_GATE_ROOT}/cmake/Hunter") |
| 242 | + # Hunter downloaded manually (e.g. by 'git clone') |
| 243 | + if(${DO_HUNTER_SYNC}) |
| 244 | + execute_process( COMMAND git pull origin develop |
| 245 | + WORKING_DIRECTORY ${HUNTER_GATE_ROOT}) |
| 246 | + endif() |
| 247 | + |
| 248 | + set(_unused "xxxxxxxxxx") |
| 249 | + set(HUNTER_GATE_SHA1 "${_unused}") |
| 250 | + set(HUNTER_GATE_VERSION "${_unused}") |
| 251 | + else() |
| 252 | + hunter_gate_user_error( |
| 253 | + "Master file not found:" |
| 254 | + " ${_master_location}" |
| 255 | + "try to update Hunter/HunterGate" |
| 256 | + ) |
| 257 | + endif() |
| 258 | + include("${_master_location}") |
| 259 | + set_property(GLOBAL PROPERTY HUNTER_GATE_DONE YES) |
| 260 | + endif() |
| 261 | +endmacro() |
0 commit comments