|
| 1 | +# Copyright (c) 2013, Ruslan Baratov |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# This is a gate file to Hunter package manager. |
| 5 | +# Usage: include this file using `include` command and add package you need: |
| 6 | +# |
| 7 | +# include("HunterGate.cmake") |
| 8 | +# hunter_add_package(Foo) |
| 9 | +# hunter_add_package(Boo COMPONENTS Bar Baz) |
| 10 | +# |
| 11 | +# Projects: |
| 12 | +# * https://github.com/hunter-packages/gate/ |
| 13 | +# * https://github.com/ruslo/hunter |
| 14 | + |
| 15 | +cmake_minimum_required(VERSION 2.8.10) |
| 16 | + |
| 17 | +set(HUNTER_MINIMUM_VERSION "0.1.2") |
| 18 | +set(HUNTER_MINIMUM_VERSION_HASH b540acf9647261ee85ed84af5b552f38ec844191) |
| 19 | + |
| 20 | +# Set HUNTER_ROOT cmake variable to suitable value. |
| 21 | +# Info about variable can be found in HUNTER_ROOT_INFO. |
| 22 | +function(hunter_gate_detect_root) |
| 23 | + # Check CMake variable |
| 24 | + if(HUNTER_ROOT) |
| 25 | + set(HUNTER_ROOT_INFO "HUNTER_ROOT detected by cmake variable" PARENT_SCOPE) |
| 26 | + return() |
| 27 | + endif() |
| 28 | + |
| 29 | + # Check environment variable |
| 30 | + string(COMPARE NOTEQUAL "$ENV{HUNTER_ROOT}" "" not_empty) |
| 31 | + if(not_empty) |
| 32 | + set(HUNTER_ROOT "$ENV{HUNTER_ROOT}" PARENT_SCOPE) |
| 33 | + set( |
| 34 | + HUNTER_ROOT_INFO |
| 35 | + "HUNTER_ROOT detected by environment variable" |
| 36 | + PARENT_SCOPE |
| 37 | + ) |
| 38 | + return() |
| 39 | + endif() |
| 40 | + |
| 41 | + # Check HOME environment variable |
| 42 | + string(COMPARE NOTEQUAL "$ENV{HOME}" "" result) |
| 43 | + if(result) |
| 44 | + set(HUNTER_ROOT "$ENV{HOME}/HunterPackages" PARENT_SCOPE) |
| 45 | + set( |
| 46 | + HUNTER_ROOT_INFO |
| 47 | + "HUNTER_ROOT set using HOME environment variable" |
| 48 | + PARENT_SCOPE |
| 49 | + ) |
| 50 | + return() |
| 51 | + endif() |
| 52 | + |
| 53 | + # Check PROGRAMFILES environment variable (windows only) |
| 54 | + if(WIN32) |
| 55 | + string(COMPARE NOTEQUAL "$ENV{PROGRAMFILES}" "" result) |
| 56 | + if(result) |
| 57 | + set(HUNTER_ROOT "$ENV{PROGRAMFILES}/HunterPackages" PARENT_SCOPE) |
| 58 | + set( |
| 59 | + HUNTER_ROOT_INFO |
| 60 | + "HUNTER_ROOT set using PROGRAMFILES environment variable" |
| 61 | + PARENT_SCOPE |
| 62 | + ) |
| 63 | + return() |
| 64 | + endif() |
| 65 | + endif() |
| 66 | + |
| 67 | + # Create in project |
| 68 | + if(NOT PROJECT_SOURCE_DIR) |
| 69 | + message(FATAL_ERROR "PROJECT_SOURCE_DIR is empty") |
| 70 | + endif() |
| 71 | + |
| 72 | + set(HUNTER_ROOT "${PROJECT_SOURCE_DIR}/HunterPackages" PARENT_SCOPE) |
| 73 | + set( |
| 74 | + HUNTER_ROOT_INFO |
| 75 | + "HUNTER_ROOT set by project sources directory" |
| 76 | + PARENT_SCOPE |
| 77 | + ) |
| 78 | +endfunction() |
| 79 | + |
| 80 | +# Download project to HUNTER_ROOT |
| 81 | +function(hunter_gate_do_download) |
| 82 | + message( |
| 83 | + STATUS |
| 84 | + "[hunter] Hunter not found, start download to '${HUNTER_ROOT}' ..." |
| 85 | + ) |
| 86 | + |
| 87 | + if(NOT PROJECT_BINARY_DIR) |
| 88 | + message( |
| 89 | + FATAL_ERROR |
| 90 | + "PROJECT_BINARY_DIR is empty. " |
| 91 | + "Move HunterGate file **after** first project command" |
| 92 | + ) |
| 93 | + endif() |
| 94 | + |
| 95 | + set(URL_BASE "https://github.com/ruslo/hunter/archive") |
| 96 | + file( |
| 97 | + WRITE |
| 98 | + "${PROJECT_BINARY_DIR}/Hunter-prefix/CMakeLists.txt" |
| 99 | + "cmake_minimum_required(VERSION 2.8.10)\n" |
| 100 | + "include(ExternalProject)\n" |
| 101 | + "ExternalProject_Add(\n" |
| 102 | + " Hunter\n" |
| 103 | + " URL\n" |
| 104 | + " \"${URL_BASE}/v${HUNTER_MINIMUM_VERSION}.tar.gz\"\n" |
| 105 | + " URL_HASH\n" |
| 106 | + " SHA1=${HUNTER_MINIMUM_VERSION_HASH}\n" |
| 107 | + " DOWNLOAD_DIR\n" |
| 108 | + " \"${HUNTER_ROOT}/Download\"\n" |
| 109 | + " SOURCE_DIR\n" |
| 110 | + " \"${HUNTER_ROOT}/Source\"\n" |
| 111 | + " CONFIGURE_COMMAND\n" |
| 112 | + " \"\"\n" |
| 113 | + " BUILD_COMMAND\n" |
| 114 | + " \"\"\n" |
| 115 | + " INSTALL_COMMAND\n" |
| 116 | + " \"\"\n" |
| 117 | + ")\n" |
| 118 | + ) |
| 119 | + |
| 120 | + execute_process( |
| 121 | + COMMAND |
| 122 | + "${CMAKE_COMMAND}" . |
| 123 | + WORKING_DIRECTORY |
| 124 | + "${PROJECT_BINARY_DIR}/Hunter-prefix" |
| 125 | + RESULT_VARIABLE |
| 126 | + HUNTER_DOWNLOAD_RESULT |
| 127 | + ) |
| 128 | + |
| 129 | + if(NOT HUNTER_DOWNLOAD_RESULT EQUAL 0) |
| 130 | + message(FATAL_ERROR "Configure download project failed") |
| 131 | + endif() |
| 132 | + |
| 133 | + execute_process( |
| 134 | + COMMAND |
| 135 | + "${CMAKE_COMMAND}" --build . |
| 136 | + WORKING_DIRECTORY |
| 137 | + "${PROJECT_BINARY_DIR}/Hunter-prefix" |
| 138 | + RESULT_VARIABLE |
| 139 | + HUNTER_DOWNLOAD_RESULT |
| 140 | + ) |
| 141 | + |
| 142 | + if(NOT HUNTER_DOWNLOAD_RESULT EQUAL 0) |
| 143 | + message(FATAL_ERROR "Build download project failed") |
| 144 | + endif() |
| 145 | + |
| 146 | + execute_process( |
| 147 | + COMMAND |
| 148 | + "${CMAKE_COMMAND}" |
| 149 | + -E |
| 150 | + touch |
| 151 | + "${HUNTER_ROOT}/installed.by.gate" |
| 152 | + ) |
| 153 | + |
| 154 | + message(STATUS "[hunter] downloaded to '${HUNTER_ROOT}'") |
| 155 | +endfunction() |
| 156 | + |
| 157 | +hunter_gate_detect_root() # set HUNTER_ROOT and HUNTER_ROOT_INFO |
| 158 | + |
| 159 | +if(NOT HUNTER_ROOT) |
| 160 | + message( |
| 161 | + FATAL_ERROR |
| 162 | + "Internal error in 'hunter_gate_detect_root': HUNTER_ROOT is not setted" |
| 163 | + ) |
| 164 | +endif() |
| 165 | + |
| 166 | +# Beautify path, fix probable problems with windows path slashes |
| 167 | +get_filename_component(HUNTER_ROOT "${HUNTER_ROOT}" ABSOLUTE) |
| 168 | + |
| 169 | +if(NOT EXISTS "${HUNTER_ROOT}") |
| 170 | + hunter_gate_do_download() |
| 171 | + if(NOT EXISTS "${HUNTER_ROOT}") |
| 172 | + message( |
| 173 | + FATAL_ERROR |
| 174 | + "Internal error in 'hunter_gate_do_download': " |
| 175 | + "directory HUNTER_ROOT not found" |
| 176 | + ) |
| 177 | + endif() |
| 178 | +endif() |
| 179 | + |
| 180 | +# at this point: HUNTER_ROOT exists and is file or directory |
| 181 | +if(NOT IS_DIRECTORY "${HUNTER_ROOT}") |
| 182 | + message( |
| 183 | + FATAL_ERROR |
| 184 | + "HUNTER_ROOT is not directory (${HUNTER_ROOT})" |
| 185 | + "(${HUNTER_ROOT_INFO})" |
| 186 | + ) |
| 187 | +endif() |
| 188 | + |
| 189 | +# at this point: HUNTER_ROOT exists and is directory |
| 190 | +file(GLOB _hunter_result "${HUNTER_ROOT}/*") |
| 191 | +list(LENGTH _hunter_result _hunter_result_len) |
| 192 | +if(_hunter_result_len EQUAL 0) |
| 193 | + # HUNTER_ROOT directory is empty, let's download it |
| 194 | + hunter_gate_do_download() |
| 195 | +endif() |
| 196 | + |
| 197 | +unset(_hunter_result) |
| 198 | +unset(_hunter_result_len) |
| 199 | + |
| 200 | +# at this point: HUNTER_ROOT exists and is not empty directory |
| 201 | +if(NOT EXISTS "${HUNTER_ROOT}/Source/cmake/Hunter") |
| 202 | + message( |
| 203 | + FATAL_ERROR |
| 204 | + "HUNTER_ROOT directory exists (${HUNTER_ROOT})" |
| 205 | + "but '${HUNTER_ROOT}/Source/cmake/Hunter' file not found" |
| 206 | + "(${HUNTER_ROOT_INFO})" |
| 207 | + ) |
| 208 | +endif() |
| 209 | + |
| 210 | +# check version |
| 211 | +if(NOT EXISTS "${HUNTER_ROOT}/Source/cmake/version.cmake") |
| 212 | + message( |
| 213 | + FATAL_ERROR |
| 214 | + "HUNTER_ROOT directory exists (${HUNTER_ROOT})" |
| 215 | + "but '${HUNTER_ROOT}/Source/cmake/version.cmake' file not found" |
| 216 | + "(${HUNTER_ROOT_INFO})" |
| 217 | + ) |
| 218 | +endif() |
| 219 | + |
| 220 | +unset(HUNTER_VERSION) |
| 221 | +include("${HUNTER_ROOT}/Source/cmake/version.cmake") |
| 222 | +if(NOT HUNTER_VERSION) |
| 223 | + message( |
| 224 | + FATAL_ERROR |
| 225 | + "Internal error: HUNTER_VERSION is not set in `version.cmake`" |
| 226 | + ) |
| 227 | +endif() |
| 228 | + |
| 229 | +if(HUNTER_VERSION VERSION_LESS HUNTER_MINIMUM_VERSION) |
| 230 | + message( |
| 231 | + "Current version is ${HUNTER_VERSION}. " |
| 232 | + "Minimum version is ${HUNTER_MINIMUM_VERSION}. " |
| 233 | + "Try autoupdate..." |
| 234 | + ) |
| 235 | + |
| 236 | + if(NOT EXISTS "${HUNTER_ROOT}/installed.by.gate") |
| 237 | + message( |
| 238 | + FATAL_ERROR |
| 239 | + "Please update version manually or remove directory `${HUNTER_ROOT}`." |
| 240 | + ) |
| 241 | + endif() |
| 242 | + |
| 243 | + if(EXISTS "${HUNTER_ROOT}/Source/.git") |
| 244 | + message( |
| 245 | + FATAL_ERROR |
| 246 | + "Internal error: `installed.by.gate` and `.git`" |
| 247 | + ) |
| 248 | + endif() |
| 249 | + # File `${HUNTER_ROOT}/installed.by.gate` exists, hence current version |
| 250 | + # installed by older gate file and can be auto-updated by current gate |
| 251 | + if(NOT EXISTS "${HUNTER_ROOT}/Source/scripts/sleep.cmake") |
| 252 | + message(FATAL_ERROR "Internal error: sleep.cmake not found") |
| 253 | + endif() |
| 254 | + |
| 255 | + set(_hunter_timeout 10) |
| 256 | + message("") |
| 257 | + message("***** AUTO UPDATE *****") |
| 258 | + message("") |
| 259 | + message("Directories:") |
| 260 | + message(" * `${HUNTER_ROOT}/Base`") |
| 261 | + message(" * `${HUNTER_ROOT}/Source`") |
| 262 | + message("will be REMOVED") |
| 263 | + message("") |
| 264 | + |
| 265 | + foreach(x RANGE ${_hunter_timeout}) |
| 266 | + math(EXPR _hunter_output "(${_hunter_timeout}) - (${x})") |
| 267 | + execute_process( |
| 268 | + COMMAND |
| 269 | + "${CMAKE_COMMAND}" |
| 270 | + -E |
| 271 | + echo_append |
| 272 | + "${_hunter_output} " |
| 273 | + ) |
| 274 | + if(NOT _hunter_output EQUAL 0) |
| 275 | + execute_process( |
| 276 | + COMMAND |
| 277 | + "${CMAKE_CTEST_COMMAND}" |
| 278 | + -S |
| 279 | + "${HUNTER_ROOT}/Source/scripts/sleep.cmake" |
| 280 | + ) |
| 281 | + endif() |
| 282 | + endforeach() |
| 283 | + |
| 284 | + # One more sanity check... |
| 285 | + string(COMPARE EQUAL "${HUNTER_ROOT}" "" _hunter_root_is_empty) |
| 286 | + if(_hunter_root_is_empty) |
| 287 | + message(FATAL_ERROR "Internal error: HUNTER_ROOT is empty") |
| 288 | + endif() |
| 289 | + |
| 290 | + # Remove old version |
| 291 | + if(EXISTS "${HUNTER_ROOT}/Base") |
| 292 | + execute_process( |
| 293 | + COMMAND |
| 294 | + "${CMAKE_COMMAND}" |
| 295 | + -E |
| 296 | + remove_directory |
| 297 | + "${HUNTER_ROOT}/Base" |
| 298 | + ) |
| 299 | + endif() |
| 300 | + if(EXISTS "${HUNTER_ROOT}/Source") |
| 301 | + execute_process( |
| 302 | + COMMAND |
| 303 | + "${CMAKE_COMMAND}" |
| 304 | + -E |
| 305 | + remove_directory |
| 306 | + "${HUNTER_ROOT}/Source" |
| 307 | + ) |
| 308 | + endif() |
| 309 | + |
| 310 | + # Download new version |
| 311 | + hunter_gate_do_download() |
| 312 | + |
| 313 | + # Sanity check: master file exists |
| 314 | + if(NOT EXISTS "${HUNTER_ROOT}/Source/cmake/Hunter") |
| 315 | + message(FATAL_ERROR "Broken download: master file not found") |
| 316 | + endif() |
| 317 | + |
| 318 | + # Sanity check: version is not less than minimum |
| 319 | + if(NOT EXISTS "${HUNTER_ROOT}/Source/cmake/version.cmake") |
| 320 | + message(FATAL_ERROR "Broken download: version.cmake not found") |
| 321 | + endif() |
| 322 | + |
| 323 | + unset(HUNTER_VERSION) |
| 324 | + include("${HUNTER_ROOT}/Source/cmake/version.cmake") |
| 325 | + if(NOT HUNTER_VERSION) |
| 326 | + message( |
| 327 | + FATAL_ERROR |
| 328 | + "Internal error: HUNTER_VERSION is not set in `version.cmake`" |
| 329 | + ) |
| 330 | + endif() |
| 331 | + |
| 332 | + if(HUNTER_VERSION VERSION_LESS HUNTER_MINIMUM_VERSION) |
| 333 | + message(FATAL_ERROR "Broken download: version is less than minimum") |
| 334 | + endif() |
| 335 | +endif() |
| 336 | + |
| 337 | +# HUNTER_ROOT found or downloaded if not exists, i.e. can be used now |
| 338 | +include("${HUNTER_ROOT}/Source/cmake/Hunter") |
| 339 | + |
| 340 | +include(hunter_status_debug) |
| 341 | +hunter_status_debug("${HUNTER_ROOT_INFO}") |
| 342 | + |
| 343 | +include(hunter_add_package) |
0 commit comments