Skip to content

Commit 92f5d45

Browse files
Merge pull request #212 from gilles-peskine-arm/config-checks-generator-framework-fix-check-generated_files
Fix generate_config_checks.py not satisfying make_generated_files --check --root
2 parents 53c7928 + 37f6573 commit 92f5d45

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

scripts/make_generated_files.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class GenerationScript:
2424
"""
2525
Representation of a script generating a configuration independent file.
2626
"""
27-
# pylint: disable=too-few-public-methods
27+
# pylint: disable=too-few-public-methods,too-many-arguments
2828
def __init__(self, script: Path, files: List[Path],
2929
output_dir_option: Optional[str] = None,
30-
output_file_option: Optional[str] = None):
30+
output_file_option: Optional[str] = None,
31+
optional: bool = False):
3132
# Path from the root of Mbed TLS or TF-PSA-Crypto of the generation script
3233
self.script = script
3334

@@ -49,6 +50,12 @@ def __init__(self, script: Path, files: List[Path],
4950
# positional argument.
5051
self.output_file_option = output_file_option
5152

53+
# Optional files are skipped in --check mode if they don't exist.
54+
# This normally shouldn't happen, but it can happen during transition
55+
# periods where we're adding a new script or a new file, and a
56+
# consuming repository hasn't been updated yet.
57+
self.optional = optional
58+
5259
def get_generation_script_files(generation_script: str):
5360
"""
5461
Get the list of the default paths of the files that a given script
@@ -77,7 +84,8 @@ def get_generation_script_files(generation_script: str):
7784
COMMON_GENERATION_SCRIPTS.append(GenerationScript(
7885
Path("scripts/generate_config_checks.py"),
7986
get_generation_script_files("scripts/generate_config_checks.py"),
80-
"", None))
87+
output_dir_option="",
88+
optional=True))
8189

8290
if build_tree.looks_like_tf_psa_crypto_root("."):
8391
TF_PSA_CRYPTO_GENERATION_SCRIPTS = [
@@ -205,6 +213,15 @@ def check_generated_files(generation_scripts: List[GenerationScript], root: Path
205213
for generation_script in generation_scripts:
206214
for file in generation_script.files:
207215
file = root / file
216+
if not file.exists():
217+
# If the script is just being added, allow its files not
218+
# to exist. This can happen, at least, when adding a new
219+
# generation script in crypto: until mbedtls is updated,
220+
# the files from that script won't be present when
221+
# the updated crypto is built from mbedtls development.
222+
if generation_script.optional:
223+
continue
224+
raise Exception(f"Expected generated file does not exist: {file}")
208225
bak_file = file.with_name(file.name + ".bak")
209226
if bak_file.exists():
210227
bak_file.unlink()
@@ -222,6 +239,10 @@ def check_generated_files(generation_scripts: List[GenerationScript], root: Path
222239
for file in generation_script.files:
223240
file = root / file
224241
bak_file = file.with_name(file.name + ".bak")
242+
if generation_script.optional and not bak_file.exists():
243+
# This file is optional and didn't exist before, so
244+
# there's nothing to compare to, or clean up.
245+
continue
225246
if not filecmp.cmp(file, bak_file):
226247
ref_file = file.with_name(file.name + ".ref")
227248
ref_file = root / ref_file

scripts/mbedtls_framework/config_checks_generator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ def generate_header_files(branch_data: BranchData,
208208

209209
def main(branch_data: BranchData) -> None:
210210
root = build_tree.guess_project_root()
211+
# Is root the current directory? The safe default is no, so compare
212+
# the paths, rather than calling `os.samefile()` which can have false
213+
# positives and can fail in edge cases.
214+
if root == os.getcwd():
215+
# Be nice and use a relative path when it's simple to do so.
216+
# (build_tree.guess_project_root() should probably do this, actually.)
217+
# This is not only nice to humans, but also necessary for
218+
# `make_generated_files.py --root DIR --check`: it calls
219+
# this script with `--list` and expects a path that is relative
220+
# to DIR, not an absolute path that is under the project root.
221+
root = os.curdir
211222
parser = argparse.ArgumentParser(description=__doc__)
212223
parser.add_argument('--list', action='store_true',
213224
help='List generated files and exit')

0 commit comments

Comments
 (0)