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 |
Thomas Daubney | d35b94b | 2023-11-22 17:00:34 +0000 | [diff] [blame] | 10 | from typing import Optional |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 11 | |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 12 | def looks_like_tf_psa_crypto_root(path: str) -> bool: |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 13 | """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 Horstmann | 2fde999 | 2023-08-29 09:48:39 +0100 | [diff] [blame] | 15 | for subdir in ['include', 'core', 'drivers', 'programs', 'tests']) |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 16 | |
| 17 | def 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 Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 22 | def looks_like_root(path: str) -> bool: |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 23 | 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] | 24 | |
Thomas Daubney | d35b94b | 2023-11-22 17:00:34 +0000 | [diff] [blame] | 25 | def crypto_core_directory(root: Optional[str] = None) -> str: |
| 26 | if root is None: |
Thomas Daubney | 755d321 | 2023-11-22 17:18:22 +0000 | [diff] [blame^] | 27 | root = guess_project_root() |
Thomas Daubney | d35b94b | 2023-11-22 17:00:34 +0000 | [diff] [blame] | 28 | if looks_like_tf_psa_crypto_root(root): |
Thomas Daubney | 13ecb69 | 2023-11-16 18:34:58 +0000 | [diff] [blame] | 29 | return "core" |
Thomas Daubney | d35b94b | 2023-11-22 17:00:34 +0000 | [diff] [blame] | 30 | elif looks_like_mbedtls_root(root): |
Thomas Daubney | 13ecb69 | 2023-11-16 18:34:58 +0000 | [diff] [blame] | 31 | return "library" |
| 32 | else: |
| 33 | raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') |
| 34 | |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 35 | def check_repo_path(): |
| 36 | """ |
| 37 | Check that the current working directory is the project root, and throw |
| 38 | an exception if not. |
| 39 | """ |
| 40 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 41 | raise Exception("This script must be run from Mbed TLS root") |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 42 | |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 43 | def chdir_to_root() -> None: |
| 44 | """Detect the root of the Mbed TLS source tree and change to it. |
| 45 | |
| 46 | The current directory must be up to two levels deep inside an Mbed TLS |
| 47 | source tree. |
| 48 | """ |
| 49 | for d in [os.path.curdir, |
| 50 | os.path.pardir, |
| 51 | os.path.join(os.path.pardir, os.path.pardir)]: |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 52 | if looks_like_root(d): |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 53 | os.chdir(d) |
| 54 | return |
| 55 | raise Exception('Mbed TLS source tree not found') |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 56 | |
| 57 | |
Thomas Daubney | 755d321 | 2023-11-22 17:18:22 +0000 | [diff] [blame^] | 58 | def guess_project_root(): |
| 59 | """Guess project source code directory. |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 60 | |
Thomas Daubney | 755d321 | 2023-11-22 17:18:22 +0000 | [diff] [blame^] | 61 | Return the first possible project root directory. |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 62 | """ |
| 63 | dirs = set({}) |
Jerry Yu | 0cb2cf6 | 2021-12-10 14:21:27 +0800 | [diff] [blame] | 64 | for frame in inspect.stack(): |
| 65 | path = os.path.dirname(frame.filename) |
| 66 | for d in ['.', os.path.pardir] \ |
| 67 | + [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] | 68 | d = os.path.abspath(os.path.join(path, d)) |
| 69 | if d in dirs: |
| 70 | continue |
| 71 | dirs.add(d) |
David Horstmann | 795d8b5 | 2023-07-18 17:03:03 +0100 | [diff] [blame] | 72 | if looks_like_root(d): |
Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 73 | return d |
Thomas Daubney | 755d321 | 2023-11-22 17:18:22 +0000 | [diff] [blame^] | 74 | raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') |