blob: a657a51383b1483a5618d53791af73d0feeb52f9 [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
Gilles Peskined9071e72022-09-18 21:17:09 +020024def 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 Yue6369b02021-12-02 13:51:26 +080031
Gilles Peskinec86f20a2021-04-22 00:20:47 +020032def 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 Horstmann795d8b52023-07-18 17:03:03 +010041 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020042 os.chdir(d)
43 return
44 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080045
46
47def guess_mbedtls_root():
48 """Guess mbedTLS source code directory.
49
50 Return the first possible mbedTLS root directory
51 """
52 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080053 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 Yue6369b02021-12-02 13:51:26 +080057 d = os.path.abspath(os.path.join(path, d))
58 if d in dirs:
59 continue
60 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010061 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080062 return d
63 raise Exception('Mbed TLS source tree not found')