From 60c8e52c6ebb709f1155c6c6f446f34ee53af0a9 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 13 Mar 2025 15:24:21 +0100 Subject: [PATCH] scripts: adapt demo_common.sh and project_detection.sh to tf-psa-crypto Signed-off-by: Valerio Setti --- scripts/demo_common.sh | 54 ++++++++++++++++++++++------ scripts/project_detection.sh | 68 ++++++++++++++++++++++++++++++++---- 2 files changed, 104 insertions(+), 18 deletions(-) diff --git a/scripts/demo_common.sh b/scripts/demo_common.sh index 004e6ab20..4ad4c7187 100644 --- a/scripts/demo_common.sh +++ b/scripts/demo_common.sh @@ -16,18 +16,43 @@ set -e -u -## $root_dir is the root directory of the Mbed TLS source tree. +DEMO_COMMON_NEED_QUERY_COMPILE_TIME_CONFIG=${DEMO_COMMON_NEED_QUERY_COMPILE_TIME_CONFIG:-1} + +need_query_compile_time_config () { + if [ $DEMO_COMMON_NEED_QUERY_COMPILE_TIME_CONFIG -eq 1 ]; then + return 0; + else + return 1; + fi +} + +## At the end of the while loop below $root_dir will point to the root directory +## of the Mbed TLS or TF-PSA-Crypto 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 +## 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=5 +while true; do + # If we went up too many folders, then give up and return a failure. if [ $n -eq 0 ]; then echo >&2 "This doesn't seem to be an Mbed TLS source tree." exit 125 fi + # If we reached the Mbed TLS root folder then we're done. + if is_mbedtls_root "$root_dir"; then + break; + fi + # If we reached the TF-PSA-Crypto root folder and the script that sourced + # this file does not need query_compile_time_config (which is only available + # in Mbed TLS repo) then we're done. + if is_tf_psa_crypto_root "$root_dir" && ! need_query_compile_time_config; then + break; + fi + n=$((n - 1)) case $root_dir in .) root_dir="..";; @@ -38,10 +63,6 @@ while ! [ -d "$root_dir/programs" ] || ! [ -d "$root_dir/library" ]; do 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 - exit 1 + # Check if file exists. + if [ ! -f "$ROOT_PATH/$PROJECT_NAME_FILE" ]; then + if $EXIT_IF_NOT_FOUND ; then + echo "$ROOT_PATH/$PROJECT_NAME_FILE does not exist... Exiting..." >&2 + exit 1 + fi + # Simply return a failure in case we were asked not to fail in case of + # missing file. + return 1 + fi + + if read -r PROJECT_NAME < "$ROOT_PATH/$PROJECT_NAME_FILE"; then :; else + return 1 fi } +# Check if the current folder is the Mbed TLS root one. +# +# Warning: if this is not run from Mbed TLS/TF-PSA-Crypto root folder, the +# script is terminated with a failure. in_mbedtls_repo () { - read_project_name_file + read_project_name_file true . test "$PROJECT_NAME" = "Mbed TLS" } +# Check if the current folder is the TF-PSA-Crypto root one. +# +# Warning: if this is not run from Mbed TLS/TF-PSA-Crypto root folder, the +# script is terminated with a failure. in_tf_psa_crypto_repo () { - read_project_name_file + read_project_name_file true . + test "$PROJECT_NAME" = "TF-PSA-Crypto" +} + +# Check if $1 is an Mbed TLS root folder. +# +# Differently from in_mbedtls_repo() above, this can be run on any folder +# without causing the script to exit. +is_mbedtls_root() { + if ! read_project_name_file false $1 ; then + return 1 + fi + + test "$PROJECT_NAME" = "Mbed TLS" +} + +# Check if $1 is a TF-PSA-Crypto root folder. +# +# Differently from in_tf_psa_crypto_repo() above, this can be run on any folder +# without causing the script to exit. +is_tf_psa_crypto_root() { + if ! read_project_name_file false $1 ; then + return 1 + fi + test "$PROJECT_NAME" = "TF-PSA-Crypto" }