# 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 *.c -o pic -lm -lpthread` line is the build, and `SHA256SUMS`.

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 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.
