blob: 4e0ae195319044746a57ba8ddc441da88ba6b4fa [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 Daubneycdbf2fd2023-11-24 10:54:56 +000026 """Return the path of the library code for either TF-PSA-Crypto or Mbed TLS."""
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000027 if root is None:
Thomas Daubney755d3212023-11-22 17:18:22 +000028 root = guess_project_root()
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000029 if looks_like_tf_psa_crypto_root(root):
Thomas Daubneybeec4522023-11-24 10:48:44 +000030 return os.path.join(root, "core")
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000031 elif looks_like_mbedtls_root(root):
Thomas Daubneybeec4522023-11-24 10:48:44 +000032 return os.path.join(root, "library")
Thomas Daubney13ecb692023-11-16 18:34:58 +000033 else:
34 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
35
Thomas Daubney89324042023-11-23 10:14:12 +000036def project_crypto_name(root: Optional[str] = None) -> str:
Thomas Daubneycdbf2fd2023-11-24 10:54:56 +000037 """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
Thomas Daubney89324042023-11-23 10:14:12 +000038 if root is None:
39 root = guess_project_root()
40 if looks_like_tf_psa_crypto_root(root):
41 return "tfpsacrypto"
42 elif looks_like_mbedtls_root(root):
43 return "mbedcrypto"
44 else:
45 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
46
Gilles Peskined9071e72022-09-18 21:17:09 +020047def check_repo_path():
48 """
49 Check that the current working directory is the project root, and throw
50 an exception if not.
51 """
52 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
53 raise Exception("This script must be run from Mbed TLS root")
Jerry Yue6369b02021-12-02 13:51:26 +080054
Gilles Peskinec86f20a2021-04-22 00:20:47 +020055def chdir_to_root() -> None:
56 """Detect the root of the Mbed TLS source tree and change to it.
57
58 The current directory must be up to two levels deep inside an Mbed TLS
59 source tree.
60 """
61 for d in [os.path.curdir,
62 os.path.pardir,
63 os.path.join(os.path.pardir, os.path.pardir)]:
David Horstmann795d8b52023-07-18 17:03:03 +010064 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020065 os.chdir(d)
66 return
67 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080068
69
Thomas Daubney755d3212023-11-22 17:18:22 +000070def guess_project_root():
71 """Guess project source code directory.
Jerry Yue6369b02021-12-02 13:51:26 +080072
Thomas Daubney755d3212023-11-22 17:18:22 +000073 Return the first possible project root directory.
Jerry Yue6369b02021-12-02 13:51:26 +080074 """
75 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080076 for frame in inspect.stack():
77 path = os.path.dirname(frame.filename)
78 for d in ['.', os.path.pardir] \
79 + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
Jerry Yue6369b02021-12-02 13:51:26 +080080 d = os.path.abspath(os.path.join(path, d))
81 if d in dirs:
82 continue
83 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010084 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080085 return d
Thomas Daubney755d3212023-11-22 17:18:22 +000086 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')