2021-04-22 00:20:47 +02:00
|
|
|
"""Mbed TLS build tree information and manipulation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
2021-12-02 06:51:26 +01:00
|
|
|
import inspect
|
|
|
|
|
2023-10-09 10:25:45 +02:00
|
|
|
def looks_like_tf_psa_crypto_root(path: str) -> bool:
|
2023-07-18 18:03:03 +02:00
|
|
|
"""Whether the given directory looks like the root of the PSA Crypto source tree."""
|
|
|
|
return all(os.path.isdir(os.path.join(path, subdir))
|
2023-08-29 10:48:39 +02:00
|
|
|
for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])
|
2021-04-22 00:20:47 +02:00
|
|
|
|
|
|
|
def looks_like_mbedtls_root(path: str) -> bool:
|
|
|
|
"""Whether the given directory looks like the root of the Mbed TLS source tree."""
|
|
|
|
return all(os.path.isdir(os.path.join(path, subdir))
|
|
|
|
for subdir in ['include', 'library', 'programs', 'tests'])
|
|
|
|
|
2023-07-18 18:03:03 +02:00
|
|
|
def looks_like_root(path: str) -> bool:
|
2023-10-09 10:25:45 +02:00
|
|
|
return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)
|
2023-07-18 18:03:03 +02:00
|
|
|
|
2022-09-18 21:17:09 +02:00
|
|
|
def check_repo_path():
|
|
|
|
"""
|
|
|
|
Check that the current working directory is the project root, and throw
|
|
|
|
an exception if not.
|
|
|
|
"""
|
|
|
|
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
|
|
|
raise Exception("This script must be run from Mbed TLS root")
|
2021-12-02 06:51:26 +01:00
|
|
|
|
2021-04-22 00:20:47 +02:00
|
|
|
def chdir_to_root() -> None:
|
|
|
|
"""Detect the root of the Mbed TLS source tree and change to it.
|
|
|
|
|
|
|
|
The current directory must be up to two levels deep inside an Mbed TLS
|
|
|
|
source tree.
|
|
|
|
"""
|
|
|
|
for d in [os.path.curdir,
|
|
|
|
os.path.pardir,
|
|
|
|
os.path.join(os.path.pardir, os.path.pardir)]:
|
2023-07-18 18:03:03 +02:00
|
|
|
if looks_like_root(d):
|
2021-04-22 00:20:47 +02:00
|
|
|
os.chdir(d)
|
|
|
|
return
|
|
|
|
raise Exception('Mbed TLS source tree not found')
|
2021-12-02 06:51:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def guess_mbedtls_root():
|
|
|
|
"""Guess mbedTLS source code directory.
|
|
|
|
|
|
|
|
Return the first possible mbedTLS root directory
|
|
|
|
"""
|
|
|
|
dirs = set({})
|
2021-12-10 07:21:27 +01:00
|
|
|
for frame in inspect.stack():
|
|
|
|
path = os.path.dirname(frame.filename)
|
|
|
|
for d in ['.', os.path.pardir] \
|
|
|
|
+ [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
|
2021-12-02 06:51:26 +01:00
|
|
|
d = os.path.abspath(os.path.join(path, d))
|
|
|
|
if d in dirs:
|
|
|
|
continue
|
|
|
|
dirs.add(d)
|
2023-07-18 18:03:03 +02:00
|
|
|
if looks_like_root(d):
|
2021-12-02 06:51:26 +01:00
|
|
|
return d
|
|
|
|
raise Exception('Mbed TLS source tree not found')
|