|
| 1 | +# Copyright (c) 2015, Ruslan Baratov |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +cmake_minimum_required(VERSION 3.0) |
| 5 | +project(Hunter) |
| 6 | + |
| 7 | +include(ExternalProject) # ExternalProject_Add |
| 8 | + |
| 9 | +# Scheme for CMake projects |
| 10 | + |
| 11 | +# This is universal CMake scheme that will work for |
| 12 | +# * single-configuration (`* Makefiles`) |
| 13 | +# * cmake -DCMAKE_BUILD_TYPE=${configuration} |
| 14 | +# * multi-configuration (Visual Studio, Xcode) |
| 15 | +# * cmake -DCMAKE_CONFIGURATION_TYPES=... |
| 16 | +# * cmake --build --config ${configuration} |
| 17 | + |
| 18 | +list(APPEND CMAKE_MODULE_PATH "@HUNTER_SELF@/cmake/modules") |
| 19 | + |
| 20 | +include(hunter_status_debug) |
| 21 | +include(hunter_assert_not_empty_string) |
| 22 | +include(hunter_user_error) |
| 23 | + |
| 24 | +hunter_status_debug("Scheme: url_sha1_cmake") |
| 25 | + |
| 26 | +# Check preconditions |
| 27 | +hunter_assert_not_empty_string("@HUNTER_SELF@") |
| 28 | +hunter_assert_not_empty_string("@HUNTER_EP_NAME@") |
| 29 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@") |
| 30 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@") |
| 31 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@") |
| 32 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@") |
| 33 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@") |
| 34 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_CONFIGURATION_TYPES@") |
| 35 | +hunter_assert_not_empty_string("@HUNTER_CACHE_FILE@") |
| 36 | +hunter_assert_not_empty_string("@HUNTER_ARGS_FILE@") |
| 37 | +hunter_assert_not_empty_string("@HUNTER_PACKAGE_LICENSE_DIR@") |
| 38 | +hunter_assert_not_empty_string("@CMAKE_GENERATOR@") |
| 39 | +hunter_assert_not_empty_string("${CMAKE_TOOLCHAIN_FILE}") |
| 40 | +hunter_assert_not_empty_string("@HUNTER_TLS_VERIFY@") |
| 41 | + |
| 42 | +if(IOS) |
| 43 | + if(CMAKE_VERSION VERSION_LESS 3.5) |
| 44 | + hunter_user_error("CMake 3.5+ version should be used for iOS toolchains") |
| 45 | + endif() |
| 46 | + string(COMPARE NOTEQUAL "${IPHONESIMULATOR_ARCHS}" "" has_simulator) |
| 47 | + set(ios_opts "-DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO") |
| 48 | + if(has_simulator) |
| 49 | + list(APPEND ios_opts "-DCMAKE_IOS_INSTALL_COMBINED=YES") |
| 50 | + endif() |
| 51 | +else() |
| 52 | + set(ios_opts "") |
| 53 | +endif() |
| 54 | + |
| 55 | +# https://cmake.org/cmake/help/v3.4/release/3.4.html#modules |
| 56 | +# https://cmake.org/cmake/help/v3.4/module/ExternalProject.html |
| 57 | +if(CMAKE_VERSION VERSION_LESS 3.4) |
| 58 | + set(uses_terminal "") |
| 59 | +else() |
| 60 | + set( |
| 61 | + uses_terminal |
| 62 | + USES_TERMINAL_DOWNLOAD 1 |
| 63 | + USES_TERMINAL_UPDATE 1 |
| 64 | + USES_TERMINAL_CONFIGURE 1 |
| 65 | + USES_TERMINAL_BUILD 1 |
| 66 | + USES_TERMINAL_TEST 1 |
| 67 | + USES_TERMINAL_INSTALL 1 |
| 68 | + ) |
| 69 | +endif() |
| 70 | + |
| 71 | +string(COMPARE NOTEQUAL "@HUNTER_JOBS_OPTION@" "" have_jobs) |
| 72 | +if(have_jobs) |
| 73 | + string(COMPARE NOTEQUAL "@XCODE_VERSION@" "" is_xcode) |
| 74 | + if(is_xcode) |
| 75 | + set(jobs_option "-jobs" "@HUNTER_JOBS_OPTION@") |
| 76 | + elseif("@MSVC_IDE@") |
| 77 | + set(jobs_option "/maxcpucount:@HUNTER_JOBS_OPTION@") |
| 78 | + else() |
| 79 | + set(jobs_option "-j@HUNTER_JOBS_OPTION@") |
| 80 | + endif() |
| 81 | +else() |
| 82 | + set(jobs_option "") |
| 83 | +endif() |
| 84 | + |
| 85 | +set(previous_project "") |
| 86 | + |
| 87 | +if("@HUNTER_PACKAGE_LOG_BUILD@") |
| 88 | + set(log_build 1) |
| 89 | +else() |
| 90 | + set(log_build 0) |
| 91 | +endif() |
| 92 | + |
| 93 | +if("@HUNTER_PACKAGE_LOG_INSTALL@" OR "@HUNTER_SUPPRESS_LIST_OF_FILES@") |
| 94 | + set(log_install 1) |
| 95 | +else() |
| 96 | + set(log_install 0) |
| 97 | +endif() |
| 98 | + |
| 99 | +string(COMPARE EQUAL "@HUNTER_PACKAGE_PROTECTED_SOURCES@" "" is_empty) |
| 100 | +if(is_empty) |
| 101 | + set(http_credentials "") |
| 102 | +else() |
| 103 | + set( |
| 104 | + http_credentials |
| 105 | + HTTP_USERNAME "@HUNTER_PACKAGE_HTTP_USERNAME@" |
| 106 | + HTTP_PASSWORD "@HUNTER_PACKAGE_HTTP_PASSWORD@" |
| 107 | + ) |
| 108 | +endif() |
| 109 | + |
| 110 | +string(COMPARE NOTEQUAL "${CMAKE_MAKE_PROGRAM}" "" has_make) |
| 111 | +if(has_make) |
| 112 | + set(make_args "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}") |
| 113 | +else() |
| 114 | + set(make_args "") |
| 115 | +endif() |
| 116 | + |
| 117 | +string(COMPARE EQUAL "${CMAKE_CFG_INTDIR}" "." is_single) |
| 118 | + |
| 119 | +set(configuration_types "@HUNTER_PACKAGE_CONFIGURATION_TYPES@") |
| 120 | +list(LENGTH configuration_types len) |
| 121 | +if(len EQUAL "1") |
| 122 | + set(no_postfix TRUE) |
| 123 | +else() |
| 124 | + set(no_postfix FALSE) |
| 125 | +endif() |
| 126 | + |
| 127 | +foreach(configuration ${configuration_types}) |
| 128 | + # All configurations use the same URL which will be downloaded only once |
| 129 | + # i.e. overhead only for unpacking archive + no files from the previous |
| 130 | + # build will be left in case package do some insource modification (wrongly) |
| 131 | + string(TOUPPER "${configuration}" configuration_upper) |
| 132 | + string(COMPARE EQUAL "${configuration_upper}" "RELEASE" is_release) |
| 133 | + set(postfix_name "CMAKE_${configuration_upper}_POSTFIX") |
| 134 | + string(COMPARE EQUAL "${${postfix_name}}" "" is_empty) |
| 135 | + |
| 136 | + if(NOT is_release AND is_empty) |
| 137 | + set("${postfix_name}" "-${configuration}") |
| 138 | + endif() |
| 139 | + |
| 140 | + if(no_postfix) |
| 141 | + set("${postfix_name}" "") |
| 142 | + endif() |
| 143 | + |
| 144 | + set(current_project "@HUNTER_EP_NAME@-${configuration}") |
| 145 | + |
| 146 | + if(is_single) |
| 147 | + set(build_type_opts "-DCMAKE_BUILD_TYPE=${configuration}") |
| 148 | + else() |
| 149 | + set(build_type_opts "-DCMAKE_CONFIGURATION_TYPES=${configuration}") |
| 150 | + endif() |
| 151 | + |
| 152 | + ExternalProject_Add( |
| 153 | + "${current_project}" |
| 154 | + GIT_REPOSITORY |
| 155 | + @HUNTER_PACKAGE_URL@ |
| 156 | + GIT_TAG |
| 157 | + @HUNTER_PACKAGE_SHA1@ |
| 158 | + GIT_SHALLOW |
| 159 | + ${http_credentials} |
| 160 | + DOWNLOAD_DIR |
| 161 | + "@HUNTER_PACKAGE_DOWNLOAD_DIR@" |
| 162 | + TLS_VERIFY |
| 163 | + "@HUNTER_TLS_VERIFY@" |
| 164 | + SOURCE_DIR |
| 165 | + "@HUNTER_PACKAGE_SOURCE_DIR@" |
| 166 | + INSTALL_DIR |
| 167 | + "@HUNTER_PACKAGE_INSTALL_PREFIX@" |
| 168 | + # not used, just avoid creating Install/<name> empty directory |
| 169 | + SOURCE_SUBDIR |
| 170 | + "@HUNTER_PACKAGE_SOURCE_SUBDIR@" |
| 171 | + BUILD_COMMAND |
| 172 | + # Separate build and install stage so we can suppress install log |
| 173 | + # which may consist of a long list of files |
| 174 | + "@CMAKE_COMMAND@" |
| 175 | + --build . |
| 176 | + --config ${configuration} |
| 177 | + -- |
| 178 | + ${jobs_option} |
| 179 | + CMAKE_ARGS |
| 180 | + "-G@CMAKE_GENERATOR@" |
| 181 | + "-C@HUNTER_CACHE_FILE@" |
| 182 | + "-C@HUNTER_ARGS_FILE@" |
| 183 | + "-D${postfix_name}=${${postfix_name}}" |
| 184 | + "${build_type_opts}" |
| 185 | + "-DCMAKE_INSTALL_PREFIX=@HUNTER_PACKAGE_INSTALL_PREFIX@" |
| 186 | + "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}" |
| 187 | + "${make_args}" |
| 188 | + ${ios_opts} |
| 189 | + INSTALL_COMMAND |
| 190 | + "@CMAKE_COMMAND@" |
| 191 | + --build . |
| 192 | + --target install |
| 193 | + --config ${configuration} |
| 194 | + COMMAND # Copy license files |
| 195 | + "@CMAKE_COMMAND@" |
| 196 | + "-C@HUNTER_ARGS_FILE@" # for 'HUNTER_INSTALL_LICENSE_FILES' |
| 197 | + "-Dsrcdir=@HUNTER_PACKAGE_SOURCE_DIR@" |
| 198 | + "-Ddstdir=@HUNTER_PACKAGE_LICENSE_DIR@" |
| 199 | + -P |
| 200 | + "@HUNTER_SELF@/scripts/try-copy-license.cmake" |
| 201 | + LOG_BUILD ${log_build} |
| 202 | + LOG_INSTALL ${log_install} |
| 203 | + ${uses_terminal} |
| 204 | + ) |
| 205 | + |
| 206 | + # Each external project must depends on previous one since they all use |
| 207 | + # the same building directory |
| 208 | + string(COMPARE EQUAL "${previous_project}" "" is_empty) |
| 209 | + if(NOT is_empty) |
| 210 | + add_dependencies( |
| 211 | + "${current_project}" |
| 212 | + "${previous_project}" |
| 213 | + ) |
| 214 | + endif() |
| 215 | + set(previous_project "${current_project}") |
| 216 | +endforeach() |
0 commit comments