: nply/foundationpkgs

commit ded8d0a188bc70b91946045a722a4638629a16a0
parent 57047a8e80be8816656faf05923fa08561c3cb0e
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Fri, 11 Sep 2026 20:26:04 +0530

Add standalone foundationpkgs CI glue
2 files changed, 346 insertions(+), 0 deletions(-)
A
docs/ci.md
|
64
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
scripts/fpkgs-ci.sh
|
282
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/docs/ci.md b/docs/ci.md
@@ -0,0 +1,64 @@
+# foundationpkgs CI glue
+
+`scripts/fpkgs-ci.sh` is the standalone integration glue for running a
+foundationpkgs-style APKBUILD repository from gitolite/minci.
+
+It is intentionally detachable: copy `scripts/fpkgs-ci.sh` plus the existing
+`scripts/build.sh` and `scripts/functions.sh` into another APKBUILD repository
+and set `FPKGS_ROOT`.
+
+## Interfaces
+
+```sh
+scripts/fpkgs-ci.sh post-receive
+scripts/fpkgs-ci.sh plan [REV]
+scripts/fpkgs-ci.sh run [REV]
+scripts/fpkgs-ci.sh key APKBUILD TARGET [REV]
+```
+
+## Trigger
+
+Install a gitolite `post-receive` hook for the repo. The hook reads refs from
+stdin and only enqueues builds for `refs/heads/main` by default.
+
+```sh
+#!/bin/sh
+FPKGS_ROOT=/home/git/repositories/nply/foundationpkgs.git \
+FPKGS_DL_ROOT=/srv/http/dl.nply.org/foundationpkgs \
+FPKGS_WORK_ROOT=/var/tmp/fpkgs-ci \
+/path/to/fpkgs-ci.sh post-receive
+```
+
+## Rebuild criteria
+
+A package/target is skipped only when an artifact manifest already exists for
+the computed build key. The build key includes:
+
+- the APKBUILD contents
+- local source/patch files referenced by `source`
+- `scripts/build.sh`, `scripts/functions.sh`, and APKBUILD-to-pacscript glue
+- target triplet
+- git revision
+- build-affecting environment (`MAKEFLAGS`, `CFLAGS`, `CPPFLAGS`, `LDFLAGS`,
+  `PKG_CONFIG_PATH`, `CC`, `CHOST`, `CBUILD`)
+- visible versions of `cc`, `make`, and `bmake`
+
+Generated `PKGBUILD`/`*.pacscript` files are runtime intermediates and are not
+source-of-truth inputs.
+
+## Publishing
+
+Artifacts are published under:
+
+```txt
+$FPKGS_DL_ROOT/<target>/<pkgname>/<pkgver>-r<pkgrel>/<buildkey>/
+```
+
+Each directory contains package tarballs, checksums, `build.log`, and a
+`manifest` file.
+
+## POSIX rule
+
+The CI glue and APKBUILD recipes should remain POSIX `sh` compatible. Avoid
+bash arrays, `[[ ... ]]`, process substitution, and bash-only parameter
+expansions.
diff --git a/scripts/fpkgs-ci.sh b/scripts/fpkgs-ci.sh
@@ -0,0 +1,282 @@
+#!/bin/sh
+# fpkgs-ci.sh - standalone CI glue for foundationpkgs-style APKBUILD repos.
+# POSIX sh. Source of truth is APKBUILD; generated pacscripts/PKGBUILDs are
+# treated as runtime intermediates and are ignored when computing build keys.
+#
+# Commands:
+#   fpkgs-ci.sh post-receive                 # read gitolite post-receive stdin
+#   fpkgs-ci.sh run [REV]                    # plan, build, publish for REV
+#   fpkgs-ci.sh plan [REV]                   # print planned work
+#   fpkgs-ci.sh key APKBUILD TARGET [REV]    # print build key
+#
+# Environment:
+#   FPKGS_ROOT       repository checkout (default: current directory)
+#   FPKGS_DL_ROOT    publication root (default: /srv/http/dl.nply.org/foundationpkgs)
+#   FPKGS_WORK_ROOT  work/cache root (default: /var/tmp/fpkgs-ci)
+#   FPKGS_TARGETS    target triplets (default: native guess_triplet)
+#   FPKGS_REF        ref to build from post-receive (default: refs/heads/main)
+#   FPKGS_MINCI_URL  optional minci CGI URL to report to
+#   FPKGS_MINCI_KEY  optional minci apikey
+#   FPKGS_MINCI_SECRET optional minci apisecret (reporting is skipped unless set)
+set -eu
+
+ZERO=0000000000000000000000000000000000000000
+SELF_ARG=$0
+SELF_DIR=$(cd "$(dirname "$SELF_ARG")" && pwd)
+SELF=$SELF_DIR/$(basename "$SELF_ARG")
+FPKGS_ROOT=${FPKGS_ROOT:-$(pwd)}
+FPKGS_DL_ROOT=${FPKGS_DL_ROOT:-/srv/http/dl.nply.org/foundationpkgs}
+FPKGS_WORK_ROOT=${FPKGS_WORK_ROOT:-/var/tmp/fpkgs-ci}
+FPKGS_REF=${FPKGS_REF:-refs/heads/main}
+
+if [ -f "$FPKGS_ROOT/scripts/functions.sh" ]; then
+	# shellcheck disable=SC1091
+	. "$FPKGS_ROOT/scripts/functions.sh"
+else
+	msg() { printf '> %s\n' "$*" >&2; }
+	warning() { printf '! WARNING: %s\n' "$*" >&2; }
+	die() { printf '! %s\n' "$*" >&2; exit 1; }
+	guess_triplet() { uname -m | awk '{ print $0 "-unknown-" }' | sed 's/x86_64-unknown-$/x86_64-unknown-linux-gnu/'; }
+fi
+
+usage() {
+	cat >&2 <<EOF
+usage: $SELF post-receive | run [REV] | plan [REV] | key APKBUILD TARGET [REV]
+EOF
+	exit 1
+}
+
+sha256_file() {
+	if command -v sha256sum >/dev/null 2>&1; then
+		sha256sum "$1" | awk '{print $1}'
+	else
+		shasum -a 256 "$1" | awk '{print $1}'
+	fi
+}
+
+sha256_stream() {
+	if command -v sha256sum >/dev/null 2>&1; then
+		sha256sum | awk '{print $1}'
+	else
+		shasum -a 256 | awk '{print $1}'
+	fi
+}
+
+native_target() {
+	guess_triplet
+}
+
+targets() {
+	if [ -n "${FPKGS_TARGETS:-}" ]; then
+		printf '%s\n' $FPKGS_TARGETS
+	else
+		native_target
+	fi
+}
+
+checkout_rev() {
+	rev=$1
+	mkdir -p "$FPKGS_WORK_ROOT/checkouts"
+	co="$FPKGS_WORK_ROOT/checkouts/$rev"
+	if [ ! -d "$co/.git" ]; then
+		rm -rf "$co"
+		git clone -q "$FPKGS_ROOT" "$co"
+		(cd "$co" && git checkout -q "$rev")
+	fi
+	printf '%s\n' "$co"
+}
+
+list_apkbuilds() {
+	root=$1
+	(cd "$root" && find . -path './.git' -prune -o -name APKBUILD -type f -print | sed 's#^./##' | sort)
+}
+
+pkg_field() {
+	apk=$1
+	field=$2
+	awk -F= -v f="$field" '
+		$1 == f {
+			v = $0; sub(/^[^=]*=/, "", v); gsub(/^"|"$/, "", v); print v; exit
+		}
+	' "$apk"
+}
+
+local_sources() {
+	apk=$1
+	dir=$(dirname "$apk")
+	awk '
+		/^source=/ { in_source=1 }
+		in_source { print }
+		in_source && /"[[:space:]]*$/ { in_source=0 }
+	' "$apk" |
+	tr ' "' '\n\n' |
+	awk 'NF && $0 !~ /^(source=|https?:\/\/|ftp:\/\/|git\+)/ { print }' |
+	while IFS= read -r src; do
+		case "$src" in
+		*::*) src=${src#*::} ;;
+		esac
+		[ -f "$dir/$src" ] && printf '%s\n' "$dir/$src"
+	done
+}
+
+build_env_fingerprint() {
+	for name in MAKEFLAGS CFLAGS CPPFLAGS LDFLAGS PKG_CONFIG_PATH CC CHOST CBUILD; do
+		eval val=\${$name:-}
+		printf '%s=%s\n' "$name" "$val"
+	done
+	(command -v cc >/dev/null 2>&1 && cc --version 2>/dev/null | sed 1q) || true
+	(command -v make >/dev/null 2>&1 && make --version 2>/dev/null | sed 1q) || true
+	(command -v bmake >/dev/null 2>&1 && bmake -V MAKE_VERSION 2>/dev/null) || true
+}
+
+build_key() {
+	apk=$1
+	target=$2
+	rev=${3:-HEAD}
+	root=$(cd "$(dirname "$apk")/../.." 2>/dev/null && pwd || pwd)
+	{
+		printf 'rev=%s\n' "$rev"
+		printf 'target=%s\n' "$target"
+		printf 'apkbuild=%s\n' "$apk"
+		printf 'apkbuild-sha256=%s\n' "$(sha256_file "$apk")"
+		for f in "$root/scripts/build.sh" "$root/scripts/functions.sh" "$root/scripts/apkbuild-to-pacscript.sh"; do
+			[ -f "$f" ] && printf 'script %s %s\n' "$(basename "$f")" "$(sha256_file "$f")"
+		done
+		local_sources "$apk" | while IFS= read -r f; do
+			printf 'local-source %s %s\n' "$(basename "$f")" "$(sha256_file "$f")"
+		done
+		build_env_fingerprint
+	} | sha256_stream
+}
+
+artifact_dir() {
+	apk=$1
+	target=$2
+	key=$3
+	pkg=$(pkg_field "$apk" pkgname)
+	ver=$(pkg_field "$apk" pkgver)
+	rel=$(pkg_field "$apk" pkgrel)
+	[ -n "$rel" ] || rel=0
+	printf '%s/%s/%s/%s-r%s/%s\n' "$FPKGS_DL_ROOT" "$target" "$pkg" "$ver" "$rel" "$key"
+}
+
+is_built() {
+	apk=$1
+	target=$2
+	key=$3
+	[ -f "$(artifact_dir "$apk" "$target" "$key")/manifest" ]
+}
+
+plan() {
+	rev=$1
+	root=$(checkout_rev "$rev")
+	list_apkbuilds "$root" | while IFS= read -r relapk; do
+		apk=$root/$relapk
+		targets | while IFS= read -r target; do
+			key=$(build_key "$apk" "$target" "$rev")
+			if is_built "$apk" "$target" "$key"; then
+				printf 'skip %s %s %s\n' "$target" "$relapk" "$key"
+			else
+				printf 'build %s %s %s\n' "$target" "$relapk" "$key"
+			fi
+		done
+	done
+}
+
+publish_output() {
+	apk=$1
+	target=$2
+	key=$3
+	dist=$4
+	log=$5
+	rev=$6
+	out=$(artifact_dir "$apk" "$target" "$key")
+	tmp=$out.tmp.$$ 
+	rm -rf "$tmp"
+	mkdir -p "$tmp"
+	find "$dist" -type f -name '*.tar.gz' -exec cp {} "$tmp/" \;
+	find "$dist" -type f -name '*.sha256' -exec cp {} "$tmp/" \;
+	cp "$log" "$tmp/build.log"
+	{
+		printf 'pkgname=%s\n' "$(pkg_field "$apk" pkgname)"
+		printf 'pkgver=%s\n' "$(pkg_field "$apk" pkgver)"
+		printf 'pkgrel=%s\n' "$(pkg_field "$apk" pkgrel)"
+		printf 'target=%s\n' "$target"
+		printf 'gitrev=%s\n' "$rev"
+		printf 'buildkey=%s\n' "$key"
+		printf 'built_at=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
+	} >"$tmp/manifest"
+	mkdir -p "$(dirname "$out")"
+	rm -rf "$out"
+	mv "$tmp" "$out"
+}
+
+run_one() {
+	root=$1
+	relapk=$2
+	target=$3
+	rev=$4
+	apk=$root/$relapk
+	key=$(build_key "$apk" "$target" "$rev")
+	if is_built "$apk" "$target" "$key"; then
+		msg "skip $relapk for $target: already built ($key)"
+		return 0
+	fi
+	work=$FPKGS_WORK_ROOT/build/$key
+	dist=$work/dist
+	sysroot=$work/sysroot
+	log=$work/build.log
+	rm -rf "$work"
+	mkdir -p "$dist" "$sysroot"
+	msg "build $relapk for $target ($key)"
+	if (cd "$root" && scripts/build.sh -o "$dist" -s "$sysroot" -t "$target" "$relapk") >"$log" 2>&1; then
+		publish_output "$apk" "$target" "$key" "$dist" "$log" "$rev"
+		msg "published $relapk for $target"
+		return 0
+	fi
+	warning "failed $relapk for $target; see $log"
+	return 1
+}
+
+run() {
+	rev=$1
+	root=$(checkout_rev "$rev")
+	status=0
+	planfile=$FPKGS_WORK_ROOT/plan.$$.txt
+	mkdir -p "$FPKGS_WORK_ROOT"
+	plan "$rev" >"$planfile"
+	while read -r action target relapk key; do
+		case "$action" in
+		build) run_one "$root" "$relapk" "$target" "$rev" || status=1 ;;
+		skip) msg "skip $relapk for $target: already built ($key)" ;;
+		esac
+	done <"$planfile"
+	rm -f "$planfile"
+	return "$status"
+}
+
+post_receive() {
+	while read -r old new ref; do
+		[ "$ref" = "$FPKGS_REF" ] || continue
+		[ "$new" = "$ZERO" ] && continue
+		# Avoid running long builds inside gitolite's hook process.
+		mkdir -p "$FPKGS_WORK_ROOT/queue"
+		job="$FPKGS_WORK_ROOT/queue/$new"
+		{
+			printf 'FPKGS_ROOT=%s\n' "$FPKGS_ROOT"
+			printf 'FPKGS_DL_ROOT=%s\n' "$FPKGS_DL_ROOT"
+			printf 'FPKGS_WORK_ROOT=%s\n' "$FPKGS_WORK_ROOT"
+			printf 'REV=%s\n' "$new"
+		} >"$job"
+		( FPKGS_ROOT=$FPKGS_ROOT FPKGS_DL_ROOT=$FPKGS_DL_ROOT FPKGS_WORK_ROOT=$FPKGS_WORK_ROOT nohup "$SELF" run "$new" >"$FPKGS_WORK_ROOT/$new.log" 2>&1 & )
+	done
+}
+
+cmd=${1:-}
+case "$cmd" in
+post-receive) post_receive ;;
+run) run "${2:-HEAD}" ;;
+plan) plan "${2:-HEAD}" ;;
+key) [ $# -ge 3 ] || usage; build_key "$2" "$3" "${4:-HEAD}" ;;
+*) usage ;;
+esac