Skip to content
pi-c Artifact [b3e14c36aa]
Login

Artifact [b3e14c36aa]

Artifact b3e14c36aac53d1777d6dd43373a8bb2c85ce652f44a94d8715a3e72c8547bea:


#!/bin/sh
# Each source compiles alone, as a project that vendors it builds it (flak vendors net.c and
# noise.c): a plain `cc -std=c11 -pedantic -c` with the include paths and no -D flag at all. A file
# that needs _DEFAULT_SOURCE or _GNU_SOURCE defines it itself, before its first include
# (docs/dev/amalgamation.md).
#
# Two checks, so the Mac catches what only glibc or musl would fail on:
#  1. the compile itself. On Linux this is the real test: glibc and musl hide POSIX and BSD calls
#     under -std=c11 until a feature-test macro asks for them. macOS shows them all, so there it
#     catches a missing include and nothing more.
#  2. the macro rule, checked on any OS from the compiler's own header list (-H): a file that
#     reaches a header outside C11's own set (unistd.h, sys/socket.h, pthread.h ...) through itself
#     or pic's headers, or calls a POSIX function a C11 header declares (strdup, clock_gettime ...),
#     defines one of _DEFAULT_SOURCE, _GNU_SOURCE, _POSIX_C_SOURCE or _XOPEN_SOURCE before its first
#     #include. Headers that system headers include are not counted, and a multiarch or libc
#     directory (aarch64-linux-gnu/, generic-musl/) is dropped from a header's name.
# Usage: sh test/pic/standalone.sh [FILE.c...] (default: every src/*.c and src/tui/*.c); CC picks
# the compiler (make test-pic-musl runs it with musl's).
set -eu
cd "$(dirname "$0")/../.."
CC=${CC:-cc}
[ $# -gt 0 ] || set -- src/*.c src/tui/*.c
INC="-Isrc -isystem vendor/libfossil -isystem vendor/zlib -isystem vendor/qrcodegen -isystem vendor/quickjs -isystem vendor/bearssl/inc"
C11="assert complex ctype errno fenv float inttypes iso646 limits locale math setjmp signal stdalign stdarg stdatomic"
C11="$C11 stdbool stddef stdint stdio stdlib stdnoreturn string tgmath threads time uchar wchar wctype"
POSIX='strdup|strndup|strnlen|strtok_r|strsignal|stpcpy|fileno|fdopen|popen|pclose|getline|getdelim|open_memstream'
POSIX="$POSIX|localtime_r|gmtime_r|clock_gettime|nanosleep|timegm|tzset|strptime|mkstemp|mkdtemp|setenv|unsetenv"
POSIX="$POSIX|realpath|posix_memalign|srandom|sigaction|sigemptyset|kill"
# The headers outside C11 that FILE reaches itself or through pic's headers, one line.
posix_headers() {
	# shellcheck disable=SC2086
	$CC -std=c11 -H -fsyntax-only $INC "$1" 2>&1 > /dev/null | awk -v main="$1" -v c11="$C11" '
		BEGIN { n = split(c11, a, " "); for (i = 1; i <= n; i++) ok[a[i] ".h"] = 1; up[0] = main }
		/^\.+ / { d = length($1); up[d] = $2
			if (up[d - 1] !~ /^\// && $2 ~ /^\//) { h = $2; sub(/.*\/include\//, "", h); sub(/^[^\/]*-[^\/]*\//, "", h); if (!(h in ok)) print h } }' |
		sort -u | tr '\n' ' '
}
# The scan's own check: net.c includes netdb.h, so an empty answer means -H or awk broke.
case $(posix_headers src/net.c) in
*netdb.h*) ;;
*) echo "standalone: $CC -H gave no header list for src/net.c; cannot check the macro rule"; exit 1 ;;
esac
rc=0
for f in "$@"; do
	# $CC may be a command with words of its own ("zig cc -target ..."): unquoted on purpose.
	# shellcheck disable=SC2086
	# -Wno-format-truncation as pic's own build has it (Makefile, PIC_WARN): gcc's warning fires on
	# snprintf into fixed buffers, where a clipped message is meant; both compilers accept the flag.
	if ! out=$($CC -std=c11 -pedantic -Wall -Werror -Wno-format-truncation -c -o /dev/null $INC "$f" 2>&1); then
		echo "standalone: $f does not compile alone:"
		printf '%s\n' "$out" | grep -E 'error' | head -3 | sed 's/^/  /'
		rc=1
		continue
	fi
	def=$(awk '/^#[ \t]*include/ { exit } /^#[ \t]*define[ \t]+_(DEFAULT_SOURCE|GNU_SOURCE|POSIX_C_SOURCE|XOPEN_SOURCE)[ \t]/ { print $2; exit }' "$f")
	[ -z "$def" ] || continue
	hdrs=$(posix_headers "$f")
	calls=$(grep -oE "(^|[^A-Za-z0-9_.>])($POSIX)[ \t]*\(" "$f" | sed -E 's/^[^A-Za-z_]*//; s/[ \t(]//g' | sort -u | tr '\n' ' ' || :)
	if [ -n "$hdrs$calls" ]; then
		echo "standalone: $f uses POSIX ($(echo $hdrs $calls)) but defines no feature-test macro before its first #include"
		rc=1
	fi
done
[ "$rc" = 0 ] && echo "standalone: ok ($# files, $CC)"
exit "$rc"