|
1 | | -#!/bin/bash |
2 | | -set -e # fail and exit on any command erroring |
3 | | -set -x # print evaluated commands |
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2025 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 | +# https://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. |
4 | 15 |
|
5 | | -which bazel |
6 | | -bazel version |
| 16 | +set -eo pipefail -o errtrace |
7 | 17 |
|
8 | | -# Attempt to build all components in SSE and basic mode. |
9 | | -# The Github Action MacOS VMs may run on different-capability CPUs, so all AVX tests |
10 | | -# are excluded from the build and test process. |
11 | | -# apps are sample applications for experts and are meant to be run on only on Linux. |
| 18 | +declare -r usage="Usage: ${0##*/} [-h | --help | help] [bazel options ...] |
| 19 | +Run the programs in tests/, and on Linux, also build the programs in apps/. |
12 | 20 |
|
| 21 | +If the first option on the command line is -h, --help, or help, this help text |
| 22 | +will be printed and the program will exit. Any other options on the command |
| 23 | +line are passed directly to Bazel." |
| 24 | + |
| 25 | +# Exit early if the user requested help. |
| 26 | +if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then |
| 27 | + echo "$usage" |
| 28 | + exit 0 |
| 29 | +fi |
| 30 | + |
| 31 | +declare features="" |
| 32 | +shopt -s nocasematch |
| 33 | +# Note: can't use Bash $OSTYPE var here b/c the value is "linux-gnu" on Win 10. |
| 34 | +case "$(uname -s)" in |
| 35 | + darwin*) |
| 36 | + features=$(sysctl machdep.cpu.features) |
| 37 | + ;; |
| 38 | + linux*) |
| 39 | + features=$(grep -m1 -i "^flags" /proc/cpuinfo) |
| 40 | + ;; |
| 41 | + windows*|cygwin*|mingw32*|msys*|mingw*) |
| 42 | + features=$(wmic cpu get Caption,InstructionSet /value 2>/dev/null) |
| 43 | + ;; |
| 44 | + *) |
| 45 | + echo "Unsupported OS: $(uname -s)" |
| 46 | + exit 1 |
| 47 | + ;; |
| 48 | +esac |
| 49 | +shopt -u nocasematch |
| 50 | + |
| 51 | +# Unless we can tell this system supports AVX, we skip those tests. |
| 52 | +declare filters="" |
| 53 | +[[ "$features" == *avx2* ]] || filters+=",-avx" |
| 54 | +[[ "$features" == *sse* ]] || filters+=",-sse" |
| 55 | +filters="${filters#,}" |
| 56 | + |
| 57 | +declare -a build_filters=() |
| 58 | +declare -a test_filters=() |
| 59 | +if [[ -n "$filters" ]]; then |
| 60 | + build_filters=( "--build_tag_filters=$filters" ) |
| 61 | + test_filters=( "--test_tag_filters=$filters" ) |
| 62 | +fi |
| 63 | + |
| 64 | +# Apps are sample programs and are only meant to run on Linux. |
13 | 65 | if [[ "$OSTYPE" == "linux-gnu"* ]]; then |
14 | | - bazel build --config=sse apps:all |
15 | | - bazel build apps:all |
| 66 | + bazel build "${build_filters[@]}" --config=sse "$@" apps:all |
| 67 | + bazel build "${build_filters[@]}" "$@" apps:all |
16 | 68 | fi |
17 | 69 |
|
18 | | -# Run all basic tests. |
19 | | -set e # Ignore errors until artifacts are collected. |
20 | | -EXIT_CODE=0 |
21 | | -for TARGET in bitstring_test channels_cirq_test circuit_qsim_parser_test expect_test \ |
22 | | - fuser_basic_test gates_qsim_test hybrid_avx_test matrix_test qtrajectory_avx_test \ |
23 | | - run_qsim_test run_qsimh_test simulator_basic_test simulator_sse_test statespace_basic_test \ |
24 | | - statespace_sse_test unitary_calculator_basic_test unitary_calculator_sse_test \ |
25 | | - unitaryspace_basic_test unitaryspace_sse_test vectorspace_test; do \ |
26 | | - if ! bazel test --test_output=errors tests:${TARGET}; then |
27 | | - EXIT_CODE=1 |
28 | | - fi |
29 | | -done |
| 70 | +# Run all basic tests. This should work on all platforms. |
| 71 | +bazel test "${build_filters[@]}" "${test_filters[@]}" "$@" tests:all |
0 commit comments