xref: /aosp_15_r20/external/tink/kokoro/testutils/install_cmake.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15################################################################################
16
17# This scripts installs CMake of a given version and SHA256. If the version is
18# not specified, DEFAULT_CMAKE_VERSION is used; similarly the digest is by
19# default DEFAULT_CMAKE_SHA256.
20#
21# NOTEs:
22#   * If not running on Kokoro, this script will do nothing.
23#   * This script MUST be sourced to update the environment of the calling
24#     script.
25#   * If a custom version is passed, the corresponding digest should be passed
26#     too.
27#
28# Usage:
29#   source ./kokoro/testutils/install_cmake.sh [version] [sha256]
30
31readonly DEFAULT_CMAKE_VERSION="3.21.3"
32readonly DEFAULT_CMAKE_SHA256="a19aa9fcf368e9d923cdb29189528f0fe00a0d08e752ba4e547af91817518696"
33
34install_cmake() {
35  local cmake_version="${1:-${DEFAULT_CMAKE_VERSION}}"
36  local cmake_sha256="${2:-${DEFAULT_CMAKE_SHA256}}"
37  local cmake_name="cmake-${cmake_version}-linux-x86_64"
38  local cmake_archive="${cmake_name}.tar.gz"
39  local cmake_url="https://github.com/Kitware/CMake/releases/download/v${cmake_version}/${cmake_archive}"
40  local cmake_tmpdir="$(mktemp -dt tink-cmake.XXXXXX)"
41  local -r cmake_folder="${cmake_tmpdir}/${cmake_name}"
42  (
43    cd "${cmake_tmpdir}"
44    curl -OLsS "${cmake_url}"
45    echo "${cmake_sha256} ${cmake_archive}" | sha256sum -c
46    mkdir -p "${cmake_folder}"
47    # This is needed because versions <= 3.19.X are named
48    # cmake-<VERSION>-Linux-x86_64.
49    tar xzf "${cmake_archive}" -C "${cmake_folder}" --strip-component=1
50  )
51  export PATH="${cmake_tmpdir}/${cmake_name}/bin:${PATH}"
52}
53
54if [[ -n "${KOKORO_ARTIFACTS_DIR:-}" ]]; then
55  # If specifying the version, users must also specify the digest.
56  if (( "$#" == 1 )); then
57    echo \
58      "The SHA256 digest must be provided too when specifying CMake's version" \
59      >&2
60    exit 1
61  fi
62  install_cmake "$@"
63fi
64