From fe72c06e406e7a4e3fbfad917dccba884cf64213 Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Mon, 9 Sep 2024 10:20:17 +0200 Subject: [PATCH 1/3] Use scripts/project_name.txt Use scripts/project_name.txt to detect between Mbed TLS and TF-PSA-Crypto project. Signed-off-by: Ronald Cron --- scripts/mbedtls_framework/build_tree.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/mbedtls_framework/build_tree.py b/scripts/mbedtls_framework/build_tree.py index 702a2cd9d..47ea0fc90 100644 --- a/scripts/mbedtls_framework/build_tree.py +++ b/scripts/mbedtls_framework/build_tree.py @@ -11,13 +11,19 @@ from typing import Optional def looks_like_tf_psa_crypto_root(path: str) -> bool: """Whether the given directory looks like the root of the PSA Crypto source tree.""" - return all(os.path.isdir(os.path.join(path, subdir)) - for subdir in ['include', 'core', 'drivers', 'programs', 'tests']) + try: + with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f: + return f.read() == "TF-PSA-Crypto\n" + except FileNotFoundError: + return False def looks_like_mbedtls_root(path: str) -> bool: """Whether the given directory looks like the root of the Mbed TLS source tree.""" - return all(os.path.isdir(os.path.join(path, subdir)) - for subdir in ['include', 'library', 'programs', 'tests']) + try: + with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f: + return f.read() == "Mbed TLS\n" + except FileNotFoundError: + return False def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) From 55b84c2ca44489eef2ade9eaed460a4f9068930a Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Mon, 9 Sep 2024 13:46:16 +0200 Subject: [PATCH 2/3] Add branch 3.6 detection in build_tree.py Signed-off-by: Ronald Cron --- scripts/mbedtls_framework/build_tree.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/mbedtls_framework/build_tree.py b/scripts/mbedtls_framework/build_tree.py index 47ea0fc90..8a72a06b4 100644 --- a/scripts/mbedtls_framework/build_tree.py +++ b/scripts/mbedtls_framework/build_tree.py @@ -7,6 +7,7 @@ import os import inspect +import re from typing import Optional def looks_like_tf_psa_crypto_root(path: str) -> bool: @@ -128,3 +129,13 @@ def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str: return root else: raise Exception('TF-PSA-Crypto source tree not found') + +def is_mbedtls_3_6_branch(root: Optional[str] = None) -> bool: + """Whether root is the root directory of the Mbed TLS 3.6 branch """ + if root is None: + root = os.getcwd() + if not looks_like_mbedtls_root(root): + return False + with open(os.path.join(root, 'include', 'mbedtls', 'build_info.h'), 'r') as f: + pattern = re.compile("#define MBEDTLS_VERSION_NUMBER.*0x0306") + return pattern.search(f.read()) is not None From 0249164939b6bed24772d0e8214be4c3b239b845 Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Wed, 11 Sep 2024 09:40:56 +0200 Subject: [PATCH 3/3] Improve Mbed TLS 3.6 working tree detection Signed-off-by: Ronald Cron --- scripts/mbedtls_framework/build_tree.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/scripts/mbedtls_framework/build_tree.py b/scripts/mbedtls_framework/build_tree.py index 8a72a06b4..75f9b6b0b 100644 --- a/scripts/mbedtls_framework/build_tree.py +++ b/scripts/mbedtls_framework/build_tree.py @@ -130,12 +130,15 @@ def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str: else: raise Exception('TF-PSA-Crypto source tree not found') -def is_mbedtls_3_6_branch(root: Optional[str] = None) -> bool: - """Whether root is the root directory of the Mbed TLS 3.6 branch """ - if root is None: - root = os.getcwd() +def is_mbedtls_3_6() -> bool: + """Whether the working tree is an Mbed TLS 3.6 one or not + + Return false in we are in TF-PSA-Crypto or in Mbed TLS but with a version + different from 3.6.x. + Raise an exception if we are neither in Mbed TLS nor in TF-PSA-Crypto. + """ + root = guess_project_root() if not looks_like_mbedtls_root(root): return False with open(os.path.join(root, 'include', 'mbedtls', 'build_info.h'), 'r') as f: - pattern = re.compile("#define MBEDTLS_VERSION_NUMBER.*0x0306") - return pattern.search(f.read()) is not None + return re.search(r"#define MBEDTLS_VERSION_NUMBER.*0x0306", f.read()) is not None