diff --git a/tests/scripts/run_demos.py b/tests/scripts/run_demos.py index 6a63d232fe..75febcdd83 100755 --- a/tests/scripts/run_demos.py +++ b/tests/scripts/run_demos.py @@ -44,7 +44,7 @@ def run_all_demos(quiet=False): Return True if all demos passed and False if a demo fails. """ - all_demos = glob.glob('programs/*/*_demo.sh') + all_demos = glob.glob('tf-psa-crypto/programs/*/*_demo.sh') if not all_demos: # Keep the message on one line. pylint: disable=line-too-long raise Exception('No demos found. run_demos needs to operate from the Mbed TLS toplevel directory.') diff --git a/tf-psa-crypto/programs/demo_common.sh b/tf-psa-crypto/programs/demo_common.sh new file mode 100644 index 0000000000..d8fcda5544 --- /dev/null +++ b/tf-psa-crypto/programs/demo_common.sh @@ -0,0 +1,137 @@ +## Common shell functions used by demo scripts programs/*/*.sh. + +## How to write a demo script +## ========================== +## +## Include this file near the top of each demo script: +## . "${0%/*}/../demo_common.sh" +## +## Start with a "msg" call that explains the purpose of the script. +## Then call the "depends_on" function to ensure that all config +## dependencies are met. +## +## As the last thing in the script, call the cleanup function. +## +## You can use the functions and variables described below. + +set -e -u + +## $root_dir is the root directory of the Mbed TLS source tree. +root_dir="${0%/*}" +# Find a nice path to the root directory, avoiding unnecessary "../". +# The code supports demo scripts nested up to 4 levels deep. +# The code works no matter where the demo script is relative to the current +# directory, even if it is called with a relative path. +n=4 # limit the search depth +while ! [ -d "$root_dir/programs" ] || ! [ -d "$root_dir/library" ]; do + if [ $n -eq 0 ]; then + echo >&2 "This doesn't seem to be an Mbed TLS source tree." + exit 125 + fi + n=$((n - 1)) + case $root_dir in + .) root_dir="..";; + ..|?*/..) root_dir="$root_dir/..";; + ?*/*) root_dir="${root_dir%/*}";; + /*) root_dir="/";; + *) root_dir=".";; + esac +done + +## $programs_dir is the directory containing the sample programs. +# Assume an in-tree build. +programs_dir="$root_dir/programs" + +## msg LINE... +## msg &2 <