Last active
December 5, 2023 03:27
-
-
Save emojifreak/180b335cd58edd9a64fc42b85f735c6d to your computer and use it in GitHub Desktop.
Linux kernel compilation by clang as Debian .deb package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
if ! which clang ld.lld llvm-ar >/dev/null; then | |
echo "Please install clang, lld, and llvm packages." | |
exit 1 | |
fi | |
if ! [ -d /usr/src/linux-config-5.15 ]; then | |
echo "Please install linux-config-5.15 package." | |
exit 1 | |
fi | |
cd /var/tmp | |
arch=`dpkg --print-architecture` | |
if [ $arch = amd64 ]; then | |
variant=amd64_none_amd64 | |
march='-march=native' | |
elif [ $arch = arm64 ]; then | |
variant=arm64_none_arm64 | |
march='-mcpu=native' | |
elif [ $arch = armhf ]; then | |
variant=armhf_none_armmp-lpae | |
march='-mtune=native' | |
else | |
echo "Please find a suitable config of $arch." | |
exit 1 | |
fi | |
KVER=5.15.4 | |
exec </dev/null >build-log-${KVER}.txt 2>&1 | |
set -xe | |
wget -T 10 -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-${KVER}.tar.xz | |
tar Jxf linux-${KVER}.tar.xz | |
cd linux-${KVER} | |
# install linux-config-5.15 from sid or experimental | |
xzcat /usr/src/linux-config-5.15/config.${variant}.xz >.config | |
cat >>.config <<EOF | |
CONFIG_MCORE2=y | |
CONFIG_DEBUG_STACK_USAGE=y | |
CONFIG_DEBUG_INFO=n | |
CONFIG_LTO_CLANG_FULL=y | |
CONFIG_TRIM_UNUSED_KSYMS=y | |
CONFIG_CFI_CLANG=y | |
CONFIG_CFI_CLANG_SHADOW=y | |
CONFIG_SHADOW_CALL_STACK=y | |
CONFIG_INIT_STACK_ALL_ZERO=y | |
CONFIG_VGA_SWITCHEROO=n | |
CONFIG_VGA_ARB=n | |
CONFIG_DEVPORT=n | |
CONFIG_DEVMEM=n | |
CONFIG_DEVKMEM=n | |
CONFIG_SND_OSSEMUL=n | |
CONFIG_ZONE_DEVICE=y | |
CONFIG_DEVICE_PRIVATE=y | |
CONFIG_PREEMPT=y | |
CONFIG_DEBUG_PREEMPT=n | |
CONFIG_HZ_100=y | |
CONFIG_HYPERVISOR_GUEST=n | |
CONFIG_KVM_GUEST=n | |
CONFIG_HYPERV=n | |
CONFIG_VBOXGUEST=n | |
CONFIG_XEN=n | |
CONFIG_PARAVIRT=n | |
CONFIG_IRQ_TIME_ACCOUNTING=y | |
CONFIG_CLEAN_CACHE=y | |
CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y | |
CONFIG_STACK_VALIDATION=y | |
CONFIG_WQ_WATCHDOG=y | |
CONFIG_XFS_FS=n | |
EOF | |
if [ $arch = armhf ]; then | |
cat >>.config <<EOF | |
CONFIG_LTO_CLANG_FULL=n | |
CONFIG_LTO_CLANG_THIN=y | |
CONFIG_PCIE_BRCMSTB=m | |
CONFIG_VIDEO_VIVID=n | |
EOF | |
fi | |
sed -i 's/-O2/-O3/g' Makefile | |
sed -i 's/-Os/-Oz/g' Makefile | |
set +e | |
if [ $arch = armhf ]; then | |
yes '' | | |
chrt --batch 0 nice -19 /usr/bin/time -v make -j `nproc` ARCH=arm LLVM=1 LLVM_IAS=1 \ | |
KCFLAGS="-pipe ${march} -fintegrated-cc1 -mllvm -polly -mllvm -polly-ast-use-context -mllvm -polly-invariant-load-hoisting -mllvm -polly-opt-fusion=max -mllvm -polly-run-inliner -mllvm -polly-vectorizer=stripmine -mllvm -polly-run-dce" \ | |
LOCALVERSION=-clang \ | |
bindeb-pkg | |
else | |
yes '' | | |
chrt --batch 0 nice -19 /usr/bin/time -v make -j `nproc` LLVM=1 LLVM_IAS=1 \ | |
KCFLAGS="-pipe ${march} -fintegrated-cc1 -mllvm -polly -mllvm -polly-ast-use-context -mllvm -polly-invariant-load-hoisting -mllvm -polly-opt-fusion=max -mllvm -polly-run-inliner -mllvm -polly-vectorizer=stripmine -mllvm -polly-run-dce" \ | |
LOCALVERSION=-clang \ | |
bindeb-pkg | |
fi | |
echo 'If compilation failed, please do "sudo apt-get build-dep linux/sid"' | |
echo "This script was verified by clang, lld, llvm in Debian/experimental and linux-5.15.2." | |
echo "CONFIG_XFS_FS=m causes runtime error in clang." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment