File: make_generated_files.py

package info (click to toggle)
mbedtls 3.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 50,424 kB
  • sloc: ansic: 164,526; sh: 25,295; python: 14,825; makefile: 2,761; perl: 1,043; tcl: 4
file content (258 lines) | stat: -rwxr-xr-x 9,549 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3

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

"""
Generate the TF-PSA-Crypto generated files
"""
import argparse
import filecmp
import shutil
import subprocess
import sys

from pathlib import Path
from typing import List, Optional

from mbedtls_framework import build_tree

class GenerationScript:
    """
    Representation of a script generating a configuration independent file.
    """
    # pylint: disable=too-few-public-methods
    def __init__(self, script: Path, files: List[Path],
                 output_dir_option: Optional[str] = None,
                 output_file_option: Optional[str] = None):
        # Path from the root of Mbed TLS or TF-PSA-Crypto of the generation script
        self.script = script

        # Executable to run the script, needed for Windows
        if script.suffix == ".py":
            self.exe = "python"
        elif script.suffix == ".pl":
            self.exe = "perl"

        # List of the default paths from the Mbed TLS or TF-PSA-Crypto root of the
        # files the script generates.
        self.files = files

        # Output directory script argument. Can be an empty string in case it is a
        # positional argument.
        self.output_dir_option = output_dir_option

        # Output file script argument. Can be an empty string in case it is a
        # positional argument.
        self.output_file_option = output_file_option

def get_generation_script_files(generation_script: str):
    """
    Get the list of the default paths of the files that a given script
    generates. It is assumed that the script supports the "--list" option.
    """
    files = []
    if generation_script.endswith(".py"):
        cmd = ["python"]
    elif generation_script.endswith(".pl"):
        cmd = ["perl"]
    cmd += [generation_script, "--list"]

    output = subprocess.check_output(cmd, universal_newlines=True)
    for line in output.splitlines():
        files.append(Path(line))

    return files

if build_tree.looks_like_tf_psa_crypto_root("."):
    TF_PSA_CRYPTO_GENERATION_SCRIPTS = [
        GenerationScript(
            Path("scripts/generate_driver_wrappers.py"),
            [Path("core/psa_crypto_driver_wrappers.h"),
             Path("core/psa_crypto_driver_wrappers_no_static.c")],
            "", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_test_keys.py"),
            [Path("tests/include/test/test_keys.h")],
            None, "--output"
        ),
        GenerationScript(
            Path("scripts/generate_psa_constants.py"),
            [Path("programs/psa/psa_constant_names_generated.c")],
            "", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_bignum_tests.py"),
            get_generation_script_files("framework/scripts/generate_bignum_tests.py"),
            "--directory", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_config_tests.py"),
            get_generation_script_files("framework/scripts/generate_config_tests.py"),
            "--directory", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_ecp_tests.py"),
            get_generation_script_files("framework/scripts/generate_ecp_tests.py"),
            "--directory", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_psa_tests.py"),
            get_generation_script_files("framework/scripts/generate_psa_tests.py"),
            "--directory", None
        ),
    ]


