|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Murex |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +set -u |
| 24 | + |
| 25 | +base_dir="$(cd "$(dirname -- "$0")" && pwd)" |
| 26 | +if [ -z "${CMAKEW_BASE_DIR+x}" ]; then CMAKEW_BASE_DIR="${base_dir}"; fi |
| 27 | +if [ -z "${CMAKEW_CACHE_DIR+x}" ]; then CMAKEW_CACHE_DIR="$(dirname "${base_dir}")/.cmake"; fi |
| 28 | +if [ -z "${CMAKEW_CMD+x}" ]; then CMAKEW_CMD="cmake"; fi |
| 29 | + |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +# cmake directory structure and information |
| 32 | +# ------------------------------------------------------------------------------ |
| 33 | + |
| 34 | +CMAKEW_VERSION_FILE="${CMAKEW_BASE_DIR}/version.txt" |
| 35 | + |
| 36 | +# ------------------------------------------------------------------------------ |
| 37 | +# Trace messages |
| 38 | +# ------------------------------------------------------------------------------ |
| 39 | + |
| 40 | +print_info() { |
| 41 | + message="$1" |
| 42 | + printf "%b" "${message}\n" | while IFS= read -r line; do >&2 printf "%b" "\e[1;34m>>> ${line} \e[0m\n"; done |
| 43 | +} |
| 44 | + |
| 45 | +print_warning() { |
| 46 | + message="$1" |
| 47 | + printf "%b" "${message}\n" | while IFS= read -r line; do >&2 printf "%b" "\e[1;33m>>> ${line} \e[0m\n"; done |
| 48 | +} |
| 49 | + |
| 50 | +print_error() { |
| 51 | + message="$1" |
| 52 | + printf "%b" "${message}\n" | while IFS= read -r line; do >&2 printf "%b" "\e[1;31m>>> ${line} \e[0m\n"; done |
| 53 | +} |
| 54 | + |
| 55 | +print_horizontal_line() { |
| 56 | + term_columns=$(tput cols) |
| 57 | + repeated=$((term_columns - 5)) |
| 58 | + line=$(head -c "${repeated}" </dev/zero | tr '\0' '-') |
| 59 | + print_info "$line" |
| 60 | +} |
| 61 | + |
| 62 | +# ------------------------------------------------------------------------------ |
| 63 | +# Download cmake from cmake website |
| 64 | +# ------------------------------------------------------------------------------ |
| 65 | + |
| 66 | +download_cmake() { |
| 67 | + version="$1" |
| 68 | + os="$2" |
| 69 | + arch="$3" |
| 70 | + archive_extension="$4" |
| 71 | + exe_path="$5" |
| 72 | + |
| 73 | + # ---------------------------------------------------------------------------- |
| 74 | + # 1) create and enter cache directory |
| 75 | + # ---------------------------------------------------------------------------- |
| 76 | + |
| 77 | + mkdir -p "${CMAKEW_CACHE_DIR}" |
| 78 | + pushd "${CMAKEW_CACHE_DIR}" >/dev/null 2>/dev/null || return 1 |
| 79 | + |
| 80 | + # ---------------------------------------------------------------------------- |
| 81 | + # 2) download cmake archive |
| 82 | + # ---------------------------------------------------------------------------- |
| 83 | + |
| 84 | + cmake_version="${version}" |
| 85 | + cmake_expected_dir="cmake-${cmake_version}-${os}-${arch}" |
| 86 | + cmake_expected_archive_file="${cmake_expected_dir}.${archive_extension}" |
| 87 | + cmake_archive_url="http://github.com/Kitware/CMake/releases/download/v${cmake_version}/${cmake_expected_archive_file}" |
| 88 | + cmake_home="cmake-${os}-${arch}" |
| 89 | + |
| 90 | + if ! [ -f "${cmake_expected_archive_file}" ] |
| 91 | + then |
| 92 | + print_info "downloading ${cmake_expected_archive_file}" |
| 93 | + if ! curl -f -# -L "${cmake_archive_url}" -o "${cmake_expected_archive_file}"; then |
| 94 | + print_error "failed to download ${cmake_archive_url}" |
| 95 | + return 1 |
| 96 | + fi |
| 97 | + fi |
| 98 | + |
| 99 | + # ---------------------------------------------------------------------------- |
| 100 | + # 3) expand cmake archive |
| 101 | + # ---------------------------------------------------------------------------- |
| 102 | + |
| 103 | + print_info "extracting cmake ${cmake_version}" |
| 104 | + case "${archive_extension}" in |
| 105 | + zip) |
| 106 | + if ! unzip -q -o "${cmake_expected_archive_file}"; then |
| 107 | + print_error "failed to expand ${cmake_expected_archive_file}" |
| 108 | + return 1 |
| 109 | + fi |
| 110 | + ;; |
| 111 | + tar.gz) |
| 112 | + if ! tar zxf "${cmake_expected_archive_file}"; then |
| 113 | + print_error "failed to expand ${cmake_expected_archive_file}" |
| 114 | + return 1 |
| 115 | + fi |
| 116 | + ;; |
| 117 | + *) |
| 118 | + print_error "archive format ${archive_extension} is currently not supported" |
| 119 | + exit 1 |
| 120 | + ;; |
| 121 | + esac |
| 122 | + |
| 123 | + # ---------------------------------------------------------------------------- |
| 124 | + # 4) make the expanded archive the current version in cache |
| 125 | + # ---------------------------------------------------------------------------- |
| 126 | + |
| 127 | + [ -d "${cmake_home}" ] && rm -Rf "${cmake_home}" |
| 128 | + mv "${cmake_expected_dir}" "${cmake_home}" |
| 129 | + |
| 130 | + popd >/dev/null 2>/dev/null || return 1 |
| 131 | +} |
| 132 | + |
| 133 | +# ------------------------------------------------------------------------------ |
| 134 | +# Return expected cmake version |
| 135 | +# ------------------------------------------------------------------------------ |
| 136 | + |
| 137 | +retrieve_expected_cmake_version() { |
| 138 | + if [ -f "${CMAKEW_VERSION_FILE}" ]; then |
| 139 | + expected_version=$(awk '{ print $2 }' < "${CMAKEW_VERSION_FILE}") |
| 140 | + # print_info "expected cmake version: ${expected_version}" |
| 141 | + echo "${expected_version}" |
| 142 | + return 0 |
| 143 | + else |
| 144 | + print_error "version file not found: ${CMAKEW_VERSION_FILE}" |
| 145 | + return 1 |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +# ------------------------------------------------------------------------------ |
| 150 | +# Return current cmake version |
| 151 | +# ------------------------------------------------------------------------------ |
| 152 | + |
| 153 | +retrieve_current_cmake_version() { |
| 154 | + exe_path="$1" |
| 155 | + current_version=$("${exe_path}" --version | head -1 | awk '{ print $3 }') |
| 156 | + # print_info "current cmake version: ${current_version}" |
| 157 | + echo "${current_version}" |
| 158 | + return 0 |
| 159 | +} |
| 160 | + |
| 161 | +# ------------------------------------------------------------------------------ |
| 162 | +# Retrieve the path to the cmake command to be launched |
| 163 | +# depending on local machine's OS and architecture |
| 164 | +# ------------------------------------------------------------------------------ |
| 165 | + |
| 166 | +retrieve_command_path() { |
| 167 | + case $(uname -s) in |
| 168 | + Darwin) |
| 169 | + os="macos" |
| 170 | + arch="universal" |
| 171 | + archive_extension="tar.gz" |
| 172 | + cmake_bin_dir="CMake.app/Contents/bin" |
| 173 | + cmd="${CMAKEW_CMD}" |
| 174 | + ;; |
| 175 | + Linux) |
| 176 | + os="linux" |
| 177 | + arch="x86_64" |
| 178 | + archive_extension="tar.gz" |
| 179 | + cmake_bin_dir="bin" |
| 180 | + cmd="${CMAKEW_CMD}" |
| 181 | + ;; |
| 182 | + MINGW64_NT-*) |
| 183 | + os="windows" |
| 184 | + arch="x86_64" |
| 185 | + archive_extension="zip" |
| 186 | + cmake_bin_dir="bin" |
| 187 | + cmd="${CMAKEW_CMD}.exe" |
| 188 | + ;; |
| 189 | + *) |
| 190 | + print_error "os $(uname -s) is currently not supported" |
| 191 | + exit 1 |
| 192 | + ;; |
| 193 | + esac |
| 194 | + |
| 195 | + # Expected cmake version |
| 196 | + expected_version=$(retrieve_expected_cmake_version) || return 1 |
| 197 | + |
| 198 | + cmake_home="cmake-${os}-${arch}" |
| 199 | + cmake_bin_path="${CMAKEW_CACHE_DIR}/${cmake_home}/${cmake_bin_dir}" |
| 200 | + cmake_exe_path="${cmake_bin_path}/${cmd}" |
| 201 | + |
| 202 | + file_missing=$(type "${cmake_exe_path}" >/dev/null 2>/dev/null; echo $?) |
| 203 | + # If the file already exists, check its current version |
| 204 | + version_mismatch=0 |
| 205 | + if [ "${file_missing}" -eq 0 ]; then |
| 206 | + current_version=$(retrieve_current_cmake_version "${cmake_exe_path}") |
| 207 | + version_mismatch="$( [ "${current_version}" = "${expected_version}" ]; echo $? )" |
| 208 | + fi |
| 209 | + # If the file does not exist or if versions do not match, download it from CMAKE GitHub repository |
| 210 | + if [ "${file_missing}" -ne 0 ] || [ "${version_mismatch}" -ne 0 ]; then |
| 211 | + download_cmake "${expected_version}" "${os}" "${arch}" "${archive_extension}" "${cmake_bin_path}" || return 1 |
| 212 | + fi |
| 213 | + |
| 214 | + echo "${cmake_exe_path}" |
| 215 | + return 0 |
| 216 | +} |
| 217 | + |
| 218 | +# ------------------------------------------------------------------------------ |
| 219 | +# Main |
| 220 | +# ------------------------------------------------------------------------------ |
| 221 | + |
| 222 | +cmd_path="$(retrieve_command_path)" |
| 223 | +# shellcheck disable=SC2181 |
| 224 | +[ $? -ne 0 ] && print_error "aborting" && exit 1 |
| 225 | +# shellcheck disable=SC2086 |
| 226 | +"${cmd_path}" "$@" |
0 commit comments