blob: ec67e4cdfab3ec5e517b1a9d92af5a042e4031db [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 Daubney04c446c2023-12-01 17:18:38 +000025def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str:
Thomas Daubney46588de2023-11-30 13:59:30 +000026 """
Thomas Daubney99030e22023-12-01 09:52:35 +000027 Return the path of the directory containing the PSA crypto core
Thomas Daubney46588de2023-11-30 13:59:30 +000028 for either TF-PSA-Crypto or Mbed TLS.
Thomas Daubney04c446c2023-12-01 17:18:38 +000029
30 Returns either the full path or relative path depending on the
31 "relative" boolean argument.
Thomas Daubney46588de2023-11-30 13:59:30 +000032 """
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000033 if root is None:
Thomas Daubney755d3212023-11-22 17:18:22 +000034 root = guess_project_root()
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000035 if looks_like_tf_psa_crypto_root(root):
Thomas Daubney04c446c2023-12-01 17:18:38 +000036 if relative:
37 return "core"
Thomas Daubneybeec4522023-11-24 10:48:44 +000038 return os.path.join(root, "core")
Thomas Daubneyd35b94b2023-11-22 17:00:34 +000039 elif looks_like_mbedtls_root(root):
Thomas Daubney04c446c2023-12-01 17:18:38 +000040 if relative:
41 return "library"
Thomas Daubneybeec4522023-11-24 10:48:44 +000042 return os.path.join(root, "library")
Thomas Daubney13ecb692023-11-16 18:34:58 +000043 else:
44 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
45
Thomas Daubney08c6dc42023-11-30 13:56:09 +000046def crypto_library_filename(root: Optional[str] = None) -> str:
Thomas Daubneycdbf2fd2023-11-24 10:54:56 +000047 """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
Thomas Daubney89324042023-11-23 10:14:12 +000048 if root is None:
49 root = guess_project_root()
50 if looks_like_tf_psa_crypto_root(root):
51 return "tfpsacrypto"
52 elif looks_like_mbedtls_root(root):
53 return "mbedcrypto"
54 else:
55 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
56
Gilles Peskined9071e72022-09-18 21:17:09 +020057def check_repo_path():
Thomas Daubneyd1f29342023-11-30 17:25:55 +000058 """Check that the current working directory is the project root, and throw
Gilles Peskined9071e72022-09-18 21:17:09 +020059 an exception if not.
60 """
61 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
62 raise Exception("This script must be run from Mbed TLS root")
Jerry Yue6369b02021-12-02 13:51:26 +080063
Gilles Peskinec86f20a2021-04-22 00:20:47 +020064def chdir_to_root() -> None:
65 """Detect the root of the Mbed TLS source tree and change to it.
66
67 The current directory must be up to two levels deep inside an Mbed TLS
68 source tree.
69 """
70 for d in [os.path.curdir,
71 os.path.pardir,
72 os.path.join(os.path.pardir, os.path.pardir)]:
David Horstmann795d8b52023-07-18 17:03:03 +010073 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020074 os.chdir(d)
75 return
76 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080077
Thomas Daubney755d3212023-11-22 17:18:22 +000078def guess_project_root():
79 """Guess project source code directory.
Jerry Yue6369b02021-12-02 13:51:26 +080080
Thomas Daubney755d3212023-11-22 17:18:22 +000081 Return the first possible project root directory.
Jerry Yue6369b02021-12-02 13:51:26 +080082 """
83 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080084 for frame in inspect.stack():
85 path = os.path.dirname(frame.filename)
86 for d in ['.', os.path.pardir] \
87 + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
Jerry Yue6369b02021-12-02 13:51:26 +080088 d = os.path.abspath(os.path.join(path, d))
89 if d in dirs:
90 continue
91 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010092 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080093 return d
Thomas Daubney755d3212023-11-22 17:18:22 +000094 raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
Thomas Daubneyd1f29342023-11-30 17:25:55 +000095
96def guess_mbedtls_root(root: Optional[str] = None) -> str:
97 """Guess Mbed TLS source code directory.
98
99 Return the first possible Mbed TLS root directory.
100 Raise an exception if we are not in Mbed TLS.
101 """
102 if root is None:
103 root = guess_project_root()
104 if looks_like_mbedtls_root(root):
105 return root
106 else:
107 raise Exception('Mbed TLS source tree not found')
Thomas Daubneydb80b232023-11-30 17:33:54 +0000108
109def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str:
110 """Guess TF-PSA-Crypto source code directory.
111
112 Return the first possible TF-PSA-Crypto root directory.
113 Raise an exception if we are not in TF-PSA-Crypto.
114 """
115 if root is None:
116 root = guess_project_root()
117 if looks_like_tf_psa_crypto_root(root):
118 return root
119 else:
120 raise Exception('TF-PSA-Crypto source tree not found')