build: Print the full TF-M version on boot

Unify the version extraction with documentation.

Print the boot TF-M version with tag and SHA, provieded by git describe
in a form: <TAG>+<SHA>

Signed-off-by: Anton Komlev <anton.komlev@arm.com>
Change-Id: I64b6202e231a98860973f7cb2ba8d5e3eeffb34f
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ef673e0..03fe0ba 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,7 @@
 
 cmake_minimum_required(VERSION 3.15)
 
-include(version.cmake)
+include(cmake/version.cmake)
 
 ############################ CONFIGURATION #####################################
 
diff --git a/cmake/version.cmake b/cmake/version.cmake
new file mode 100644
index 0000000..97750f5
--- /dev/null
+++ b/cmake/version.cmake
@@ -0,0 +1,15 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+execute_process(COMMAND git describe --tags --always
+    OUTPUT_VARIABLE TFM_VERSION_FULL
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+string(REGEX REPLACE "TF-M" "" TFM_VERSION_FULL ${TFM_VERSION_FULL})
+# remove a commit number
+string(REGEX REPLACE "-[0-9]+-g" "+" TFM_VERSION_FULL ${TFM_VERSION_FULL})
+string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" TFM_VERSION ${TFM_VERSION_FULL})
diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt
index c6caa21..69f129c 100644
--- a/docs/CMakeLists.txt
+++ b/docs/CMakeLists.txt
@@ -23,7 +23,7 @@
 
 ################################## ENV #########################################
 
-include(../version.cmake)
+include(../cmake/version.cmake)
 
 project("Trusted Firmware M. Documentation" VERSION ${TFM_VERSION} LANGUAGES)
 
diff --git a/docs/tfm_env.py.in b/docs/tfm_env.py.in
index c4f7a34..ca5a927 100644
--- a/docs/tfm_env.py.in
+++ b/docs/tfm_env.py.in
@@ -20,5 +20,5 @@
               "SPHINXCFG_TEMPLATE_FILE"    : "@SPHINXCFG_TEMPLATE_FILE@",
               "SPHINXCFG_COPY_FILES"       : "@SPHINXCFG_COPY_FILES@",
               "SPHINXCFG_RENDER_CONF"      : "@SPHINXCFG_RENDER_CONF@",
-              "SPHINXCFG_TFM_VERSION"      : "v@CMAKE_PROJECT_VERSION@",
+              "SPHINXCFG_TFM_VERSION"      : "@TFM_VERSION_FULL@",
             }
diff --git a/secure_fw/spm/cmsis_func/main.c b/secure_fw/spm/cmsis_func/main.c
index b08a0d4..c5fa6b5 100644
--- a/secure_fw/spm/cmsis_func/main.c
+++ b/secure_fw/spm/cmsis_func/main.c
@@ -183,7 +183,7 @@
     FIH_LABEL_CRITICAL_POINT();
 
     /* Print the TF-M version */
