forked from fengguang/lkp-tests
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathkbuild.sh
367 lines (336 loc) · 8.86 KB
/
kbuild.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
#!/bin/bash
KBUILD_SUPPORTED_ARCHS="alpha
arc
arm
arm64
csky
hexagon
i386
loongarch
m68k
microblaze
mips
openrisc
parisc
parisc64
nios2
powerpc
powerpc64
riscv
s390
sh
sparc
sparc64
um
x86_64
xtensa
"
warn_empty()
{
[[ "$1" ]] && return
echo "empty parameter" >&2
return 1
}
is_supported_compiler_option()
{
local compiler_bin=$1
local option=$2
# $ gcc-7 -finvalid -Winvalid -xc /dev/null 2>&1
# gcc-7: error: unrecognized command line option '-finvalid'; did you mean '-finline'?
# gcc-7: error: unrecognized command line option '-Winvalid'; did you mean '-Winline'?
#
# $ gcc-7 -Werror=attribute-alias -xc /dev/null
# cc1: error: -Werror=attribute-alias: no option -Wattribute-alias
# $ gcc-7 -Wno-error=attribute-alias -xc /dev/null
# cc1: error: -Werror=attribute-alias: no option -Wattribute-alias
#
# $ gcc-11 -Winvalid-option -xc /dev/null
# gcc-11: error: unrecognized command-line option ‘-Winvalid-option’; did you mean ‘-Winvalid-pch’?
#
# $ clang-15 -Winvalid-option -finvalid -xc /dev/null
# warning: unknown warning option '-Winvalid-option' [-Wunknown-warning-option]
#
# $ clang-15 -finvalid -xc /dev/null 2>&1
# clang-15: error: unknown argument: '-fstrict-flex-arrays=3'
# if an option is "-Wno-<diagnostic-type>", need to remove "no" and check the the remaining flag.
#
# $ gcc-7 -Wno-attribute-alias -xc /dev/null
# /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
# (.text+0x17): undefined reference to `main'
# collect2: error: ld returned 1 exit status
#
# $ gcc-7 -Wattribute-alias -xc /dev/null
# gcc-7: error: unrecognized command line option '-Wattribute-alias'; did you mean '-Wattributes'?
# delete the "no-" prefix only for warning options
echo $option | grep -q -e "Werror" -e "Wno-error" || option=${option//-Wno-/-W}
$compiler_bin $option -xc /dev/null 2>&1 | grep -q \
-e "unrecognized command.line option" \
-e "no option" \
-e "unknown warning option" \
-e "unknown argument" \
&& return 1
return 0
}
add_kcflag()
{
local flag=$1
is_supported_compiler_option "$compiler_bin" "$flag" && kcflags="$kcflags $flag"
}
add_kbuild_kcflags()
{
local kcflags_file=$1
while read flag
do
add_kcflag "$flag"
done < <(grep -v -e "^#" -e "^$" $kcflags_file)
}
is_llvm_equal_one_supported()
{
# handle v2.6.X version, similar to is_clang_supported_arch function
local kernel_version_major=${kernel_version_major%%.*}
# LLVM=1 is introduced by below commit which merged into v5.7-rc1
# irb(main):001:0> Git.open.gcommit('a0d1c951ef0').merged_by
# => "v5.7-rc1"
# commit a0d1c951ef08ed24f35129267e3595d86f57f5d3
# Author: Masahiro Yamada <[email protected]>
# Date: Wed Apr 8 10:36:23 2020 +0900
# kbuild: support LLVM=1 to switch the default tools to Clang/LLVM
#
# As Documentation/kbuild/llvm.rst implies, building the kernel with a
# full set of LLVM tools gets very verbose and unwieldy.
#
# Provide a single switch LLVM=1 to use Clang and LLVM tools instead
# of GCC and Binutils. You can pass it from the command line or as an
# environment variable.
is_kernel_version "<" 5.7 && return 1
if [[ $ARCH = "s390" ]]; then
return 1
elif [[ $ARCH =~ "powerpc" || $ARCH =~ "mips" || $ARCH =~ "riscv" ]]; then
# https://www.kernel.org/doc/html/v5.18/kbuild/llvm.html
# https://www.kernel.org/doc/html/v5.19/kbuild/llvm.html
is_kernel_version "<" 5.18 && return 1
fi
return 0
}
setup_llvm_ias()
{
local opt_cc=$1
if [[ $ARCH =~ "powerpc" ]]; then
# f12b034afeb3 ("scripts/Makefile.clang: default to LLVM_IAS=1")
# above commit is merged by v5.15-rc1, and will enable clang integrated assembler by default
# it will raise below errors:
# clang-14: error: unsupported argument '-mpower4' to option 'Wa,'
# clang-14: error: unsupported argument '-many' to option 'Wa,'
# explicitly set LLVM_IAS=0 to disable integrated assembler and switch back to gcc assembler
is_kernel_version ">" 5.14 && is_kernel_version "<" 5.18 && echo "LLVM_IAS=0"
elif [[ $ARCH =~ "hexagon" ]]; then
is_kernel_version "<" 5.15 && echo "LLVM_IAS=1"
elif [[ $ARCH =~ arm ]]; then
is_kernel_version "<" 5.15 && [[ $opt_cc = "LLVM=1" ]] && echo "LLVM_IAS=1"
fi
}
src_arch()
{
local ARCH=$1
case $ARCH in
i386|x86_64)
echo x86;;
sparc32|sparc64)
echo sparc;;
parisc|parisc64)
echo parisc;;
sh64)
echo sh;;
tilepro|tilegx)
echo tile;;
powerpc64)
echo powerpc;;
*)
echo $ARCH;;
esac
}
get_config_value()
{
local config=$1
local config_file=$2
grep -s -h "^$config=" $config_file .config $KBUILD_OUTPUT/.config $BUILD_DIR/.config |
head -n1 |
cut -f2- -d= |
sed 's/\"//g'
}
# is_config_enabled CONFIG_BOOT_LINK_OFFSET
# - 202003/h8300-randconfig-a001-20200327:CONFIG_BOOT_LINK_OFFSET= # false
# - 202003/sh-randconfig-a001-20200313:CONFIG_BOOT_LINK_OFFSET=0x00800000 # true
#
# is_config_enabled CONFIG_BOOT_LINK_OFFSET=
# - 202003/h8300-randconfig-a001-20200327:CONFIG_BOOT_LINK_OFFSET= # true
# - 202003/sh-randconfig-a001-20200313:CONFIG_BOOT_LINK_OFFSET=0x00800000 # true
is_config_enabled()
{
local config="$1"
local config_file
[[ $config ]] || return
if [[ $2 ]]; then
config_file="$2"
elif [[ -s .config ]]; then
config_file=.config
elif [[ $KBUILD_OUTPUT ]] && [[ -s $KBUILD_OUTPUT/.config ]]; then
config_file=$KBUILD_OUTPUT/.config
elif [[ $BUILD_PATH ]] && [[ -s $BUILD_PATH/.config ]]; then
config_file=$BUILD_PATH/.config
elif [[ $BUILD_DIR ]] && [[ -s $BUILD_DIR/.config ]]; then
config_file=$BUILD_DIR/.config
else
return 2 # ENOENT
fi
# $ echo "CONFIG_CPU_BIG_ENDIAN=y" | grep "^CONFIG_CPU_BIG_ENDIAN=[^n]"; echo $?
# CONFIG_CPU_BIG_ENDIAN=y
# 0
# $ echo "CONFIG_CPU_BIG_ENDIAN=n" | grep "^CONFIG_CPU_BIG_ENDIAN=[^n]"; echo $?
# 1
[[ $config =~ '=' ]] || config+='=[^n]'
grep -q "^$config" "$config_file"
}
setup_cross_vars()
{
case $ARCH in
arm)
cross_pkg=arm-linux-gnueabi
# cross_gcc=arm-linux-gnueabihf-gcc
crosstool=arm-linux-gnueabi
;;
arm64)
cross_pkg=aarch64-linux-gnu
crosstool=aarch64-linux
;;
mips)
if is_config_enabled CONFIG_64BIT; then
if is_config_enabled CONFIG_CPU_LITTLE_ENDIAN; then
cross_pkg=mips64el-linux-gnuabi64
crosstool=mips64el-linux
else
cross_pkg=mips64-linux-gnuabi64
crosstool=mips64-linux
fi
elif is_config_enabled CONFIG_32BIT; then
if is_config_enabled CONFIG_CPU_LITTLE_ENDIAN; then
cross_pkg=mipsel-linux-gnu
crosstool=mipsel-linux
else
cross_pkg=mips-linux-gnu
crosstool=mips-linux
fi
else
cross_pkg=mips-linux-gnu
crosstool=mips-linux
fi
;;
powerpc|powerpc64)
if is_config_enabled CONFIG_PPC64; then
if is_config_enabled CONFIG_CPU_LITTLE_ENDIAN; then
cross_pkg=powerpc64le-linux-gnu
crosstool=powerpc64le-linux
else
cross_pkg=powerpc64-linux-gnu
crosstool=powerpc64-linux
fi
else
cross_pkg=powerpc-linux-gnu
crosstool=powerpc-linux
fi
;;
sh)
cross_pkg=sh4-linux-gnu
crosstool=sh4-linux
;;
alpha)
cross_pkg=alpha-linux-gnu
crosstool=alpha-linux
;;
sparc64)
cross_pkg=sparc64-linux-gnu
crosstool=sparc64-linux
;;
sparc)
if is_config_enabled CONFIG_64BIT; then
cross_pkg=sparc64-linux-gnu
crosstool=sparc64-linux
else
crosstool=sparc-linux
fi
;;
parisc)
if is_config_enabled CONFIG_64BIT; then
cross_pkg=hppa64-linux-gnu
crosstool=hppa64-linux
else
cross_pkg=hppa-linux-gnu
crosstool=hppa-linux
fi
;;
parisc64)
cross_pkg=hppa64-linux-gnu
crosstool=hppa64-linux
;;
openrisc)
crosstool=or1k-linux
;;
s390)
cross_pkg=s390x-linux-gnu
crosstool=s390-linux
;;
m68k)
cross_pkg=m68k-linux-gnu
crosstool=m68k-linux
;;
xtensa)
crosstool=xtensa-linux
;;
arc)
# start to support big endian arc toolchain form gcc-9.3.0
# for earlier gcc version, will failed to find arceb-elf for
# big endian arceb-elf tool chain
if is_config_enabled CONFIG_CPU_BIG_ENDIAN; then
crosstool=arceb-elf
else
crosstool=arc-elf
fi
;;
c6x)
crosstool=c6x-elf
;;
riscv)
if is_config_enabled CONFIG_64BIT; then
cross_pkg=riscv64-linux-gnu
crosstool=riscv64-linux
elif is_config_enabled CONFIG_32BIT; then
crosstool=riscv32-linux
else
cross_pkg=riscv64-linux-gnu
crosstool=riscv64-linux
fi
;;
loongarch)
crosstool=loongarch64-linux
;;
# nios2
*)
crosstool=$ARCH-linux
;;
esac
}
# usage:
# is_kernel_version "<=" 5.16
# is_kernel_version "==" 6.0
is_kernel_version()
{
local operator=$1
local other=$2
warn_empty "$operator" || return
warn_empty "$other" || return
warn_empty "$kernel_version_major" || return
warn_empty "$kernel_version_minor" || return
local other_kernel_version_major=${other%.*}
local other_kernel_version_minor=${other#*.}
(( (kernel_version_major * 100 + kernel_version_minor) $operator (other_kernel_version_major * 100 + other_kernel_version_minor) ))
}