Skip to content

Commit acf5045

Browse files
authored
Merge pull request #1108 from pq-code-package/color_tests
lint, autogen, format: Improve and unify UX
2 parents af7b121 + 1f40536 commit acf5045

File tree

3 files changed

+208
-58
lines changed

3 files changed

+208
-58
lines changed

scripts/autogen

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,44 @@ montgomery_factor = pow(2, 16, modulus)
3131
# - header guards
3232
# - #undef's for CU-local macros
3333

34+
# Standard color definitions
35+
GREEN = "\033[32m"
36+
RED = "\033[31m"
37+
BLUE = "\033[94m"
38+
BOLD = "\033[1m"
39+
NORMAL = "\033[0m"
40+
41+
42+
def clear_status_line():
43+
"""Clear any existing status line by overwriting with spaces and returning to start of line"""
44+
print(f"\r{' ' * 160}", end="", flush=True)
45+
3446

3547
def status_update(task, msg):
36-
print(f"\r{'':<160}", end="", flush=True)
37-
print(f"\r[{task}]: {msg} ...", end="", flush=True)
48+
clear_status_line()
49+
print(f"\r{BLUE}[{task}]{NORMAL}: {msg} ...", end="", flush=True)
50+
51+
52+
def high_level_status(msg):
53+
clear_status_line()
54+
print(
55+
f"\r{GREEN}{NORMAL} {msg}"
56+
) # This will end with a newline, clearing the status line
57+
58+
59+
def info(msg):
60+
clear_status_line()
61+
print(f"\r{GREEN}info{NORMAL} {msg}")
62+
63+
64+
def error(msg):
65+
clear_status_line()
66+
print(f"\r{RED}error{NORMAL} {msg}")
67+
68+
69+
def file_updated(filename):
70+
clear_status_line()
71+
print(f"\r{BOLD}updated {filename}{NORMAL}")
3872

3973

4074
def gen_header():
@@ -240,7 +274,7 @@ class CondParser:
240274
return CondParser.print_exp(self.parse_condition(exp))
241275

242276

243-
def adjust_preprocessor_comments_for_filename(content, source_file):
277+
def adjust_preprocessor_comments_for_filename(content, source_file, show_status=False):
244278
"""Automatically add comments to large `#if ... #else ... #endif`
245279
blocks indicating the guarding conditions.
246280
@@ -270,7 +304,8 @@ def adjust_preprocessor_comments_for_filename(content, source_file):
270304
271305
```
272306
"""
273-
status_update("if-else", source_file)
307+
if show_status:
308+
status_update("if-else", source_file)
274309

275310
content = content.split("\n")
276311
new_content = []
@@ -390,7 +425,9 @@ def adjust_preprocessor_comments_for_filename(content, source_file):
390425
def gen_preprocessor_comments_for(source_file, dry_run=False):
391426
with open(source_file, "r") as f:
392427
content = f.read()
393-
new_content = adjust_preprocessor_comments_for_filename(content, source_file)
428+
new_content = adjust_preprocessor_comments_for_filename(
429+
content, source_file, show_status=True
430+
)
394431
update_file(source_file, new_content, dry_run=dry_run)
395432

396433

@@ -402,10 +439,17 @@ def gen_preprocessor_comments(dry_run=False):
402439
)
403440

404441

405-
def update_file(filename, content, dry_run=False, force_format=False):
442+
def update_file(
443+
filename,
444+
content,
445+
dry_run=False,
446+
force_format=False,
447+
skip_preprocessor_comments=False,
448+
):
406449

407450
if force_format is True or filename.endswith((".c", ".h", ".i")):
408-
content = adjust_preprocessor_comments_for_filename(content, filename)
451+
if skip_preprocessor_comments is False:
452+
content = adjust_preprocessor_comments_for_filename(content, filename)
409453
content = format_content(content)
410454

411455
if os.path.exists(filename) is True:
@@ -418,15 +462,15 @@ def update_file(filename, content, dry_run=False, force_format=False):
418462
return
419463

