#!/usr/bin/env python3 """Run mklittlefs with a fixed wall clock for reproducible images.""" from __future__ import annotations import os import shutil import subprocess import sys from pathlib import Path def build_time_override(output_path: Path) -> None: source_path = Path(__file__).with_name("reproducible_time.c") compiler = shutil.which("cc") if compiler is None: raise RuntimeError("A host C compiler named 'cc' is required") subprocess.run( [ compiler, "-shared", "-fPIC", "-O2", str(source_path), "-o", str(output_path), ], check=True, ) def main() -> int: real_tool = os.environ.get("MAZE32_REAL_MKFSTOOL") build_directory = os.environ.get("MAZE32_BUILD_DIR") if not real_tool or not build_directory: raise RuntimeError("PlatformIO did not provide the filesystem tool environment") override_path = Path(build_directory) / "reproducible_time.so" override_path.parent.mkdir(parents=True, exist_ok=True) build_time_override(override_path) environment = os.environ.copy() existing_preload = environment.get("LD_PRELOAD") environment["LD_PRELOAD"] = ( f"{override_path}:{existing_preload}" if existing_preload else str(override_path) ) completed = subprocess.run([real_tool, *sys.argv[1:]], env=environment) return completed.returncode if __name__ == "__main__": raise SystemExit(main())