2024-03-10 16:04:07 +01:00
#!/bin/bash
set -eo pipefail
# Variables
2024-03-10 21:58:24 +01:00
## Cloning (update these if you fork this project)
2024-03-10 16:04:07 +01:00
export "BUILDTOOL_REPOSITORY=https://git.staropensource.de/JeremyStarTM/kernel-optimized.git"
2024-03-10 21:58:24 +01:00
export "BUILDTOOL_CLONEDIR=jstm-kernel-optimized"
## Base packages to install, packages will be added by questions
2024-03-10 16:04:07 +01:00
export "BUILDTOOL_PACKAGES=base-devel git rustup"
2024-03-10 21:58:24 +01:00
## Building command line stuff
2024-03-10 21:14:12 +01:00
export " BUILDTOOL_BUILDCMDLINE=nice -20 env MAKEFLAGS=-j $( nproc) "
2024-03-10 21:58:24 +01:00
## OOOOoooo colors! (not true color tho)
export "BUILDTOOL_COLOR_SPECIAL=\e[0;35m"
export "BUILDTOOL_COLOR_WARN=\e[0;33m"
export "BUILDTOOL_COLOR_ERROR=\e[0;31m"
export "BUILDTOOL_COLOR_RESET=\e[0m"
# Logging
## Empty
function empty( ) {
echo ""
}
## Special
function special( ) {
echo -e " ${ BUILDTOOL_COLOR_SPECIAL } ${ * } ${ BUILDTOOL_COLOR_RESET } "
}
## Informational (header)
function infoh( ) {
echo -e " ${ BUILDTOOL_COLOR_RESET } :: ${ * } "
}
## Informational (extension)
function infoe( ) {
echo -e " ${ BUILDTOOL_COLOR_RESET } ${ * } "
}
## Warning (header)
function warnh( ) {
echo -e " ${ BUILDTOOL_COLOR_WARN } :: Warning: ${ * } ${ BUILDTOOL_COLOR_RESET } "
}
## Warning (extension)
function warne( ) {
2024-03-10 22:19:59 +01:00
echo -e " ${ BUILDTOOL_COLOR_WARN } ${ * } ${ BUILDTOOL_COLOR_RESET } "
2024-03-10 21:58:24 +01:00
}
## Error (header)
function errorh( ) {
echo -e " ${ BUILDTOOL_COLOR_ERROR } :: Error: ${ * } ${ BUILDTOOL_COLOR_RESET } "
}
## Error (extension)
function errore( ) {
2024-03-10 22:19:59 +01:00
echo -e " ${ BUILDTOOL_COLOR_ERROR } ${ * } ${ BUILDTOOL_COLOR_RESET } "
2024-03-10 21:58:24 +01:00
}
# Banner
special " ${ BUILDTOOL_COLOR_RESET } ${ BUILDTOOL_COLOR_SPECIAL } _ _ _ _ _ _ _ _ "
special " (_)___| |_ _ __ ___ | | _____ _ __ _ __ ___| | ___ _ __ | |_(_)_ __ ___ (_)_______ __| |"
special " | / __| __| '_ \` _ \\ _____| |/ / _ \\ '__| '_ \\ / _ \\ |_____ / _ \\| '_ \\| __| | '_ \` _ \\| |_ / _ \\/ _\` |"
special " | \\__ \\ |_| | | | | |_____| < __/ | | | | | __/ |_____| (_) | |_) | |_| | | | | | | |/ / __/ (_| |"
special " _/ |___/\\__|_| |_| |_| |_|\\_\\___|_| |_| |_|\\___|_| \\___/| .__/ \\__|_|_| |_| |_|_/___\\___|\\__,_| ${ BUILDTOOL_COLOR_RESET } "
special "|__/ |_|"
empty
2024-03-10 16:04:07 +01:00
# Checks
## Check for Arch Linux
(
2024-03-10 21:26:37 +01:00
# shellcheck disable=SC1091
2024-03-10 16:04:07 +01:00
source "/etc/os-release"
if [ " ${ NAME } " != "Arch Linux" ] || [ " ${ PRETTY_NAME } " != "Arch Linux" ] || [ " ${ ID } " != "arch" ] ; then
if [ " ${ BUILDTOOL_COMPILE_NONARCH } " != "true" ] ; then
2024-03-10 21:58:24 +01:00
errorh "Not running on Arch Linux"
errore "This kernel can only compile on Arch Linux"
errore "If you are sure that you want to compile this kernel on other distributions"
errore "set the \$BUILDTOOL_COMPILE_NONARCH environment variable to \"true\""
2024-03-10 16:04:07 +01:00
exit 1
else
2024-03-10 21:58:24 +01:00
warnh "Compiling on a non-arch distribution. This may lead to unexpected errors!"
2024-03-10 16:04:07 +01:00
fi
fi
)
## Check for root
if [ " ${ UID } " = = "0" ] ; then
2024-03-10 21:58:24 +01:00
errorh "Can't build kernel as root"
errore "Please create a new user for building the kernel"
2024-03-10 16:04:07 +01:00
exit 1
fi
## Check for sudo
if ! which sudo & > /dev/null; then
2024-03-10 21:58:24 +01:00
errorh "Could not find sudo in \$PATH"
errore "Please ensure that you have sudo installed"
2024-03-10 16:04:07 +01:00
exit 1
fi
# Questions
## Ask if mold should replace ld/lld
function ask_mold( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to use mold as your linker (much faster) [Y/n]? " ) " BUILDTOOL_MOLD
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_MOLD } " in
"y" | "Y" | "" )
export " BUILDTOOL_PACKAGES= ${ BUILDTOOL_PACKAGES } mold "
2024-03-10 21:14:12 +01:00
export " BUILDTOOL_BUILDCMDLINE= ${ BUILDTOOL_BUILDCMDLINE } LDFLAGS=-fuse-ld=mold RUSTFLAGS=\"-C link-arg=-fuse-ld=mold\" "
2024-03-10 16:04:07 +01:00
; ;
"n" | "N" ) ; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_mold
esac
}
## Ask if the governor should be adjusted
function ask_cpupower( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to adjust the cpu governor (improves build performance) [Y/n]? " ) " BUILDTOOL_CPUPOWER
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_CPUPOWER } " in
"y" | "Y" | "" )
export " BUILDTOOL_PACKAGES= ${ BUILDTOOL_PACKAGES } cpupower "
; ;
"n" | "N" ) ; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_cpupower
esac
}
## Ask for building in tmpfs
function ask_tmpfs( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to build in a tmpfs (improves build performance, protects your disk from wearing out faster, ~32 GiB memory required) [Y/n]? " ) " BUILDTOOL_TMPFS
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_TMPFS } " in
"y" | "Y" | "" )
export " BUILDTOOL_CLONEDIR=/tmp/ ${ BUILDTOOL_CLONEDIR } "
; ;
"n" | "N" )
export " BUILDTOOL_CLONEDIR= $( pwd ) / ${ BUILDTOOL_CLONEDIR } "
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_tmpfs
esac
}
## Ask for kernel config modification using xconfig
function ask_xconfig( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to configure the kernel using xconfig before build [y/N]? " ) " BUILDTOOL_XCONFIG
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_XCONFIG } " in
"y" | "Y" )
export "BUILDTOOL_PKGBUILD_XCONFIG=_makexconfig=SET"
; ;
"n" | "N" | "" )
export "BUILDTOOL_PKGBUILD_XCONFIG=_makexconfig="
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_xconfig
esac
}
## Ask for kernel config modification using xconfig
function ask_nconfig( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to configure the kernel using nconfig before build [y/N]? " ) " BUILDTOOL_NCONFIG
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_NCONFIG } " in
"y" | "Y" )
export "BUILDTOOL_PKGBUILD_NCONFIG=_makenconfig=SET"
; ;
"n" | "N" | "" )
export "BUILDTOOL_PKGBUILD_NCONFIG=_makenconfig="
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_nconfig
esac
}
## Ask if the final kernel configuration should be copied
function ask_cpfinalconfig( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to copy the final kernel configuration before build [y/N]? " ) " BUILDTOOL_CPFINALCONFIG
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_CPFINALCONFIG } " in
"y" | "Y" )
export "BUILDTOOL_PKGBUILD_CPFINALCONFIG=_copyfinalconfig=SET"
; ;
"n" | "N" | "" )
export "BUILDTOOL_PKGBUILD_CPFINALCONFIG=_copyfinalconfig="
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_cpfinalconfig
esac
}
## Ask if only active modules should be compiled into the kernel
function ask_modprobeddb( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to only build active kernel modules (modprobed-db must be installed, read \"https://wiki.archlinux.org/index.php/Modprobed-db\" first) [y/N]? " ) " BUILDTOOL_MODPROBEDDB
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_MODPROBEDDB } " in
"y" | "Y" )
if ! which modprobed-db & > /dev/null; then
2024-03-10 21:58:24 +01:00
errorh "Could not find modprobed-db in \$PATH."
errore "Please ensure that you have modprobed-db installed."
2024-03-10 16:04:07 +01:00
ask_modprobeddb
return 1
fi
export " BUILDTOOL_PACKAGES= ${ BUILDTOOL_PACKAGES } modprobed-db "
export "BUILDTOOL_PKGBUILD_MODPROBEDDB=_localmodcfg=SET"
; ;
"n" | "N" | "" )
export "BUILDTOOL_PKGBUILD_MODPROBEDDB=_localmodcfg="
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y or N"
2024-03-10 16:04:07 +01:00
ask_modprobeddb
esac
}
## Ask for sub architecture
function ask_subarchitecture( ) {
2024-03-10 21:58:24 +01:00
infoh "Displaying sub architecture list"
infoe " 1. AMD Opteron/Athlon64/Hammer/K8 (MK8)"
infoe " 2. AMD Opteron/Athlon64/Hammer/K8 with SSE3 (MK8SSE3)"
infoe " 3. AMD 61xx/7x50/PhenomX3/X4/II/K10 (MK10)"
infoe " 4. AMD Barcelona (MBARCELONA)"
infoe " 5. AMD Bobcat (MBOBCAT)"
infoe " 6. AMD Jaguar (MJAGUAR)"
infoe " 7. AMD Bulldozer (MBULLDOZER)"
infoe " 8. AMD Piledriver (MPILEDRIVER)"
infoe " 9. AMD Steamroller (MSTEAMROLLER)"
infoe "10. AMD Excavator (MEXCAVATOR)"
infoe "11. AMD Zen (MZEN)"
infoe "12. AMD Zen 2 (MZEN2)"
infoe "13. AMD Zen 3 (MZEN3)"
infoe "14. AMD Zen 4 (MZEN4)"
infoe "15. Intel P4 / older Netburst based Xeon (MPSC)"
infoe "16. Intel Core 2 (MCORE2)"
infoe "17. Intel Atom (MATOM)"
infoe "18. Intel Nehalem (MNEHALEM)"
infoe "19. Intel Westmere (MWESTMERE)"
infoe "20. Intel Silvermont (MSILVERMONT)"
infoe "21. Intel Goldmont (MGOLDMONT)"
infoe "22. Intel Goldmont Plus (MGOLDMONTPLUS)"
infoe "23. Intel Sandy Bridge (MSANDYBRIDGE)"
infoe "24. Intel Ivy Bridge (MIVYBRIDGE)"
infoe "25. Intel Haswell (MHASWELL)"
infoe "26. Intel Broadwell (MBROADWELL)"
infoe "27. Intel Skylake (MSKYLAKE)"
infoe "28. Intel Skylake X (MSKYLAKEX)"
infoe "29. Intel Cannon Lake (MCANNONLAKE)"
infoe "30. Intel Ice Lake (MICELAKE)"
infoe "31. Intel Cascade Lake (MCASCADELAKE)"
infoe "32. Intel Cooper Lake (MCOOPERLAKE)"
infoe "33. Intel Tiger Lake (MTIGERLAKE)"
infoe "34. Intel Sapphire Rapids (MSAPPHIRERAPIDS)"
infoe "35. Intel Rocket Lake (MROCKETLAKE)"
infoe "36. Intel Alder Lake (MALDERLAKE)"
infoe "37. Intel Raptor Lake (MRAPTORLAKE)"
infoe "38. Intel Meteor Lake (MMETEORLAKE)"
infoe "39. Intel Emerald Rapids (MEMERALDRAPIDS)"
infoe "40. Generic-x86-64 (GENERIC_CPU)"
infoe "41. Generic-x86-64-v2 (GENERIC_CPU2)"
infoe "42. Generic-x86-64-v3 (GENERIC_CPU3)"
infoe "43. Generic-x86-64-v4 (GENERIC_CPU4)"
infoe "44. Intel-Native optimizations autodetected by the compiler (MNATIVE_INTEL)"
infoe "45. AMD-Native optimizations autodetected by the compiler (MNATIVE_AMD)"
2024-03-10 16:04:07 +01:00
read -rp ":: Which sub architecture do you want to build Linux for (see above, enter 40 if unsure) [40]? " BUILDTOOL_SUBARCHITECTURE
case " ${ BUILDTOOL_SUBARCHITECTURE } " in
"" )
export "BUILDTOOL_PKGBUILD_SUBARCHITECTURE=_subarch=40"
; ;
*[ !0-9] *)
2024-03-10 21:58:24 +01:00
errorh "Invalid subarchitecture"
2024-03-10 16:04:07 +01:00
ask_subarchitecture
return 1
; ;
*)
if [ ! " ${ BUILDTOOL_SUBARCHITECTURE } " -gt 0 ] || [ ! " ${ BUILDTOOL_SUBARCHITECTURE } " -lt 46 ] ; then
2024-03-10 21:58:24 +01:00
errorh "Invalid subarchitecture"
2024-03-10 16:04:07 +01:00
ask_subarchitecture
else
export " BUILDTOOL_PKGBUILD_SUBARCHITECTURE=_subarch= ${ BUILDTOOL_SUBARCHITECTURE } "
fi
; ;
esac
}
## Ask for kernel debug mode
function ask_debug( ) {
2024-03-10 21:58:24 +01:00
read -rp " $( infoh "Do you want to enable kernel debug mode [y/N/i(gnore)]? " ) " BUILDTOOL_DEBUG
2024-03-10 16:04:07 +01:00
case " ${ BUILDTOOL_DEBUG } " in
"y" | "Y" )
export "BUILDTOOL_PKGBUILD_DEBUG=_debug=y"
; ;
"n" | "N" | "" )
export "BUILDTOOL_PKGBUILD_DEBUG=_debug=n"
; ;
"i" | "I" )
export "BUILDTOOL_PKGBUILD_DEBUG=_debug="
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y, N or I"
2024-03-10 16:04:07 +01:00
ask_debug
esac
}
2024-03-10 21:26:37 +01:00
## Ask for clone directory conflict resolution (includes check)
function ask_clonedir_conflictresolution( ) {
2024-03-10 22:23:33 +01:00
if [ ! -d " ${ BUILDTOOL_CLONEDIR } " ] && [ ! -L " ${ BUILDTOOL_CLONEDIR } " ] ; then
2024-03-10 21:26:37 +01:00
return
2024-03-10 22:23:33 +01:00
elif [ -a " ${ BUILDTOOL_CLONEDIR } " ] && [ ! -d " ${ BUILDTOOL_CLONEDIR } " ] && [ ! -L " ${ BUILDTOOL_CLONEDIR } " ] ; then
warnh " Something that isn't a directory or symlink exists at location \" ${ BUILDTOOL_CLONEDIR } \" "
2024-03-10 21:26:37 +01:00
fi
2024-03-10 21:58:24 +01:00
read -rp " $( infoh " Warning: \" ${ BUILDTOOL_CLONEDIR } \" already exists. What should be done to resolve the conflict [A(bort)/r(ecompile)/f(resh)]? " ) " BUILDTOOL_CLONEDIR_CONFLICT
2024-03-10 21:26:37 +01:00
case " ${ BUILDTOOL_CLONEDIR_CONFLICT } " in
"a" | "A" | "" )
2024-03-10 21:58:24 +01:00
errorh "Conflict resolution failed"
2024-03-10 21:26:37 +01:00
exit 1
; ;
2024-03-10 22:17:00 +01:00
"r" | "R" )
export "BUILDTOOL_MAKEPKG_REUSE= --noextract"
; ;
2024-03-10 21:26:37 +01:00
"f" | "F" )
2024-03-10 21:58:24 +01:00
infoh "Removing existing \$BUILDTOOL_CLONEDIR directory"
2024-03-10 22:23:33 +01:00
rm -rf " ${ BUILDTOOL_CLONEDIR } "
2024-03-10 21:26:37 +01:00
; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Invalid answer. Please answer with Y, N or I."
2024-03-10 21:26:37 +01:00
ask_debug
esac
}
2024-03-10 16:04:07 +01:00
ask_mold
ask_cpupower
ask_tmpfs
ask_xconfig
ask_nconfig
ask_cpfinalconfig
ask_modprobeddb
ask_subarchitecture
ask_debug
2024-03-10 21:26:37 +01:00
ask_clonedir_conflictresolution
2024-03-10 16:04:07 +01:00
# Pre-building
## Install dependencies
2024-03-10 21:58:24 +01:00
infoh "Installing dependencies"
2024-03-10 16:04:07 +01:00
if ! sudo pacman -Syu --asdeps --needed ${ BUILDTOOL_PACKAGES } ; then
2024-03-10 21:58:24 +01:00
errorh "Installing dependencies failed: pacman returned with non-zero exit code"
2024-03-10 16:04:07 +01:00
exit 1
fi
if ! rustup default nightly; then
2024-03-10 21:58:24 +01:00
errorh "Installing dependencies failed: rustup returned with non-zero exit code"
2024-03-10 16:04:07 +01:00
exit 1
fi
## Adjust cpu governor
case " ${ BUILDTOOL_CPUPOWER } " in
"y" | "Y" | "" )
2024-03-10 21:58:24 +01:00
infoh "Adjusting cpu governor"
2024-03-10 16:04:07 +01:00
if ! sudo cpupower frequency-set -g performance; then
2024-03-10 21:58:24 +01:00
errorh "Adjusting cpu governor failed: cpupower returned with non-zero exit code"
2024-03-10 16:04:07 +01:00
return 1
fi
; ;
"n" | "N" ) ; ;
*)
2024-03-10 21:58:24 +01:00
errorh "Internal inconsistency detected: Value of \$BUILDTOOL_CPUPOWER is not a valid boolean answer"
2024-03-10 16:04:07 +01:00
exit 2
; ;
esac
# Clone repository
2024-03-10 22:19:13 +01:00
if [ " ${ BUILDTOOL_CLONEDIR_CONFLICT } " = = "r" ] || [ " ${ BUILDTOOL_CLONEDIR_CONFLICT } " = = "R" ] ; then
warnh "Skipping cloning process"
else
infoh "Cloning repository"
git clone " ${ BUILDTOOL_REPOSITORY } " " ${ BUILDTOOL_CLONEDIR } "
fi
2024-03-10 16:04:07 +01:00
cd " ${ BUILDTOOL_CLONEDIR } "
2024-03-10 21:14:12 +01:00
# Print debug information
if [ " ${ BUILDTOOL_DEBUG } " = = "true" ] ; then
2024-03-10 21:58:24 +01:00
infoh "Printing debug information"
echo "+++ env +++"
2024-03-10 21:14:12 +01:00
env| grep "BUILDTOOL_" | sort
2024-03-10 21:58:24 +01:00
echo "+++ env +++"
2024-03-10 21:14:12 +01:00
echo " build cmdline: ${ BUILDTOOL_BUILDCMDLINE } " ${ BUILDTOOL_PKGBUILD_XCONFIG } " " ${ BUILDTOOL_PKGBUILD_NCONFIG } " " ${ BUILDTOOL_PKGBUILD_CPFINALCONFIG } " " ${ BUILDTOOL_PKGBUILD_MODPROBEDDB } " " ${ BUILDTOOL_PKGBUILD_SUBARCHITECTURE } " " ${ BUILDTOOL_PKGBUILD_DEBUG } " makepkg --syncdeps -p PKGBUILD.buildtool"
fi
2024-03-10 21:58:24 +01:00
# Build package
infoh "Building package"
2024-03-10 22:17:00 +01:00
if ! ${ BUILDTOOL_BUILDCMDLINE } " ${ BUILDTOOL_PKGBUILD_XCONFIG } " " ${ BUILDTOOL_PKGBUILD_NCONFIG } " " ${ BUILDTOOL_PKGBUILD_CPFINALCONFIG } " " ${ BUILDTOOL_PKGBUILD_MODPROBEDDB } " " ${ BUILDTOOL_PKGBUILD_SUBARCHITECTURE } " " ${ BUILDTOOL_PKGBUILD_DEBUG } " makepkg --syncdeps${ BUILDTOOL_MAKEPKG_REUSE } -p PKGBUILD.buildtool; then
2024-03-10 21:58:45 +01:00
errorh "Failed package compilation: makepkg returned with non-zero exit code"
errore " Please report this at \" ${ BUILDTOOL_ISSUETRACKER } \" "
exit 1
fi
# Finish message
infoh "Finished kernel compilation successfully"
infoe "You can now install your kernel and kernel headers."