2021-04-22 00:20:47 +02:00
|
|
|
"""Mbed TLS build tree information and manipulation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-04-22 00:20:47 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2021-12-02 06:51:26 +01:00
|
|
|
import inspect
|
2023-11-22 18:00:34 +01:00
|
|
|
from typing import Optional
|
2021-12-02 06:51:26 +01:00
|
|
|
|
2023-10-09 10:25:45 +02:00
|
|
|
def looks_like_tf_psa_crypto_root(path: str) -> bool:
|
2023-07-18 18:03:03 +02:00
|
|
|
"""Whether the given directory looks like the root of the PSA Crypto source tree."""
|
|
|
|
return all(os.path.isdir(os.path.join(path, subdir))
|
2023-08-29 10:48:39 +02:00
|
|
|
for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])
|
2021-04-22 00:20:47 +02:00
|
|
|
|
|
|
|
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'])
|
|
|
|
|
2023-07-18 18:03:03 +02:00
|
|
|
def looks_like_root(path: str) -> bool:
|
2023-10-09 10:25:45 +02:00
|
|
|
return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)
|
2023-07-18 18:03:03 +02:00
|
|
|
|
2023-12-01 18:18:38 +01:00
|
|
|
def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str:
|
2023-11-30 14:59:30 +01:00
|
|
|
"""
|
2023-12-01 10:52:35 +01:00
|
|
|
Return the path of the directory containing the PSA crypto core
|
2023-11-30 14:59:30 +01:00
|
|
|
for either TF-PSA-Crypto or Mbed TLS.
|
2023-12-01 18:18:38 +01:00
|
|
|
|
|
|
|
Returns either the full path or relative path depending on the
|
|
|
|
"relative" boolean argument.
|
2023-11-30 14:59:30 +01:00
|
|
|
"""
|
2023-11-22 18:00:34 +01:00
|
|
|
if root is None:
|
2023-11-22 18:18:22 +01:00
|
|
|
root = guess_project_root()
|
2023-11-22 18:00:34 +01:00
|
|
|
if looks_like_tf_psa_crypto_root(root):
|
2023-12-01 18:18:38 +01:00
|
|
|
if relative:
|
|
|
|
return "core"
|
2023-11-24 11:48:44 +01:00
|
|
|
return os.path.join(root, "core")
|
2023-11-22 18:00:34 +01:00
|
|
|
elif looks_like_mbedtls_root(root):
|
2023-12-01 18:18:38 +01:00
|
|
|
if relative:
|
|
|
|
return "library"
|
2023-11-24 11:48:44 +01:00
|
|
|
return os.path.join(root, "library")
|
2023-11-16 19:34:58 +01:00
|
|
|
else:
|
|
|
|
raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
|
|
|
|
|
2023-11-30 14:56:09 +01:00
|
|
|
def crypto_library_filename(root: Optional[str] = None) -> str:
|
2023-11-24 11:54:56 +01:00
|
|
|
"""Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
|
2023-11-23 11:14:12 +01:00
|
|
|
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')
|
|
|
|
|
2022-09-18 21:17:09 +02:00
|
|
|
def check_repo_path():
|
2023-11-30 18:25:55 +01:00
|
|
|
"""Check that the current working directory is the project root, and throw
|
2022-09-18 21:17:09 +02:00
|
|
|
an exception if not.
|
|
|
|
"""
|
|
|
|
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
|
|
|
raise Exception("This script must be run from Mbed TLS root")
|
2021-12-02 06:51:26 +01:00
|
|
|
|
2021-04-22 00:20:47 +02:00
|
|
|
def chdir_to_root() -> None:
|
|
|
|
"""Detect the root of the Mbed TLS source tree and change to it.
|
|
|
|
|
|
|
|
The current directory must be up to two levels deep inside an Mbed TLS
|
|
|
|
source tree.
|
|
|
|
"""
|
|
|
|
for d in [os.path.curdir,
|
|
|
|
os.path.pardir,
|
|
|
|
os.path.join(os.path.pardir, os.path.pardir)]:
|
2023-07-18 18:03:03 +02:00
|
|
|
if looks_like_root(d):
|
2021-04-22 00:20:47 +02:00
|
|
|
os.chdir(d)
|
|
|
|
return
|
|
|
|
raise Exception('Mbed TLS source tree not found')
|
2021-12-02 06:51:26 +01:00
|
|
|
|
2023-11-22 18:18:22 +01:00
|
|
|
def guess_project_root():
|
|
|
|
"""Guess project source code directory.
|
2021-12-02 06:51:26 +01:00
|
|
|
|
2023-11-22 18:18:22 +01:00
|
|
|
Return the first possible project root directory.
|
2021-12-02 06:51:26 +01:00
|
|
|
"""
|
|
|
|
dirs = set({})
|
2021-12-10 07:21:27 +01:00
|
|
|
for frame in inspect.stack():
|
|
|
|
path = os.path.dirname(frame.filename)
|
|
|
|
for d in ['.', os.path.pardir] \
|
|
|
|
+ [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
|
2021-12-02 06:51:26 +01:00
|
|
|
d = os.path.abspath(os.path.join(path, d))
|
|
|
|
if d in dirs:
|
|
|
|
continue
|
|
|
|
dirs.add(d)
|
2023-07-18 18:03:03 +02:00
|
|
|
if looks_like_root(d):
|
2021-12-02 06:51:26 +01:00
|
|
|
return d
|
2023-11-22 18:18:22 +01:00
|
|
|
raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
|
2023-11-30 18:25:55 +01:00
|
|
|
|
|
|
|
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')
|
2023-11-30 18:33:54 +01:00
|
|
|
|
|
|
|
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')
|