Source browser

scripts/amalgamate.py

Read the complete file below or download its original bytes. Line numbers provide stable links to a declaration or processing step.

File fingerprint
1#!/usr/bin/env python3
2"""Rebuild the dependency-free public header from its maintained sections."""
3from pathlib import Path  # Read and write the source files.
4import argparse  # Read the optional check flag.
5
6root = Path(__file__).resolve().parents[1]  # Find the checkout containing detail/.
7parser = argparse.ArgumentParser(description=__doc__)  # Describe this tool in --help.
8parser.add_argument("--check", action="store_true")  # Support a read-only comparison mode.
9args = parser.parse_args()  # Read the requested mode.
10parts = [  # Start the output with its source note and include guard.
11    "// shdr 1.0.0\n",  # Identify the release directly in the distributed header.
12    "// C++11 CPU and OpenGL compute pipelines.\n",  # Explain where to edit.
13    "#ifndef SHDR_SINGLE_HEADER_INCLUDED\n#define SHDR_SINGLE_HEADER_INCLUDED\n",  # Avoid duplicate definitions.
14]  # The maintained sections are appended next.
15for name in (  # Append each maintained source section in dependency order.
16    "math.hpp",  # Define portable numeric types before the runtime uses them.
17    "runtime.hpp",  # Define resources, kernels, devices and pipeline execution.
18    "async.hpp",  # Extend those pipelines with completion handles and CPU workers.
19    "egl.hpp",  # Add the optional headless device after its base runtime is defined.
20    "macros.hpp",  # Declare adapter macros after all underlying runtime types.
21):  # Keep dependency order.
22    parts.append("\n// ---- " + name + " ----\n")  # Mark the start of each section.
23    parts.append((root / "detail" / name).read_text())  # Copy the complete section.
24parts.append("\n#endif // SHDR_SINGLE_HEADER_INCLUDED\n")  # Close the public include guard.
25output = "".join(parts)  # Assemble the header from its exact source sections.
26if args.check:  # Compare the assembled header with the committed file.
27    if (root / "shdr.h").read_text() != output:  # Detect any missing regeneration.
28        raise SystemExit("shdr.h is stale. Run scripts/amalgamate.py")  # Explain the repair.
29else:  # Normal mode writes the assembled header.
30    (root / "shdr.h").write_text(output)  # Replace the generated file with current sections.