diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index a657a5138..ec67e4cdf 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -7,6 +7,7 @@ import os import inspect +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.""" @@ -21,9 +22,40 @@ def looks_like_mbedtls_root(path: str) -> bool: def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) -def check_repo_path(): +def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str: """ - Check that the current working directory is the project root, and throw + Return the path of the directory containing the PSA crypto core + for either TF-PSA-Crypto or Mbed TLS. + + Returns either the full path or relative path depending on the + "relative" boolean argument. + """ + if root is None: + root = guess_project_root() + if looks_like_tf_psa_crypto_root(root): + if relative: + return "core" + return os.path.join(root, "core") + elif looks_like_mbedtls_root(root): + if relative: + return "library" + return os.path.join(root, "library") + else: + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + +def crypto_library_filename(root: Optional[str] = None) -> str: + """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS.""" + if root is None: + root = guess_project_root() + if looks_like_tf_psa_crypto_root(root): + return "tfpsacrypto" + elif looks_like_mbedtls_root(root): + return "mbedcrypto" + else: + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + +def check_repo_path(): + """Check that the current working directory is the project root, and throw an exception if not. """ if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): @@ -43,11 +75,10 @@ def chdir_to_root() -> None: return raise Exception('Mbed TLS source tree not found') +def guess_project_root(): + """Guess project source code directory. -def guess_mbedtls_root(): - """Guess mbedTLS source code directory. - - Return the first possible mbedTLS root directory + Return the first possible project root directory. """ dirs = set({}) for frame in inspect.stack(): @@ -60,4 +91,30 @@ def guess_mbedtls_root(): dirs.add(d) if looks_like_root(d): return d - raise Exception('Mbed TLS source tree not found') + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + +def guess_mbedtls_root(root: Optional[str] = None) -> str: + """Guess Mbed TLS source code directory. + + Return the first possible Mbed TLS root directory. + Raise an exception if we are not in Mbed TLS. + """ + if root is None: + root = guess_project_root() + if looks_like_mbedtls_root(root): + return root + else: + raise Exception('Mbed TLS source tree not found') + +def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str: + """Guess TF-PSA-Crypto source code directory. + + Return the first possible TF-PSA-Crypto root directory. + Raise an exception if we are not in TF-PSA-Crypto. + """ + if root is None: + root = guess_project_root() + if looks_like_tf_psa_crypto_root(root): + return root + else: + raise Exception('TF-PSA-Crypto source tree not found')