if build_tree.looks_like_mbedtls_root(".") and not build_tree.is_mbedtls_3_6():
    MBEDTLS_GENERATION_SCRIPTS = [
        GenerationScript(
            Path("scripts/generate_errors.pl"),
            [Path("library/error.c")],
            None, "tf-psa-crypto/drivers/builtin/include/mbedtls \
                   include/mbedtls/ \
                   scripts/data_files"
        ),
        GenerationScript(
            Path("scripts/generate_features.pl"),
            [Path("library/version_features.c")],
            None, "include/mbedtls/ scripts/data_files"
        ),
        GenerationScript(
            Path("framework/scripts/generate_ssl_debug_helpers.py"),
            [Path("library/ssl_debug_helpers_generated.c")],
            "", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_test_keys.py"),
            [Path("tests/include/test/test_keys.h")],
            None, "--output"
        ),
        GenerationScript(
            Path("framework/scripts/generate_test_cert_macros.py"),
            [Path("tests/include/test/test_certs.h")],
            None, "--output"
        ),
        GenerationScript(
            Path("scripts/generate_query_config.pl"),
            [Path("programs/test/query_config.c")],
            None, "include/mbedtls/mbedtls_config.h \
                   tf-psa-crypto/include/psa/crypto_config.h \
                   scripts/data_files/query_config.fmt"
        ),
        GenerationScript(
            Path("framework/scripts/generate_config_tests.py"),
            get_generation_script_files("framework/scripts/generate_config_tests.py"),
            "--directory", None
        ),
        GenerationScript(
            Path("framework/scripts/generate_tls13_compat_tests.py"),
            [Path("tests/opt-testcases/tls13-compat.sh")],
            None, "--output"
        ),
        GenerationScript(
            Path("framework/scripts/generate_tls_handshake_tests.py"),
            [Path("tests/opt-testcases/handshake-generated.sh")],
            None, "--output"
        ),
        GenerationScript(
            Path("scripts/generate_visualc_files.pl"),
            get_generation_script_files("scripts/generate_visualc_files.pl"),
            "--directory", None
        ),
    ]

def get_generated_files(generation_scripts: List[GenerationScript]):
    """
    List the generated files in Mbed TLS or TF-PSA-Crypto. The path from root
    is returned for each generated files.
    """
    files = []
    for generation_script in generation_scripts:
        files += generation_script.files

    return files

def make_generated_files(generation_scripts: List[GenerationScript]):
    """
    Generate the configuration independent files in their default location in
    the Mbed TLS or TF-PSA-Crypto tree.
    """
    for generation_script in generation_scripts:
        subprocess.run([generation_script.exe, str(generation_script.script)], check=True)

def check_generated_files(generation_scripts: List[GenerationScript], root: Path):
    """
    Check that the given root directory contains the generated files as expected/
    generated by this script.
    """
    for generation_script in generation_scripts:
        for file in generation_script.files:
            file = root / file
            bak_file = file.with_name(file.name + ".bak")
            if bak_file.exists():
                bak_file.unlink()
            file.rename(bak_file)

        command = [generation_script.exe, str(generation_script.script)]
        if generation_script.output_dir_option is not None:
            command += [generation_script.output_dir_option,
                        str(root / Path(generation_script.files[0].parent))]
        elif generation_script.output_file_option is not None:
            command += generation_script.output_file_option.split()
            command += [str(root / Path(generation_script.files[0]))]
        subprocess.run([item for item in command if item.strip()], check=True)

        for file in generation_script.files:
            file = root / file
            bak_file = file.with_name(file.name + ".bak")
            if not filecmp.cmp(file, bak_file):
                ref_file = file.with_name(file.name + ".ref")
                ref_file = root / ref_file
                if ref_file.exists():
                    ref_file.unlink()
                shutil.copy(file, ref_file)
                print(f"Generated file {file} not identical to the reference one {ref_file}.")
            file.unlink()
            bak_file.rename(file)

def main():
    """
    Main function of this program
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('--list', action='store_true',
                        default=False, help='List generated files.')
    parser.add_argument('--root', metavar='DIR',
                        help='Root of the tree containing the generated files \
                              to check (default: Mbed TLS or TF-PSA-Cryto root.)')
    parser.add_argument('--check', action='store_true',
                        default=False, help='Check the generated files in root')

    args = parser.parse_args()

    if not build_tree.looks_like_root("."):
        raise RuntimeError("This script must be run from Mbed TLS or TF-PSA-Crypto root.")

    if build_tree.looks_like_tf_psa_crypto_root("."):
        generation_scripts = TF_PSA_CRYPTO_GENERATION_SCRIPTS
    elif not build_tree.is_mbedtls_3_6():
        generation_scripts = MBEDTLS_GENERATION_SCRIPTS
    else:
        raise Exception("No support for Mbed TLS 3.6")

    if args.list:
        files = get_generated_files(generation_scripts)
        for file in files:
            print(str(file))
    elif args.check:
        check_generated_files(generation_scripts, Path(args.root or "."))
    else:
        make_generated_files(generation_scripts)

if __name__ == "__main__":
    sys.exit(main())