summaryrefslogtreecommitdiff
path: root/build.sh
diff options
context:
space:
mode:
Diffstat (limited to 'build.sh')
-rwxr-xr-xbuild.sh113
1 files changed, 113 insertions, 0 deletions
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..8568131
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,113 @@
+#!/bin/bash
+
+set -e
+set -x
+
+BUILDDIR=/tmp/build
+SRCDIR=$(dirname $(realpath "$0"))
+
+SIZE_CFLAGS="-I/tmp/build/include -I/tmp/build/include/ncurses -Os -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables"
+SIZE_LDFLAGS="-L/tmp/build/lib -Wl,--gc-sections -Wl,--strip-all"
+
+NPROC=$(grep -sc processor /proc/cpuinfo || echo 4)
+
+[ -d "$BUILDDIR" ] && exit 1
+
+mkdir -p "$BUILDDIR"/rootfs/{dev,lib/terminfo/l,nethack,sbin}
+pushd "$BUILDDIR"
+
+
+# install build dependencies
+grep -q ^deb-src /etc/apt/sources.list
+su -c "export DEBIAN_FRONTEND=noninteractive;
+ apt-get update;
+ apt-get install --yes build-essential gcc-4.9 upx-ucl gawk lilo ca-certificates;
+ apt-get build-dep --yes musl nethack ncurses linux"
+
+
+# download sources
+pushd "$SRCDIR"
+[ -f "ncurses-6.0.tar.gz" ] || wget "https://ftp.gnu.org/gnu/ncurses/ncurses-6.0.tar.gz"
+[ $(md5sum "ncurses-6.0.tar.gz" | cut -d ' ' -f 1) = "ee13d052e1ead260d7c28071f46eefb1" ] || exit 1
+[ -f "nethack-360-src.tgz" ] || wget "http://prdownloads.sourceforge.net/nethack/nethack-360-src.tgz?download" -O "nethack-360-src.tgz"
+[ $(md5sum "nethack-360-src.tgz" | cut -d ' ' -f 1) = "d42147b26e5fb4746fb72536ce145984" ] || exit 1
+[ -f "linux-2.6.32.70.tar.xz" ] || wget "https://cdn.kernel.org/pub/linux/kernel/v2.6/longterm/v2.6.32/linux-2.6.32.70.tar.xz"
+[ $(md5sum "linux-2.6.32.70.tar.xz" | cut -d ' ' -f 1) = "1a3ae82aab5acb3cb42e8514ee460a71" ] || exit 1
+popd
+
+
+# build musl
+apt-get source musl
+
+pushd musl-*/
+patch -p1 < "$SRCDIR/musl.patch"
+dpkg-buildpackage -us -uc -jauto
+popd
+
+su -c "dpkg -i musl_*.deb musl-dev_*.deb musl-tools_*.deb"
+
+
+# build ncurses
+tar zxf "$SRCDIR"/ncurses-*.tar.gz
+
+pushd ncurses-*/
+./configure --prefix="$BUILDDIR" --with-terminfo-dirs=/lib/terminfo --without-shared --without-gpm AWK="gawk" CC="musl-gcc" CFLAGS="$SIZE_CFLAGS" LDFLAGS="$SIZE_LDFLAGS"
+make -j$NPROC
+make install
+install -m 644 "$BUILDDIR/share/terminfo/l/linux" "$BUILDDIR/rootfs/lib/terminfo/l/"
+popd
+
+
+# extract kernel for headers
+tar Jxf "$SRCDIR"/linux-*.tar.xz
+cp -a linux-*/include/linux "$BUILDDIR/include/"
+
+
+# build nethack
+tar zxf "$SRCDIR"/nethack-*-src.tgz
+
+pushd nethack-*/
+patch -p1 < "$SRCDIR/nethack.patch"
+pushd sys/unix
+./setup.sh
+popd
+make -j$NPROC nethack CC="musl-gcc" LINK="musl-gcc" CFLAGS="-I../include $SIZE_CFLAGS -flto" LFLAGS="$SIZE_LDFLAGS -flto -static"
+make -C dat CFLAGS="-I../include"
+make dlb CC="musl-gcc" LINK="musl-gcc" CFLAGS="-I../include $SIZE_CFLAGS -flto" LFLAGS="$SIZE_LDFLAGS -flto"
+upx --ultra-brute --overlay=strip src/nethack
+install -m 755 src/nethack "$BUILDDIR/rootfs/nethack"
+install -m 644 dat/nhdat "$BUILDDIR/rootfs/nethack"
+touch "$BUILDDIR/rootfs/nethack/perm"
+popd
+
+
+# build init
+musl-gcc "$SRCDIR/init.c" -o "$BUILDDIR/rootfs/sbin/init" $SIZE_CFLAGS $SIZE_LDFLAGS -flto -static
+
+
+# build kernel
+echo "nod /dev/console 0600 0 0 c 5 1" > rootfs-files.txt
+pushd linux-*/
+patch -p1 < "$SRCDIR/kernel.patch"
+cp "$SRCDIR/config-kernel" .config
+make oldconfig
+make -j$NPROC bzImage CC="gcc-4.9"
+popd
+
+
+# create floppy
+dd if=/dev/zero of=floppy.img bs=1024 count=1440
+/sbin/mkfs.minix floppy.img
+mkdir -p "$BUILDDIR/mnt"
+su -c "
+ set -ex;
+ mount floppy.img \"$BUILDDIR/mnt\";
+ mkdir \"$BUILDDIR\"/mnt/{boot,dev};
+ mount /dev \"$BUILDDIR/mnt/dev\" -o bind;
+ cp \"$BUILDDIR\"/linux-*/arch/x86/boot/bzImage \"$SRCDIR/lilo.conf\" \"$BUILDDIR/mnt/\";
+ lilo -g -r \"$BUILDDIR/mnt\" -C /lilo.conf;
+ rm \"$BUILDDIR/mnt/lilo.conf\";
+ umount \"$BUILDDIR/mnt/dev\" \"$BUILDDIR/mnt\";
+"
+
+printf "\n\nFinished! Result is at $BUILDDIR/floppy.img\n\n"