Skip to content
pi-c Artifact [1e29df489d]
Login

Artifact [1e29df489d]

Artifact 1e29df489d33dd9ac8954d0f90d4e57b32a540d16d2cf1934e1047d418c401a8:


#!/bin/sh
# pic's own sources as one translation unit, on stdout: the pic.c of the amalgamation bundle
# (docs/dev/amalgamation.md). Run from the repository root:
#
#   sh tools/amalgamate.sh [-l] FILE.c... > pic.c
#
# The Makefile passes $(PIC_SRC), so the amalgamation has the order the objects have. Each
# internal header (src/*.h, src/tui/*.h) is inlined once, at its first include, and later
# includes of it are dropped; .inc files are inlined where they are included. System and vendor
# includes stay as written. A macro a .c file defines is undefined at the end of that file, so
# the next file starts from the headers' macros only. -l adds #line directives, so compiler
# messages name the original files. One pass, one output, no temporary files.
set -eu

lines=0
if [ "${1:-}" = "-l" ]; then
	lines=1
	shift
fi
[ $# -gt 0 ] || { echo "usage: amalgamate.sh [-l] FILE.c..." >&2; exit 2; }

for f in "$@"; do
	[ -f "$f" ] || { echo "amalgamate.sh: no such file: $f" >&2; exit 1; }
done

awk -v lines="$lines" '
function dirname(p) { return sub(/\/[^\/]*$/, "", p) ? p : "." }
function exists(p,   x, r) { r = (getline x < p); if (r >= 0) close(p); return r >= 0 }
function resolve(cur, name,   d) {
	d = dirname(cur) "/" name
	if (exists(d)) return d
	d = "src/" name
	if (exists(d)) return d
	return ""
}
function norm(p) { while (sub(/[^\/]+\/\.\.\//, "", p)); sub(/^\.\//, "", p); return p }
function lineno(n, file) { if (lines) printf "#line %d \"%s\"\n", n, file }
function emit(file, is_c,   line, n, name, path, defs, nd, i, m) {
	nd = 0
	printf "/* start of file %s */\n", file
	lineno(1, file)
	n = 0
	while ((getline line < file) > 0) {
		n++
		if (match(line, /^[ \t]*#[ \t]*include[ \t]*"[^"]+"/)) {
			name = line
			sub(/^[ \t]*#[ \t]*include[ \t]*"/, "", name)
			sub(/".*$/, "", name)
			path = resolve(file, name)
			if (path != "") {
				path = norm(path)
				if (path ~ /\.inc$/ || !(path in seen)) {
					seen[path] = 1
					emit(path, 0)
					lineno(n + 1, file)
				}
				continue
			}
		}
		if (is_c && match(line, /^[ \t]*#[ \t]*define[ \t]+[A-Za-z_][A-Za-z0-9_]*/)) {
			m = substr(line, RSTART, RLENGTH)
			sub(/^[ \t]*#[ \t]*define[ \t]+/, "", m)
			defs[++nd] = m
		}
		print line
	}
	close(file)
	for (i = 1; i <= nd; i++) printf "#undef %s\n", defs[i]
	printf "/* end of file %s */\n", file
}
BEGIN {
	print "/* pic, as one translation unit. Made by tools/amalgamate.sh; do not edit."
	print " * Build: see README in this directory. */"
	print "#define PIC_AMALGAMATION 1"
	print "#ifndef _GNU_SOURCE"
	print "#define _GNU_SOURCE 1"
	print "#endif"
	print "#ifndef _DEFAULT_SOURCE"
	print "#define _DEFAULT_SOURCE 1"
	print "#endif"
	print "#ifndef _DARWIN_C_SOURCE"
	print "#define _DARWIN_C_SOURCE 1"
	print "#endif"
	print "#ifndef HAVE_LIBFOSSIL_CONFIG_H"
	print "#define HAVE_LIBFOSSIL_CONFIG_H 1"
	print "#endif"
	for (i = 1; i < ARGC; i++) emit(norm(ARGV[i]), 1)
	exit 0
}' "$@"