Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 1 | """Mbed TLS build tree information and manipulation. |
| 2 | """ |
| 3 | |
| 4 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 5 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 6 | # |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 7 | |
| 8 | import os |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 9 | import inspect |
| 10 | |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 11 | def looks_like_tf_psa_crypto_root(path: str) -> bool: |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 12 | """Whether the given directory looks like the root of the PSA Crypto source tree.""" |
| 13 | return all(os.path.isdir(os.path.join(path, subdir)) |
David Horstmann | 2fde999 | 2023-08-29 09:48:39 +0100 | [diff] [blame] | 14 | for subdir in ['include', 'core', 'drivers', 'programs', 'tests']) |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 15 | |
| 16 | def looks_like_mbedtls_root(path: str) -> bool: |
| 17 | """Whether the given directory looks like the root of the Mbed TLS source tree.""" |
| 18 | return all(os.path.isdir(os.path.join(path, subdir)) |
| 19 | for subdir in ['include', 'library', 'programs', 'tests']) |
| 20 | |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 21 | def looks_like_root(path: str) -> bool: |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 22 | return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 23 | |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 24 | def check_repo_path(): |
| 25 | """ |
| 26 | Check that the current working directory is the project root, and throw |
| 27 | an exception if not. |
| 28 | """ |
| 29 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 30 | raise Exception("This script must be run from Mbed TLS root") |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 31 | |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 32 | def chdir_to_root() -> None: |
| 33 | """Detect the root of the Mbed TLS source tree and change to it. |
| 34 | |
| 35 | The current directory must be up to two levels deep inside an Mbed TLS |
| 36 | source tree. |
| 37 | """ |
| 38 | for d in [os.path.curdir, |
| 39 | os.path.pardir, |
| 40 | os.path.join(os.path.pardir, os.path.pardir)]: |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 41 | if looks_like_root(d): |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 42 | os.chdir(d) |
| 43 | return |
| 44 | raise Exception('Mbed TLS source tree not found') |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def guess_mbedtls_root(): |
| 48 | """Guess mbedTLS source code directory. |
| 49 | |
| 50 | Return the first possible mbedTLS root directory |
| 51 | """ |
| 52 | dirs = set({}) |
Jerry Yu | 0cb2cf6 | 2021-12-10 14:21:27 +0800 | [diff] [blame] | 53 | for frame in inspect.stack(): |
| 54 | path = os.path.dirname(frame.filename) |
| 55 | for d in ['.', os.path.pardir] \ |
| 56 | + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]: |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 57 | d = os.path.abspath(os.path.join(path, d)) |
| 58 | if d in dirs: |
| 59 | continue |
| 60 | dirs.add(d) |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 61 | if looks_like_root(d): |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 62 | return d |
| 63 | raise Exception('Mbed TLS source tree not found') |