-    SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
+    SPMLOG_INFMSG("\033[1;34mBooting TF-M "VERSION_FULLSTR"\033[0m\r\n");
 
     spm_err = tfm_spm_db_init();
     if (spm_err != SPM_ERR_OK) {
diff --git a/secure_fw/spm/cmsis_psa/main.c b/secure_fw/spm/cmsis_psa/main.c
index 1777e44..4e886bb 100644
--- a/secure_fw/spm/cmsis_psa/main.c
+++ b/secure_fw/spm/cmsis_psa/main.c
@@ -133,7 +133,7 @@
     FIH_LABEL_CRITICAL_POINT();
 
     /* Print the TF-M version */
-    SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
+    SPMLOG_INFMSG("\033[1;34mBooting TF-M "VERSION_FULLSTR"\033[0m\r\n");
 
     /*
      * Prioritise secure exceptions to avoid NS being able to pre-empt
diff --git a/secure_fw/spm/include/tfm_version.h.in b/secure_fw/spm/include/tfm_version.h.in
index fa815f7..ff19e49 100644
--- a/secure_fw/spm/include/tfm_version.h.in
+++ b/secure_fw/spm/include/tfm_version.h.in
@@ -11,13 +11,13 @@
 /*
  * Defines for TFM version.
  */
-#cmakedefine TFM_VERSION   @TFM_VERSION@
-
-#define VERSION_STRING     ""
+#cmakedefine TFM_VERSION        @TFM_VERSION@
+#cmakedefine TFM_VERSION_FULL   @TFM_VERSION_FULL@
 
 #define VERSTR(x)          #x
 #define VERCON(x)          VERSTR(x)
 
-#define VERSION_FULLSTR    VERCON(TFM_VERSION)""VERSION_STRING
+#define VERSION_STR        VERCON(TFM_VERSION)
+#define VERSION_FULLSTR    VERCON(TFM_VERSION_FULL)
 
 #endif /* __TFM_VERSION_H__ */
diff --git a/tools/documentation/tfm_cmake_defaults.py b/tools/documentation/tfm_cmake_defaults.py
index 5b15e5f..0acfbc8 100644
--- a/tools/documentation/tfm_cmake_defaults.py
+++ b/tools/documentation/tfm_cmake_defaults.py
@@ -128,6 +128,25 @@
     tfm_def_doxygen_loc = find_package("doxygen")
     tfm_def_doxygen_dot_loc = find_package("dot")
 
+    try:
+        vrex = re.compile(r'TF-M(?P<GIT_VERSION>v.+?)'
+                          r'(-[0-9]+-g)?(?P<GIT_SHA>[a-f0-9]{7,})?$')
+
+        tfm_def_tfm_version = check_output("git describe --tags --always",
+                                           shell = True, encoding = 'UTF-8')
+
+        _v = vrex.match(tfm_def_tfm_version)
+
+        tfm_def_tfm_version = _v.group("GIT_VERSION")
+        if _v.group("GIT_SHA"):
+            tfm_def_tfm_version += "+" + _v.group("GIT_SHA")[:7]
+
+    except Exception as E:
+        try:
+            tfm_def_tfm_version
+        except NameError:
+            tfm_def_tfm_version = "Unknown"
+
 else:
     # #################### Cmake Defaults ################################## #
     tfm_def_root_dir = os.path.abspath(cmake_env["TFM_ROOT_DIR"])
@@ -159,44 +178,6 @@
     with open("tfm_env.py", "w", encoding='utf-8') as F:
         F.write("cmake_env =" + json.dumps(cmake_env))
 
-
-# Version will be retrieved in that order Git -> Cmake -> Boilerplate
-try:
-    vrex = re.compile(r'(?P<GIT_HASH>[a-f0-9]{40})'
-                      r'+\s+tag\s+refs\/tags\/TF-Mv(?P<VER_MAJ>\d+).'
-                      r'(?P<VER_MIN>\d+).?(?P<VER_HOT>\d+)(?P<RC>-RC\d+)?')
-
-    tfm_def_tfm_version = check_output("git for-each-ref refs/tags --sort=-taggerdate --count=1",
-                                        shell = True, encoding = 'UTF-8')
-
-    _v = vrex.search(tfm_def_tfm_version)
-    version  = [ _v.group("VER_MAJ"),
-                 _v.group("VER_MIN"),
-                 _v.group("VER_HOT"),
-                 _v.group("RC")]
-    git_hash  = _v.group("GIT_HASH")
-
-    # Sanitize the verison and remove empty entries
-    version = [i.replace("-","") for i in version if i]
-    tfm_def_tfm_version = "v"+".".join(version)
-
-    vlrex = re.compile(r'^(?P<GIT_HASH_LATEST>[a-f0-9]{40})')
-
-    git_hash_latest = check_output("git rev-parse HEAD",
-                                    shell = True, encoding = 'UTF-8')
-
-    git_hash_latest = vlrex.search(git_hash_latest).group('GIT_HASH_LATEST')
-
-    if git_hash != git_hash_latest:
-        git_hash_latest = git_hash_latest[:7]
-        tfm_def_tfm_version += "+ ({})".format(git_hash_latest)
-
-except Exception as E:
-    try:
-        tfm_def_tfm_version
-    except NameError:
-        tfm_def_tfm_version = "Unknown"
-
 # #################### User Defaults ######################################## #
 
 # Directories, referenced by TF-M root, which may contain releval documents
diff --git a/version.cmake b/version.cmake
deleted file mode 100644
index 6261cd0..0000000
--- a/version.cmake
+++ /dev/null
@@ -1,7 +0,0 @@
-#-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-#-------------------------------------------------------------------------------
-set(TFM_VERSION 1.5.0)