1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh
# Opt-in Debian packaging using the existing recipe build and native dpkg-deb.
# No sudo, APT, signing key, or live installation is used during a build.
set -eu
# Package defaults must not inherit the CI account's private-directory umask.
umask 022
self=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
. "$self/functions.sh"
[ "$#" = 4 ] || die 'usage: build-deb.sh APKBUILD OUT TARGET KEEP'
recipe=$(CDPATH= cd -- "$(dirname "$1")" && pwd)/$(basename "$1")
output=$2
target=$3
keep=$4
[ "$(canonical_os)" = Linux ] || die 'Debian packages must be built on Linux'
command -v dpkg-deb >/dev/null 2>&1 || die 'dpkg-deb is required'
case "$target" in x86_64-unknown-linux-gnu) debarch=amd64 ;; *) die 'Initial Debian backend supports only Debian amd64' ;; esac
work=$(mktemp -d "${TMPDIR:-/tmp}/fpkgs-deb.XXXXXX")
cleanup() { if [ "$keep" = 1 ]; then msg "keeping $work"; else rm -rf "$work"; fi; }
trap cleanup 0
trap 'exit 1' HUP INT TERM
mkdir -p "$output/$target" "$work/stage"
output=$(CDPATH= cd -- "$output/$target" && pwd)
# Recipes explicitly opt in with their Debian version and dependencies.
srcdir=$work/src
pkgdir=$work/stage
CHOST=$target
. "$recipe"
[ -n "${deb_pkgver:-}" ] || die 'Recipe has not opted into Debian packaging (deb_pkgver)'
debversion=$deb_pkgver-${pkgrel:-0}
dpkg --validate-version "$debversion"
# Stable archive timestamps across CI reruns; payload changes still need pkgrel bumps.
SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(git -C "$(dirname "$recipe")" log -1 --format=%ct -- . 2>/dev/null || :)}
SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-0}
case "$SOURCE_DATE_EPOCH" in *[!0-9]*) die 'Invalid SOURCE_DATE_EPOCH' ;; esac
export SOURCE_DATE_EPOCH
FPKGS_REPRODUCIBLE=1 FPKGS_FORMAT=tar.gz "$self/build.sh" -f tar.gz -t "$target" -o "$work/tar" -s "$work/sysroot" -n "$work/native" "$recipe"
archive_target=$target
case "${arch:-}" in
noarch | any) archive_target=noarch; debarch=all ;;
esac
archive=$work/tar/$archive_target/$pkgname-$pkgver-r${pkgrel:-0}.tar.gz
[ -f "$archive" ] || die 'Missing recipe output'
# Extraction is always into private staging, never the live filesystem.
tar --no-same-owner -xzf "$archive" -C "$work/stage"
[ ! -e "$work/stage/DEBIAN" ] || die 'Recipe payload must not supply Debian control metadata'
mkdir "$work/stage/DEBIAN"
{
printf 'Package: %s\nVersion: %s\nArchitecture: %s\n' "$pkgname" "$debversion" "$debarch"
printf 'Maintainer: Foundation Packages <packages@nply.org>\n'
printf 'Section: utils\nPriority: optional\nHomepage: %s\n' "$url"
[ -z "${deb_depends:-}" ] || printf 'Depends: %s\n' "$deb_depends"
printf 'Description: %s\n' "$pkgdesc"
} > "$work/stage/DEBIAN/control"
if [ -n "${deb_postinst:-}" ]; then
case "$deb_postinst" in */* | .* | *[!a-zA-Z0-9_.-]*) die 'Unsafe deb_postinst filename' ;; esac
install -m755 "$(dirname "$recipe")/$deb_postinst" "$work/stage/DEBIAN/postinst"
fi
(cd "$work/stage" && find . -type f ! -path './DEBIAN/*' -exec md5sum {} + | LC_ALL=C sort) > "$work/stage/DEBIAN/md5sums"
artifact=$output/${pkgname}_${debversion}_${debarch}.deb
find "$work/stage" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
dpkg-deb --root-owner-group --build "$work/stage" "$artifact"
sha256sum "$artifact" | awk '{print $1}' > "$artifact.sha256"
msg "wrote $artifact"