This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
driver.sh
executable file
·453 lines (405 loc) · 14.5 KB
/
driver.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
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env bash
set -eu
setup_variables() {
while [[ ${#} -ge 1 ]]; do
case ${1} in
"AR="* | "ARCH="* | "CC="* | "LD="* | "LLVM="* | "LLVM_IAS="* | "NM"=* | "OBJCOPY"=* | "OBJDUMP"=* | "OBJSIZE"=* | "REPO="* | "STRIP"=*) export "${1?}" ;;
"-c" | "--clean") cleanup=true ;;
"-j" | "--jobs")
shift
jobs=$1
;;
"-j"*) jobs=${1/-j/} ;;
"--lto") disable_lto=false ;;
"-h" | "--help")
cat usage.txt
exit 0
;;
esac
shift
done
if [ "${ARCH:-0}" -eq "0" ]; then
cat usage.txt
exit 1
fi
# Turn on debug mode after parameters in case -h was specified
set -x
# torvalds/linux is the default repo if nothing is specified
case ${REPO:=linux} in
"android"*)
tree=common
branch=${REPO}
url=https://android.googlesource.com/kernel/${tree}
;;
"linux")
owner=torvalds
tree=linux
;;
"linux-next")
owner=next
tree=linux-next
;;
"4.4" | "4.9" | "4.14" | "4.19" | "5.4")
owner=stable
branch=linux-${REPO}.y
tree=linux
;;
esac
[[ -z "${url:-}" ]] && url=git://git.kernel.org/pub/scm/linux/kernel/git/${owner}/${tree}.git
SUBARCH=${ARCH}
case ${SUBARCH} in
"arm32_v5")
config=multi_v5_defconfig
make_target=zImage
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
;;
"arm32_v6")
config=aspeed_g5_defconfig
make_target=zImage
timeout=4 # This architecture needs a bit of a longer timeout due to some flakiness on Travis
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
;;
"arm32_v7")
config=multi_v7_defconfig
make_target=zImage
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
;;
"arm64")
case ${REPO} in
android-*)
case ${branch} in
*4.9-q | *4.14-stable) config=cuttlefish_defconfig ;;
*) config=gki_defconfig ;;
esac
;;
*) config=defconfig ;;
esac
make_target=Image.gz
timeout=8 # '-cpu max' with QEMU takes longer to boot, increase timeout to 8m for Travis
export CROSS_COMPILE=aarch64-linux-gnu-
;;
"mips")
config=malta_kvm_guest_defconfig
make_target=vmlinux
export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
;;
"mipsel")
config=malta_kvm_guest_defconfig
make_target=vmlinux
export ARCH=mips
export CROSS_COMPILE=mipsel-linux-gnu-
;;
"ppc32")
config=ppc44x_defconfig
make_target=uImage
export ARCH=powerpc
export CROSS_COMPILE=powerpc-linux-gnu-
;;
"ppc64")
config=pseries_defconfig
make_target=vmlinux
export ARCH=powerpc
export CROSS_COMPILE=powerpc64-linux-gnu-
;;
"ppc64le")
config=powernv_defconfig
make_target=zImage.epapr
export ARCH=powerpc
export CROSS_COMPILE=powerpc64le-linux-gnu-
;;
"riscv")
config=defconfig
make_target=Image
export CROSS_COMPILE=riscv64-linux-gnu-
;;
"s390")
BOOT=false
config=defconfig
make_target=bzImage
export CROSS_COMPILE=s390x-linux-gnu-
# llvm-objcopy: error: invalid output format: 'elf64-s390'
# https://github.com/llvm/llvm-project/blob/llvmorg-11-init/llvm/tools/llvm-objcopy/CopyConfig.cpp#L242-L275
OBJCOPY=${CROSS_COMPILE}objcopy
OBJDUMP=${CROSS_COMPILE}objdump
;;
"x86")
config=i386_defconfig
make_target=bzImage
export ARCH=i386
;;
"x86_64")
case ${REPO} in
android-*)
case ${branch} in
*4.9-q | *4.14-stable) config=x86_64_cuttlefish_defconfig ;;
*) config=gki_defconfig ;;
esac
;;
*)
config=defconfig
;;
esac
make_target=bzImage
;;
# Unknown arch, error out
*)
echo "Unknown ARCH specified!"
exit 1
;;
esac
export ARCH=${ARCH}
}
# Clone/update the boot-utils
# It would be nice to use submodules for this but those don't always play well with Travis
# https://github.com/ClangBuiltLinux/continuous-integration/commit/e9054499bb1cb1a51cd1cdc73dc3c1dfa45b4199
function update_boot_utils() {
images_url=https://github.com/ClangBuiltLinux/boot-utils
if [[ -d boot-utils ]]; then
cd boot-utils
git fetch --depth=1 ${images_url} main
git reset --hard FETCH_HEAD
cd ..
else
git clone --depth=1 ${images_url}
fi
}
check_dependencies() {
# Check for existence of needed binaries
command -v nproc
command -v timeout
command -v unbuffer
command -v zstd
update_boot_utils
oldest_llvm_version=7
latest_llvm_version=$(curl -LSs https://raw.githubusercontent.com/llvm/llvm-project/main/llvm/CMakeLists.txt | grep -s -F "set(LLVM_VERSION_MAJOR" | cut -d ' ' -f 4 | sed 's/)//')
for llvm_version in $(seq "${latest_llvm_version}" -1 "${oldest_llvm_version}"); do
debian_llvm_bin=/usr/lib/llvm-${llvm_version}/bin
if [[ -d ${debian_llvm_bin} ]]; then
export PATH=${debian_llvm_bin}:${PATH}
break
fi
done
READELF=llvm-readelf
command -v "${READELF}"
# Check for LD, CC, and AR environmental variables
# and print the version string of each. If CC and AR
# don't exist, try to find them.
# clang's integrated assembler and lld aren't ready for all architectures so
# it's just simpler to fall back to GNU as/ld when AS/LD isn't specified to
# avoid architecture specific selection logic.
"${LD:="${CROSS_COMPILE:-}"ld}" --version
if [[ -z "${LLVM_IAS:-}" ]]; then
LLVM_IAS=0
command -v "${CROSS_COMPILE:-}"as
fi
if [[ -z "${CC:-}" ]]; then
CC=clang
command -v "${CC}"
fi
${CC} --version 2>/dev/null || {
set +x
echo
echo "Looks like ${CC} could not be found in PATH!"
echo
echo "Please install as recent a version of clang as you can from your distro or"
echo "properly specify the CC variable to point to the correct clang binary."
echo
echo "If you don't want to install clang, you can either download AOSP's prebuilt"
echo "clang [1] or build it from source [2] then add the bin folder to your PATH."
echo
echo "[1]: https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/"
echo "[2]: https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source"
echo
exit
}
if [[ -z "${AR:-}" ]]; then
for AR in llvm-ar "${CROSS_COMPILE:-}"ar; do
command -v "${AR}" 2>/dev/null && break
done
fi
check_ar_version
"${AR}" --version
if [[ -z "${NM:-}" ]]; then
for NM in llvm-nm "${CROSS_COMPILE:-}"nm; do
command -v "${NM}" 2>/dev/null && break
done
fi
if [[ -z "${OBJCOPY:-}" ]]; then
for OBJCOPY in llvm-objcopy "${CROSS_COMPILE:-}"objcopy; do
command -v "${OBJCOPY}" 2>/dev/null && break
done
fi
if [[ -z "${OBJDUMP:-}" ]]; then
for OBJDUMP in llvm-objdump "${CROSS_COMPILE:-}"objdump; do
command -v "${OBJDUMP}" 2>/dev/null && break
done
fi
if [[ -z "${OBJSIZE:-}" ]]; then
for OBJSIZE in llvm-size "${CROSS_COMPILE:-}"size; do
command -v "${OBJSIZE}" 2>/dev/null && break
done
fi
if [[ -z "${STRIP:-}" ]]; then
for STRIP in llvm-strip "${CROSS_COMPILE:-}"strip; do
command -v "${STRIP}" 2>/dev/null && break
done
fi
check_objcopy_strip_version
"${OBJCOPY}" --version
"${STRIP}" --version
}
# Optimistically check to see that the user has a llvm-ar
# with https://reviews.llvm.org/rL354044. If they don't,
# fall back to GNU ar and let them know.
check_ar_version() {
if ${AR} --version | grep -q "LLVM" &&
[[ $(${AR} --version | grep version | sed -e 's/.*LLVM version //g' -e 's/[[:blank:]]*$//' -e 's/\.//g' -e 's/svn//' -e 's/git//') -lt 900 ]]; then
set +x
echo
echo "${AR} found but appears to be too old to build the kernel (needs to be at least 9.0.0)."
echo
echo "Please either update llvm-ar from your distro or build it from source!"
echo
echo "See https://github.com/ClangBuiltLinux/linux/issues/33 for more info."
echo
echo "Falling back to GNU ar..."
echo
AR=${CROSS_COMPILE:-}ar
set -x
fi
}
# Optimistically check to see that the user has an llvm-{objcopy,strip}
# with https://reviews.llvm.org/rGedeebad7715774b8481103733dc5d52dac43bdf3.
# If they don't, fall back to GNU objcopy and let them know.
check_objcopy_strip_version() {
for TOOL in ${OBJCOPY} ${STRIP}; do
if ${TOOL} --version | grep -q "LLVM" &&
[[ $(${TOOL} --version | grep version | sed -e 's/.*LLVM version //g' -e 's/[[:blank:]]*$//' -e 's/\.//g' -e 's/svn//' -e 's/git//') -lt 1000 ]]; then
set +x
echo
echo "${TOOL} found but appears to be too old to build the kernel (needs to be at least 10.0.0)."
echo
echo "Please either update ${TOOL} from your distro or build it from source!"
echo
echo "See https://github.com/ClangBuiltLinux/linux/issues/478 for more info."
echo
echo "Falling back to GNU ${TOOL//llvm-/}..."
echo
case ${TOOL} in
*objcopy*) OBJCOPY=${CROSS_COMPILE:-}objcopy ;;
*strip*) STRIP=${CROSS_COMPILE:-}strip ;;
esac
set -x
fi
done
}
mako_reactor() {
if [[ ${LLVM:-0} -eq "1" ]]; then
time \
KBUILD_BUILD_TIMESTAMP="Thu Jan 1 00:00:00 UTC 1970" \
KBUILD_BUILD_USER=driver \
KBUILD_BUILD_HOST=clangbuiltlinux \
make -j"${jobs:-$(nproc)}" \
LLVM=1 \
LLVM_IAS="${LLVM_IAS}" \
"${@}"
else
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/kbuild/kbuild.txt
time \
KBUILD_BUILD_TIMESTAMP="Thu Jan 1 00:00:00 UTC 1970" \
KBUILD_BUILD_USER=driver \
KBUILD_BUILD_HOST=clangbuiltlinux \
make -j"${jobs:-$(nproc)}" \
AR="${AR}" \
CC="${CC}" \
HOSTCC="${CC}" \
HOSTLD="${HOSTLD:-ld}" \
KCFLAGS="-Wno-implicit-fallthrough" \
LD="${LD}" \
LLVM_IAS="${LLVM_IAS}" \
NM="${NM}" \
OBJCOPY="${OBJCOPY}" \
OBJDUMP="${OBJDUMP}" \
OBJSIZE="${OBJSIZE}" \
READELF="${READELF}" \
STRIP="${STRIP}" \
"${@}"
fi
}
apply_patches() {
patches_folder=$1
if [[ -d ${patches_folder} ]]; then
git apply -v -3 "${patches_folder}"/*.patch
else
return 0
fi
}
build_linux() {
# Wrap CC in ccache if it is available (it's not strictly required)
CC="$(command -v ccache) ${CC}"
[[ ${LD} =~ lld ]] && HOSTLD=${LD}
if [[ -d ${tree} ]]; then
cd ${tree}
git fetch --depth=1 ${url} ${branch:=master}
git reset --hard FETCH_HEAD
else
git clone --depth=1 -b ${branch:=master} --single-branch ${url}
cd ${tree}
fi
git show -s | cat
llvm_all_folder="../patches/llvm-all"
apply_patches "${llvm_all_folder}/kernel-all"
apply_patches "${llvm_all_folder}/${REPO}/arch-all"
apply_patches "${llvm_all_folder}/${REPO}/${SUBARCH}"
llvm_version_folder="../patches/llvm-$(echo __clang_major__ | ${CC} -E -x c - | tail -n 1)"
apply_patches "${llvm_version_folder}/kernel-all"
apply_patches "${llvm_version_folder}/${REPO}/arch-all"
apply_patches "${llvm_version_folder}/${REPO}/${SUBARCH}"
# Only clean up old artifacts if requested, the Linux build system
# is good about figuring out what needs to be rebuilt
[[ -n "${cleanup:-}" ]] && mako_reactor mrproper
mako_reactor ${config}
# If we're using a defconfig, enable some more common config options
# like debugging, selftests, and common drivers
if [[ ${config} =~ defconfig ]]; then
cat ../configs/common.config >>.config
# Some torture test configs cause issues on PowerPC and x86_64
[[ $ARCH != "x86_64" && $ARCH != "powerpc" ]] && cat ../configs/tt.config >>.config
# For RISC-V, disable a couple of configs:
# * CONFIG_EFI: https://github.com/ClangBuiltLinux/linux/issues/1143
# * CONFIG_KALLSYMS_ALL: https://github.com/ClangBuiltLinux/linux/issues/1144
# Every other configuration option disabled is to get rid of CONFIG_KALLSYMS_ALL.
if [[ $ARCH = "riscv" ]]; then
./scripts/config \
-d DEBUG_LOCK_ALLOC \
-d DEBUG_WW_MUTEX_SLOWPATH \
-d EFI \
-d KALLSYMS_ALL \
-d LOCK_STAT \
-d PROVE_LOCKING
fi
# Disable LTO and CFI unless explicitly requested
${disable_lto:=true} && ./scripts/config -d CONFIG_LTO -d CONFIG_LTO_CLANG
fi
[[ $SUBARCH == "mips" ]] && ./scripts/config -e CPU_BIG_ENDIAN -d CPU_LITTLE_ENDIAN
# Make sure we build with CONFIG_DEBUG_SECTION_MISMATCH so that the
# full warning gets printed and we can file and fix it properly.
./scripts/config -e DEBUG_SECTION_MISMATCH
mako_reactor olddefconfig &>/dev/null
mako_reactor ${make_target}
[[ $ARCH =~ arm ]] && mako_reactor dtbs
"${READELF}" --string-dump=.comment vmlinux
cd "${OLDPWD}"
}
boot_qemu() {
${BOOT:=true} || return 0
./boot-utils/boot-qemu.sh -a "${SUBARCH}" -k "${tree}" -t "${timeout:-2}"m
}
setup_variables "${@}"
check_dependencies
build_linux
boot_qemu