Artifact 63354e85f704efd32192d955aacc5807904b07ec76ddca9b57b7bccdcb5423f0:
- File
docs/dev/amalgamation.md
— part of check-in
[9dabe49948]
at
2026-11-21 19:00:33
on branch amalgam
— Amalgamation: pic as one pic.c beside the vendored sources, built by one cc line
make amalgamation writes pic-amalgamation-VERSION/ and its tarball; make test-amal builds it from the README's own line and runs crash, print and tools against that binary, and is part of make test-pic. About 60 file-local names that collided in one translation unit take their file's prefix; test/pic/amalgam.sh fails on a new collision. (user: dmestas, size: 3286) [annotate] [blame] [check-ins using]
# Amalgamation `make amalgamation` writes `build/amalgamation/pic-amalgamation-VERSION/` and its `.tar.gz` (VERSION is the newest CHANGELOG date; `PIC_AMAL_VERSION=` overrides it). The bundle is flat, so one compiler line builds it: - `pic.c`: all of pic's own sources as one translation unit; - `libfossil.c`, `sqlite3.c`, zlib's `.c` and `.h` files and BearSSL's, shipped as vendored (the [vendoring](vendoring.md) pins apply), with their licenses in `licenses/`; - `README`, whose `cc -O2 -D... *.c -o pic -lm -lpthread` line is the build, and `SHA256SUMS`. The `-D` flags configure the dependencies, which are not edited: libfossil's config header, no iconv, zlib's `unistd.h`, and SQLite's FTS5, math functions, no loadable extensions and serialized threading. They are in one variable, `FLAGS`, in `tools/amalgamation.sh`; the README is written from it. ## How pic.c is made `tools/amalgamate.sh FILE.c...` (POSIX sh and awk, one pass, standard output, no temporary files) joins the files in the order of `PIC_SRC` in the Makefile, which is the order the objects have. Each internal header (`src/*.h`, `src/tui/*.h`) is inlined once, where it is first included, and later includes of it are dropped; the generated `.inc` files are inlined where they are included. System and vendor includes stay. Every file has `/* start of file X */` and `/* end of file X */` banners, and `-l` adds `#line` directives so compiler messages name the original files. The unit defines `PIC_AMALGAMATION` and the feature macros each source gets from the Makefile's flags. The amalgamation has no fault points: it is the release build (no `PIC_FAULTS`). ## The collision rule In one translation unit, a name a file keeps to itself can no longer be file-local. So no two source files may define the same static function or variable, typedef, struct, union or enum tag, or enumerator, and a local must not shadow another file's static (`-Wshadow` is fatal). When two files want the same name, one keeps it and the other takes its file's prefix (`an_fail`, `oc_fail`, `tl_now_ms`); a plain rename, nothing else changes. The prefixes are short and follow the file: `an` anthropic, `oc` openai_chat, `ors` openai_responses, `gem` gemini, `pan`/`pch`/`pre`/`pge` the provider decoders, `tl` tools, `rpc`, `app`, and so on. A macro a `.c` file defines is not a collision: the generator undefines it at the end of that file. A macro two files share belongs in a header. `test/pic/amalgam.sh` enforces the rule (`make test-amal` runs it, and so does `make test-pic`): it lists duplicate names, then compiles `pic.c` with the full warning flags of the Makefile. ## Testing it `make test-amal` unpacks the tarball, builds it with the `cc` line from its own README, and runs `crash.sh --no-faults`, `print.sh` and `tools.sh` against that binary, in parallel with the rest of `make test-pic`. ## Adding a source file Add it to `PIC_SRC` (the amalgamation takes the list from there, and the check fails for a file that is missing), put its internal header's include in the file that needs it, and run `make test-amal`. If it reports a duplicate name, rename in the new file. A new vendored source needs its copy line in `tools/amalgamation.sh`; two vendored files of the same name fail the bundle.