Skip to content
pi-c Artifact [3cd34e4b29]
Login

Artifact [3cd34e4b29]

Artifact 3cd34e4b2913ec27c00701d5b9265cc3a3b5dbb04e21336de22abff6a25eefee:


# 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, BearSSL's, `qrcodegen.c` and `.h`, and
  QuickJS's, shipped as vendored (the [vendoring](vendoring.md) pins apply), with their licenses in
  `licenses/`;
- `README`, whose `cc -O2 -fwrapv *.c -o pic -lm -lpthread` line is the build, and `SHA256SUMS`.
  `-fwrapv` is QuickJS's: it counts on signed overflow wrapping.

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`; `_GNU_SOURCE` and `CONFIG_VERSION` in
each QuickJS `.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, QuickJS). 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`.

## Each file alone

Every source also compiles by itself, as a project that vendors one builds it (flak vendors `net.c`
and `noise.c`): a plain `cc -std=c11 -pedantic -c` with the include paths and no `-D` flag. So a
file includes every header it uses, rather than leaning on an earlier file of the unit, and one that
reaches a header outside C11's own set (`unistd.h`, `sys/socket.h`, `pthread.h`, `strings.h`), or
calls what glibc hides under `-std=c11` (`strdup`, `realpath`, `clock_gettime`, `getaddrinfo`,
`PATH_MAX`, `O_CLOEXEC`), starts, before its first include, with

```c
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
```

and `_GNU_SOURCE` the same way when it needs `strptime`. glibc and musl both take `_DEFAULT_SOURCE`
as POSIX 2008 plus the BSD calls, and macOS ignores it. The value is 1, as the Makefile's `-D` and
the unit's own defines give it, so the three agree.

`test/pic/standalone.sh` checks it (`make test-standalone`, and `make test-pic`):

| Check | Catches | Where it bites |
|-------|---------|----------------|
| the compile, with `$CC` | a missing include; on glibc or musl, a missing macro | everywhere; Linux for macros |
| the macro rule, from `$CC -H`'s header list and a list of POSIX calls in C11 headers | a file that uses POSIX and defines no macro | everywhere, the Mac included |
| the compile again with `zig cc -target ARCH-linux-gnu.2.39` and `-linux-musl` | glibc's and musl's own hiding | the Mac, when zig is installed |

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