File: build_tree.py

package info (click to toggle)
mbedtls 3.6.5-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,488 kB
  • sloc: ansic: 164,842; sh: 25,443; python: 15,512; makefile: 3,131; perl: 1,043; tcl: 4
file content (150 lines) | stat: -rw-r--r-- 5,285 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Mbed TLS build tree information and manipulation.
"""

# Copyright The Mbed TLS Contributors
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
#

import os
import inspect
import re
from typing import Optional

def looks_like_tf_psa_crypto_root(path: str) -> bool:
    """Whether the given directory looks like the root of the PSA Crypto source tree."""
    try:
        with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f:
            return f.read() == "TF-PSA-Crypto\n"
    except FileNotFoundError:
        return False

def looks_like_mbedtls_root(path: str) -> bool:
    """Whether the given directory looks like the root of the Mbed TLS source tree."""
    try:
        with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f:
            return f.read() == "Mbed TLS\n"
    except FileNotFoundError:
        return False

def looks_like_root(path: str) -> bool:
    return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)

def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str:
    """
    Return the path of the directory containing the PSA crypto core
    for either TF-PSA-Crypto or Mbed TLS.

    Returns either the full path or relative path depending on the
    "relative" boolean argument.
    """
    if root is None:
        root = guess_project_root()
    if looks_like_tf_psa_crypto_root(root):
        if relative:
            return "core"
        return os.path.join(root, "core")
    elif looks_like_mbedtls_root(root):
        if is_mbedtls_3_6():
            path = "library"
        else:
            path = "tf-psa-crypto/core"
        if relative:
            return path
        return os.path.join(root, path)
    else:
        raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')

def crypto_library_filename(root: Optional[str] = None) -> str:
    """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
    if root is None:
        root = guess_project_root()
    if looks_like_tf_psa_crypto_root(root):
        return "tfpsacrypto"
    elif looks_like_mbedtls_root(root):
        return "mbedcrypto"
    else:
        raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')

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")

def chdir_to_root() -> None:
    """Detect the root of the Mbed TLS or TF-PSA-Crypto source tree and change to it.

    The current directory must be up to two levels deep inside an Mbed TLS or
    TF-PSA-Crypto source tree.
    """
    for d in [os.path.curdir,
              os.path.pardir,
              os.path.join(os.path.pardir, os.path.pardir)]:
        if looks_like_root(d):
            os.chdir(d)
            return
    raise Exception('Mbed TLS or TF-PSA-Crypto source tree not found')

def guess_project_root():
    """Guess project source code directory.

    Return the first possible project root directory.
    """
    dirs = set({})
    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)]:
            d = os.path.abspath(os.path.join(path, d))
            if d in dirs:
                continue
            dirs.add(d)
            if looks_like_root(d):
                return d
    raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')

def guess_mbedtls_root(root: Optional[str] = None) -> str:
    """Guess Mbed TLS source code directory.

    Return the first possible Mbed TLS root directory.
    Raise an exception if we are not in Mbed TLS.
    """
    if root is None:
        root = guess_project_root()
    if looks_like_mbedtls_root(root):
        return root
    else:
        raise Exception('Mbed TLS source tree not found')

def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str:
    """Guess TF-PSA-Crypto source code directory.

    Return the first possible TF-PSA-Crypto root directory.
    Raise an exception if we are not in TF-PSA-Crypto.
    """
    if root is None:
        root = guess_project_root()
    if looks_like_tf_psa_crypto_root(root):
        return root
    else:
        raise Exception('TF-PSA-Crypto source tree not found')

def framework_root(root: Optional[str] = None) -> str:
    """Return the path to the framework directory for this project."""
    if root is None:
        root = guess_project_root()
    return os.path.join(root, 'framework')

def is_mbedtls_3_6() -> bool:
    """Whether the working tree is an Mbed TLS 3.6 one or not

    Return false if we are in TF-PSA-Crypto or in Mbed TLS but with a version
    different from 3.6.x.
    Raise an exception if we are neither in Mbed TLS nor in TF-PSA-Crypto.
    """
    root = guess_project_root()
    if not looks_like_mbedtls_root(root):
        return False
    with open(os.path.join(root, 'include', 'mbedtls', 'build_info.h'), 'r') as f:
        return re.search(r"#define MBEDTLS_VERSION_NUMBER.*0x0306", f.read()) is not None