project_detection.sh (3464B)
1 # project-detection.sh 2 # 3 # Copyright The Mbed TLS Contributors 4 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5 # 6 # Purpose 7 # 8 # This script contains functions for shell scripts to 9 # help detect which project (Mbed TLS, TF-PSA-Crypto) 10 # or which Mbed TLS branch they are in. 11 12 # Project detection. 13 # 14 # Both Mbed TLS and TF-PSA-Cryto repos have a "scripts/project_name.txt" file 15 # which contains the name of the project. They are used in scripts to know in 16 # which project/folder we're in. 17 # This function accepts 2 parameters: 18 # - $1: boolean value which defines the behavior in case 19 # "scripts/project_name.txt" is not found: 20 # - 1: exit with error message 21 # - 0: simply return an error 22 # - $2: mandatory value which defined the root folder where to look for 23 # "scripts/project_name.txt". 24 read_project_name_file () { 25 EXIT_IF_NOT_FOUND=$1 26 ROOT_PATH=$2 27 28 PROJECT_NAME_FILE="scripts/project_name.txt" 29 30 # Check if file exists. 31 if [ ! -f "$ROOT_PATH/$PROJECT_NAME_FILE" ]; then 32 if $EXIT_IF_NOT_FOUND ; then 33 echo "$ROOT_PATH/$PROJECT_NAME_FILE does not exist... Exiting..." >&2 34 exit 1 35 fi 36 # Simply return a failure in case we were asked not to fail in case of 37 # missing file. 38 return 1 39 fi 40 41 if read -r PROJECT_NAME < "$ROOT_PATH/$PROJECT_NAME_FILE"; then :; else 42 return 1 43 fi 44 } 45 46 # Check if the current folder is the Mbed TLS root one. 47 # 48 # Warning: if this is not run from Mbed TLS/TF-PSA-Crypto root folder, the 49 # script is terminated with a failure. 50 in_mbedtls_repo () { 51 read_project_name_file true . 52 test "$PROJECT_NAME" = "Mbed TLS" 53 } 54 55 # Check if the current folder is the TF-PSA-Crypto root one. 56 # 57 # Warning: if this is not run from Mbed TLS/TF-PSA-Crypto root folder, the 58 # script is terminated with a failure. 59 in_tf_psa_crypto_repo () { 60 read_project_name_file true . 61 test "$PROJECT_NAME" = "TF-PSA-Crypto" 62 } 63 64 # Check if $1 is an Mbed TLS root folder. 65 # 66 # Differently from in_mbedtls_repo() above, this can be run on any folder 67 # without causing the script to exit. 68 is_mbedtls_root() { 69 if ! read_project_name_file false $1 ; then 70 return 1 71 fi 72 73 test "$PROJECT_NAME" = "Mbed TLS" 74 } 75 76 # Check if $1 is a TF-PSA-Crypto root folder. 77 # 78 # Differently from in_tf_psa_crypto_repo() above, this can be run on any folder 79 # without causing the script to exit. 80 is_tf_psa_crypto_root() { 81 if ! read_project_name_file false $1 ; then 82 return 1 83 fi 84 85 test "$PROJECT_NAME" = "TF-PSA-Crypto" 86 } 87 88 #Branch detection 89 read_build_info () { 90 SCRIPT_DIR=$(pwd) 91 92 BUILD_INFO_FILE="include/mbedtls/build_info.h" 93 94 if [ ! -f "$BUILD_INFO_FILE" ]; then 95 echo "File $BUILD_INFO_FILE not found." 96 exit 1 97 fi 98 99 MBEDTLS_VERSION_MAJOR=$(grep "^#define MBEDTLS_VERSION_MAJOR" "$BUILD_INFO_FILE" | awk '{print $3}') 100 MBEDTLS_VERSION_MINOR=$(grep "^#define MBEDTLS_VERSION_MINOR" "$BUILD_INFO_FILE" | awk '{print $3}') 101 102 if [ -z "$MBEDTLS_VERSION_MAJOR" ]; then 103 echo "MBEDTLS_VERSION_MAJOR not found in $BUILD_INFO_FILE." 104 exit 1 105 fi 106 107 if [ -z "$MBEDTLS_VERSION_MINOR" ]; then 108 echo "MBEDTLS_VERSION_MINOR not found in $BUILD_INFO_FILE." 109 exit 1 110 fi 111 } 112 113 in_3_6_branch () { 114 read_build_info 115 test $MBEDTLS_VERSION_MAJOR = "3" && test $MBEDTLS_VERSION_MINOR = "6" 116 } 117 118 in_4_x_branch () { 119 read_build_info 120 test $MBEDTLS_VERSION_MAJOR = "4" 121 }