#!/bin/sh
# The release bundle (docs/dev/amalgamation.md): pic.c, the vendored dependency sources as they
# are, a README with the one build line, SHA256SUMS, and a tarball of the directory.
#
# sh tools/amalgamation.sh OUTDIR NAME FILE.c... (the Makefile passes $(PIC_SRC))
#
# Writes OUTDIR/NAME/ and OUTDIR/NAME.tar.gz. Run from the repository root.
set -eu
[ $# -ge 3 ] || { echo "usage: amalgamation.sh OUTDIR NAME FILE.c..." >&2; exit 2; }
base=$1
out=$1/$2
name=$2
shift 2
# The dependencies are shipped as vendored, but their configuration is -D flags in the Makefile.
# The bundle has no flags, so the bundler writes each define at the top of its copy of the file
# that needs it, guarded so a caller's -D still wins. The vendor/ originals are not touched.
with_defs() { # FILE DEFINE...: copy FILE (from vendor) into the bundle behind the defines
src=$1; dst=$out/$(basename "$1"); shift
{
echo "/* Added by tools/amalgamation.sh: the build flags this file needs, so that a plain cc builds it. */"
for d in "$@"; do
n=${d%%=*}; v=1; [ "$n" = "$d" ] || v=${d#*=}
printf '#ifndef %s\n#define %s %s\n#endif\n' "$n" "$n" "$v"
done
cat "$src"
} > "$dst"
}
rm -rf "$out" "$out.tar.gz"
mkdir -p "$out/licenses"
sh tools/amalgamate.sh "$@" > "$out/pic.c"
cp vendor/libfossil/libfossil.h vendor/libfossil/libfossil-config.h vendor/libfossil/sqlite3.h vendor/zlib/*.h "$out/"
with_defs vendor/libfossil/libfossil.c HAVE_LIBFOSSIL_CONFIG_H WITHOUT_ICONV
with_defs vendor/libfossil/sqlite3.c SQLITE_THREADSAFE=1 SQLITE_ENABLE_FTS5 SQLITE_ENABLE_MATH_FUNCTIONS SQLITE_OMIT_LOAD_EXTENSION
for f in vendor/zlib/*.c; do with_defs "$f" Z_HAVE_UNISTD_H; done
cp vendor/bearssl/inc/*.h vendor/bearssl/src/*.h vendor/bearssl/src/*.c vendor/bearssl/src/*/*.c "$out/"
cp vendor/qrcodegen/qrcodegen.c vendor/qrcodegen/qrcodegen.h "$out/"
qjs_version=$(cat vendor/quickjs/VERSION)
for f in vendor/quickjs/*.c; do with_defs "$f" _GNU_SOURCE "CONFIG_VERSION=\"$qjs_version\""; done
cp vendor/quickjs/*.h "$out/"
cp LICENSE "$out/licenses/pic"
cp vendor/libfossil/LICENSE "$out/licenses/libfossil-and-sqlite"
cp vendor/zlib/LICENSE "$out/licenses/zlib"
cp vendor/bearssl/LICENSE.txt "$out/licenses/bearssl"
cp vendor/qrcodegen/LICENSE "$out/licenses/qrcodegen"
cp vendor/quickjs/LICENSE "$out/licenses/quickjs"
# One directory, so two sources of the same name would silently overwrite each other.
dups=$(for d in vendor/libfossil vendor/zlib vendor/qrcodegen vendor/quickjs vendor/bearssl/inc vendor/bearssl/src vendor/bearssl/src/*; do
[ -d "$d" ] && ls "$d" | grep '\.[ch]$' || true
done | sort | uniq -d)
[ -z "$dups" ] || { echo "amalgamation.sh: same name in two vendor directories: $dups" >&2; exit 1; }
cat > "$out/README" <<README
$name
pic, a durable coding agent in C11, as plain C sources. pic.c is all of pic's own code as one
translation unit. libfossil.c, sqlite3.c, zlib's files, BearSSL's files, qrcodegen.c and QuickJS's
files are the vendored dependencies, as vendored but for the #define lines the bundler wrote at the
top of libfossil.c, sqlite3.c, zlib's and QuickJS's .c files. Build with a C11 compiler, libc, libm
and pthread; nothing else is linked:
cc -O2 -fwrapv *.c -o pic -lm -lpthread
Those defines are the build configuration: libfossil reads libfossil-config.h and is built without
iconv, zlib includes unistd.h, and SQLite is built with FTS5, the math functions, no loadable
extensions and serialized threading, as pic uses it, and QuickJS gets its version and _GNU_SOURCE.
Each is guarded, so -DNAME=value on the cc line overrides it. -fwrapv is QuickJS's: it counts on
signed integer overflow wrapping, as its own Makefile builds it. Compiling all the files takes about half a minute.
Licenses are in licenses/. SHA256SUMS lists every file.
README
(cd "$out" && for f in *; do [ -f "$f" ] && echo "$f"; done; for f in licenses/*; do echo "$f"; done) | LC_ALL=C sort |
while read -r f; do
if command -v sha256sum >/dev/null 2>&1; then (cd "$out" && sha256sum "$f"); else (cd "$out" && shasum -a 256 "$f"); fi
done | grep -v ' SHA256SUMS$' > "$out/SHA256SUMS.new"
mv "$out/SHA256SUMS.new" "$out/SHA256SUMS"
tar -C "$base" -czf "$out.tar.gz" "$name"