iglupkg/iglupkg.sh
2022-07-29 23:08:10 +00:00

257 lines
5.2 KiB
Bash
Executable file

#!/bin/sh
set -e
export HOST_ARCH=$(uname -m)
if uname -o | grep GNU >/dev/null; then
export HOST_TRIPLE="$HOST_ARCH-unknown-linux-gnu"
else
export HOST_TRIPLE="$HOST_ARCH-unknown-linux-musl"
fi
command -V bad > /dev/null 2> /dev/null || bad() {
shift
"$@"
}
bad --gmake command -V gmake > /dev/null 2> /dev/null || gmake() {
make "$@"
}
usage() {
echo "usage: $(basename $0) [fbp]"
exit 1
}
fatal() {
echo "ERROR: $@"
exit 1
}
warn() {
echo "WARNING: $@"
}
to_run=
while [ ! -z "$1" ]; do
case "$1" in
--with-cross=*)
ARCH=$(echo "$1" | cut -d'=' -f2)
[ -z "$ARCH" ] && fatal '--with-cross=<arch> requires an argument'
echo "INFO: cross compiling for $ARCH"
WITH_CROSS="$ARCH"
;;
--with-cross)
fatal '--with-cross=<arch> requires an argument'
;;
--with-cross-dir=*)
WITH_CROSS_DIR=$(echo "$1" | cut -d'=' -f2)
[ -z "$WITH_CROSS_DIR" ] && fatal '--with-cross-dir=<sysroot> requires an argument'
[ -d "$WITH_CROSS_DIR" ] 2>/dev/null || warn "$WITH_CROSS_DIR does not exist"
echo "INFO: using toolchain libraries from $WITH_CROSS_DIR"
;;
--with-cross-dir)
fatal '--with-cross-dir=<sysroot> requires an argument'
;;
--for-cross)
echo 'INFO: for cross'
FOR_CROSS=1
;;
--for-cross-dir=*)
FOR_CROSS_DIR_SET=1
FOR_CROSS_DIR=$(echo "$1" | cut -d'=' -f2)
#[ -z "$FOR_CROSS_DIR" ] && fatal '--for-cross-dir=<sysroot> requires an argument'
echo "INFO: packaging for prefix $FOR_CROSS_DIR"
;;
--for-cross-dir)
fatal '--for-cross-dir=<sysroot> requires an argument'
;;
--no-ext)
NOEXT=1
;;
fbp)
to_run="f b p"
;;
fb)
to_run="f b"
;;
f)
to_run="f"
;;
bp)
to_run="b p"
;;
b)
to_run="b"
;;
p)
to_run="p"
;;
*)
fatal "invalid argument $1"
;;
esac
shift
done
[ -z "$WITH_CROSS_DIR" ] && WITH_CROSS_DIR=/usr/$ARCH-linux-musl
[ -z "$FOR_CROSS_DIR_SET" ] && FOR_CROSS_DIR=/usr/$ARCH-linux-musl
if [ -z "$ARCH" ]; then
export ARCH=$HOST_ARCH
fi
if [ ! -z "$FOR_CROSS" ]; then
cross=-$ARCH
fi
export TRIPLE="$ARCH-unknown-linux-musl"
[ -z "$CC" ] && export CC=cc
[ -z "$CXX" ] && export CXX=c++
export AR=ar
export RANLIB=ranlib
export CROSS_EXTRA_LDFLAGS="--target=$TRIPLE --sysroot=$WITH_CROSS_DIR"
export CFLAGS="-O3"
export CROSS_EXTRA_CFLAGS="--target=$TRIPLE --sysroot=$WITH_CROSS_DIR"
export CXXFLAGS=$CFLAGS
export CROSS_EXTRA_CXXFLAGS="$CROSS_EXTRA_CFLAGS -nostdinc++ -isystem $WITH_CROSS_DIR/include/c++/v1/"
auto_cross() {
if [ -z "$FOR_CROSS" ]; then
PREFIX=/usr
else
PREFIX=$FOR_CROSS_DIR
fi
[ -z "$WITH_CROSS" ] && return
export CFLAGS="$CFLAGS $CROSS_EXTRA_CFLAGS"
export CXXFLAGS="$CFLAGS $CROSS_EXTRA_CXXFLAGS"
export LDFLAGS="$CROSS_EXTRA_LDFLAGS"
}
export JOBS=$(nproc)
[ -f build.sh ] || fatal 'build.sh not found'
. ./build.sh
if command -V iglu 2>/dev/null; then
missing=""
missing=$(echo $mkdeps | tr ":" "\n" | while read mkdep; do \
[ -e /usr/share/iglupkg/$mkdep ] || echo " $mkdep"; done)
[ ! -z "$missing" ] && fatal "missing make dependencies: $missing"
missing=$(echo $deps | tr ":" "\n" | while read dep; do \
[ -e /usr/share/iglupkg/$dep ] || echo " $dep"; done)
[ ! -z "$missing" ] && fatal "missing runtime dependencies: $missing"
fi
srcdir="$(pwd)/src"
outdir="$(pwd)/out"
pkgdir="$(pwd)/out/$pkgname$cross.$pkgver"
[ -d "$pkgdir" ] || warn "package already built. Pass f b or p."
_genmeta() {
echo "[pkg]"
if [ ! -z $@ ]; then
echo "pkgname=$pkgname-$@"
else
echo "pkgname=$pkgname"
fi
echo "pkgver=$pkgver"
echo "deps=$deps"
echo ""
echo "[license]"
license
echo ""
echo "[backup]"
backup
echo ""
echo "[fs]"
if [ ! -z $@ ]; then
cd "$pkgdir-$@"
else
cd "$pkgdir"
fi
find *
cd "$srcdir"
}
_f() {
echo "fetching $pkgname"
rm -rf "$pkgdir"
rm -rf "$srcdir"
mkdir -p "$srcdir"
cd "$srcdir"
fetch
cd "$srcdir"
:> .fetched
}
_b() {
echo "building $pkgname"
rm -rf "$pkgdir"
cd "$srcdir"
[ -f .fetched ] || fatal 'must fetch before building'
MAKEFLAGS=-j"$JOBS" SAMUFLAGS=-j"$JOBS" build
cd "$srcdir"
:> .built
}
_p() {
echo "packaging $pkgname"
rm -rf "$pkgdir"
cd "$srcdir"
[ -f .built ] || fatal 'must build before packaging'
mkdir -p "$pkgdir"
package
install -d "$pkgdir/usr/share/iglupkg/"
cd "$srcdir"
_genmeta > "$pkgdir/usr/share/iglupkg/$pkgname$cross"
cd "$pkgdir"
if command -V zstd > /dev/null 2> /dev/null; then
tar --owner=0 --group=0 -cf ../$pkgname$cross.$pkgver.tar.zst * -I zstd
else
tar --owner=0 --group=0 -cf ../$pkgname$cross.$pkgver.tar *
fi
if [ $ext ] && [ ! -z $NOEXT ]; then
echo "$ext" | tr ":" "\n" | while read e; do
echo "packaging $pkgname-$e"
cd "$srcdir"
mkdir -p "$pkgdir-$e"
package_$(echo $e | tr "-" "_")
install -d "$pkgdir-$e/usr/share/iglupkg/"
cd "$srcdir"
_genmeta $e > "$pkgdir-$e/usr/share/iglupkg/$pkgname$cross"
cd "$pkgdir-$e"
if command -V zstd > /dev/null 2> /dev/null; then
tar --owner=0 --group=0 -cf ../$pkgname-$e$cross.$pkgver.tar.zst * -I zstd
else
tar --owner=0 --group=0 -cf ../$pkgname-$e$cross.$pkgver.tar *
fi
done
fi
echo "done packaging"
}
if [ -z "$to_run" ]; then
[ -f "$srcdir/.fetched" ] || _f
[ -f "$srcdir/.built" ] || _b
[ -d "$pkgdir" ] || _p
else
set -- $to_run
while [ ! -z "$1" ]; do
_"$1"
shift
done
fi