forked from fengguang/lkp-tests
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathmake.cross
executable file
·439 lines (361 loc) · 10.6 KB
/
make.cross
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
#!/bin/bash
#
# linux kernel CROSS make wrapper
#
# It will download/unpack the cross tool chain if necessary,
# then invoke make with suitable options.
#
# It detects ARCH in 4 ways.
#
# - make.i386 # make it a symlink to this script
# - make.cross ARCH=i386
# - cd obj-i386; make.cross
# - export ARCH=i386; make.cross
#
# Specify compiler and version (default is gcc):
# - COMPILER=gcc-9.3.0 make.cross ARCH=arm64
# - COMPILER=clang make.cross ARCH=arm64
#
# Copyright (c) 2014, Intel Corporation.
# Author: Fengguang Wu <[email protected]>
# Credit: Tony Breeds <[email protected]> for crosstool
set -e
lkp_src_kbuild=$(dirname "$(realpath "$0")")
source $lkp_src_kbuild/kbuild.sh
check_install_path()
{
if [[ $COMPILER_INSTALL_PATH ]]; then
echo "Compiler will be installed in $COMPILER_INSTALL_PATH"
else
return 1
fi
}
install_packages()
{
local ret=0
[[ -x /usr/bin/xz ]] || {
echo Please install: xz-utils or xz
ret=1
}
[[ -x /usr/bin/lftp ]] || {
echo Please install: lftp
ret=1
}
[[ -x /usr/bin/ldd ]] || {
echo Please install: libc-bin
ret=1
}
return $ret
}
install_dependence()
{
local exec=$1
local errors=$(ldd $exec | grep "not found")
[[ $errors ]] || return 0
echo "$errors" | grep -q "libc.so.6.*GLIBC_" && {
echo Please update: libc6 or glibc
}
echo "ldd $exec"
echo "$errors"
return 1
}
download_extract()
{
local URL="$1"
local file="$(basename $URL)"
local dir="$COMPILER_INSTALL_PATH/$(basename $(dirname $URL))"
mkdir -p $COMPILER_INSTALL_PATH || {
echo "Can't use $COMPILER_INSTALL_PATH as compiler install path, please choose a different COMPILER_INSTALL_PATH"
return 1
}
mkdir -p $dir || return
cd $dir || return
echo lftpget -c $URL
lftpget -c $URL || return
cd - || return
echo tar Jxf $dir/$file -C $COMPILER_INSTALL_PATH
tar Jxf $dir/$file -C $COMPILER_INSTALL_PATH
}
show_url_hint()
{
echo "Please set new URL env variable and rerun"
echo " * crosstool provided by kernel org:"
if [[ $COMPILER =~ clang ]]; then
echo " export URL=https://cdn.kernel.org/pub/tools/llvm/files"
else
echo " export URL=https://cdn.kernel.org/pub/tools/crosstool/files/bin/x86_64"
fi
echo " * crosstool provided by 0-Day CI:"
echo " export URL=https://download.01.org/0day-ci/cross-package"
}
get_toolchain_list()
{
timeout 3m lftp -c "open $URL/ && find -d 3" | sort -V > $list || {
echo "Cannot get list from $URL"
show_url_hint
return 1
}
return 0
}
get_installed_cross_gcc()
{
local cross_gcc_candidates=($COMPILER_INSTALL_PATH/${COMPILER}*/${crosstool}/bin/${crosstool}-gcc)
local cross_gcc=$(echo "${cross_gcc_candidates[@]}" | tr ' ' '\n' | sort -V | tail -1)
[[ -x $cross_gcc ]] && echo $cross_gcc
}
get_installed_cross_clang()
{
local cross_clang
if [[ $URL =~ cdn.kernel.org ]]; then
local cross_clang_candidates=($COMPILER_INSTALL_PATH/${COMPILER//clang/llvm}*/bin/clang)
cross_clang=$(echo "${cross_clang_candidates[@]}" | tr ' ' '\n' | sort -V | tail -1)
else
cross_clang=$COMPILER_INSTALL_PATH/$COMPILER/bin/clang
fi
[[ -x $cross_clang ]] && echo $cross_clang
}
install_crosstool_clang()
{
[[ $URL ]] || URL='https://cdn.kernel.org/pub/tools/llvm/files'
local cross_clang=$(get_installed_cross_clang)
[[ $cross_clang ]] && return
local file
if [[ $URL =~ cdn.kernel.org ]]; then
# ./llvm-13.0.1-x86_64.tar.xz
# ./llvm-14.0.6-x86_64.tar.xz
# ./llvm-15.0.7-x86_64.tar.xz
# ./llvm-16.0.0-x86_64.tar.xz
# ./llvm-16.0.1-x86_64.tar.xz
# ./llvm-17.0.3-x86_64.tar.xz
# ./llvm-17.0.4-x86_64.tar.xz
local list=/tmp/0day-ci-llvm-files
get_toolchain_list || return
file=$(grep -E "${COMPILER//clang/llvm}\..*-x86_64\.tar\.xz$" $list | tail -1)
rm $list
else
file="$COMPILER/$COMPILER.tar.xz"
fi
[[ $file ]] || {
echo "Cannot find $COMPILER under $URL"
show_url_hint
return 1
}
download_extract "$URL/$file" || {
echo "Failed to download $URL/$file"
return 1
}
}
install_crosstool_gcc()
{
local cross_gcc=$(get_installed_cross_gcc)
[[ $cross_gcc ]] && return
[[ $URL ]] || local URL='https://cdn.kernel.org/pub/tools/crosstool/files/bin/x86_64'
local list=/tmp/0day-ci-crosstool-files
get_toolchain_list || return
local file
local gcc_arch_pattern=$(echo "${crosstool}" | sed 's/*/.*/g')
local gcc_version
[[ $COMPILER =~ - ]] && gcc_version=${COMPILER##*-}
if [[ $gcc_version ]]; then
# for arch has more than 1 cross tool available, like x86_64-gcc-9.3.0-nolibc_arm-linux-gnueabi.tar.xz
# and x86_64-gcc-9.3.0-nolibc_arm-linux-gnueabihf.tar.xz for arm, match "arm-linux-gnueabi." instead of
# "arm-linux-gnueabi" to get single tool package which match exactly.
file=$(grep "${gcc_arch_pattern}\..*tar\.xz$" $list | grep "${gcc_version}" | tail -1)
else
file=$(grep "${gcc_arch_pattern}\..*tar\.xz$" $list | tail -1)
fi
[[ $file ]] || {
echo "Cannot find $gcc_arch_pattern under $URL check $list"
show_url_hint
return 1
}
rm $list
download_extract "$URL/$file" || {
echo "Failed to download $URL/$file"
return 1
}
}
install_crosstool()
{
if [[ "$COMPILER" =~ "clang" ]]; then
install_crosstool_clang || {
echo "clang crosstool install failed"
return 1
}
else
install_crosstool_gcc || {
echo "gcc crosstool install failed"
return 1
}
fi
}
install_cross_compiler()
{
install_packages && install_crosstool
}
setup_gcc_exec()
{
install_cross_compiler || {
echo "Install gcc cross compiler failed"
return 1
}
gcc_exec=$(get_installed_cross_gcc)
[[ $gcc_exec ]] || {
echo "No gcc cross compiler for $ARCH"
return 1
}
# load build-in depends libs
local deplibs_path=($COMPILER_INSTALL_PATH/${COMPILER}*/${crosstool}/libexec/gcc/${crosstool}/*)
deplibs_path=$(tr ' ' '\n' <<< ${deplibs_path[@]} | sort -V | tail -n1)
[[ -d $deplibs_path ]] && export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$deplibs_path
install_dependence "$gcc_exec"
}
update_path_env_for_parisc()
{
local crosstool=$1
setup_gcc_exec || return
gcc_path=$(dirname $gcc_exec)
[[ $PATH =~ $gcc_path ]] || export PATH=$gcc_path:$PATH
}
setup_parisc_crosstool()
{
# 64bit build needs both hppa-linux and hppa64-linux
# > Assembler messages:
# > ../arch/parisc/kernel/vdso32/sigtramp.S:39: Error: unknown pseudo-op: `.proc'
# > ../arch/parisc/kernel/vdso32/restart_syscall.S:16: Error: bad or irreducible absolute expression
# > ../arch/parisc/kernel/vdso32/sigtramp.S:40: Error: unknown pseudo-op: `.callinfo'
update_path_env_for_parisc hppa-linux || return
[[ $crosstool =~ hppa64-linux ]] && {
update_path_env_for_parisc hppa64-linux || return
}
return 0
# https://lore.kernel.org/lkml/[email protected]/
# Please drop the "CROSS_COMPILE=hppa64-linux-" part.
# It will autodetect the cross compiler, so this is sufficient:
# make W=1 --keep-going -j32 O=./build_dir ARCH=parisc64 prepare
}
setup_crosstool_gcc()
{
local crosstool
local gcc_exec
setup_cross_vars
if [[ $crosstool ]]; then
if [[ $crosstool =~ hppa ]]; then
setup_parisc_crosstool || return
else
setup_gcc_exec || return
opt_cross="CROSS_COMPILE=${gcc_exec%gcc}"
fi
else
opt_cross=
fi
setup_kcflags "$gcc_exec"
echo "PATH=$PATH"
return 0
}
setup_crosstool_clang()
{
install_cross_compiler || {
echo "Install clang compiler failed"
return 1
}
local cross_clang=$(get_installed_cross_clang)
[[ $cross_clang ]] || return
install_dependence "$cross_clang" || return
local cross_clang_path=$(dirname $cross_clang)
local opt_ldd="LD=${cross_clang_path}/ld.lld HOSTLD=${cross_clang_path}/ld.lld"
local opt_objcopy="OBJCOPY=llvm-objcopy"
[[ $ARCH = s390 ]] && {
opt_ldd=
# from binutils-s390x-linux-gnu
opt_objcopy="OBJCOPY=/usr/s390x-linux-gnu/bin/objcopy"
}
export PATH="$cross_clang_path:$PATH"
echo "PATH=$PATH"
local kernel_version=$(make kernelversion)
local kernel_version_major=$(echo "$kernel_version" | cut -d. -f1)
local kernel_version_minor=$(echo "$kernel_version" | cut -d. -f2)
# https://www.kernel.org/doc/html/latest/kbuild/llvm.html
if is_llvm_equal_one_supported; then
opt_cross="LLVM=1"
else
opt_cross="HOSTCC=$cross_clang CC=$cross_clang $opt_ldd $opt_objcopy \
AR=llvm-ar NM=llvm-nm STRIP=llvm-strip OBJDUMP=llvm-objdump OBJSIZE=llvm-size READELF=llvm-readelf HOSTCXX=clang++ HOSTAR=llvm-ar"
fi
# TODO: CROSS_COMPILE is not necessary for new kernel
setup_cross_vars
[[ $ARCH = s390 ]] && crosstool=s390x-linux
opt_cross="$opt_cross CROSS_COMPILE=$crosstool-"
local opt_llvm_ias=$(setup_llvm_ias "$opt_cross")
[[ $opt_llvm_ias ]] && opt_cross="$opt_cross $opt_llvm_ias"
setup_kcflags "$cross_clang"
:
}
setup_crosstool()
{
if [[ "$COMPILER" =~ "clang" ]]; then
setup_crosstool_clang
else
setup_crosstool_gcc
fi
}
setup_kcflags()
{
local compiler_bin=$1
local kcflags=
add_kbuild_kcflags $lkp_src_kbuild/etc/kbuild-kcflags
[[ $kcflags ]] && opt_kbuild_cflags+=(KCFLAGS="$kcflags")
}
shopt -s nullglob
COMPILER=${COMPILER:-"gcc"}
if [[ ! "$0" =~ 'make.cross' && "$0" =~ make\.([a-z0-9_]+) ]]; then
export ARCH="${BASH_REMATCH[1]}"
elif [[ "$*" =~ ARCH=([a-z0-9_]+) ]]; then
export ARCH="${BASH_REMATCH[1]}"
elif [[ ${PWD##*-} =~ ^($(echo $KBUILD_SUPPORTED_ARCHS | sed 's/ /|/g'))$ ]]; then
echo "ARCH set to ${PWD##*-}"
export ARCH=${PWD##*-}
elif [[ ! $ARCH ]]; then
export ARCH=x86_64
fi
[[ "$*" =~ ARCH=([a-z0-9_]+) ]] && [[ $ARCH != ${BASH_REMATCH[1]} ]] && {
echo "Conflicting ARCH specified! $ARCH ${BASH_REMATCH[1]}"
exit 1
}
if [[ "$*" =~ O=([^ ]+) ]]; then
BUILD_DIR="${BASH_REMATCH[1]}"
elif [[ -n "$KBUILD_OUTPUT" ]]; then
BUILD_DIR="$KBUILD_OUTOUT"
elif [[ -d obj-$ARCH ]]; then
BUILD_DIR=obj-$ARCH
O=O=obj-$ARCH
else
BUILD_DIR="."
fi
check_install_path || {
echo "Please set COMPILER_INSTALL_PATH to specify compiler install path"
echo "E.g. COMPILER_INSTALL_PATH=\$HOME/0day COMPILER=clang-17 make.cross ARCH=x86_64"
echo
exit 1
}
opt_kbuild_cflags=()
setup_crosstool || {
echo "setup_crosstool failed"
exit 1
}
[[ "$*" =~ (-j|--jobs) ]] || {
nr_cpu=$(getconf _NPROCESSORS_CONF)
opt_jobs="--jobs=$((nr_cpu * 2))"
}
[[ "$*" =~ "ARCH=$ARCH" ]] || {
opt_arch="ARCH=$ARCH"
}
[[ -f arch/$(src_arch $ARCH)/boot/dts/Makefile ]] && make_dts="CONFIG_OF_ALL_DTBS=y CONFIG_DTC=y"
[[ -f .make-env ]] && source ./.make-env
if [[ -d source && -L source ]]; then
echo make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@"
exec make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@"
else
echo make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@"
exec make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@"
fi