Skip to content

Rewrite dev_tools/test_libs.sh #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 10, 2025
88 changes: 65 additions & 23 deletions dev_tools/test_libs.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
#!/bin/bash
set -e # fail and exit on any command erroring
set -x # print evaluated commands
#!/usr/bin/env bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

which bazel
bazel version
set -eo pipefail -o errtrace

# Attempt to build all components in SSE and basic mode.
# The Github Action MacOS VMs may run on different-capability CPUs, so all AVX tests
# are excluded from the build and test process.
# apps are sample applications for experts and are meant to be run on only on Linux.
declare -r usage="Usage: ${0##*/} [-h | --help | help] [bazel options ...]
Run the programs in tests/, and on Linux, also build the programs in apps/.

If the first option on the command line is -h, --help, or help, this help text
will be printed and the program will exit. Any other options on the command
line are passed directly to Bazel."

# Exit early if the user requested help.
if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
echo "$usage"
exit 0
fi

declare features=""
shopt -s nocasematch
# Note: can't use Bash $OSTYPE var here b/c the value is "linux-gnu" on Win 10.
case "$(uname -s)" in
darwin*)
features=$(sysctl machdep.cpu.features)
;;
linux*)
features=$(grep -m1 -i "^flags" /proc/cpuinfo)
;;
windows*|cygwin*|mingw32*|msys*|mingw*)
features=$(wmic cpu get Caption,InstructionSet /value 2>/dev/null)
;;
*)
echo "Unsupported OS: $(uname -s)"
exit 1
;;
esac
shopt -u nocasematch

# Unless we can tell this system supports AVX, we skip those tests.
declare filters=""
[[ "$features" == *avx2* ]] || filters+=",-avx"
[[ "$features" == *sse* ]] || filters+=",-sse"
filters="${filters#,}"

declare -a build_filters=()
declare -a test_filters=()
if [[ -n "$filters" ]]; then
build_filters=( "--build_tag_filters=$filters" )
test_filters=( "--test_tag_filters=$filters" )
fi

# Apps are sample programs and are only meant to run on Linux.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
bazel build --config=sse apps:all
bazel build apps:all
bazel build "${build_filters[@]}" --config=sse "$@" apps:all
bazel build "${build_filters[@]}" "$@" apps:all
fi

# Run all basic tests.
set e # Ignore errors until artifacts are collected.
EXIT_CODE=0
for TARGET in bitstring_test channels_cirq_test circuit_qsim_parser_test expect_test \
fuser_basic_test gates_qsim_test hybrid_avx_test matrix_test qtrajectory_avx_test \
run_qsim_test run_qsimh_test simulator_basic_test simulator_sse_test statespace_basic_test \
statespace_sse_test unitary_calculator_basic_test unitary_calculator_sse_test \
unitaryspace_basic_test unitaryspace_sse_test vectorspace_test; do \
if ! bazel test --test_output=errors tests:${TARGET}; then
EXIT_CODE=1
fi
done
# Run all basic tests. This should work on all platforms.
bazel test "${build_filters[@]}" "${test_filters[@]}" "$@" tests:all
Loading