#!/bin/sh
set -e
# This is a SlackBuild script for building as-original-as-possible kernel packages.
# It combines existing SlackBuilds and other files in the slackware source tree with vanilla SlackBuild conventions in a way that is clean and functional.
# Unlike the "official" method of building a Slackware kernel, it builds and installs everything in $TMP, so it doesn't taint the host system, and you can build it as a regular user under fakeroot, if you wanted to.
# This often means that the $PKG directory is used as an intermediate directory to run a SlackBuild in, and that said SlackBuilds may need to be modified to run properly in $PKG.
# This script is tailored for x86_64. i?86 support is untested, and would need more modifications (such as building the -smp kernels and fixing $TMP in the packaging SlackBuilds).
# Other architectures can be added, provided the source tree for them is anywhere close to the original slackware tree.
# This SlackBuild should be placed in the "source/k" directory of the source DVD.
# However, this directory doesn't contain all the files we need, as they're scattered all throughout the slackware tree.
# This code may help prepare the source directory:
if [ "$1" = prepare ]; then
shift
if [ $# -lt 3 ]; then
echo Usage: $0 prepare \<source\> \<install\> \<output\> 1>&2
echo e.g. $0 prepare /mnt/tmp/slackware-14.2-source-dvd/source /mnt/tmp/slackware64-14.2-install-dvd/slackware64 ./kernel 1>&2
exit 1
fi
SOURCE=$1
INSTALL=$2
OUT=$3
# Copy the "source/k" directory
cp -aT $SOURCE/k $OUT
# Copy the "source/d/kernel-headers" and "source/a/kernel-firmware" directories into it
cp -at $OUT $SOURCE/d/kernel-headers $SOURCE/a/kernel-firmware
# Copy "slackware*/k/kernel-source-*-noarch-*.txt" to use as slack-desc for kernel-source into it
mkdir -p $OUT/kernel-source
cp -aT $INSTALL/k/kernel-source-*-noarch-*.txt $OUT/kernel-source/slack-desc
# Copy the SlackBuild
cp -at $OUT $0
exit
fi
PKGNAM=${PKGNAM:-linux}
BASEVER=${BASEVER:-4.4.14}
VERSION=${VERSION:-$BASEVER}
ARCH=${ARCH:-x86_64}
KARCH=${KARCH:-x86}
BUILD=${BUILD:-1}
CWD=$(pwd)
TMP=${TMP:-/tmp/kernel}
NUMJOBS=${NUMJOBS:-" -j7 "}
export VERSION ARCH BUILD TMP
# Extract the kernel source
mkdir -p $TMP
cd $TMP
rm -rf $PKGNAM-$BASEVER $PKGNAM-$VERSION
tar xvf $CWD/$PKGNAM-$BASEVER.tar.xz
# Rename the kernel source directory to the proper VERSION
# This is useful if you're applying stable patches and the actual VERSION is different from the one the tarball is named after
if [ "$BASEVER" != "$VERSION" ]; then
mv $PKGNAM-$BASEVER $PKGNAM-$VERSION
fi
# Apply patches
cd $PKGNAM-$VERSION
#xzcat $CWD/patch-x.x.xx.xz | patch -p1 --verbose
#
# Build kernel-headers
#
cd $TMP/$PKGNAM-$VERSION
# Install the kernel headers
PKG=$TMP/package-kernel-headers
rm -rf $PKG
mkdir -p $PKG/usr
make $NUMJOBS INSTALL_HDR_PATH=$PKG/usr headers_install
# Apply some modifications that are present in the original kernel-headers package
find $PKG/usr -type f -a ! -name '*.h' -delete
rm -rf $PKG/usr/include/drm
mv $PKG/usr/include/asm $PKG/usr/include/asm-$KARCH
ln -sf asm-$KARCH $PKG/usr/include/asm
# Install the slack-desc
mkdir -p $PKG/install
cat $CWD/kernel-headers/slack-desc > $PKG/install/slack-desc
# Create the package
cd $PKG
/sbin/makepkg -l y -c n $TMP/kernel-headers-$(echo $VERSION | tr - _)-$KARCH-$BUILD.txz
#
# Build kernel-huge
#
cd $TMP/$PKGNAM-$VERSION
# Build the kernel image
make $NUMJOBS mrproper
cat $CWD/config-$ARCH/config-huge-* > .config
make $NUMJOBS oldconfig
make $NUMJOBS bzImage
# Prepare the directory to run the packaging SlackBuild in
PKG=$TMP/kernel-huge
rm -rf $PKG
mkdir -p $PKG
cp arch/$KARCH/boot/bzImage $PKG
cp System.map $PKG
cp .config $PKG/config
# Copy the packaging SlackBuild
cp $CWD/packaging-$ARCH/kernel-huge/kernel-huge.SlackBuild $PKG
cp $CWD/packaging-$ARCH/kernel-huge/slack-desc $PKG
# Run the packaging SlackBuild
cd $PKG
./kernel-huge.SlackBuild
#
# Build kernel-generic
#
cd $TMP/$PKGNAM-$VERSION
# Build the kernel image
make $NUMJOBS mrproper
cat $CWD/config-$ARCH/config-generic-* > .config
make $NUMJOBS oldconfig
make $NUMJOBS bzImage
# Prepare the directory to run the packaging SlackBuild in
PKG=$TMP/kernel-generic
rm -rf $PKG
mkdir -p $PKG
cp arch/$KARCH/boot/bzImage $PKG
cp System.map $PKG
cp .config $PKG/config
# Copy the packaging SlackBuild
cp $CWD/packaging-$ARCH/kernel-generic/kernel-generic.SlackBuild $PKG
cp $CWD/packaging-$ARCH/kernel-generic/slack-desc $PKG
# Run the packaging SlackBuild
cd $PKG
./kernel-generic.SlackBuild
#
# Build kernel-modules
#
cd $TMP/$PKGNAM-$VERSION
# Build the kernel modules with the already-present kernel-generic config
make $NUMJOBS modules
# Prepare the directory to run the packaging SlackBuild in
PKG=$TMP/kernel-modules
rm -rf $PKG
mkdir -p $PKG
make $NUMJOBS INSTALL_MOD_PATH=$PKG modules_install
# Point the build and source links to the proper source directory in /usr/src, as packaged in kernel-source
rm -f $PKG/lib/modules/$VERSION/build $PKG/lib/modules/$VERSION/source
ln -s /usr/src/$PKGNAM-$VERSION $PKG/lib/modules/$VERSION/build
ln -s /usr/src/$PKGNAM-$VERSION $PKG/lib/modules/$VERSION/source
# Copy the packaging SlackBuild
cp $CWD/packaging-$ARCH/kernel-modules/kernel-modules.SlackBuild $PKG
cp $CWD/packaging-$ARCH/kernel-modules/slack-desc $PKG
# Prefix absolute module paths in the SlackBuild with $CWD, since we installed the modules there
sed -i -e 's@/lib/modules/${KERNELRELEASE}@$CWD&@' $PKG/kernel-modules.SlackBuild
# Run the packaging SlackBuild
cd $PKG
KERNELRELEASE=$VERSION ./kernel-modules.SlackBuild
#
# Build kernel-source
#
cd $TMP/$PKGNAM-$VERSION
# Cleanup the source directory that was prepared and built with the kernel-generic config
make $NUMJOBS clean
make $NUMJOBS prepare
rm .version .config.old
# Install the kernel source
PKG=$TMP/package-kernel-source
rm -rf $PKG
mkdir -p $PKG/usr/src
ln -s $PKGNAM-$VERSION $PKG/usr/src/$PKGNAM
# Install the slack-desc
mkdir -p $PKG/install
cat $CWD/kernel-source/slack-desc > $PKG/install/slack-desc
# Create the package
cd $PKG
mv $TMP/$PKGNAM-$VERSION usr/src
/sbin/makepkg -l y -c n $TMP/kernel-source-$(echo $VERSION | tr - _)-noarch-$BUILD.txz
#
# Build kernel-firmware
#
cd $TMP
# Prepare the directory to run the packaging SlackBuild in
PKG=$TMP/kernel-firmware
rm -rf $PKG
mkdir -p $PKG
cp -aT $CWD/kernel-firmware $PKG
# Use --depth=1 to cause git to not to download the entire history
sed -i -e 's/git clone/& --depth=1/' $PKG/kernel-firmware.SlackBuild
# Run the packaging SlackBuild
cd $PKG
unset VERSION # The script uses `date` to make a $VERSION
./kernel-firmware.SlackBuild