jstm-optimized/buildtool.sh

436 lines
17 KiB
Bash
Raw Permalink Normal View History

2024-03-10 16:04:07 +01:00
#!/bin/bash
set -eo pipefail
2024-03-11 01:50:25 +01:00
# Set language to C.UTF-8 (for easier debugging)
export "LANG=C.UTF-8"
export "LC_MESSAGES=${LANG}"
2024-03-10 16:04:07 +01:00
# Variables
## Repository URLs (update these if you fork this project)
export "BUILDTOOL_REPOSITORY=https://git.staropensource.de/JeremyStarTM/jstm-optimized.git"
export "BUILDTOOL_ISSUETRACKER=https://git.staropensource.de/JeremyStarTM/jstm-optimized/issues"
2024-03-10 21:58:24 +01:00
export "BUILDTOOL_CLONEDIR=jstm-kernel-optimized"
## Packages to install. This list only includes the bare minimum for kernel compilation, options may add packages to this list
2024-03-10 16:04:07 +01:00
export "BUILDTOOL_PACKAGES=base-devel git rustup"
## Command line flags passed to makepkg
export "BUILDTOOL_BUILDCMDLINE=nice -20 env MAKEFLAGS=-j$(nproc)"
## OOOOoooo colors!
2024-03-10 21:58:24 +01:00
export "BUILDTOOL_COLOR_SPECIAL=\e[0;35m"
2024-03-11 01:51:29 +01:00
export "BUILDTOOL_COLOR_DIAG=\e[0;36m"
2024-03-10 21:58:24 +01:00
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 ""
2024-03-10 21:58:24 +01:00
}
## Special
function special() {
echo -e "${BUILDTOOL_COLOR_SPECIAL}${*}${BUILDTOOL_COLOR_RESET}"
2024-03-10 21:58:24 +01:00
}
2024-03-11 01:51:29 +01:00
## Diagnostic (header)
function diagh() {
[[ -z "${BUILDTOOL_DEBUG}" ]] && return
echo -e "${BUILDTOOL_COLOR_DIAG}[DIAG] ${*}${BUILDTOOL_COLOR_RESET}"
2024-03-11 01:51:29 +01:00
}
## Diagnostic (extension)
function diage() {
[[ -z "${BUILDTOOL_DEBUG}" ]] && return
echo -e "${BUILDTOOL_COLOR_DIAG} ${*}${BUILDTOOL_COLOR_RESET}"
2024-03-11 01:51:29 +01:00
}
2024-03-10 21:58:24 +01:00
## Informational (header)
function infoh() {
echo -e "${BUILDTOOL_COLOR_RESET}[INFO] ${*}"
2024-03-10 21:58:24 +01:00
}
## Informational (extension)
function infoe() {
echo -e "${BUILDTOOL_COLOR_RESET} ${*}"
2024-03-10 21:58:24 +01:00
}
## Warning (header)
function warnh() {
echo -e "${BUILDTOOL_COLOR_WARN}[WARN] ${*}${BUILDTOOL_COLOR_RESET}"
2024-03-10 21:58:24 +01:00
}
## Warning (extension)
function warne() {
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}[ERR!] ${*}${BUILDTOOL_COLOR_RESET}"
2024-03-10 21:58:24 +01:00
}
## Error (extension)
function errore() {
echo -e "${BUILDTOOL_COLOR_ERROR} ${*}${BUILDTOOL_COLOR_RESET}"
2024-03-10 21:58:24 +01:00
}
# Banner
special " _ _ _ _ _ _"
special " (_)___| |_ _ __ ___ ___ _ __ | |_(_)_ __ ___ (_)_______ __| |"
special " | / __| __| '_ \` _ \\ _____ / _ \\| '_ \\| __| | '_ \` _ \\| |_ / _ \\/ _\` |"
special " | \\__ \\ |_| | | | | |_____| (_) | |_) | |_| | | | | | | |/ / __/ (_| |"
special " _/ |___/\\__|_| |_| |_| \\___/| .__/ \\__|_|_| |_| |_|_/___\\___|\\__,_|"
2024-08-14 18:23:07 +02:00
special "|__/ |_|"
2024-03-10 21:58:24 +01:00
empty
2024-03-10 16:04:07 +01:00
2024-03-11 01:51:29 +01:00
# Print environment configuration
[[ -n "${BUILDTOOL_DEBUG}" ]] && warnh "\$BUILDTOOL_DEBUG is set, debug mode is enabled"
[[ -n "${BUILDTOOL_LOCALDIR}" ]] && warnh "\$BUILDTOOL_LOCALDIR is set, will build kernel package from current working directory"
2024-03-10 22:26:57 +01:00
2024-03-10 16:04:07 +01:00
# Checks
## Check for Arch Linux
(
# shellcheck disable=SC1091
source "/etc/os-release"
if [ "${NAME}" != "Arch Linux" ] || [ "${PRETTY_NAME}" != "Arch Linux" ] || [ "${ID}" != "arch" ]; then
if [ -z "${BUILDTOOL_COMPILE_NONARCH}" ]; then
errorh "Not running on Arch Linux"
errore "This build tool can only be executed on Arch Linux"
errore "If you are sure that you want to run the build tool on a non-Arch distro,"
errore "set \$BUILDTOOL_COMPILE_NONARCH."
exit 1
else
warnh "\$BUILDTOOL_COMPILE_NONARCH is set, allowing compiling on a non-arch distribution"
fi
fi
2024-03-10 16:04:07 +01:00
)
## Check for root
if [ "${UID}" == "0" ]; then
errorh "Can't build kernel as root"
errore "Please create a new user and then run the build tool as the new user"
exit 1
2024-03-10 16:04:07 +01:00
fi
## Check for sudo
if ! which sudo &> /dev/null; then
errorh "Could not find sudo in \$PATH"
errore "Please ensure that you have sudo installed in \$PATH"
exit 1
2024-03-10 16:04:07 +01:00
fi
# Questions
## Ask if mold should replace ld/lld
function ask_mold() {
read -rp "$(infoh "Do you want to use mold as your linker (much faster) [Y/n]? ")" BUILDTOOL_MOLD
case "${BUILDTOOL_MOLD}" in
"y"|"Y"|"")
export "BUILDTOOL_PACKAGES=${BUILDTOOL_PACKAGES} mold"
export "BUILDTOOL_BUILDCMDLINE=${BUILDTOOL_BUILDCMDLINE} LDFLAGS=-fuse-ld=mold RUSTFLAGS=\"-C link-arg=-fuse-ld=mold\""
;;
"n"|"N") ;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_mold
esac
2024-03-10 16:04:07 +01:00
}
## Ask if the governor should be adjusted
function ask_cpupower() {
read -rp "$(infoh "Do you want to adjust the cpu governor (improves build performance) [Y/n]? ")" BUILDTOOL_CPUPOWER
case "${BUILDTOOL_CPUPOWER}" in
"y"|"Y"|"")
export "BUILDTOOL_PACKAGES=${BUILDTOOL_PACKAGES} cpupower"
;;
"n"|"N") ;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_cpupower
esac
2024-03-10 16:04:07 +01:00
}
## Ask for building in tmpfs
function ask_tmpfs() {
if [ -n "${BUILDTOOL_LOCALDIR}" ]; then
warnh "Skipped ask_tmpfs, building in local directory instead (\$BUILDTOOL_LOCALDIR is set)"
export "BUILDTOOL_CLONEDIR=$(pwd)"
return
fi
read -rp "$(infoh "Do you want to build in a tmpfs (improves build performance, protects your disk from wearing out faster, ~8 GiB free memory required) [Y/n]? ")" BUILDTOOL_TMPFS
case "${BUILDTOOL_TMPFS}" in
"y"|"Y"|"")
export "BUILDTOOL_CLONEDIR=/tmp/${BUILDTOOL_CLONEDIR}"
;;
"n"|"N")
export "BUILDTOOL_CLONEDIR=$(pwd)/${BUILDTOOL_CLONEDIR}"
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_tmpfs
esac
2024-03-10 16:04:07 +01:00
}
2024-03-22 15:34:41 +01:00
## Ask for kernel config modification using menuconfig
function ask_menuconfig() {
read -rp "$(infoh "Do you want to configure the kernel using menuconfig before build [y/N]? ")" BUILDTOOL_MENUCONFIG
case "${BUILDTOOL_MENUCONFIG}" in
"y"|"Y")
export "BUILDTOOL_PKGBUILD_MENUCONFIG=_makemenuconfig=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_MENUCONFIG=_makemenuconfig="
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_menuconfig
esac
2024-03-22 15:34:41 +01:00
}
2024-03-10 16:04:07 +01:00
## Ask for kernel config modification using xconfig
function ask_xconfig() {
read -rp "$(infoh "Do you want to configure the kernel using xconfig before build [y/N]? ")" BUILDTOOL_XCONFIG
case "${BUILDTOOL_XCONFIG}" in
"y"|"Y")
export "BUILDTOOL_PKGBUILD_XCONFIG=_makexconfig=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_XCONFIG=_makexconfig="
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_xconfig
esac
2024-03-10 16:04:07 +01:00
}
## Ask for kernel config modification using xconfig
function ask_nconfig() {
read -rp "$(infoh "Do you want to configure the kernel using nconfig before build [y/N]? ")" BUILDTOOL_NCONFIG
case "${BUILDTOOL_NCONFIG}" in
"y"|"Y")
export "BUILDTOOL_PKGBUILD_NCONFIG=_makenconfig=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_NCONFIG=_makenconfig="
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_nconfig
esac
2024-03-10 16:04:07 +01:00
}
## Ask if the final kernel configuration should be copied
function ask_cpfinalconfig() {
read -rp "$(infoh "Do you want to copy the final kernel configuration before build [y/N]? ")" BUILDTOOL_CPFINALCONFIG
case "${BUILDTOOL_CPFINALCONFIG}" in
"y"|"Y")
export "BUILDTOOL_PKGBUILD_CPFINALCONFIG=_copyfinalconfig=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_CPFINALCONFIG=_copyfinalconfig="
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_cpfinalconfig
esac
2024-03-10 16:04:07 +01:00
}
## Ask if only active modules should be compiled into the kernel
function ask_modprobeddb() {
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
case "${BUILDTOOL_MODPROBEDDB}" in
"y"|"Y")
if ! which modprobed-db &> /dev/null; then
errorh "Could not find modprobed-db in \$PATH."
errore "Please ensure that you have modprobed-db installed."
ask_modprobeddb
return 1
fi
export "BUILDTOOL_PACKAGES=${BUILDTOOL_PACKAGES} modprobed-db"
export "BUILDTOOL_PKGBUILD_MODPROBEDDB=_localmodcfg=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_MODPROBEDDB=_localmodcfg="
;;
*)
errorh "Invalid answer. Please answer with Y or N"
ask_modprobeddb
esac
2024-03-10 16:04:07 +01:00
}
## 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)"
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]*)
errorh "Invalid subarchitecture"
ask_subarchitecture
return 1
;;
*)
if [ ! "${BUILDTOOL_SUBARCHITECTURE}" -gt 0 ] || [ ! "${BUILDTOOL_SUBARCHITECTURE}" -lt 46 ]; then
errorh "Invalid subarchitecture"
ask_subarchitecture
else
export "BUILDTOOL_PKGBUILD_SUBARCHITECTURE=_subarch=${BUILDTOOL_SUBARCHITECTURE}"
fi
;;
esac
2024-03-10 16:04:07 +01:00
}
## Ask for kernel debug mode
function ask_debug() {
read -rp "$(infoh "Do you want to enable kernel debug mode [y/N/i(gnore)]? ")" BUILDTOOL_KERNELDEBUG
case "${BUILDTOOL_KERNELDEBUG}" in
"y"|"Y")
export "BUILDTOOL_PKGBUILD_DEBUG=_debug=y"
;;
"n"|"N"|"")
export "BUILDTOOL_PKGBUILD_DEBUG=_debug=n"
;;
"i"|"I")
export "BUILDTOOL_PKGBUILD_DEBUG=_debug="
;;
*)
errorh "Invalid answer. Please answer with Y, N or I"
ask_debug
esac
2024-03-10 16:04:07 +01:00
}
2024-03-10 21:26:37 +01:00
## Ask for clone directory conflict resolution (includes check)
function ask_clonedir_conflictresolution() {
if [ -n "${BUILDTOOL_LOCALDIR}" ]; then
warnh "Skipped conflict resolution (\$BUILDTOOL_LOCALDIR is set)"
export "BUILDTOOL_CLONEDIR_CONFLICT=r"
fi
[[ ! -d "${BUILDTOOL_CLONEDIR}" ]] && [[ ! -L "${BUILDTOOL_CLONEDIR}" ]] && return
[[ -a "${BUILDTOOL_CLONEDIR}" ]] && [[ ! -d "${BUILDTOOL_CLONEDIR}" ]] && [[ ! -L "${BUILDTOOL_CLONEDIR}" ]] && warnh "Something that isn't a directory or symlink exists at location \"${BUILDTOOL_CLONEDIR}\""
2024-08-31 21:46:10 +02:00
[[ -z "${BUILDTOOL_CLONEDIR_CONFLICT}" ]] && 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
case "${BUILDTOOL_CLONEDIR_CONFLICT}" in
"a"|"A"|"")
errorh "Conflict resolution failed"
exit 1
;;
"r"|"R")
2024-08-31 21:46:10 +02:00
if [ -a "${BUILDTOOL_CLONEDIR}/src" ] || [ -a "/tmp/makepkg/linux-jstm-optimized/src" ]; then
diagh "src/ directory present, appending \"--noextract\""
export "BUILDTOOL_MAKEPKG_REUSE= --noextract"
2024-08-31 21:46:10 +02:00
elif [ -a "${BUILDTOOL_CLONEDIR}/pkg" ] || [ -a "/tmp/makepkg/linux-jstm-optimized/pkg" ]; then
diagh "pkg/ directory present, appending \"--force\""
export "BUILDTOOL_MAKEPKG_REUSE=${BUILDTOOL_MAKEPKG_REUSE} --force"
fi
;;
"f"|"F")
infoh "Removing existing \$BUILDTOOL_CLONEDIR directory"
2024-08-31 21:46:10 +02:00
rm -rf "${BUILDTOOL_CLONEDIR}" "/tmp/makepkg/linux-jstm-optimized"
;;
*)
2024-08-31 21:46:10 +02:00
errorh "Invalid answer. Please answer with A, R or F"
ask_debug
esac
2024-03-10 21:26:37 +01:00
}
2024-03-10 16:04:07 +01:00
ask_mold
ask_cpupower
ask_tmpfs
2024-03-22 15:40:42 +01:00
ask_menuconfig
2024-03-10 16:04:07 +01:00
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-11 00:34:20 +01:00
# shellcheck disable=SC2086
2024-03-10 16:04:07 +01:00
if ! sudo pacman -Syu --asdeps --needed ${BUILDTOOL_PACKAGES}; then
errorh "Installing dependencies failed: pacman returned with non-zero exit code"
exit 1
2024-03-10 16:04:07 +01:00
fi
if ! rustup default nightly; then
errorh "Installing dependencies failed: rustup returned with non-zero exit code"
exit 1
2024-03-10 16:04:07 +01:00
fi
## Adjust cpu governor
case "${BUILDTOOL_CPUPOWER}" in
"y"|"Y"|"")
infoh "Adjusting cpu governor"
if ! sudo cpupower frequency-set -g performance; then
errorh "Adjusting cpu governor failed: cpupower returned with non-zero exit code"
return 1
fi
;;
"n"|"N") ;;
*)
errorh "Internal inconsistency detected: Value of \$BUILDTOOL_CPUPOWER is not a valid boolean answer"
exit 2
;;
2024-03-10 16:04:07 +01:00
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"
2024-03-10 22:19:13 +01:00
else
infoh "Cloning repository"
git clone "${BUILDTOOL_REPOSITORY}" "${BUILDTOOL_CLONEDIR}"
2024-03-10 22:19:13 +01:00
fi
2024-03-10 16:04:07 +01:00
cd "${BUILDTOOL_CLONEDIR}"
git submodule update --init --recursive
cd kernel
# Patch PKGBUILD
sed "s/pkgbase\=linux\-clear/pkgbase\=linux\-jstm\-optimized/g" -i PKGBUILD
2024-03-10 16:04:07 +01:00
# Print debug information
2024-03-10 22:26:57 +01:00
if [ -n "${BUILDTOOL_DEBUG}" ]; then
diagh "Printing debug information"
diage "Environment variables:"
env|grep "BUILDTOOL_"|sort
diagh "More debug information:"
diage "Build cmdline=${BUILDTOOL_BUILDCMDLINE} ${BUILDTOOL_PKGBUILD_MENUCONFIG} ${BUILDTOOL_PKGBUILD_XCONFIG} ${BUILDTOOL_PKGBUILD_NCONFIG} ${BUILDTOOL_PKGBUILD_CPFINALCONFIG} ${BUILDTOOL_PKGBUILD_MODPROBEDDB} ${BUILDTOOL_PKGBUILD_SUBARCHITECTURE} ${BUILDTOOL_PKGBUILD_DEBUG} makepkg --syncdeps -p PKGBUILD"
diage "cwd=$(pwd)"
fi
2024-03-10 21:58:24 +01:00
# Build package
infoh "Building package"
2024-03-11 00:34:20 +01:00
# shellcheck disable=SC2086
if ! ${BUILDTOOL_BUILDCMDLINE} "${BUILDTOOL_PKGBUILD_MENUCONFIG}" "${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; then
errorh "Failed package compilation: makepkg returned with non-zero exit code"
errore "Please report this at \"${BUILDTOOL_ISSUETRACKER}\""
exit 1
2024-03-10 21:58:45 +01:00
fi
# Finish message
infoh "Finished kernel compilation successfully"
2024-03-22 14:43:15 +01:00
infoe "You can now install your kernel and kernel headers using this command:"
infoe "pacman -U \"${BUILDTOOL_CLONEDIR}/kernel/\"linux-jstm-optimized-*linux*-x86_64.pkg.tar.zst"