xref: /aosp_15_r20/external/tink/kokoro/testutils/run_cmake_tests.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 script builds with CMake and runs tests within a given directory.
18#
19# Users must spcify the CMake project directory. Optionally, users may specify
20# a list of additional CMake arguments.
21#
22# Usage:
23#   ./kokoro/testutils/run_cmake_tests.sh \
24#     <project directory> \
25#     [<additional CMake param> <additional CMake param> ...]
26
27set -eEo pipefail
28
29usage() {
30  echo "Usage: $0 <project directory> \\"
31  echo "         [<additional CMake param> <additional CMake param> ...]"
32  exit 1
33}
34
35CMAKE_PROJECT_DIR=
36ADDITIONAL_CMAKE_PARAMETERS=
37
38#######################################
39# Process command line arguments.
40#
41# Globals:
42#   PROJECT_DIRECTORY
43#
44#######################################
45process_args() {
46  CMAKE_PROJECT_DIR="$1"
47  readonly CMAKE_PROJECT_DIR
48
49  if [[ -z "${CMAKE_PROJECT_DIR}" ]]; then
50    usage
51  fi
52
53  shift 1
54  ADDITIONAL_CMAKE_PARAMETERS=("$@")
55  readonly ADDITIONAL_CMAKE_PARAMETERS
56}
57
58main() {
59  process_args "$@"
60  local -r cmake_parameters=(
61    -DTINK_BUILD_TESTS=ON
62    -DCMAKE_CXX_STANDARD=14
63    -DCMAKE_CXX_STANDARD_REQUIRED=ON
64    "${ADDITIONAL_CMAKE_PARAMETERS[@]}"
65  )
66  # We need an absolute path to the CMake project directory.
67  local -r tink_cmake_project_dir="$(cd "${CMAKE_PROJECT_DIR}" && pwd)"
68  local -r cmake_build_dir="$(mktemp -dt cmake-build.XXXXXX)"
69  cd "${cmake_build_dir}"
70  cmake --version
71  cmake "${tink_cmake_project_dir}" "${cmake_parameters[@]}"
72  cmake --build . --parallel "$(nproc)"
73  CTEST_OUTPUT_ON_FAILURE=1 ctest --parallel "$(nproc)"
74}
75
76main "$@"
77