Parse dependency versions from TF-M CMake

To make sure CI totally aligned with TF-M, it is better to automatically
parse dependency versions by CI instead of passing a fixed version via
Jenkins environment variable.

This patch adds the logic to select dependency version specified in TF-M
CMake system by default. And CI user can also manually change the
dependency version by setting the corresponding Jenkins environment
variable when triggerring CI jobs.

For the dependency version which differs from platforms, for example
tf-m-extras, the version is double checked with the value in CMake Cache
in each single build-config job.

Signed-off-by: Xinyu Zhang <xinyu.zhang@arm.com>
Change-Id: Ia528783f4235786530c7f25785facae93534c1d3
diff --git a/run-build.sh b/run-build.sh
index f114702..c01c6b3 100755
--- a/run-build.sh
+++ b/run-build.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -13,6 +13,31 @@
 # Expected to have trusted-firmware-m cloned to same level as this git tree
 #
 
+. $(dirname $0)/util_cmake.sh
+. $(dirname $0)/util_git.sh
+
+# For dependency that differs from platforms, the versions need to be checkded
+# in each single build job.
+function check_dependency_version() {
+    TFM_EXTRAS_PATH="${WORKSPACE}/tf-m-extras"
+    TFM_EXTRAS_REFSPEC="$(get_cmake_cache ${WORKSPACE}/trusted-firmware-m/build TFM_EXTRAS_REPO_VERSION)"
+
+    # Array containing "<repo path>;<refspec>" elements
+    dependency_repos=(
+        "${TFM_EXTRAS_PATH};${TFM_EXTRAS_REFSPEC}"
+    )
+
+    for repo in ${dependency_repos[@]}; do
+        # Parse the repo elements
+        REPO_PATH="$(echo "${repo}" | awk -F ';' '{print $1}')"
+        REPO_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $2}')"
+
+        if [ ! -z "$REPO_REFSPEC" ] ; then
+            git_checkout $REPO_PATH $REPO_REFSPEC
+        fi
+    done
+}
+
 set -ex
 
 if [ -z "$CONFIG_NAME" ] ; then
@@ -49,12 +74,12 @@
 fi
 
 if [ -z "$cmake_config_cmd" ] ; then
-    echo "No CMake config commands found."
+    echo "No CMake config command found."
     exit 1
 fi
 
 if [ -z "$cmake_build_cmd" ] ; then
-    echo "No build image commands found."
+    echo "No CMake build command found."
     exit 1
 fi
 
@@ -80,4 +105,16 @@
 mkdir trusted-firmware-m/build
 cd trusted-firmware-m/build
 
-eval "set -ex ; $cmake_config_cmd; $cmake_build_cmd; $post_build_cmd"
+set +e
+eval $cmake_config_cmd
+cmake_cfg_error=$?
+set -e
+
+check_dependency_version
+
+if [ $cmake_cfg_error != 0 ] ; then
+    rm -rf trusted-firmware-m/build/*
+    eval $cmake_config_cmd
+fi
+
+eval "$cmake_build_cmd; $post_build_cmd"