-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·443 lines (387 loc) · 13.6 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
# This script allows users to simplify the Kernel
# configuration, compilation & installation process.
# https://github.com/saloniamatteo/kernel
# ======================================================
# Directory containing the provided assets
# Change this to reflect your preferences
# Default is /usr/src/usr-kernel
CUSTDIR="/usr/src/usr-kernel"
# Kernel versions
# NOTE: $KVER MUST be of the form x.y.z, otherwise things WILL break!
KVER="6.14.0" # Primary Kernel version
PVER="-gentoo" # Kernel "patch" version
# Set this variable if your Kernel is not located under /usr/src/
# Default is empty
#KERNELDIR=""
# How many threads to use to build Kernel
# T470p = -j6
# T440p = -j4
# PC = -j9
# You can set any value you like.
JOBS="-j6"
# Name of the Kernel config file under $CUSTDIR/$KERNVER
# Choose between "config", "config.t440p", "config.pc"
# You can provide your own config file by placing it under $CUSTDIR/$KERNVER.
CONFIGFILE="config"
# ======================================================
# Do not modify these unless you know what you're doing.
# Full Kernel version
KERNVER="${KVER}${PVER}"
# Location of Clear Linux patch directory
CLEARDIR="$CUSTDIR/clear-patches"
# Location of included patches
PATCHDIR="$CUSTDIR/patches"
# Location of BORE sched patch
BOREDIR="$CUSTDIR/bore-scheduler"
# Location of v4l2loopback directory
V4L2DIR="$CUSTDIR/v4l2loopback"
# Location of CPU family optimizations directory
CFODIR="$CUSTDIR/kernel_compiler_patch"
# Location of Kernel-specific user directory
USRDIR="$CUSTDIR/$KERNVER"
# Check if KERNELDIR is set
if [ -z ${KERNELDIR} ]; then
# Try to set kernel directory to /usr/src/linux-$KERNVER;
# If it does not exist, try /usr/src/linux
if [ -d "/usr/src/linux-$KERNVER" ]; then
KERNELDIR="/usr/src/linux-$KERNVER"
else if [ -d "/usr/src/linux" ]; then
KERNELDIR="/usr/src/linux"
else
echo "Could not find Kernel directory."
exit
fi
fi
else if [ ! -d $KERNELDIR ]; then
echo "KERNELDIR $KERNELDIR is invalid."
exit
fi
fi
# Check if user directory exists
if [ ! -d "$USRDIR" ]; then
echo "Could not find Custom Kernel directory."
exit
fi
# Check that required files exist in user directory
# Note: we don't check if patches exist because
# we can apply them optionally
if [ ! -f "$USRDIR/config" ]; then
echo "Config is missing from Custom Kernel directory."
exit
fi
# Set variables based on flag status
F_SKIP_BUILD=0 # Do not build the kernel
F_SKIP_CFG=0 # Do not copy the Kernel config to Kernel directory
F_DISTCC=0 # Use distcc to speed up compilation
F_CCACHE=0 # Use ccache to speed up compilation
F_FASTMATH=0 # Build Kernel with Unsafe Fast Math
F_GRAPHITE=0 # Build Kernel with Graphite
F_PRINT_HELP=0 # Print help and exit
F_CLEARLINUX_PATCHES=0 # Enable Clear Linux patches
F_MENUCONFIG=0 # Run 'make menuconfig' in Kernel directory and exit
F_CPU_OPTS=0 # Build Kernel with CPU family optimisations
F_PATCHES=0 # Apply provided patches (recommended)
F_BORE=0 # Build Kernel with BORE scheduler
F_V4L2=0 # Build v4l2loopback Kernel module
F_PRINT_VARS=0 # Print flags and exit
F_PRINT_FLAGS=0 # Print variables and exit
[[ $@ =~ "-b" || $@ =~ "--skip-build" ]] && F_SKIP_BUILD=1
[[ $@ =~ "-c" || $@ =~ "--skip-cfg" ]] && F_SKIP_CFG=1
[[ $@ =~ "-d" || $@ =~ "--distcc" ]] && F_DISTCC=1
[[ $@ =~ "-e" || $@ =~ "--ccache" ]] && F_CCACHE=1
[[ $@ =~ "-f" || $@ =~ "--fastmath" ]] && F_FASTMATH=1
[[ $@ =~ "-g" || $@ =~ "--graphite" ]] && F_GRAPHITE=1
[[ $@ =~ "-h" || $@ =~ "--help" ]] && F_PRINT_HELP=1
[[ $@ =~ "-l" || $@ =~ "--clearl-ps" ]] && F_CLEARLINUX_PATCHES=1
[[ $@ =~ "-m" || $@ =~ "--menuconfig" ]] && F_MENUCONFIG=1
[[ $@ =~ "-o" || $@ =~ "--cpu-opts" ]] && F_CPU_OPTS=1
[[ $@ =~ "-p" || $@ =~ "--patches" ]] && F_PATCHES=1
[[ $@ =~ "-r" || $@ =~ "--bore" ]] && F_BORE=1
[[ $@ =~ "-v" || $@ =~ "--v4l2" ]] && F_V4L2=1
[[ $@ =~ "-y" || $@ =~ "--flags" ]] && F_PRINT_FLAGS=1
[[ $@ =~ "-z" || $@ =~ "--vars" ]] && F_PRINT_VARS=1
# Flag presets
if [[ $@ =~ "--preset-configure" ]]; then
F_SKIP_CFG=0 # Necessary as "-c" is detected (override)
F_CLEARLINUX_PATCHES=1
F_MENUCONFIG=1
F_CPU_OPTS=1
F_PATCHES=1
F_BORE=1
else if [[ $@ =~ "--preset-build" ]]; then
F_SKIP_BUILD=0 # Necessary as "-b" is detected (override)
F_FASTMATH=1
F_GRAPHITE=1
F_CLEARLINUX_PATCHES=1
F_CPU_OPTS=1
F_PATCHES=1
F_BORE=1
fi
fi
# Print help
if [ $F_PRINT_HELP = 1 ]; then
printf "Flags:
-b,--skip-build Do not build the Kernel
-c,--skip-cfg Do not copy the Kernel config to Kernel directory
-d,--distcc Use distcc to speed up compilation /!\\
-e,--ccache Use ccache to speed up compilation /!\\
-f,--fastmath Build Kernel with Unsafe Fast Math [*]
-g,--graphite Build Kernel with Graphite [*]
-h,--help Print this help and exit
-l,--clearl-ps Enable Clear Linux patches [*]
-m,--menuconfig Run 'make menuconfig' in Kernel directory and exit
-o,--cpu-opts Build Kernel with CPU family optimisations [*]
-p,--patches Apply provided patches (recommended) [*]
-r,--bore Build Kernel with BORE scheduler [*]
-v,--v4l2 Build v4l2loopback Kernel module
-y,--flags Print flags and exit
-z,--vars Print variables and exit
Presets (mutually exclusive):
--preset-configure Selects the following flags:
-f, -g, -l, -o, -p, -r
--preset-build Selects the following flags:
-l, -m, -o, -p, -r
Note:
- All options marked with '[*]', when enabled, may improve
Kernel performance, whether it may be CPU, I/O, network, etc.
- It is highly recommended to enable Clear Linux patches,
CPU family optimizations, provided patches, as well as the BORE scheduler.
Unsafe fast math may have a negligible performance increase,
just like Graphite, depending on your system. Use at your own risk!
- If you're planning on using v4l2loopback with your own config,
please read: https://github.com/umlaeute/v4l2loopback/discussions/604
Warning /!\\:
- Distcc is recommended only if it is properly set up,
and when you have powerful enough hosts.
- CCache is recommended only if it is properly set up,
and only when recompiling the same Kernel multiple times.
Gentoo users:
It is highly recommended you use 'sys-kernel/installkernel' to automatically
create the initramfs & update the bootloader config, by respecting your
system preferences (e.g. initramfs->dracut, bootloader->GRUB).
https://packages.gentoo.org/packages/sys-kernel/installkernel
"
exit
fi
# Print flags
if [ $F_PRINT_FLAGS = 1 ]; then
printf "Flags:
F_SKIP_BUILD=$F_SKIP_BUILD
F_SKIP_CFG=$F_SKIP_CFG
F_DISTCC=$F_DISTCC
F_CCACHE=$F_CCACHE
F_FASTMATH=$F_FASTMATH
F_GRAPHITE=$F_GRAPHITE
F_PRINT_HELP=$F_PRINT_HELP
F_CLEARLINUX_PATCHES=$F_CLEARLINUX_PATCHES
F_MENUCONFIG=$F_MENUCONFIG
F_CPU_OPTS=$F_CPU_OPTS
F_PATCHES=$F_PATCHES
F_BORE=$F_BORE
F_V4L2=$F_V4L2
F_PRINT_VARS=$F_PRINT_VARS
F_PRINT_FLAGS=$F_PRINT_FLAGS
"
exit
fi
# Print variables
if [ $F_PRINT_VARS = 1 ]; then
printf "Variables:
CONFIGFILE=$CONFIGFILE
JOBS=$JOBS
KVER=$KVER
PVER=$PVER
KERNVER=$KERNVER
CUSTDIR=$CUSTDIR
CLEARDIR=$CLEARDIR
PATCHDIR=$PATCHDIR
BOREDIR=$BOREDIR
V4L2DIR=$V4L2DIR
USRDIR=$USRDIR
KERNELDIR=$KERNELDIR
"
exit
fi
# Make sure we're in the right directory
cd "$KERNELDIR"
# Revert and remove any patches first
echo "Reverting and removing pre-existing patches (if any)" &&
for p in *.patch; do
echo "Reverting patch $p..."
patch -Rfsp1 -i $p
done
rm *.patch
# Copy Kernel config
if [ $F_SKIP_CFG = 0 ]; then
echo "Copying config" &&
cp "$USRDIR/$CONFIGFILE" "$KERNELDIR/config" &&
cp config .config ||
exit
else
echo "Skipping copying config.."
fi
# Copy Clear Linux patches
if [ $F_CLEARLINUX_PATCHES = 1 ]; then
CLEAR_PATCHES=(
"0002-sched-core-add-some-branch-hints-based-on-gcov-analy.patch"
"0102-increase-the-ext4-default-commit-age.patch"
"0104-pci-pme-wakeups.patch"
"0106-intel_idle-tweak-cpuidle-cstates.patch"
"0108-smpboot-reuse-timer-calibration.patch"
"0111-ipv4-tcp-allow-the-memory-tuning-for-tcp-to-go-a-lit.patch"
"0120-do-accept-in-LIFO-order-for-cache-efficiency.patch"
"0121-locking-rwsem-spin-faster.patch"
"0122-ata-libahci-ignore-staggered-spin-up.patch"
"0131-add-a-per-cpu-minimum-high-watermark-an-tune-batch-s.patch"
"0135-initcall-only-print-non-zero-initcall-debug-to-speed.patch"
"0136-crypto-kdf-make-the-module-init-call-a-late-init-cal.patch"
"0158-clocksource-only-perform-extended-clocksource-checks.patch"
"0161-ACPI-align-slab-buffers-for-improved-memory-performa.patch"
)
echo "Copying Clear Linux patches"
for patch in ${CLEAR_PATCHES[@]}; do
echo "Copying $patch"
cp "$CLEARDIR/$patch" "$KERNELDIR" || exit
done
fi
# Copy CPU family opts patches
if [ $F_CPU_OPTS = 1 ]; then
# Check if CPU family optimizations directory exists
if [ ! -d "$CFODIR" ]; then
echo "Could not find CPU family optimisations directory."
exit
fi
echo "Copying CPU family optimisation patches"
cp "$CFODIR/more-ISA-levels-and-uarches-for-kernel-6.1.79+.patch" "$KERNELDIR" || exit
fi
# Copy provided patches
if [ $F_PATCHES = 1 ]; then
echo "Copying provided patches"
cp $PATCHDIR/*.patch $KERNELDIR || exit
fi
# Copy BORE sched patch
if [ $F_BORE = 1 ]; then
# Extract the major version
# Example: KVER: 6.11.7, KVER_MAJ: 6.11
KVER_MAJ="${KVER%.*}"
echo "Copying BORE patch"
cp $BOREDIR/patches/stable/linux-$KVER_MAJ-bore/*patch "$KERNELDIR" || exit
fi
echo "Applying Clear Linux and/or user patches (if any)"
for p in *.patch; do
echo "Applying patch '$p'..." && patch -Np1 -i $p
done
#echo "Setting version..." &&
#scripts/setlocalversion --save-scmversion ||
# make olddefconfig
echo "Running make olddefconfig" && make $JOBS olddefconfig || exit
# make oldconfig & make prepare
echo "Running make oldconfig && make prepare" && make $JOBS oldconfig && make $JOBS prepare || exit
# Backup existing Kernel .config
echo "Copying existing Kernel config to config.last" && cp .config config.last || exit
# make clean
echo "Running make clean" && make $JOBS clean || exit
# make menuconfig
if [ $F_MENUCONFIG = 1 ]; then
make $JOBS menuconfig
exit
fi
# Skip Kernel build?
if [ $F_SKIP_BUILD = 0 ]; then
# Don't use build timestap (slows down cache)
export KBUILD_BUILD_TIMESTAMP=""
# Check if we can use ccache
if [ $F_CCACHE = 1 ]; then
echo "Using ccache..."
cc="ccache gcc"
else
cc="gcc"
fi
# Check if we can use distcc
if [ $F_DISTCC = 1 ]; then
echo "Using distcc..."
cc="distcc $cc"
fi
# Specify which compiler we're using
echo "Compiler command (CC): $cc"
# Check if Unsafe Fast Math is enabled
if [ $F_FASTMATH = 1 ]; then
MATH="-fno-signed-zeros -fno-trapping-math -fassociative-math -freciprocal-math -fno-math-errno -ffinite-math-only -fno-rounding-math -fno-signaling-nans -fcx-limited-range -fexcess-precision=fast"
else
MATH=""
fi
# Check if Graphite is enabled
if [ $F_GRAPHITE = 1 ]; then
GRAPHITE="-fgraphite-identity -floop-nest-optimize"
else
GRAPHITE=""
fi
# Extra optimimization flags:
# -fivopts:
# Perform induction variable optimizations (strength reduction,
# induction variable merging and induction variable elimination) on trees.
# -fmodulo-sched:
# Perform swing modulo scheduling immediately before the first
# scheduling pass. This pass looks at innermost loops and reorders
# their instructions by overlapping different iterations.
# -floop-interchange:
# Perform loop interchange outside of graphite.
# This flag can improve cache performance on loop nest,
# and allow further loop optimizations, like vectorization,
# to take place.
OPTS="-fivopts -fmodulo-sched -floop-interchange"
# Kernel build timer
build_start=$(date "+%s")
echo "Started build at $(date --date=@$build_start)"
echo "Building Kernel Version $(make kernelrelease)"
# Build Kernel
make CC="$cc" KCFLAGS="$KCFLAGS $MATH $GRAPHITE $OPTS" $JOBS || exit
make CC="$cc" $JOBS modules_prepare || exit
# Kernel build timer
build_end=$(date "+%s")
build_diff=$(expr $build_end - $build_start)
echo "Finished Kernel build at $(date --date=@$build_end)."
echo "Took $(date -d@$build_diff -u +%H:%M:%S)."
# Modules + Kernel install timer
install_start=$(date "+%s")
echo "Started Modules + Kernel install at $(date --date=@$install_start)"
# Install modules + Kernel
# NOTE: it is highly recommended you use "installkernel".
# https://packages.gentoo.org/packages/sys-kernel/installkernel
make CC="$cc" $JOBS modules_install || exit
make CC="$cc" $JOBS install || exit
# Modules + Kernel install timer stop
install_end=$(date "+%s")
install_diff=$(expr $install_end - $install_start)
build_total=$(expr $build_diff + $install_diff)
echo "Finished modules + Kernel install at $(date --date=@$install_end)."
echo "Took $(date -d@$install_diff -u +%H:%M:%S)."
echo "Total time (Kernel build + install): $(date -d@$build_total -u +%H:%M:%S)."
# V4L2loopback
if [ $F_V4L2 = 1 ]; then
# Check if V4L2DIR exists
if [ ! -d "$V4L2DIR" ]; then
echo "Could not find v4l2loopback directory.";
exit
fi
# V4L2loopback timer
build_v4l2_start=$(date "+%s")
echo "Started v4l2loopback build at $(date --date=@$build_v4l2_start)"
# Compile V4L2loopback
cd $V4L2DIR
make $JOBS KERNELRELEASE=$KERNVER || exit
make KERNELRELEASE=$KERNVER install || exit
# Reload module dependencies
depmod -a
# V4L2loopback timer
build_v4l2_end=$(date "+%s")
build_v4l2_diff=$(expr $build_v4l2_end - $build_v4l2_start)
build_total=$(expr $build_v4l2_diff + $build_diff + $install_diff)
echo "Finished v4l2loopback build at $(date --date=@$build_v4l2_end)."
echo "Took $(date -d@$build_v4l2_diff -u +%H:%M:%S)."
echo "Total time (Kernel build + install + v4l2loopback): $(date -d@$build_total -u +%H:%M:%S)."
fi
else
echo "Skipping building Kernel. Exiting..."
fi