420464
if dry_run is False:
465+
file_updated(filename)
421466
with open(filename, "w+") as f:
422467
f.write(content)
423468
else:
424469
filename_new = f"{filename}.new"
425-
print(
426-
f"Autogenerated file {filename} needs updating. Have you called scripts/autogen?",
427-
file=sys.stderr,
470+
error(
471+
f"Autogenerated file {filename} needs updating. Have you called scripts/autogen?"
428472
)
429-
print(f"Writing new version to {filename_new}", file=sys.stderr)
473+
info(f"Writing new version to {filename_new}")
430474
with open(filename_new, "w") as f:
431475
f.write(content)
432476
# If the file exists, print diff between old and new version for debugging
@@ -2000,6 +2044,7 @@ def synchronize_backends(
20002044
update_via_copy(
20012045
f"dev/aarch64_{ty}/meta.h",
20022046
"mlkem/src/native/aarch64/meta.h",
2047+
dry_run=dry_run,
20032048
transform=lambda c: adjust_header_guard_for_filename(
20042049
c, "mlkem/src/native/aarch64/meta.h"
20052050
),
@@ -2439,10 +2484,13 @@ def gen_c_citations_for(filename, bibliography, dry_run=False):
24392484
# Remember this usage of the bibliography entry
24402485
entry.register_usages(uses)
24412486

2442-
update_file(filename, "\n".join(content), dry_run=dry_run)
2487+
update_file(
2488+
filename, "\n".join(content), dry_run=dry_run, skip_preprocessor_comments=True
2489+
)
24432490

24442491

24452492
def gen_citations_for(filename, bibliography, dry_run=False):
2493+
status_update("citations", filename)
24462494
if filename.endswith(".md"):
24472495
gen_markdown_citations_for(filename, bibliography, dry_run=dry_run)
24482496
elif filename.endswith((".c", ".h", ".S")):
@@ -2557,13 +2605,18 @@ def _main():
25572605
os.chdir(os.path.join(os.path.dirname(__file__), ".."))
25582606

25592607
gen_citations(args.dry_run)
2608+
high_level_status("Generated citations")
25602609

25612610
if args.slothy == []:
25622611
args.slothy = slothy_choices
2563-
gen_slothy(args.slothy, args.dry_run)
2612+
if args.slothy is not None:
2613+
gen_slothy(args.slothy, args.dry_run)
2614+
high_level_status("Generated SLOTHY optimized assembly")
25642615

25652616
check_asm_register_aliases()
2617+
high_level_status("Checked assembly register aliases")
25662618
check_asm_loop_labels()
2619+
high_level_status("Checked assembly loop labels")
25672620

25682621
gen_c_zeta_file(args.dry_run)
25692622
gen_aarch64_hol_light_zeta_file(args.dry_run)
@@ -2573,21 +2626,29 @@ def _main():
25732626
gen_avx2_zeta_file(args.dry_run)
25742627
gen_avx2_rej_uniform_table(args.dry_run)
25752628
gen_avx2_mulcache_twiddles_file(args.dry_run)
2629+
high_level_status("Generated zeta and lookup tables")
25762630

25772631
if platform.machine().lower() in ["arm64", "aarch64"]:
25782632
gen_hol_light_asm(args.dry_run)
2633+
high_level_status("Generated HOL Light assembly")
25792634

25802635
synchronize_backends(
25812636
dry_run=args.dry_run,
25822637
clean=args.aarch64_clean,
25832638
no_simplify=args.no_simplify,
25842639
force_cross=args.force_cross,
25852640
)
2641+
high_level_status("Synchronized backends")
2642+
25862643
gen_header_guards(args.dry_run)
2644+
high_level_status("Generated header guards")
25872645
gen_preprocessor_comments(args.dry_run)
2646+
high_level_status("Generated preprocessor comments")
25882647
gen_monolithic_source_file(args.dry_run)
25892648
gen_monolithic_asm_file(args.dry_run)
2649+
high_level_status("Generated monolithic source files")
25902650
gen_undefs(args.dry_run)
2651+
high_level_status("Generated undefs")
25912652

25922653
synchronize_backends(
25932654
dry_run=args.dry_run,
@@ -2596,9 +2657,10 @@ def _main():
25962657
force_cross=args.force_cross,
25972658
no_simplify=args.no_simplify,
25982659
)
2660+
high_level_status("Completed final backend synchronization")
25992661

26002662
check_macro_typos()
2601-
print()
2663+
high_level_status("Checked macro typos")
26022664

26032665

26042666
if __name__ == "__main__":

scripts/format

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,49 @@ set -o pipefail
1010
# consts
1111
ROOT="$(realpath "$(dirname "$0")"/../)"
1212

13-
GREEN="$(tput setaf 2)"
14-
NORMAL="$(tput sgr0)"
13+
# Standard color definitions
14+
GREEN="\033[32m"
15+
RED="\033[31m"
16+
BLUE="\033[94m"
17+
BOLD="\033[1m"
18+
NORMAL="\033[0m"
1519

1620
# utility
1721
info()
1822
{
19-
printf "%s %b\n" "${GREEN}info" "${NORMAL}${*}"
23+
printf "%b %b\n" "${GREEN}info" "${NORMAL}${*}"
24+
}
25+
26+
error()
27+
{
28+
printf "%b %b\n" "${RED}error" "${NORMAL}${*}"
2029
}
2130

2231
info "Formatting nix files"
2332
if ! command -v nixpkgs-fmt 2>&1 >/dev/null; then
24-
echo "nixpkgs-fmt not found. Are you running in a nix shell? See BUILDING.md."
33+
error "nixpkgs-fmt not found. Are you running in a nix shell? See BUILDING.md."
2534
exit 1
2635
fi
2736

2837
nixpkgs-fmt "$ROOT"
2938

3039
info "Formatting shell scripts"
3140
if ! command -v shfmt 2>&1 >/dev/null; then
32-
echo "shfmt not found. Are you running in a nix shell? See BUILDING.md."
41+
error "shfmt not found. Are you running in a nix shell? See BUILDING.md."
3342
exit 1
3443
fi
3544
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))
3645

3746
info "Formatting python scripts"
3847
if ! command -v black 2>&1 >/dev/null; then
39-
echo "black not found. Are you running in a nix shell? See BUILDING.md."
48+
error "black not found. Are you running in a nix shell? See BUILDING.md."
4049
exit 1
4150
fi
4251
black --include "(scripts/tests|scripts/simpasm|scripts/autogen|scripts/check-namespace|\.py$)" "$ROOT"
4352

4453
info "Formatting c files"
4554
if ! command -v clang-format 2>&1 >/dev/null; then
46-
echo "clang-format not found. Are you running in a nix shell? See BUILDING.md."
55+
error "clang-format not found. Are you running in a nix shell? See BUILDING.md."
4756
exit 1
4857
fi
4958

0 commit comments

Comments
 (0)