blob: 86c838900024796b96be628b4a9e90e3196d6f9d [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():
Thomas Daubneyd1f29342023-11-30 17:25:55 +000051 """Check that the current working directory is the project root, and throw
Gilles Peskined9071e72022-09-18 21:17:09 +020052 an exception if not.
53 """
54 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
55 raise Exception("This script must be run from Mbed TLS root")
Jerry Yue6369b02021-12-02 13:51:26 +080056
Gilles Peskinec86f20a2021-04-22 00:20:47 +020057def chdir_to_root() -> None:
58 """Detect the root of the Mbed TLS source tree and change to it.
59
60 The current directory must be up to two levels deep inside an Mbed TLS
61 source tree.
62 """
63 for d in [os.path.curdir,
64 os.path.pardir,
65 os.path.join(os.path.pardir, os.path.pardir)]:
David Horstmann795d8b52023-07-18 17:03:03 +010066 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020067 os.chdir(d)
68 return
69 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080070
Thomas Daubney755d3212023-11-22 17:18:22 +000071def guess_project_root():
72 """Guess project source code directory.
Jerry Yue6369b02021-12-02 13:51:26 +080073
Thomas Daubney755d3212023-11-22 17:18:22 +000074 Return the first possible project root directory.
Jerry Yue6369b02021-12-02 13:51:26 +080075 """
76 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080077 for frame in inspect.stack():
78 path = os.path.dirname(frame.filename)
79 for d in ['.', os.path.pardir] \
80 + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
Jerry Yue6369b02021-12-02 13:51:26 +080081 d = os.path.abspath(os.path.join(path, d))
82 if d in dirs:
83 continue
84 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010085 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080086 return d
Thomas Daubney755d3212023-11-22 17:18:22 +000087 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
Thomas Daubneyd1f29342023-11-30 17:25:55 +000088
89def guess_mbedtls_root(root: Optional[str] = None) -> str:
90 """Guess Mbed TLS source code directory.
91
92 Return the first possible Mbed TLS root directory.
93 Raise an exception if we are not in Mbed TLS.
94 """
95 if root is None:
96 root = guess_project_root()
97 if looks_like_mbedtls_root(root):
98 return root
99 else:
100 raise Exception('Mbed TLS source tree not found')
Thomas Daubneydb80b232023-11-30 17:33:54 +0000101
102def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str:
103 """Guess TF-PSA-Crypto source code directory.
104
105 Return the first possible TF-PSA-Crypto root directory.
106 Raise an exception if we are not in TF-PSA-Crypto.
107 """
108 if root is None:
109 root = guess_project_root()
110 if looks_like_tf_psa_crypto_root(root):
111 return root
112 else:
113 raise Exception('TF-PSA-Crypto source tree not found')