forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·130 lines (115 loc) · 3.25 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /bin/bash
#
# Kernel compile script for WSL2 kernel
# Copyright (C) 2023-2024 InFeRnO.
# Setup environment
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
clear='\033[0m'
KERNEL_PATH=$PWD
ARCH=x86
DEFCONFIG=custom_defconfig
LLVM_DIR=.clang
LLVM_URL="https://cdn.kernel.org/pub/tools/llvm/files/llvm-19.1.0-x86_64.tar.gz"
LLVM_VER="llvm-19.1.0-x86_64"
export PATH=$PWD/.clang/$LLVM_VER/bin:$PATH
BUILD_CC="LLVM=1"
clone_clang() {
mkdir $LLVM_DIR
wget -O llvm.tar.gz $LLVM_URL
tar -xzf llvm.tar.gz -C $LLVM_DIR
}
ArchLinux() {
# Check if yay is installed
if ! command -v yay &> /dev/null
then
echo -e "${red}yay is not installed, please install it first!${clear}"
exit
else
yay -S lineageos-devel aosp-devel zstd tar wget curl base-devel lib32-ncurses lib32-zlib lib32-readline cpio flex bison pahole-git dwarves wget --noconfirm
fi
}
Ubuntu() {
sudo apt install build-essential bc flex bison dwarves libssl-dev libelf-dev cpio wget -y
}
regenerate_defconfig() {
cd $KERNEL_PATH
make $BUILD_CC O=out ARCH=$ARCH $DEFCONFIG savedefconfig
cp out/.config arch/$ARCH/configs/$DEFCONFIG
}
build_kernel() {
cd $KERNEL_PATH
make $BUILD_CC O=out ARCH=$ARCH $DEFCONFIG savedefconfig
# Begin compilation
start=$(date +%s)
make $BUILD_CC O=out ARCH=$ARCH -j`nproc` ${BUILD_CC} 2>&1 | tee error.log
if [ -f $KERNEL_PATH/out/arch/$ARCH/boot/bzImage ]; then
make_zip
echo -e "${green}Kernel Compilation successful.${clear}"
else
echo -e "${red}Compilation failed!${clear}"
echo -e "${red}Check error.log for more info!${clear}"
exit
fi
}
distro_check(){
if [ -f /etc/arch-release ]; then
echo -e "${green}Arch Linux detected!${clear}"
ArchLinux
elif [ -f /etc/lsb-release ]; then
echo -e "${green}Debian based distro detected!${clear}"
Ubuntu
else
echo -e "${red}Unsupported OS or ARCH!${clear}"
exit
fi
}
make_zip(){
zip_name="WSL2-Linux-v5.15.$(grep "^SUBLEVEL =" Makefile | awk '{print $3}')-$(date +"%Y%m%d-%H%M").zip"
zip out/$zip_name $KERNEL_PATH/out/arch/$ARCH/boot/bzImage
echo -e "${green}out: ${KERNEL_PATH}/out/${zip_name}${clear}"
echo -e "${clear}"
echo -e "${green}Completed in $(($(date +%s) - start)) seconds.${clear}"
}
start=$(date +%s)
# Parse Args
while [[ $# -gt 0 ]]; do
case "$1" in
--regenerate) # To regenerate defconfig
REGENERATE_DEFCONFIG=true
shift
;;
--clean) # To clean build kernel
CLEAN_BUILD=true
shift
;;
--setup) # install nessesary packages for kbuild
SETUP=true
shift
;;
*) # ¯_(ツ)_/¯
echo "$1: ¯_(ツ)_/¯"
exit 1
;;
esac
done
if [ "$REGENERATE_DEFCONFIG" = true ]; then
regenerate_defconfig
echo -e "${green}Defconfig regenerated successfully!${clear}"
exit
fi
if [ "$CLEAN_BUILD" = true ]; then
rm -rf out/
echo -e "${green}Entire out folder removed.${clear}"
fi
if [ "$SETUP" = true ]; then
distro_check
fi
if [ -d "$LLVM_DIR" ]; then
echo -e "${green}LLVM CLANG already exists. Skipping download.${clear}"
else
clone_clang
fi
build_kernel