abuild/abuild
2008-11-04 10:34:40 +00:00

388 lines
7.9 KiB
Bash
Executable File

#!/bin/sh
# script to build apk packages (light version og makepkg)
# Copyright (c) 2008 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#
# Depends on: busybox utilities, fakeroot,
#
myver=0.2
# read config
MAKEAPK_CONF=${MAKEAPK_CONF:-/etc/abuild.conf}
[ -f "$MAKEAPK_CONF" ] && . "$MAKEAPK_CONF"
startdir="$PWD"
srcdir=${srcdir:-"$startdir/src"}
pkgdir=${pkgdir:-"$startdir/pkg"}
pkgrel=0
# defaults
SRCDEST=${SRCDEST:-$startdir}
PKGDEST=${PKGDEST:-$startdir}
default_cmds="checkdeps clean fetch checkmd5 unpack rootpkg"
all_cmds="checksum fetch unpack rootpkg build package cleancache clean srcpkg"
# functions
msg() {
local sub=
[ -n "$INSUB" ] && sub="(sub)"
[ -z "$quiet" ] && echo ">>> $pkgname $sub:" "$@" >&2
}
warning() {
echo ">>> Warning:" "$@" >&2
}
die() {
quiet=
msg "$@" >&2
exit 1
}
# check if we have needed packages to build this thing
checkdeps() {
local i
local missing=
for i in $makedepends; do
apk_info -e $i || missing="$missing $i"
done
[ -z "$missing" ] && return 0
warning "Missing makedepends: $missing"
return 1
}
checkmd5() {
if [ -z "$source" ]; then
return 0
fi
if [ -z "$md5sums" ]; then
die "Use 'abuild checksum >>$APKBUILD' to generate a checksum"
fi
cd "$srcdir" && echo "$md5sums" | md5sum -c
}
uri_fetch() {
local uri="$1"
local d="${s##*/}" # $(basename $s)
local opts
[ -n "$quiet" ] && opts="-q"
[ -f "$SRCDEST/$d" ] && return 0
mkdir -p "$SRCDEST"
if [ -f "$SRCDEST/$d.part" ]; then
msg "Partial download found. Trying to resume"
opts="$opts -c"
fi
wget $opts -O "$SRCDEST/$d.part" "$uri" \
&& mv "$SRCDEST/$d.part" "$SRCDEST/$d"
}
fetch() {
local s
mkdir -p "$srcdir"
for s in $source; do
case "$s" in
http://*|ftp://*)
uri_fetch "$s" || return 1
;;
esac
ln -sf "$SRCDEST/${s##*/}" "$srcdir"/
done
}
# unpack the sources
unpack() {
local u
mkdir -p "$srcdir"
for u in $source; do
local s="$SRCDEST/${u##*/}" # $(basename $s)
case "$s" in
*.tar.gz)
tar -C "$srcdir" -zxf "$s" || return 1;;
*.tar.bz2)
tar -C "$srcdir" -jxf "$s" || return 1;;
esac
done
}
# cleanup source and package dir
clean() {
rm -rf "$srcdir"
rm -rf "$pkgdir"
local i
for i in $subpackages; do
rm -rf "$pkgdir-$i"
done
}
# cleanup fetched sources
cleancache() {
local s
for s in $source; do
case "$s" in
http:/*|ftp:/*)
rm -f "$SRCDIR/$(basename $s)";;
esac
done
}
cleanpkg() {
local p=$pkgname-$pkgver
[ $pkgrel -ne 0 ] && p="$p-r$pkgrel"
rm -f "$PKGDEST/$p.apk" "$PKGDEST/$p.src.tar.gz"
local i
for i in $subpackages; do
INSUB=1 subpkgdir="$pkgdir-$i" $0 $i cleanpkg
done
}
runpart() {
local part=$1
msg "$part"
$part || die "$part failed"
}
# override those in your build script
build() {
die "No build() function found in $APKBUILD"
}
subpkg() {
[ -z "$subpackages" ] && return 0
local i
cd "$startdir"
for i in $subpackages; do
INSUB=1 subpkgdir="$pkgdir-$i" $0 $i package || return 1
done
}
package_apk() {
local dir=${subpkgdir:-$pkgdir}
local p="$pkgname-$pkgver"
[ "$pkgrel" -ne 0 ] && p="$p-r$pkgrel"
local pkg="$p.apk"
# local db="$dir/var/db/apk/$p"
# mkdir -p "$db"
# echo "$arch" > "$db/ARCH"
# echo "$pkgdesc" > "$db/DESC"
# echo "$license" > "$db/LICENSE"
# echo "$depends" > "$db/DEPEND"
# echo "$url" > "$db/WWW"
[ ! -d "$dir" ] && die "Missing $dir"
cd "$dir"
msg "Creating $pkg"
local builddate=$(date -u "+%s")
local size=$(du -sk | awk '{print $1 * 1024}')
echo "# Generated by $(basename $0) $myver" >.PKGINFO
if [ -n "$FAKEROOTKEY" ]; then
echo "# using $(fakeroot -v)" >> .PKGINFO
fi
echo "# $(date -u)" >> .PKGINFO
cat >> .PKGINFO <<EOF
pkgname = $pkgname
pkgver = $pkgver
pkgdesc = $pkgdesc
url = $url
builddate = $builddate
packager = ${PACKAGER:-"Unknown"}
size = $size
EOF
local i
for i in $license; do
echo "license = $i" >>.PKGINFO
done
for i in $replaces; do
echo "replaces = $i" >>.PKGINFO
done
for i in $depends; do
echo "depend = $i" >>.PKGINFO
done
for i in $conflicts; do
echo "conflict = $i" >>.PKGINFO
done
for i in $provides; do
echo "provides = $i" >>.PKGINFO
done
for i in $backup; do
echo "backup = $i" >>.PKGINFO
done
local metafiles=".PKGINFO"
if [ -n "$install" ]; then
cp "$srcdir/$install" "$dir/.INSTALL" || return 1
chmod +x "$dir/.INSTALL"
metafiles="$metafiles .INSTALL"
fi
# for i in pre-install post-install pre-deinstall post-deinstall; do
# [ -f ../$i ] && cp ../$i "$db"/
# done
set *
[ "$1" = '*' ] && set --
( cd "$dir" && tar -zcf "$PKGDEST/$pkg" $metafiles $@ )
}
package() {
package_apk
}
# predefined subpackage doc
doc() {
pkgname="$pkgname-doc"
depends=""
local i
for i in doc man info html sgml; do
if [ -d "$pkgdir/usr/share/$i" ]; then
mkdir -p "$subpkgdir/usr/share"
mv "$pkgdir/usr/share/$i" "$subpkgdir/usr/share/"
fi
done
# remove if empty, ignore error (not empty)
rmdir "$pkgdir/usr/share" "$pkgdir/usr" 2>/dev/null
[ -d "$subpkgdir/usr/share/man" ] && depends="man"
return 0
}
# predefined subpackage mod
mod() {
pkgname="$pkgname-mod"
depends="$kernel"
for i in firmware modules; do
if [ -d "$pkgdir/lib/$i" ]; then
rm -rf "$subpkgdir/lib"
mkdir -p "$subpkgdir/lib"
mv "$pkgdir/lib/$i" "$subpkgdir/lib"
fi
done
}
# predefined subpackage dev
dev() {
depends="$pkgname"
pkgname="$pkgname-dev"
for i in $(cd "$pkgdir" && find usr/lib -name '*.a' -o \
-name '*.la' -o -name '*.o' 2>/dev/null) \
usr/include usr/lib/pkgconfig usr/share/aclocal; do
if [ -e "$pkgdir/$i" ] || [ -L "$pkgdir/$i" ]; then
d="$subpkgdir/${i%/*}" # dirname $i
mkdir -p "$d"
mv "$pkgdir/$i" "$d"
fi
done
}
# build and package in fakeroot
rootpkg() {
cd "$startdir"
fakeroot $0 build subpkg package
}
srcpkg() {
local p="$pkgname-$pkgver"
[ "$pkgrel" -ne 0 ] && p="$p-r$pkgrel"
local prefix="${startdir##*/}"
local i files="$prefix/APKBUILD"
for i in $source; do
files="$files $prefix/${i##*/}"
done
mkdir -p "$PKGDEST"
(cd .. && tar -zcf $PKGDEST/$p.src.tar.gz $files)
}
# check if package is up to date
up2date() {
local p="$pkgname-$pkgver"
[ "$pkgrel" -ne 0 ] && p="$p-r$pkgrel"
local pkg="$PKGDEST/$p.apk"
local i s
cd "$startdir"
[ -f "$pkg" ] || return 1
for i in $source APKBUILD; do
local s="$SRCDEST/${i##*/}" # $(basename $i)
if [ "$s" -nt "$pkg" ]; then
return 1
fi
done
return 0
}
usage() {
echo "$(basename $0) $myver"
echo "usage: $0 [options] [cmd] ..."
echo "Options:"
echo " -h Show this help"
echo " -f Force specified cmd, even if they are already done"
echo " -q Quiet"
echo ""
echo "Commands:"
echo " checksum Generate checksum to be included in $APKBUILD"
echo " fetch Fetch sources to \$SRCDEST and verify checksums"
echo " unpack Unpack sources to \$srcdir"
echo " build Compile and install package into \$pkgdir"
echo " package Create package in \$PKGDEST"
echo " rootpkg Run '$0 build package' as fakeroot"
echo " clean Remove temp build and install dirs"
echo " cleanpkg Remove already built binary and source package"
echo " cleancache Remove downloaded files from \$SRCDEST"
echo " srcpkg Make a source package"
echo " up2date Compare target and sources dates"
echo ""
exit 0
}
checksum() {
local s files
for s in $source; do
files="$files ${s##*/}"
done
md5sums="$(cd "$SRCDEST" && md5sum $files)" || die "md5sum failed"
echo "md5sums=\"$md5sums\""
}
APKBUILD="${APKBUILD:-./APKBUILD}"
while getopts "hfq" opt; do
case $opt in
'h') usage;;
'f') force=1;;
'q') quiet=1;;
esac
done
shift $(( $OPTIND - 1 ))
# source the buildfile
[ -f "$APKBUILD" ] || die "Could not find $APKBUILD (PWD=$PWD)"
. "$APKBUILD"
# If we are handling a sub package then reset subpackages
[ -n "$INSUB" ] && subpackages=
trap 'die "Aborted by user"' INT
[ -n "$forceunpack" ] && rm -f "$srcdir"/.unpack
if [ -z "$1" ]; then
if up2date && [ -z "$force" ]; then
msg "Package is up to date"
else
set $default_cmds
fi
fi
while [ $# -gt 0 ]; do
runpart $1
shift
done