Artifact 463c96657335bb36f7664f5f958e237f1bc0b5290dcc0dd69c005e42b4579b9d:
- File docs/dev/amalgamation.md — part of check-in [71544ebb6a] at 2026-11-22 21:12:17 on branch keel-providers — Provider adapters: tiers, docs, the toy adapter test (user: pi-c-agent, size: 5030) [annotate] [blame] [check-ins using] [more...]
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.cand.hfiles, BearSSL's, andqrcodegen.cand.h, shipped as vendored (the vendoring pins apply), with their licenses inlicenses/;README, whosecc -O2 *.c -o pic -lm -lpthreadline is the build, andSHA256SUMS.
There are no -D flags. The dependencies are not edited in vendor/, but their configuration is
build flags in the Makefile, so the bundler writes each define, guarded by #ifndef (a caller's -D
still wins), at the top of the bundle's copy of the file that needs it: libfossil's config header and
no iconv in libfossil.c; SQLite's FTS5, math functions, no loadable extensions and serialized
threading in sqlite3.c; zlib's unistd.h in each zlib .c. The list is with_defs in
tools/amalgamation.sh. pic.c gets its own feature macros and the libfossil config define from
tools/amalgamate.sh. A new define a dependency needs goes in that list.
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 bundle has no fault points: it is the release build (no PIC_FAULTS).
pic is built from it
make pic and make pic-dev do not compile src/*.c one by one: they run the generator over
PIC_SRC into build/pic/release/pic.c and build/pic/dev/pic.c, compile that one unit, and link
it with the separately compiled vendored objects (SQLite, libfossil, zlib, BearSSL, qrcodegen). Dev's unit is
made with -l, so compiler errors, ASan/UBSan traces and lldb name src/FILE.c:LINE; release's has
no #line lines, like the bundle's. Both are compiled with the full PIC_WARN flags, vendor
headers as -isystem, so the build of the shipped unit is also the check that it compiles clean.
The unit depends on every source, every header and .inc under src/ and tools/amalgamate.sh;
src/changelog.inc and src/manual.inc are named because a clean tree does not have them yet. There
is no per-file rebuild: any change recompiles the whole unit (a few seconds release, about ten with
the sanitizers on a laptop), the price of a clean build that is faster and of testing the code that
ships. pic-lib.c is the same unit without main.c, for test_hooks, test_adapter and
pic_bench, which bring their own main. The small unit tests that link a few sources
(test_sse, test_tui, ...) still compile those files themselves.
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 and files missing from PIC_SRC. Compiling the merged unit under the full
warning flags is make pic and make pic-dev.
Testing it
make test-amal (the names check, then the shipped bundle) 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.