blob: b48a2771129d3a03ed6c19e2dfc5890ea9ea778d [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
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18
19import os
Jerry Yue6369b02021-12-02 13:51:26 +080020import inspect
21
David Horstmann795d8b52023-07-18 17:03:03 +010022def looks_like_psa_crypto_root(path: str) -> bool:
23 """Whether the given directory looks like the root of the PSA Crypto source tree."""
24 return all(os.path.isdir(os.path.join(path, subdir))
David Horstmann2fde9992023-08-29 09:48:39 +010025 for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])
Gilles Peskinec86f20a2021-04-22 00:20:47 +020026
27def looks_like_mbedtls_root(path: str) -> bool:
28 """Whether the given directory looks like the root of the Mbed TLS source tree."""
29 return all(os.path.isdir(os.path.join(path, subdir))
30 for subdir in ['include', 'library', 'programs', 'tests'])
31
David Horstmann795d8b52023-07-18 17:03:03 +010032def looks_like_root(path: str) -> bool:
33 return looks_like_psa_crypto_root(path) or looks_like_mbedtls_root(path)
34
Gilles Peskined9071e72022-09-18 21:17:09 +020035def 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 Yue6369b02021-12-02 13:51:26 +080042
Gilles Peskinec86f20a2021-04-22 00:20:47 +020043def 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 Horstmann795d8b52023-07-18 17:03:03 +010052 if looks_like_root(d):
Gilles Peskinec86f20a2021-04-22 00:20:47 +020053 os.chdir(d)
54 return
55 raise Exception('Mbed TLS source tree not found')
Jerry Yue6369b02021-12-02 13:51:26 +080056
57
58def guess_mbedtls_root():
59 """Guess mbedTLS source code directory.
60
61 Return the first possible mbedTLS root directory
62 """
63 dirs = set({})
Jerry Yu0cb2cf62021-12-10 14:21:27 +080064 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 Yue6369b02021-12-02 13:51:26 +080068 d = os.path.abspath(os.path.join(path, d))
69 if d in dirs:
70 continue
71 dirs.add(d)
David Horstmann795d8b52023-07-18 17:03:03 +010072 if looks_like_root(d):
Jerry Yue6369b02021-12-02 13:51:26 +080073 return d
74 raise Exception('Mbed TLS source tree not found')