blob: ff8d57594d23edc9855fcb519deda8a656f4429a [file] [log] [blame]
Gilles Peskinec86f20a2021-04-22 00:20:47 +02001"""Mbed TLS build tree information and manipulation.
2"""
3
4# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinec86f20a2021-04-22 00:20:47 +02006#
Gilles Peskinec86f20a2021-04-22 00:20:47 +02007
8import os
Jerry Yue6369b02021-12-02 13:51:26 +08009import inspect
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000010from typing import Optional
Jerry Yue6369b02021-12-02 13:51:26 +080011
Ronald Cron070e8652023-10-09 10:25:45 +020012def looks_like_tf_psa_crypto_root(path: str) -> bool:
David Horstmann795d8b52023-07-18 17:03:03 +010013 """Whether the given directory looks like the root of the PSA Crypto source tree."""
14 return all(os.path.isdir(os.path.join(path, subdir))
David Horstmann2fde9992023-08-29 09:48:39 +010015 for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])
Gilles Peskinec86f20a2021-04-22 00:20:47 +020016
17def looks_like_mbedtls_root(path: str) -> bool:
18 """Whether the given directory looks like the root of the Mbed TLS source tree."""
19 return all(os.path.isdir(os.path.join(path, subdir))
20 for subdir in ['include', 'library', 'programs', 'tests'])
21
David Horstmann795d8b52023-07-18 17:03:03 +010022def looks_like_root(path: str) -> bool:
Ronald Cron070e8652023-10-09 10:25:45 +020023 return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)
David Horstmann795d8b52023-07-18 17:03:03 +010024
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000025def crypto_core_directory(root: Optional[str] = None) -> str:
Thomas Daubney46588de2023-11-30 13:59:30 +000026 """
27 Return the path of the directory containing the PSA crypto core
28 for either TF-PSA-Crypto or Mbed TLS.
29 """
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000030 if root is None:
Thomas Daubney755d3212023-11-22 17:18:22 +000031 root = guess_project_root()
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000032 if looks_like_tf_psa_crypto_root(root):
Thomas Daubneybeec4522023-11-24 10:48:44 +000033 return os.path.join(root, "core")
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000034 elif looks_like_mbedtls_root(root):
Thomas Daubneybeec4522023-11-24 10:48:44 +000035 return os.path.join(root, "library")
Thomas Daubney13ecb692023-11-16 18:34:58 +000036 else:
37 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
38
Thomas Daubney08c6dc42023-11-30 13:56:09 +000039def crypto_library_filename(root: Optional[str] = None) -> str:
Thomas Daubneycdbf2fd2023-11-24 10:54:56 +000040 """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
Thomas Daubney89324042023-11-23 10:14:12 +000041 if root is None:
42 root = guess_project_root()
43 if looks_like_tf_psa_crypto_root(root):
44 return "tfpsacrypto"
45 elif looks_like_mbedtls_root(root):
46 return "mbedcrypto"
47 else:
48 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
49
Gilles Peskined9071e72022-09-18 21:17:09 +020050def check_repo_path():
51 """
52 Check that the current working directory is the project root, and throw
53 an exception if not.
54 """
55 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
56 raise Exception("This script must be run from Mbed TLS root")
Jerry Yue6369b02021-12-02 13:51:26 +080057
Gilles Peskinec86f20a2021-04-22 00:20:47 +020058def chdir_to_root() -> None:
59 """Detect the root of the Mbed TLS source tree and change to it.
60
61 The current directory must be up to two levels deep inside an Mbed TLS
62 source tree.
63 """
64 for d in [os.path.curdir,
65 os.path.pardir,
66 os.path.join(os.path.pardir, os.path.pardir)]:
David Horstmann795d8b52023-07-18 17:03:03 +010067 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020068 os.chdir(d)
69 return
70 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080071
72
Thomas Daubney755d3212023-11-22 17:18:22 +000073def guess_project_root():
74 """Guess project source code directory.
Jerry Yue6369b02021-12-02 13:51:26 +080075
Thomas Daubney755d3212023-11-22 17:18:22 +000076 Return the first possible project root directory.
Jerry Yue6369b02021-12-02 13:51:26 +080077 """
78 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080079 for frame in inspect.stack():
80 path = os.path.dirname(frame.filename)
81 for d in ['.', os.path.pardir] \
82 + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
Jerry Yue6369b02021-12-02 13:51:26 +080083 d = os.path.abspath(os.path.join(path, d))
84 if d in dirs:
85 continue
86 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010087 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080088 return d
Thomas Daubney755d3212023-11-22 17:18:22 +000089 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')