blob: bd93c47f650ea37611b16839e90a3bf7a633e2a2 [file] [log] [blame]
#!/bin/bash
#
# kbuild -- covenience script for build kernels
#
# This script will build a kernel using an external build directory.
# To use it, create the directory ".git/kbuild" in the top-level of
# the kernel sources (note: if using worktrees, this script will first
# look for the worktree's gitdir for a kbuild directory; and if it's
# not there, use the .git/kbuildin the primary/common gitdir), and
# create a file $GIT_DIR/kbuild/config with the following contents:
#
# BLD_DIR=/build/ext4
# KERN_ARCH=x86_64
#
# This will deposit the object files into the /build/ext4 directory,
# and create the modules.tar.gz directory for use with kvm-xfstests
# and gce-xfstests.
if test -n "$KBUILD_DIR"
then
DIR="$KBUILD_DIR"
else
DIR="$(dirname $0)"
fi
. $DIR/../run-fstests/util/arch-funcs
# N=$(($(getconf _NPROCESSORS_ONLN) / 2))
N=$(($(getconf _NPROCESSORS_ONLN) * 1))
NO_ACTION=
if GITDIR=$(git rev-parse --git-dir 2> /dev/null) ; then
if ! test -d "$GITDIR/kbuild" &&
common=$(git rev-parse --git-common-dir 2> /dev/null) ; then
GITDIR="$common"
fi
else
GITDIR=.git
fi
if test ! -f MAINTAINERS ; then
echo "Not at top-level of kernel source tree?" 1>&2
exit 1
fi
mkdir -p "$GITDIR/kbuild"
if test -f "$GITDIR/kbuild/config" ; then
. "$GITDIR/kbuild/config"
elif test -f "$GITDIR/kbuild.conf" ; then
. "$GITDIR/kbuild.conf"
mv "$GITDIR/kbuild.conf" "$GITDIR/kbuild/config"
fi
set_default_arch
while [ "$1" != "" ];
do
case "$1" in
--arch)
shift
ARCH="$1"
;;
--arm64)
ARCH=arm64
;;
--dpkg)
DO_DPKG=yes
DPKG_EXPLICIT=yes
;;
--no-dpkg)
DO_DPKG=
DPKG_EXPLICIT=yes
;;
--oldconfig)
DO_OLDCONFIG=yes
;;
--get-build-dir)
DO_GET_BUILD_DIR=yes
;;
--get-kbuild-config)
if test -f "$GITDIR/kbuild/config" ; then
echo "$GITDIR/kbuild/config"
exit 0
else
exit 1
fi
;;
--get-kbuild-dir)
if test -d "$GITDIR/kbuild" ; then
echo "$GITDIR/kbuild"
exit 0
else
exit 1
fi
;;
--i386|-32)
ARCH=i386
;;
--no-action)
NO_ACTION=echo
;;
--kunit|--test)
DO_KUNIT=yes
;;
-j)
shift
N="$1"
;;
-*)
echo "unknown option: $1"
exit 1
;;
*)
break
esac
shift
done
set_canonicalized_arch "$ARCH"
set_cross_compile
case "$ARCH" in
arm64)
if test -n "$BLD_DIR_ARM64" ; then
BLD_DIR=$BLD_DIR_ARM64
fi
;;
i386)
if test -n "$BLD_DIR_32" ; then
BLD_DIR=$BLD_DIR_32
elif test -n "$BLD_DIR_I386" ; then
BLD_DIR=$BLD_DIR_I386
fi
;;
amd64)
if test -n "$BLD_DIR_X86_64" ; then
BLD_DIR=$BLD_DIR_X86_64
fi
;;
*)
echo "unknown architecture: $KERN_ARCH"
exit 1
;;
esac
if test -n "$1" -a -z "$DPKG_EXPLICIT" ; then
DO_DPKG=
fi
if test -n "$DO_GET_BUILD_DIR"; then
if test -z "$BLD_DIR" ; then
BLD_DIR=.
fi
echo $BLD_DIR
exit 0
fi
if test -n "$BLD_DIR" -a ! -d "$BLD_DIR" ; then
mkdir -p "$BLD_DIR"
if test -f "$GITDIR/kbuild/kernel-config" ; then
cp "$GITDIR/kbuild/kernel-config" "$BLD_DIR/.config"
fi
for i in x509.genkey signing_key.pem signing_key.x509
do
if test -f "$GITDIR/kbuild/$i" ; then
mkdir -p "$BLD_DIR/certs"
cp "$GITDIR/kbuild/$i" "$BLD_DIR/certs"
fi
done
fi
MAKE_ARGS=("ARCH=${KERN_ARCH:-x86_64}" "-j$N")
if test -n "$BLD_DIR" ; then
MAKE_ARGS+=("O=$BLD_DIR")
else
BLD_DIR="."
fi
if test -n "$CROSS_COMPILE" ; then
MAKE_ARGS+=("CROSS_COMPILE=$CROSS_COMPILE")
fi
if grep -q CONFIG_CC_IS_CLANG=y "$BLD_DIR/.config" 2>/dev/null ; then
MAKE_ARGS+=("CC=clang")
fi
if test -n "$DO_OLDCONFIG" ; then
time make "${MAKE_ARGS[@]}" olddefconfig
fi
rm -f "$BLD_DIR/linux-image.deb" "$BLD_DIR/linux-image-dbg.deb" \
"$BLD_DIR/linux-headers.deb" "$BLD_DIR/linux-libc-dev.deb"
if test -n "$DO_KUNIT" ; then
$NO_ACTION ./tools/testing/kunit/kunit.py run
exit 0
fi
if test -n "$DO_DPKG" ; then
test -f "$BLD_DIR/modules.order" || touch "$BLD_DIR/modules.order"
$NO_ACTION make "${MAKE_ARGS[@]}" prepare
REL=$(make "${MAKE_ARGS[@]}" kernelrelease | grep -v ^make)
MAJOR=$(echo $REL | awk -F. '{print $1}')
MINOR=$(echo $REL | awk -F. '{print $2}')
if test -f "$BLD_DIR/.version" ; then
NUM=$(cat "$BLD_DIR/.version")
# Starting in 6.1, how "make bindeb-pkg" handled the
# .version numbering randomly changed; accomodate that
# here. Things are broken starting in 6.1-rc1 until 6.1-rc6
# but we won't worry about that here. See commit
# 5db8face97f8 ("kbuild: Restore .version auto-increment behaviour
# for Debian packages") which fixed the problem but in a way
# which was different compared to how 6.0 and earlier kernels
# handled things.
if test "$MAJOR" -ge 6 -a "$MINOR" -ge 1 ; then
NUM=$(( $NUM + 1 ))
fi
else
NUM=1
fi
$NO_ACTION time nice make "KDEB_PKGVERSION=$REL-$NUM" "KDEB_SOURCENAME=linux-${REL}" "${MAKE_ARGS[@]}" \
bindeb-pkg "$@"
err=$?
d="$BLD_DIR/.."
if test -f "$BLD_DIR/debian/arch" ; then
arch=$(cat $BLD_DIR/debian/arch)
else
arch=$(dpkg-architecture -q DEB_TARGET_ARCH)
fi
NUM=$(cd $d ; /bin/ls -t linux-${REL}_${REL}*.changes | head -1 | \
sed -e "s/linux-${REL}_${REL}-//" -e "s/_${arch}.changes//")
v="${REL}-${NUM}_${arch}"
if test -f "$d/linux-image-${REL}_${v}.deb" ; then
$NO_ACTION mv "$d/linux-image-${REL}_${v}.deb" "$BLD_DIR/linux-image.deb"
fi
if test -f "$d/linux-image-${REL}-dbg_${v}.deb" ; then
$NO_ACTION mv "$d/linux-image-${REL}-dbg_${v}.deb" "$BLD_DIR/linux-image-dbg.deb"
fi
if test -f "$d/linux-headers-${REL}_${v}.deb" ; then
$NO_ACTION mv "$d/linux-headers-${REL}_${v}.deb" "$BLD_DIR/linux-headers.deb"
fi
if test -f "$d/linux-libc-dev_${v}.deb" ; then
$NO_ACTION mv "$d/linux-libc-dev_${v}.deb" "$BLD_DIR/linux-libc-dev.deb"
fi
$NO_ACTION rm -f "$d/linux-${REL}_${v}.buildinfo" "$d/linux-${REL}_${v}.changes"
else
$NO_ACTION time nice make "${MAKE_ARGS[@]}" "$@"
err=$?
fi
if test -z "$*" -a "$err" == 0 ; then
git describe > $BLD_DIR/.git_version
fi
if test -z "$*" -a "$err" == 0 && \
grep -q CONFIG_MODULES=y $BLD_DIR/.config ; then
TMPMODDIR=$(mktemp --tmpdir -d kbuild-modules.XXXXXXXX)
echo "Generating $BLD_DIR/modules.tar.xz"
$NO_ACTION make "${MAKE_ARGS[@]}" modules_install INSTALL_MOD_PATH="$TMPMODDIR" > /dev/null
$NO_ACTION tar -C "$TMPMODDIR" -c --owner=0 --group=0 --numeric-owner \
--mode=go+u-w -f - lib/modules | xz > "$BLD_DIR/modules.tar.xz"
$NO_ACTION rm -rf "$TMPMODDIR"
else
$NO_ACTION rm -f "$BLD_DIR/modules.tar.xz"
fi
$NO_ACTION cp "$BLD_DIR/.config" "$GITDIR/kbuild/kernel-config"
for i in x509.genkey signing_key.pem signing_key.x509
do
if test -f "$BLD_DIR/certs/$i" ; then
$NO_ACTION cp "$BLD_DIR/certs/$i" "$GITDIR/kbuild"
fi
done
exit $err