blob: 4a42d9a2bab5fedb43e60f23d461838101169691 [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
10
Ronald Cron070e8652023-10-09 10:25:45 +020011def looks_like_tf_psa_crypto_root(path: str) -> bool:
David Horstmann795d8b52023-07-18 17:03:03 +010012 """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 Horstmann2fde9992023-08-29 09:48:39 +010014 for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])
Gilles Peskinec86f20a2021-04-22 00:20:47 +020015
16def 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 Horstmann795d8b52023-07-18 17:03:03 +010021def looks_like_root(path: str) -> bool:
Ronald Cron070e8652023-10-09 10:25:45 +020022 return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)
David Horstmann795d8b52023-07-18 17:03:03 +010023
Thomas Daubney13ecb692023-11-16 18:34:58 +000024def crypto_core_directory() -> str:
25 if looks_like_tf_psa_crypto_root(os.path.curdir):
26 return "core"
27 elif looks_like_mbedtls_root(os.path.curdir):
28 return "library"
29 else:
30 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
31
Gilles Peskined9071e72022-09-18 21:17:09 +020032def check_repo_path():
33 """
34 Check that the current working directory is the project root, and throw
35 an exception if not.
36 """
37 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
38 raise Exception("This script must be run from Mbed TLS root")
Jerry Yue6369b02021-12-02 13:51:26 +080039
Gilles Peskinec86f20a2021-04-22 00:20:47 +020040def chdir_to_root() -> None:
41 """Detect the root of the Mbed TLS source tree and change to it.
42
43 The current directory must be up to two levels deep inside an Mbed TLS
44 source tree.
45 """
46 for d in [os.path.curdir,
47 os.path.pardir,
48 os.path.join(os.path.pardir, os.path.pardir)]:
David Horstmann795d8b52023-07-18 17:03:03 +010049 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020050 os.chdir(d)
51 return
52 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080053
54
55def guess_mbedtls_root():
56 """Guess mbedTLS source code directory.
57
58 Return the first possible mbedTLS root directory
59 """
60 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080061 for frame in inspect.stack():
62 path = os.path.dirname(frame.filename)
63 for d in ['.', os.path.pardir] \
64 + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
Jerry Yue6369b02021-12-02 13:51:26 +080065 d = os.path.abspath(os.path.join(path, d))
66 if d in dirs:
67 continue
68 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010069 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080070 return d
71 raise Exception('Mbed TLS source tree not found')