jjb: babeltrace: use clang-format-16
[lttng-ci.git] / scripts / lttng-modules / param-build.sh
CommitLineData
51c9c62d 1#!/bin/bash
f3d8604b 2#
53300bf0 3# Copyright (C) 2016-2023 Michael Jeanson <mjeanson@efficios.com>
f3d8604b
MJ
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
51c9c62d
MJ
18set -exu
19
39961776 20# Parameters
53300bf0
MJ
21platform=${platforms:-}
22cross_arch=${cross_arch:-}
23ktag=${ktag:-}
24kgitrepo=${kgitrepo:-}
25mversion=${mversion:-}
26mgitrepo=${mgitrepo:-}
27make_args=()
28
29DEBUG=${DEBUG:-}
41a8bbc3
KS
30FAIL_ON_WARNINGS="${FAIL_ON_WARNINGS:-}"
31if [[ "${FAIL_ON_WARNINGS}" == "false" ]] ; then
32 FAIL_ON_WARNINGS=''
33fi
53300bf0 34
447eaf93
KS
35# Derive arch from label if it isn't set
36if [ -z "${arch:-}" ] ; then
37 # Labels may be platform specific, eg. jammy-amd64, deb12-armhf
38 regex='[[:alnum:]]+-([[:alnum:]]+)'
53300bf0 39 if [[ "${platform}" =~ ${regex} ]] ; then
447eaf93
KS
40 arch="${BASH_REMATCH[1]}"
41 else
53300bf0 42 arch="${platform:-}"
447eaf93
KS
43 fi
44fi
a86b55bc 45
39961776 46
339db64d
MJ
47## FUNCTIONS ##
48
f3d8604b
MJ
49# Kernel version compare functions
50verlte() {
51fa2ab3 51 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ]
f3d8604b
MJ
52}
53
54verlt() {
28b10e79 55 # shellcheck disable=SC2015
51fa2ab3 56 [ "$1" = "$2" ] && return 1 || verlte "$1" "$2"
f3d8604b
MJ
57}
58
59vergte() {
51fa2ab3 60 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -n1)" ]
f3d8604b
MJ
61}
62
63vergt() {
28b10e79 64 # shellcheck disable=SC2015
51fa2ab3 65 [ "$1" = "$2" ] && return 1 || vergte "$1" "$2"
f3d8604b
MJ
66}
67
53300bf0
MJ
68print_header() {
69 set +x
70
71 local message=" $1 "
72 local message_len
73 local padding_len
74
75 message_len="${#message}"
76 padding_len=$(( (80 - (message_len)) / 2 ))
77
78 printf '\n'; printf -- '#%.0s' {1..80}; printf '\n'
79 printf -- '-%.0s' {1..80}; printf '\n'
80 printf -- '#%.0s' $(seq 1 $padding_len); printf '%s' "$message"; printf -- '#%.0s' $(seq 1 $padding_len); printf '\n'
81 printf -- '-%.0s' {1..80}; printf '\n'
82 printf -- '#%.0s' {1..80}; printf '\n\n'
83
84 set -x
85}
86
5a196804
MJ
87git_clone_modules_sources() {
88 mkdir -p "$MODULES_GIT_DIR"
28b10e79 89
41a8bbc3
KS
90 # If the version starts with "refs/" or looks like a commit hash,
91 # checkout the specific git ref, otherwise treat it as a branch name.
92 pattern="^[0-9a-f]{40}$"
93 if [ "${mversion:0:5}" = "refs/" ] || [[ "${mversion}" =~ $pattern ]]; then
28b10e79
MJ
94 git clone --no-tags --depth=1 "${mgitrepo}" "$MODULES_GIT_DIR"
95 (cd "$MODULES_GIT_DIR" && git fetch origin "${mversion}" && git checkout FETCH_HEAD)
96 else
97 git clone --no-tags --depth=1 -b "${mversion}" "${mgitrepo}" "$MODULES_GIT_DIR"
98 fi
5a196804
MJ
99}
100
101# Checkout a shallow kernel tree of the specified tag
102git_clone_linux_sources() {
103 mkdir -p "$LINUX_GIT_DIR"
cb36638c
KS
104 case "${distroversion:-}" in
105 el*)
106 git clone -b "${ktag}" "${kgitrepo}" src/linux-rpm
107
108 # Get the source files
109 pushd src/linux-rpm
110 "${WORKSPACE}/src/getsrc/getsrc/getsrc.sh"
111 tar -x -C "${LINUX_GIT_DIR}" --strip-components=1 -f SOURCES/linux-*.tar.xz
112 popd
113
114 # Pretend we're a repo like the default expects
115 pushd "${LINUX_GIT_DIR}"
116 git init .
117 git config user.name 'Jenkins'
118 git config user.email 'jenkins@efficios.com'
119 git add .
120 git commit -a -m 'Initial commit'
121 git tag "${ktag}"
122 echo "${LINUX_GIT_REF_REPO_DIR}" > .git/objects/info/alternates
123 popd
124 ;;
125
126 *)
127 git clone --depth=1 -b "${ktag}" --reference-if-able "$LINUX_GIT_REF_REPO_DIR" "${kgitrepo}" "$LINUX_GIT_DIR"
128 ;;
129 esac
5a196804 130}
7e02032c 131
7e02032c 132
5a196804
MJ
133# Export the kernel sources from the git repo
134git_export_linux_sources() {
135 cd "$LINUX_GIT_DIR"
136 git archive "${ktag}" | tar -x -C "$LINUX_SRCOBJ_DIR"
137}
138
139
140# Upload the tar archive to the object store
141upload_archive_obj() {
142 s3cmd -c "$WORKSPACE/.s3cfg" put "$WORKSPACE/$obj_name" "$obj_url_prefix/"
143 rm -f "$WORKSPACE/$obj_name"
144}
145
146
147extract_archive_obj() {
02c6a56a 148 tar -xf "$WORKSPACE/$obj_name" -C "$LINUX_OBJ_DIR" -I pbzip2
5a196804
MJ
149 rm -f "$WORKSPACE/$obj_name"
150}
151
152
153tar_archive_obj() {
154 cd "$LINUX_OBJ_DIR"
155 tar -cf "$WORKSPACE/$obj_name" -I pbzip2 .
156 cd -
157}
158
53300bf0 159# List all GCC versions present in the PATH
6c0b2710
KS
160list_gccs() {
161 local gccs
162 gccs=()
163 IFS=: read -r -a path_array <<< "$PATH"
164 while read -r gcc ; do
165 gccs+=("$gcc")
166 done < <(find "${path_array[@]}" -maxdepth 1 -regex '.*/gcc-[0-9\.]+$' -printf '%f\n' | sort -t- -k2 -V -r)
167 echo "${gccs[@]}"
168}
169
5a196804
MJ
170# Find the most recent GCC version supported by the kernel sources
171select_compiler() {
5a196804 172
53300bf0 173 # Enter linux source dir
5a196804
MJ
174 cd "$LINUX_SRCOBJ_DIR"
175
ae365617
MJ
176 # Get the kernel version using the host toolchain, some cross-compiled arch may be broken
177 kversion=$(unset ARCH; unset CROSS_COMPILE; make -s kernelversion)
178
179 if [ "${cross_arch}" = "riscv64" ] && verlt "${kversion}" "5.12"; then
180 echo "RISC-V support was upstreamed in kernel v4.19 but kprobes support was only added in v5.12. Don't try to build it."
181 exit 0
182 fi
5a196804 183
4f6c1ba9
KS
184 if [ "${cross_arch}" = "arm64" ] && verlt "${kversion}" "3.7"; then
185 echo "ARM64 support was added as of v3.7. Don't try to build it."
186 exit 0
187 fi
188
189 if [ "${cross_arch}" = "arm64" ] && verlt "${kversion}" "3.18"; then
190 echo "lttng-modules requires gcc >= 5.1 for ARM64 due to compiler bugs in gcc."
191 echo "gcc-5 support was added to the kernel as of v3.18. Don't this to build it."
192 exit 0
193 fi
194
a86b55bc 195 if { verlt "$kversion" "4.4"; }; then
53300bf0 196 # Force gcc-4.8 for kernels before 4.4
a86b55bc 197 selected_cc='gcc-4.8'
4f6c1ba9
KS
198 # Due to compiler bugs in gcc on arm64, lttng-modules disallows
199 # compilation with gcc < 5.1.
200 if [[ "${cross_arch}" == "arm64" ]] ; then
201 selected_cc='gcc-5.5'
202 export PATH="${PATH:-}:/usr/local/gcc5.5/bin"
203 export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:/usr/local/gcc5.5/lib"
204 fi
53300bf0
MJ
205 selected_cc_version=$(echo "${selected_cc}" | cut -d'-' -f2)
206 else
207 for cc in $(list_gccs) ; do
208 if "${CROSS_COMPILE:-}${cc}" -I include/ -D__LINUX_COMPILER_H -D__LINUX_COMPILER_TYPES_H -E include/linux/compiler-gcc.h; then
209 cc_version=$(echo "${cc}" | cut -d'-' -f2)
210 if { verlt "${kversion}" "5.17"; } && { vergt "${cc_version}" "11"; } ; then
211 # Using gcc-12+ with '-Wuse-after-free' breaks the build of older
212 # kernels (in particular, objtool). Some releases on LTS
213 # branches between 4.x and 5.15 can be built with gcc-12.
214 # @see https://lore.kernel.org/lkml/20494.1643237814@turing-police/
215 # @see https://gitlab.com/linux-kernel/stable/-/commit/52a9dab6d892763b2a8334a568bd4e2c1a6fde66
216 continue
217 fi
218 selected_cc="$cc"
219 selected_cc_version="$cc_version"
220 break
221 fi
222 done
a86b55bc
KS
223 fi
224
efdab1b7 225 if [ -z "$selected_cc" ]; then
5a196804
MJ
226 echo "Found no suitable compiler."
227 exit 1
228 fi
229
53300bf0
MJ
230 # lttng-modules requires gcc >= 5.1 for aarch64
231 # @see https://github.com/lttng/lttng-modules/commit/be06402dbdbea2f3394e60ec15c5d3356e2be416
232 if { verlt "${selected_cc_version}" "5.1"; } && [ "${cross_arch}" = "arm64" ] ; then
233 echo "Building lttng-modules on aarch64 requires gcc >= 5.1"
234 exit 1
235 fi
236
237 cd -
238}
239
240export_kbuild_flags() {
241 local _KAFLAGS
242 local _KCFLAGS
243 local _KCPPFLAGS
244 local _HOSTCFLAGS
245
a86b55bc
KS
246 _KAFLAGS=()
247 _KCFLAGS=()
248 _KCPPFLAGS=()
249 _HOSTCFLAGS=()
53300bf0 250
4f6c1ba9 251 if { vergte "$selected_cc_version" "6"; }; then
425e4992 252 # Older kernel Makefiles do not expect the compiler to default to PIE
a86b55bc
KS
253 _KAFLAGS+=(-fno-pie)
254 _KCFLAGS+=(
255 -fno-pie
256 -no-pie
257 -fno-stack-protector
258 )
259 _KCPPFLAGS+=(-fno-pie)
260 fi
261
a86b55bc
KS
262 if { vergte "${selected_cc_version}" "10"; } && { verlt "${kversion}" "5.10"; } ; then
263 # gcc-10 changed the default from '-fcommon' to '-fno-common', which
264 # causes a linker failure. '-fcommon' can be set on the HOSTCFLAGS
265 # to avoid the issue.
266 # @see https://gitlab.com/linux-kernel/stable/-/commit/e33a814e772cdc36436c8c188d8c42d019fda639
267 _HOSTCFLAGS+=(-fcommon)
268 fi
269
53300bf0
MJ
270 if { verlt "${kversion}" "5.14"; } && [ "${cross_arch:-}" == "armhf" ] ; then
271 # Work-around for producing instructions that aren't valid for the
272 # default architectures.
273 # Eg. Error: selected processor does not support `cpsid i' in ARM mode
274 _KCFLAGS+=(-march=armv7-a -mfpu=vfpv3-d16)
275 _KCPPFLAGS+=(-march=armv7-a -mfpu=vfpv3-d16)
a86b55bc
KS
276 fi
277
278 if { vergt "${selected_cc_version}" "8"; } && { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.17"; } ; then
279 # This was added to -Wall in gcc 9 but some kernels do not include the fixes
280 # @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88583
281 _KCFLAGS+=(-Wno-error=packed-not-aligned)
425e4992
MJ
282 fi
283
a86b55bc
KS
284 export KAFLAGS="${_KAFLAGS[*]}"
285 export KCFLAGS="${_KCFLAGS[*]}"
286 export KCPPFLAGS="${_KCPPFLAGS[*]}"
287 export HOSTCFLAGS="${_HOSTCFLAGS[*]}"
5a196804 288 export CC="${CROSS_COMPILE:-}${selected_cc}"
1d5a0a3c 289 export HOSTCC="${selected_cc}"
5a196804 290
a86b55bc
KS
291 make_args=(
292 CC="${CC}"
293 HOSTCC="${HOSTCC}"
294 HOSTCFLAGS="${HOSTCFLAGS:-}"
295 )
53300bf0 296
a86b55bc
KS
297 if [ -n "${DEBUG}" ] ; then
298 make_args+=(
299 V=1
300 )
301 fi
5a196804
MJ
302}
303
a86b55bc
KS
304patch_linux_kernel() {
305 local commit_hash
306 commit_hash="$1"
53300bf0
MJ
307
308 # Show the title of the patch in the build log
309 git -C "${LINUX_GIT_REF_REPO_DIR}" show --oneline -s "${commit_hash}"
310
311 # Apply patch, don't fail if it doesn't apply cleanly
a86b55bc
KS
312 set +e
313 git -C "${LINUX_GIT_REF_REPO_DIR}" format-patch -n1 --stdout "${commit_hash}" | patch -p1
314 set -e
53300bf0 315
a86b55bc
KS
316 if [ "$?" -gt 1 ] ; then
317 echo "Serious issue patching"
318 exit 1
319 fi
320}
5a196804
MJ
321
322build_linux_kernel() {
323 cd "$LINUX_SRCOBJ_DIR"
324
a86b55bc 325 kversion=$(make -s kernelversion "${make_args[@]}")
23ebcd4b 326 pahole_version="$(pahole --version | tr -d 'v')"
a86b55bc
KS
327
328 if { verlt "${kversion}" "3.3"; } && [ "${vanilla_config}" = "imx_v6_v7_defconfig" ] ; then
329 # imx_v6_v7 didn't exist before 06965c39b4c63933fa0a1cde2237ef85477c5655
330 if { verlt "${kversion}" "3.2"; } ; then
331 vanilla_config='mx5_defconfig'
332 else
333 vanilla_config='mx51_defconfig'
334 fi
335 fi
336
337 if { verlt "${kversion}" "3.13"; } && [ "${vanilla_config}" = "pseries_le_defconfig" ] ; then
338 # pseries_le_deconfig was introduced in f53e462e907cbaed29c49c0f10f5b8f614e1bf1d
339 vanilla_config='pseries_defconfig'
340 fi
5a196804 341
7e02032c 342 # Generate kernel configuration
a1ae361e 343 case "$ktag" in
cb36638c
KS
344 *el*)
345 # Copy the EL kernel configuration
346 el_arch="${cross_arch:-$arch}"
347 case "${el_arch}" in
348 amd64)
349 el_arch=x86_64
350 ;;
351 arm64)
352 el_arch=aarch64
353 ;;
354 ppc64el)
355 el_arch=ppc64le
356 ;;
357 *)
358 ;;
359 esac
360 ls "${WORKSPACE}/src/linux-rpm/SOURCES/"
361 if [ -f "${WORKSPACE}/src/linux-rpm/SOURCES/kernel-${el_arch}.config" ] ; then
362 cp "${WORKSPACE}/src/linux-rpm/SOURCES/kernel-${el_arch}.config" .config
363 elif [ -f "${WORKSPACE}/src/linux-rpm/SOURCES/kernel-${el_arch}-rhel.config" ] ; then
364 cp "${WORKSPACE}/src/linux-rpm/SOURCES/kernel-${el_arch}-rhel.config" .config
365 fi
366
367 # Eg.
368 # mm/mempolicy.c: In function ‘mpol_parse_str’:
369 # mm/mempolicy.c:2980:26: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
370 export KCFLAGS="${KCFLAGS} -Wno-error -Wno-all -Wno-error=stringop-overflow -Wno-error=address-of-packed-member"
371 ;;
372
7e02032c 373 Ubuntu*)
f16755cf
MJ
374 if [ "${cross_arch}" = "powerpc" ] && vergte "${kversion}" "4.10"; then
375 echo "Ubuntu removed big endian powerpc configuration from kernel >= 4.10. Don't try to build it."
376 exit 0
377 fi
378
379 if [ "${cross_arch}" = "riscv64" ] && verlt "${kversion}" "6.2"; then
380 echo "Ubuntu added RISC-V config to their 'regular' kernels in v6.2. Don't try to build it."
381 exit 0
39961776 382 fi
af101606 383
009efde7
KS
384 FAKEROOT_ARGS=(
385 'KW_DEFCONFIG_DIR=.'
386 )
387 fakeroot debian/rules clean "${FAKEROOT_ARGS[@]}"
0c1bde3d 388
073dc82c 389 # Hack for kernel Ubuntu-hwe-5.8
0c1bde3d
KS
390 # The debian/control file is produced by the clean target above, so this
391 # check needs to happen afterwards.
392 if [ ! -f "debian/compat" ] && ! grep -q debhelper-compat debian/control; then
073dc82c
MJ
393 echo "10" > "debian/compat"
394 fi
395
0c1bde3d
KS
396 # genconfigs check can fail in certain cases, eg. when a more recent
397 # compiler exposes kernel configuration flags which aren't set in the
398 # Ubuntu annotations.
399 # In any case, the configuration will be updated with any missing values
400 # later in our build script.
009efde7
KS
401 FAKEROOT_ARGS+=('do_skip_checks=true')
402
403 # Some Ubuntu tags default the toolchain using `gcc?=gcc-XX` in
404 # `debian/rules.d/0-common-vars.mk`. This may fail if the gcc version
405 # used as a default isn't available.
406 # For example, Ubuntu-6.8.0-7.7 sets `gcc?=gcc-13`, and that version
407 # of gcc isn't available on the deb12-amd64 ci-nodes.
408 # Work has also already been done in `select_compiler` to make our
409 # own decision of which compiler to use. As a result of both cases,
410 # our compiler choice should be passed into genconfigs.
411 FAKEROOT_ARGS+=("gcc=${selected_cc}")
412
413 fakeroot debian/rules genconfigs "${FAKEROOT_ARGS[@]}"
5a196804 414 cp CONFIGS/"${ubuntu_config}" .config
7e02032c 415 ;;
c45b04df 416
7e02032c 417 *)
16844a6d
MJ
418 # Force 32bit build on i386, default is 64bit
419 if [ "$arch" = "i386" ]; then
5a196804
MJ
420 export ARCH="i386"
421 fi
422
a86b55bc 423 make "${vanilla_config}" "${make_args[@]}"
7e02032c
MJ
424 ;;
425 esac
426
a86b55bc
KS
427 if [ "${vanilla_config}" = "pseries_defconfig" ] && [ "${cross_arch}" = "ppc64el" ] ; then
428 # @see diff <(git show v3.13:arch/powerpc/configs/pseries_defconfig) <(git show v3.13:arch/powerpc/configs/pseries_le_defconfig)
429 scripts/config --enable CONFIG_CPU_LITTLE_ENDIAN
430 scripts/config --enable CONFIG_CMA
431 scripts/config --disable CONFIG_XMON_DEFAULT
432 # scripts/config --disable CONFIG_VIRTUALIZATION
433 # scripts/config --disable CONFIG_KVM_BOOK3S_64
434 scripts/config --disable CONFIG_KVM_BOOK3S_64_HV
435 fi
436
4d8b6258
KS
437 if [ -f "init/Kconfig.suse" ] ; then
438 # Get values from git tag, eg. 'rpm-5.14.21-150400.24.108'
439 # Note: the "150400" type of SUSE major version is only present on tags
440 # from 2022 and newer (about half-way through SLE15SP3).
441 # This will not work as expected on earlier tags.
442 SLES_RELEASE="$(echo "${ktag}" | cut -d '-' -f 3 | cut -d'.' -f 1)"
443 scripts/config --set-val CONFIG_SUSE_VERSION $((10#"$(echo "${SLES_RELEASE}" | head -c 2)"))
444 scripts/config --set-val CONFIG_SUSE_PATCHLEVEL $((10#"$(echo "${SLES_RELEASE}" | head -c 4 | tail -c 2)"))
445
446 # Disable the renesas clk driver that has build issues,
447 # eg. drivers/clk/renesas/renesas-rzg2l-cpg.c:185:17: error: ‘clk’ undeclared (first use in this function)
448 scripts/config --disable CONFIG_CLK_RENESAS
449
450 # From drives/spi/spi-atmel.c
451 # ./include/linux/gpio/consumer.h:141:49: note: expected ‘struct gpio_desc *’ but argument is of type 'int'
452 scripts/config --disable CONFIG_SPI_ATMEL
453 scripts/config --disable CONFIG_SPI_AT91_USART
454 scripts/config --disable CONFIG_SPI_ATMEL_QUADSPI
455
456 # drivers/net/wireless/mediatek/mt76/mt7915/testmode.c: In function ‘mt7915_tm_set_wmm_qid’:
457 # drivers/net/wireless/mediatek/mt76/mt7915/testmode.c:176:30: error: ‘struct mt7915_vif’ has no member named ‘mt76’
458 scripts/config --disable CONFIG_WLAN_VENDOR_MEDIATEK
459
460 # drivers/net/wireless/microchip/wilc1000/cfg80211.c: In function ‘wilc_wfi_cfg_parse_ch_attr’:
461 # drivers/net/wireless/microchip/wilc1000/cfg80211.c:970:17: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
462 scripts/config --disable CONFIG_WLAN_VENDOR_MICROCHIP
463
464 # fs/f2fs/file.c: In function ‘punch_hole’:
465 # fs/f2fs/file.c:1093:49: error: ‘mapping’ undeclared (first use in this function)
466 scripts/config --disable CONFIG_F2FS_FS
467 fi
468
53b51a15 469 # oldnoconfig was renamed in 4.19
5a196804 470 if vergte "$kversion" "4.19"; then
53b51a15 471 update_conf_target="olddefconfig"
5a196804 472 else
53b51a15 473 update_conf_target="oldnoconfig"
5a196804
MJ
474 fi
475
476 # Fix 'defined(@array)' was removed from recent perl
477 if [ -f "kernel/timeconst.pl" ]; then
478 sed -i 's/defined(\@\(.*\))/@\1/' kernel/timeconst.pl
479 fi
480
481 # Fix syntax of inline assembly which is confused with C++11 raw strings on gcc >= 5
53b51a15 482 if [ "$HOSTCC" != "gcc-4.8" ]; then
5a196804
MJ
483 if [ -f "arch/x86/kvm/svm.c" ]; then
484 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/svm.c
485 fi
486
487 if [ -f "arch/x86/kvm/vmx.c" ]; then
488 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/vmx.c
489 fi
490 fi
491
a86b55bc
KS
492 if { verlt "${kversion}" "5.11"; } && { vergte "${kversion}" "4.10"; } ; then
493 # Binutils > 2.35 strips empty symbol tables, causing obltool to fail
494 # in certain cases when files are empty.
495 # @see https://gitlab.com/linux-kernel/stable/-/commit/1d489151e9f9d1647110277ff77282fe4d96d09b
496 #
497 # There doesn't seem to be any LD/AS/AR flags to control this behaviour,
498 # therefore patching tools/objtool/elf.c is attempted.
499 patch_linux_kernel 1d489151e9f9d1647110277ff77282fe4d96d09b
500 if { verlt "${kversion}" "4.18"; } ; then
501 patch_linux_kernel e81e0724432542af8d8c702c31e9d82f57b1ff31
502 fi
503 fi
504
9383502e 505 if { vergt "${selected_cc_version}" "7"; } && { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.17"; } ; then
a86b55bc
KS
506 # Builds fail due to -Werror=restrict in pager.o and str_error_r.o
507 if { verlt "${kversion}" "4.16"; } ; then
508 # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS
509 # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ad343a98e74e85aa91d844310e797f96fee6983b
510 patch_linux_kernel ad343a98e74e85aa91d844310e797f96fee6983b
511 fi
512 if { verlt "${kversion}" "4.17"; } ; then
513 # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS
514 # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=854e55ad289ef8888e7991f0ada85d5846f5afb9
515 patch_linux_kernel 854e55ad289ef8888e7991f0ada85d5846f5afb9
516 fi
517
ad23f63b
KS
518 fi
519
520 if { vergt "${selected_cc_version}" "9"; } && { verlt "${kversion}" "5.6"; } ; then
a86b55bc
KS
521 # Duplicate decalarations of __force_order
522 # @see https://gitlab.com/linux-kernel/stable/-/commit/df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc
523 # However, kaslr_64.c doesn't exit in 4.15, 4.16, it's named pagetable.c
524 if [ -f arch/x86/boot/compressed/pagetable.c ] ; then
525 sed -i '/^unsigned long __force_order;$/d' arch/x86/boot/compressed/pagetable.c
526 fi
9ae537d7
KS
527 if [ -f arch/x86/boot/compressed/kaslr_64.c ] ; then
528 patch_linux_kernel df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc
529 fi
530 fi
531
532 if { vergte "${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then
533 # In some cases, compiling net/bpfilter can fail with the following error:
534 # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory
535 # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1
536 #
537 # While the issue is potentially in a number of old versions, it has only
538 # been observed in v4.18-rt
539 #
540 patch_linux_kernel 303a339f30a9441c4695d3d2cc78f1b33cd959ff
541 fi
542
543 if { vergte "${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then
544 # In some cases, compiling net/bpfilter can fail with the following error:
545 # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory
546 # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1
547 #
548 # While the issue is potentially in a number of old versions, it has only
549 # been observed in v4.18-rt
550 #
551 patch_linux_kernel 303a339f30a9441c4695d3d2cc78f1b33cd959ff
a86b55bc
KS
552 fi
553
554 if { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ; then
555 # Some old kernels fail to build when make is too new
556 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
557 patch_linux_kernel 9564a8cf422d7b58f6e857e3546d346fa970191e
9383502e
MJ
558 fi
559
560 if { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.55"; } ; then
561 # Some old kernels fail to build when make is too new
562 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
563 patch_linux_kernel e82885490a611f2b75a6c27cd7bb09665c1740be
564 fi
565
566 if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ) || \
567 ( { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.56"; } ) ; then
568 # Some old kernels fail to build when make is too new
a86b55bc
KS
569 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
570 patch_linux_kernel 9feeb638cde083c737e295c0547f1b4f28e99583
571 fi
572
c0849ee1
MJ
573 if ( { vergte "${kversion}" "4.12"; } && { verlt "${kversion}" "4.20.17"; } ) || \
574 ( { vergte "${kversion}" "5.0"; } && { verlt "${kversion}" "5.0.12"; } ) ; then
7b15f9fa
KS
575 # Old kernels can fail to build while on newer host kernels with errors
576 # such as:
577 # In file included from scripts/selinux/genheaders/genheaders.c:19:
578 # ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map.
a86b55bc
KS
579 # @see https://gitlab.com/linux-kernel/stable/-/commit/dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e
580 #
581 patch_linux_kernel dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e
582 fi
583
c05bf06f
MJ
584 # Compatibility with binutils >= ~ 2.31
585 if { vergte "${kversion}" "3.19"; } && { verlt "${kversion}" "4.16"; } ; then
a86b55bc
KS
586 patch_linux_kernel b21ebf2fb4cde1618915a97cc773e287ff49173e
587 fi
c05bf06f
MJ
588 if { vergte "${kversion}" "3.17"; } && { verlt "${kversion}" "3.18.69"; } ; then
589 patch_linux_kernel edb9d2d5e647e7a8521b0d35f8452deb02dfd138
590 fi
591 if { vergte "${kversion}" "3.17"; } && { verlt "${kversion}" "3.18.100"; } ; then
592 patch_linux_kernel 3be6583f0b6f1bf1ee650ebf473d9dee36836527
593 patch_linux_kernel 12d839211d080f6a9c370398c41a260365d34c62
594 fi
595 if { vergte "${kversion}" "3.16"; } && { verlt "${kversion}" "3.16.82"; } ; then
596 patch_linux_kernel ad10e6d464796f2a481de4039a43b9cfca034e1c
a86b55bc
KS
597 fi
598
4d7942d1 599 if ( { vergte "${kversion}" "3.14"; } && { verlt "${kversion}" "4.4"; } ) ||
9ae537d7 600 ( { vergte "${kversion}" "4.8"; } && { verlt "${kversion}" "4.18"; } ); then
a86b55bc 601 # While the original motivation of this patch is for fixing builds using
4d7942d1 602 # clang, the same error occurs between linux >= 3.14 and < 4.4, and in
a86b55bc 603 # 4.15, 4.16.
9ae537d7 604 # For rt-linux, the error has been observed in 4.8, 4.11, and 4.13.
a86b55bc
KS
605 #
606 # This patch only partially applies due to changes in kernel/Makefile,
607 # so a supplementary patch is needed
608 #
609 # Without this patch, builds fail with
610 # Cannot find symbol for section 2: .text.
611 # kernel/elfcore.o: failed
612 #
613 # @see https://github.com/linuxppc/issues/issues/388
614 # @see https://lore.kernel.org/lkml/20201204165742.3815221-2-arnd@kernel.org/
615 #
616 patch_linux_kernel 6e7b64b9dd6d96537d816ea07ec26b7dedd397b9
617 if grep -q elfcore.o kernel/Makefile ; then
618 sed -i '/^.* += elfcore.o$/d' kernel/Makefile
619 fi
620 fi
9bbfa9db
MJ
621 # Same as above for the v4.4 branch
622 if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.257"; } ); then
623 patch_linux_kernel 3140b0740b31cc63cf2ee08bc3f746b423eb068d
624 if grep -q elfcore.o kernel/Makefile ; then
625 sed -i '/^.* += elfcore.o$/d' kernel/Makefile
626 fi
627 fi
a86b55bc 628
15ef760e 629 if { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ; then
53300bf0
MJ
630 # Kernels between v4.5 and v4.8 built with gcc >= 8 on arm will hit an
631 # assembler error :
15ef760e
MJ
632 #
633 # kernel/.tmp_fork.s: Assembler messages:
634 # kernel/.tmp_fork.s:1790: Error: .err encountered
53300bf0
MJ
635 #
636 # @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85745
15ef760e
MJ
637 #
638 patch_linux_kernel 9f73bd8bb445e0cbe4bcef6d4cfc788f1e184007
639 fi
640
c0849ee1
MJ
641 if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.136"; } ) ||
642 ( { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ); then
9ae537d7
KS
643 # Hacky patch to deal with the following build error:
644 # Cannot find symbol for section 7: .text.unlikely.
645 # kernel/kexec_file.o: failed
646 # make[1]: *** [scripts/Makefile.build:291: kernel/kexec_file.o] Error 1
647 #
648 # This error happens with binutils 2.36 and 2.37, but should probably not
649 # be an issue with binutils 2.38.
650 # @see https://github.com/linuxppc/issues/issues/388
651 # @see https://github.com/bminor/binutils-gdb/commit/c09c8b42021180eee9495bd50d8b35e683d3901b
652 #
653 # There is some sort of config (unspecified in past discussions) which
654 # provokes the error, and there was never a potential fix merged in
655 # this discussion, in part because the build systems of the kernel
656 # switched to objtool instead.
657 #
658 # @see https://lore.kernel.org/all/20210215162209.5e2a475b@gandalf.local.home/
659 #
660 sed -i 's/return txtname;/return shdr0->sh_size ? txtname : NULL;/' scripts/recordmcount.h
661
662 # After applying the above patch, the build continues but fails with
663 # head64.c:(.text.exit+0x5): undefined reference to `__gcov_exit'
664 #
665 scripts/config --disable CONFIG_GCOV_KERNEL
666 fi
667
c0849ee1
MJ
668 if { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.5.5"; } ; then
669 # drivers/staging/wilc1000/wilc_spi.c:123:34: error: storage size of ‘wilc1000_spi_ops’ isn’t known
670 patch_linux_kernel ce7b516f3f9e11fe4ee06fad0d7e853bb6e8f160
671 fi
672
53b51a15
MJ
673 # Newer binutils don't accept 3 operand 'cmp' instructions on ppc64
674 # Convert them to 'cmpw' which was previously done silently
675 if verlt "$kversion" "4.9"; then
53300bf0
MJ
676 find arch/powerpc/ -name "*.S" -print0 | xargs -0 sed -i "s/\(cmp\)\(\s\+[a-zA-Z0-9]\+,\s*[a-zA-Z0-9]\+,\s*[a-zA-Z0-9]\+\)/cmpw\2/"
677 find arch/powerpc/ -name "*.S" -print0 | xargs -0 sed -i "s/\(cmpli\)\(\s\+[a-zA-Z0-9]\+,\s*[a-zA-Z0-9]\+,\s*[a-zA-Z0-9]\+\)/cmplwi\2/"
678 sed -i "s/\$pie \-o \"\$ofile\"/\$pie --no-dynamic-linker -o \"\$ofile\"/" arch/powerpc/boot/wrapper
53b51a15
MJ
679 fi
680
9ae537d7
KS
681 if [ "$(scripts/config --state CONFIG_EXTCON_ADC_JACK)" != "n" ] &&
682 ( { vergte "${kversion}" "4.2"; } && { verlt "${kversion}" "4.12"; } ); then
683 # 73b6ecdb93e8e77752cae9077c424fcdc6f23c39 introduced a change where
684 # extcon-adc-jack.h has an incompatible pointer type.
685 # In GCC >= 5 this will provoke a warning and build failure.
686 # Eg.
687 # drivers/extcon/extcon-adc-jack.c: In function ‘adc_jack_probe’:
688 # drivers/extcon/extcon-adc-jack.c:111:64: error: passing argument 2 of ‘devm_extcon_dev_allocate’ from incompatible pointer type [-Werror=incompatible-pointer-types]
689 # make[2]: *** [scripts/Makefile.build:295: drivers/extcon/extcon-adc-jack.o] Error 1
690 #
691 # 8a522bf2d4f788306443d36b26b54f0aedcdfdbe (in 4.11) has a fix for this warning
692 #
693 patch_linux_kernel 8a522bf2d4f788306443d36b26b54f0aedcdfdbe
694 fi
695
9383502e
MJ
696 if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.15.2"; } ) || \
697 ( { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.18"; } ) ; then
698 # Fix an objtool Segmentation fault
699 patch_linux_kernel dd12561854824fd1f05baf2a1b794faa046e2425
700 fi
701
702 if { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.268"; } ; then
703 # Builds fail due to -Werror=use-after-free in sigchain.o and help.o
704 patch_linux_kernel e89bb266b710ce056f141f29f091fd468a4a8185
705 fi
706
5a196804
MJ
707 # Fix a typo in v2.6.36.x
708 if [ -f "arch/x86/kernel/entry_64.S" ]; then
709 sed -i 's/END(do_hypervisor_callback)/END(xen_do_hypervisor_callback)/' arch/x86/kernel/entry_64.S
710 fi
711
53b51a15
MJ
712 # Fix compiler switch in vdso Makefile for 2.6.36 to 2.6.36.2
713 if { vergte "$kversion" "2.6.36" && verlte "$kversion" "2.6.36.3"; }; then
714 sed -i 's/-m elf_x86_64/-m64/' arch/x86/vdso/Makefile
715 sed -i 's/-m elf_i386/-m32/' arch/x86/vdso/Makefile
716 fi
717
5a196804
MJ
718 # Fix kernel < 3.0 with gcc >= 4.7
719 if verlt "$kversion" "3.0"; then
720 sed -i '/linux\/compiler.h/a #include <linux\/linkage.h> \/* For asmregparm *\/' arch/x86/include/asm/ptrace.h
721 sed -i 's/extern long syscall_trace_enter/extern asmregparm long syscall_trace_enter/' arch/x86/include/asm/ptrace.h
722 sed -i 's/extern void syscall_trace_leave/extern asmregparm void syscall_trace_leave/' arch/x86/include/asm/ptrace.h
723 echo "header-y += linkage.h" >> include/linux/Kbuild
724 fi
725
a86b55bc
KS
726 if [ "${cross_arch}" = "powerpc" ] ; then
727 if { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.16"; } ; then
728 # Avoid register errors such as
729 # aes_generic.c:(.text+0x4e0): undefined reference to `_restgpr_31_x'
730 # @see https://gitlab.com/linux-kernel/stable/-/commit/148b974deea927f5dbb6c468af2707b488bfa2de
731 make_args+=(
732 CFLAGS_aes_generic.o=''
733 )
734 fi
735 fi
736
23ebcd4b
KS
737 if [ "$(scripts/config --state CONFIG_DEBUG_INFO_BTF)" == "y" ] &&
738 { vergte "${pahole_version}" "1.24"; } &&
cb36638c
KS
739 (
740 ( { vergte "${kversion}" "5.10"; } && { verlt "${kversion}" "6.0"; } ) || [[ "${ktag}" =~ .el8 ]]
741 ) ; then
23ebcd4b
KS
742 # Some kernels Eg. Ubuntu-hwe-5.13-5.13.0-52.59_20.04.1
743 # fail with the following error:
744 # BTFIDS vmlinux
745 # FAILED: load BTF from vmlinux: Invalid argument
746 #
747 # When CONFIG_DEBUG_INFO_BTF is set, certain versions of pahole require
748 # `--skip_encoding_btf_enum64` to be passed as the kernel doesn't define
749 # BTF_KIND_ENUM64.
750 #
751 # Introduced in 341dfcf8d78eaa3a2dc96dea06f0392eb2978364 (~v5.10)
752 # @see https://lore.kernel.org/bpf/20220825171620.cioobudss6ovyrkc@altlinux.org/t/
753 #
754 if [ -f "scripts/pahole-flags.sh" ] ; then
53300bf0 755 # shellcheck disable=SC2016
23ebcd4b
KS
756 sed -i 's/ -J ${PAHOLE_FLAGS} / -J ${PAHOLE_FLAGS} --skip_encoding_btf_enum64 /' scripts/link-vmlinux.sh
757 else
53300bf0 758 # shellcheck disable=SC2016
23ebcd4b 759 sed -i 's/ -J ${extra_paholeopt} / -J ${extra_paholeopt} --skip_encoding_btf_enum64 /' scripts/link-vmlinux.sh
cb36638c
KS
760 # Some older versions of RHEL don't have '${extra_paholeopt}'
761 sed -i 's/${PAHOLE} -J ${1}/${PAHOLE} -J --skip_encoding_btf_enum64 ${1}/' scripts/link-vmlinux.sh
23ebcd4b
KS
762 fi
763 fi
764
7e02032c 765 # GCC 4.8
53b51a15 766 if [ "$HOSTCC" == "gcc-4.8" ]; then
a86b55bc
KS
767 scripts/config --disable CONFIG_CC_STACKPROTECTOR_STRONG
768 scripts/config --disable CONFIG_PPC_OF_BOOT_TRAMPOLINE
53b51a15 769 fi
7e02032c
MJ
770
771 # Don't try to sign modules
53b51a15 772 scripts/config --disable CONFIG_MODULE_SIG
7e02032c 773
9ef18fa2 774 # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails
53b51a15 775 scripts/config --disable CONFIG_STACK_VALIDATION
5a196804
MJ
776
777 # Cause problems with inline assembly on i386
53b51a15 778 scripts/config --disable CONFIG_DEBUG_SECTION_MISMATCH
5a196804 779
1d5a0a3c 780 # Don't build samples, they are broken on some kernel releases
53b51a15
MJ
781 scripts/config --disable CONFIG_SAMPLES
782 scripts/config --disable CONFIG_BUILD_DOCSRC
c45b04df
MJ
783
784 # Disable kcov
53b51a15 785 scripts/config --disable CONFIG_KCOV
c45b04df
MJ
786
787 # Broken on some RT kernels
53b51a15 788 scripts/config --disable CONFIG_HYPERV
1d5a0a3c 789
1e300ea9 790 # Broken drivers
53b51a15
MJ
791 scripts/config --disable CONFIG_RAPIDIO_TSI721
792 scripts/config --disable CONFIG_SGI_XP
793 scripts/config --disable CONFIG_MFD_WM8994
794 scripts/config --disable CONFIG_DRM_RADEON
795 scripts/config --disable CONFIG_SND_SOC_WM5100
a9aea01c
KS
796 # More recent compiler optimizations (from gcc 8 onwards )expose build errors
797 # with netronome on older kernels.
798 # Observed in 4.11-rt, 4.15-4.17, 5.0-rt - 5.16-rt
799 # It seems easier to disable the driver than to attempt patching.
800 # Eg.
801 # In function ‘ur_load_imm_any’,
802 # inlined from ‘jeq_imm’ at drivers/net/ethernet/netronome/nfp/bpf/jit.c:3146:13:
803 # ./include/linux/compiler.h:350:45: error: call to ‘__compiletime_assert_1062’ declared with attribute error: FIELD_FIT: value too large for the field
804 # 350 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
805 #
806 scripts/config --disable CONFIG_NET_VENDOR_NETRONOME
807 # Eg.
808 # In function ‘memcpy’,
809 # inlined from ‘kszphy_get_strings’ at drivers/net/phy/micrel.c:664:3:
810 # ./include/linux/string.h:305:25: error: call to ‘__read_overflow2’ declared with attribute error: detected read beyond size of object passed as 2nd parameter
811 # 305 | __read_overflow2();
812 # | ^~~~~~~~~~~~~~~~~~
813 # make[3]: *** [scripts/Makefile.build:308: drivers/net/phy/micrel.o] Error 1
814 #
815 scripts/config --disable CONFIG_MICREL_PHY
816
1e300ea9 817
1ca3a3ee 818 # IGBVF won't build with recent gcc on 2.6.38.2
5a196804 819 if { vergte "$kversion" "2.6.37" && verlt "$kversion" "2.6.38"; }; then
53b51a15 820 scripts/config --disable CONFIG_IGBVF
5a196804 821 fi
9ef18fa2 822
c72041eb
MJ
823 # Don't fail the build on warnings
824 scripts/config --disable CONFIG_WERROR
a86b55bc 825 scripts/config --enable CONFIG_PPC_DISABLE_WERROR
c72041eb 826
51fa2ab3 827 # Set required options
53b51a15
MJ
828 scripts/config --enable CONFIG_TRACEPOINTS
829 scripts/config --enable CONFIG_KALLSYMS
830 scripts/config --enable CONFIG_HIGH_RES_TIMERS
831 scripts/config --enable CONFIG_KPROBES
832 scripts/config --enable CONFIG_FTRACE
833 scripts/config --enable CONFIG_BLK_DEV_IO_TRACE
834 scripts/config --enable CONFIG_KALLSYMS_ALL
835 scripts/config --enable CONFIG_HAVE_SYSCALL_TRACEPOINTS
836 scripts/config --enable CONFIG_PERF_EVENTS
837 scripts/config --enable CONFIG_EVENT_TRACING
838 scripts/config --enable CONFIG_KRETPROBES
7e02032c 839
1ca3a3ee
KS
840 # Starting in linux 6.9-rc1, TRIM_UNUSED_SYMS seems to be true
841 # for out build configurations. In earlier versions the default
842 # was set depending on the value of `COMPILE_TEST`.
843 # See upstream commit d2d5cba5d92c4ed23caa86228a1bc31b07e90fe9.
844 scripts/config --disable CONFIG_TRIM_UNUSED_KSYMS
845
a86b55bc
KS
846 if [ -n "${DEBUG}" ] ; then
847 cat .config
848 fi
5a196804 849
a86b55bc
KS
850 make "$update_conf_target" "${make_args[@]}"
851 make -j"$NPROC" "${make_args[@]}"
5a196804 852
80eda7bb 853 krelease=$(make -s kernelrelease "${make_args[@]}")
5a196804
MJ
854
855 # Save the kernel and modules
856 mkdir -p "$LINUX_INSTOBJ_DIR/boot"
a86b55bc
KS
857 make INSTALL_MOD_PATH="$LINUX_INSTOBJ_DIR" INSTALL_MOD_STRIP=1 modules_install "${make_args[@]}"
858 make INSTALL_MOD_PATH="$LINUX_INSTOBJ_DIR" INSTALL_PATH="$LINUX_INSTOBJ_DIR/boot" install "${make_args[@]}"
5a196804
MJ
859 rm -f "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/build"
860 ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
861 ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
862}
863
864
865extract_distro_headers() {
866
867 # Enter linux source dir
868 cd "${LINUX_SRCOBJ_DIR}"
869
870
871 # For RT kernels, copy version file
872 if [ -s localversion-rt ]; then
873 cp -a localversion-rt "${LINUX_HDROBJ_DIR}"
874 fi
875
876 # Copy all Makefile related stuff
877 find . -path './include/*' -prune \
878 -o -path './scripts/*' -prune -o -type f \
879 \( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
880 -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
881 -print | cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
882
883 # Copy base scripts and include dirs
884 cp -a scripts include "${LINUX_HDROBJ_DIR}"
7e02032c 885
5a196804
MJ
886 # Copy arch includes
887 (find arch -name include -type d -print0 | \
efdab1b7 888 xargs -0 -n1 -I '{}' find '{}' -type f) | \
5a196804
MJ
889 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
890
891 # Copy arch scripts
892 (find arch -name scripts -type d -print0 | \
efdab1b7 893 xargs -0 -n1 -I '{}' find '{}' -type f) | \
5a196804
MJ
894 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
895
896 # Cleanup scripts
897 rm -f "${LINUX_HDROBJ_DIR}/scripts/*.o"
898 rm -f "${LINUX_HDROBJ_DIR}/scripts/*/*.o"
899
04873cc3 900 # On powerpc 32bits this object is required to link modules
5a196804 901 if [ "${karch}" = "powerpc" ]; then
efdab1b7 902 if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte "${kversion}" "5.4"; then
10528202
MJ
903 :
904 else
04873cc3
MJ
905 cp -a --parents arch/powerpc/lib/crtsavres.[So] "${LINUX_HDROBJ_DIR}/"
906 fi
5a196804
MJ
907 fi
908
909 # On arm64 between 4.13 and 1.15 this object is required to build with ftrace support
910 if [ "${karch}" = "arm64" ]; then
911 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
912 cp -a --parents arch/arm64/kernel/ftrace-mod.[So] "${LINUX_HDROBJ_DIR}/"
913 fi
914 fi
915
ae365617
MJ
916 # On riscv with 5.14 the vsdo objects are required
917 if [ "${karch}" = "riscv" ] && \
918 ( { vergte "${kversion}" "5.14"; } && { verlt "${kversion}" "5.15"; } ); then
919 cp -a --parents arch/riscv/kernel/vdso/*.o "${LINUX_HDROBJ_DIR}/"
920 fi
921
5a196804
MJ
922 # Newer kernels need objtool to build modules when CONFIG_STACK_VALIDATION=y
923 if [ -f tools/objtool/objtool ]; then
924 cp -a --parents tools/objtool/objtool "${LINUX_HDROBJ_DIR}/"
925 fi
926
927 if [ -f "arch/x86/kernel/macros.s" ]; then
928 cp -a --parents arch/x86/kernel/macros.s "${LINUX_HDROBJ_DIR}/"
929 fi
930
931 # Copy modules related stuff, if available
932 if [ -s Module.symvers ]; then
933 cp Module.symvers "${LINUX_HDROBJ_DIR}"
934 fi
935
936 if [ -s System.map ]; then
937 cp System.map "${LINUX_HDROBJ_DIR}"
938 fi
939
940 if [ -s Module.markers ]; then
941 cp Module.markers "${LINUX_HDROBJ_DIR}"
942 fi
943
944 # Copy config file
945 cp .config "${LINUX_HDROBJ_DIR}"
946
947 # Make sure the Makefile and version.h have a matching timestamp so that
948 # external modules can be built
949 if [ -s "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h" ]; then
950 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h"
951 elif [ -s "${LINUX_HDROBJ_DIR}/include/linux/version.h" ]; then
952 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/linux/version.h"
953 else
954 echo "Missing version.h"
955 exit 1
956 fi
957 touch -r "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/generated/autoconf.h"
958
959 # Copy .config to include/config/auto.conf so "make prepare" is unnecessary.
780f6285
MJ
960 if [ ! -f "${LINUX_HDROBJ_DIR}/include/config/auto.conf" ]; then
961 cp "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/config/auto.conf"
962 fi
5a196804
MJ
963
964 # Finally clean the object files from the full source tree
965 make clean
966
967 # And regen the modules support files
a86b55bc 968 make modules_prepare "${make_args[@]}"
51fa2ab3 969
04873cc3 970 # On powerpc 32bits this object is required to link modules
d815b018 971 if [ "${karch}" = "powerpc" ]; then
efdab1b7 972 if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte "${kversion}" "5.4"; then
10528202
MJ
973 :
974 else
a86b55bc 975 make arch/powerpc/lib/crtsavres.o "${make_args[@]}"
04873cc3 976 fi
d815b018
MJ
977 fi
978
7a76a4d0 979 # On arm64 between 4.13 and 4.15 this object is required to build with ftrace support
a1ae361e 980 if [ "${karch}" = "arm64" ]; then
c236528e 981 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
a86b55bc 982 make arch/arm64/kernel/ftrace-mod.o "${make_args[@]}"
a1ae361e
MJ
983 fi
984 fi
985
7e02032c 986 # Version specific tasks
a1ae361e 987 case "$ktag" in
7e02032c
MJ
988 Ubuntu*)
989 # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code
a1ae361e 990 ABINUM="$(echo "$ktag" | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)')"
5a196804
MJ
991 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> include/generated/utsrelease.h
992 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> "${LINUX_HDROBJ_DIR}/include/generated/utsrelease.h"
7e02032c 993 ;;
4d8b6258
KS
994 rpm-*)
995 # Update the definition of UTS_RELEASE to match something akin to '5.14.21-150400.24.108-default'
996 if [ -f "init/Kconfig.suse" ] ; then
997 SLESVERSION="$(echo "${ktag}" | cut -d'-' -f 3)-default"
998 sed -E -i "s%^(#define UTS_RELEASE \"[\.a-z0-9]+)(\")%\1-${SLESVERSION}\2%g" include/generated/utsrelease.h
999 sed -E -i "s%^(#define UTS_RELEASE \"[\.a-z0-9]+)(\")%\1-${SLESVERSION}\2%g" "${LINUX_HDROBJ_DIR}/include/generated/utsrelease.h"
1000 fi
1001 ;;
7e02032c 1002 esac
7e02032c
MJ
1003}
1004
1005
339db64d
MJ
1006build_modules() {
1007
5a196804
MJ
1008 local kdir="$1"
1009 local outdir="$2"
1010 local kversion
1011
1012 kversion=$(make -C "$LINUX_HDROBJ_DIR" -s kernelversion)
339db64d 1013
339db64d 1014 # Enter lttng-modules source dir
5a196804 1015 cd "${MODULES_GIT_DIR}"
339db64d 1016
41a8bbc3
KS
1017 # Try to catch some compatibility problems by turning some
1018 # warnings into errors, but only on -rc kernels.
1019 pattern="rc[0-9]+$"
1020 if [[ "${kversion}" =~ ${pattern} ]] || [[ -n "${FAIL_ON_WARNINGS:-}" ]] ; then
1021 export KCFLAGS="${KCFLAGS} -Wall -Werror"
1022 # Without the verbose build, the warnings aren't visible.
1023 export V=1
1024 fi
1025
339db64d
MJ
1026 # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the
1027 # timekeeping subsystem. We want those build to fail.
5a196804
MJ
1028 if { vergte "$kversion" "3.10" && verlte "$kversion" "3.10.13"; } || \
1029 { vergte "$kversion" "3.11" && verlte "$kversion" "3.11.2"; }; then
339db64d
MJ
1030
1031 set +e
1032
1033 # Build modules
a86b55bc 1034 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}"
53300bf0 1035 ret=$?
339db64d 1036
5a196804
MJ
1037 set -e
1038
339db64d 1039 # We expect this build to fail, if it doesn't, fail the job.
0f71a764 1040 if [ "$ret" -eq 0 ]; then
e9b44189 1041 echo "This build should have failed."
339db64d
MJ
1042 exit 1
1043 fi
1044
1045 # We have to publish at least one file or the build will fail
5a196804 1046 echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${outdir}/BROKEN.txt.ko"
339db64d 1047
5a196804 1048 KERNELDIR="${kdir}" make clean
7e02032c 1049
339db64d
MJ
1050 else # Regular build
1051
1052 # Build modules against full kernel sources
a86b55bc 1053 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}"
339db64d
MJ
1054
1055 # Install modules to build dir
5a196804 1056 KERNELDIR="${kdir}" make INSTALL_MOD_PATH="${outdir}" modules_install
339db64d
MJ
1057
1058 # Clean build dir
5a196804 1059 KERNELDIR="${kdir}" make clean
339db64d
MJ
1060 fi
1061}
1062
1063
1064## MAIN ##
1065
f3d8604b
MJ
1066# Use all CPU cores
1067NPROC=$(nproc)
1068
5a196804
MJ
1069MODULES_GIT_DIR="${WORKSPACE}/src/lttng-modules"
1070LINUX_GIT_DIR="${WORKSPACE}/src/linux"
339db64d 1071
5a196804
MJ
1072LINUX_OBJ_DIR="${WORKSPACE}/linux"
1073LINUX_SRCOBJ_DIR="${LINUX_OBJ_DIR}/sources"
1074LINUX_HDROBJ_DIR="${LINUX_OBJ_DIR}/headers"
1075LINUX_INSTOBJ_DIR="${LINUX_OBJ_DIR}/install"
339db64d 1076
5a196804
MJ
1077MODULES_OUTPUT_KSRC_DIR="${WORKSPACE}/build/lttng-modules-ksrc"
1078MODULES_OUTPUT_KHDR_DIR="${WORKSPACE}/build/lttng-modules-khdr"
339db64d 1079
5a196804
MJ
1080LINUX_GIT_REF_REPO_DIR="$HOME/gitcache/linux-stable.git/"
1081
1082OBJ_STORE_URL="s3://jenkins"
1083
1084cd "$WORKSPACE"
1085
1086# Create build directories
1087mkdir -p "${LINUX_SRCOBJ_DIR}" "${LINUX_HDROBJ_DIR}" "${LINUX_INSTOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
1088
53300bf0 1089print_header "Clone LTTng-modules sources"
5a196804 1090git_clone_modules_sources
339db64d 1091
7e02032c 1092# Setup cross compile env if available
39961776 1093if [ "x${cross_arch}" != "x" ]; then
7e02032c
MJ
1094
1095 case "$cross_arch" in
1096 "armhf")
1097 karch="arm"
1098 cross_compile="arm-linux-gnueabihf-"
53b51a15 1099 vanilla_config="imx_v6_v7_defconfig"
7e02032c
MJ
1100 ubuntu_config="armhf-config.flavour.generic"
1101 ;;
1102
1103 "arm64")
1104 karch="arm64"
1105 cross_compile="aarch64-linux-gnu-"
53b51a15 1106 vanilla_config="defconfig"
7e02032c
MJ
1107 ubuntu_config="arm64-config.flavour.generic"
1108 ;;
1109
1110 "powerpc")
1111 karch="powerpc"
1112 cross_compile="powerpc-linux-gnu-"
e9b44189 1113 vanilla_config="ppc44x_defconfig"
7e02032c
MJ
1114 ubuntu_config="powerpc-config.flavour.powerpc-smp"
1115 ;;
1116
1117 "ppc64el")
1118 karch="powerpc"
1119 cross_compile="powerpc64le-linux-gnu-"
e9b44189 1120 vanilla_config="pseries_le_defconfig"
7e02032c
MJ
1121 ubuntu_config="ppc64el-config.flavour.generic"
1122 ;;
1123
ae365617
MJ
1124 "riscv64")
1125 karch="riscv"
1126 cross_compile="riscv64-linux-gnu-"
1127 vanilla_config="defconfig"
1128 ubuntu_config="riscv64-config.flavour.generic"
1129 ;;
1130
7e02032c 1131 *)
51fa2ab3 1132 echo "Unsupported cross arch $cross_arch"
7e02032c
MJ
1133 exit 1
1134 ;;
1135 esac
339db64d 1136
7e02032c
MJ
1137 # Export variables used by Kbuild for cross compilation
1138 export ARCH="${karch}"
1139 export CROSS_COMPILE="${cross_compile}"
339db64d 1140
7e02032c 1141# Set arch specific values if we are not cross compiling
39961776 1142elif [ "x${arch}" != "x" ]; then
e9b44189 1143
7e02032c 1144 case "$arch" in
16844a6d 1145 "i386")
7e02032c 1146 karch="x86"
1490b049 1147 vanilla_config="allmodconfig"
7e02032c
MJ
1148 ubuntu_config="i386-config.flavour.generic"
1149 ;;
1150
16844a6d 1151 "amd64")
7e02032c 1152 karch="x86"
1490b049 1153 vanilla_config="allmodconfig"
7e02032c
MJ
1154 ubuntu_config="amd64-config.flavour.generic"
1155 ;;
1156
1157 "armhf")
1158 karch="arm"
1490b049 1159 vanilla_config="allmodconfig"
7e02032c
MJ
1160 ubuntu_config="armhf-config.flavour.generic"
1161 ;;
1162
1163 "arm64")
1164 karch="arm64"
1490b049 1165 vanilla_config="allmodconfig"
7e02032c
MJ
1166 ubuntu_config="arm64-config.flavour.generic"
1167 ;;
1168
1169 "powerpc")
1170 karch="powerpc"
1490b049 1171 vanilla_config="allmodconfig"
7e02032c
MJ
1172 ubuntu_config="powerpc-config.flavour.powerpc-smp"
1173 ;;
1174
1175 "ppc64el")
1176 karch="powerpc"
1490b049 1177 vanilla_config="allmodconfig"
7e02032c
MJ
1178 ubuntu_config="ppc64el-config.flavour.generic"
1179 ;;
1180
ae365617
MJ
1181 "riscv64")
1182 karch="riscv"
1183 vanilla_config="allmodconfig"
1184 ubuntu_config="riscv64-config.flavour.generic"
1185 ;;
1186
7e02032c
MJ
1187 *)
1188 echo "Unsupported arch $arch"
1189 exit 1
1190 ;;
1191 esac
1192else
04873cc3 1193 echo "No arch or cross_arch specified"
7e02032c
MJ
1194 exit 1
1195fi
339db64d 1196
339db64d 1197
b214d997 1198
5a196804
MJ
1199# First get the kernel build from the object store, or build it, if it's
1200# not available.
339db64d 1201
53300bf0 1202set +x
5a196804
MJ
1203echo "# Setup endpoint
1204host_base = obj.internal.efficios.com
1205host_bucket = obj.internal.efficios.com
1206bucket_location = us-east-1
1207use_https = True
7e02032c 1208
5a196804
MJ
1209# Setup access keys
1210access_key = jenkins
1211secret_key = echo123456
339db64d 1212
5a196804
MJ
1213# Enable S3 v4 signature APIs
1214signature_v2 = False" > "$WORKSPACE/.s3cfg"
53300bf0 1215set -x
339db64d 1216
c45b04df 1217url_hash="$(echo -n "$kgitrepo" | md5sum | awk '{ print $1 }')"
5a196804 1218obj_name="linux.tar.bz2"
87445931 1219
efdab1b7 1220if [ -z "${cross_arch}" ]; then
53300bf0 1221 obj_url_prefix="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/$arch/native"
87445931 1222else
53300bf0 1223 obj_url_prefix="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/${cross_arch}"
87445931
MJ
1224fi
1225
5a196804 1226obj_url="$obj_url_prefix/$obj_name"
339db64d 1227
5a196804 1228set +e
e18b6e95
KS
1229# In s3cmd 2.3, the return code of get when an object does not exist (64)
1230# is different than in 2.2 (12). The return codes of 's3cmd info' are
1231# consistent between 2.2 and 2.3.
1232s3cmd -c "$WORKSPACE/.s3cfg" info "$obj_url"
5a196804
MJ
1233ret=$?
1234set -e
339db64d 1235
5a196804
MJ
1236case "$ret" in
1237 "0")
53300bf0 1238 print_header "Get sources and prebuilt kernel"
e18b6e95 1239 s3cmd -c "$WORKSPACE/.s3cfg" get "$obj_url"
5a196804 1240 extract_archive_obj
53300bf0
MJ
1241
1242 print_header "Select compiler and set build flags"
1243 select_compiler
1244 export_kbuild_flags
5a196804 1245 ;;
a1ae361e 1246
5a196804 1247 "12")
53300bf0 1248 print_header "Clone kernel sources"
339db64d 1249
5a196804
MJ
1250 # Build all the things and upload
1251 # then finish the module build...
339db64d 1252
5a196804
MJ
1253 git_clone_linux_sources
1254 git_export_linux_sources
339db64d 1255
53300bf0 1256 print_header "Select compiler and set build flags"
5a196804 1257 select_compiler
53300bf0 1258 export_kbuild_flags
a1ae361e 1259
5a196804 1260 ## PREPARE FULL LINUX SOURCE TREE
53300bf0 1261 print_header "Build kernel from source"
5a196804 1262 build_linux_kernel
d138fc44 1263
5a196804
MJ
1264 ## EXTRACT DISTRO STYLE KERNEL HEADERS / DEVEL
1265 extract_distro_headers
339db64d 1266
53300bf0 1267 print_header "Upload kernel to object storage"
5a196804 1268 tar_archive_obj
5a196804 1269 upload_archive_obj
339db64d 1270
5a196804 1271 ;;
339db64d 1272
5a196804
MJ
1273 *)
1274 echo "Unknown error? Abort"
1275 exit 1
1276 ;;
1277esac
339db64d 1278
a86b55bc 1279
7e02032c 1280## BUILD modules
5a196804
MJ
1281# Either we downloaded a pre-build kernel or we built it and uploaded
1282# the archive for future builds.
1283
1284cd "$WORKSPACE"
f3d8604b 1285
53300bf0 1286print_header "Build modules against full kernel sources"
5a196804 1287build_modules "${LINUX_SRCOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}"
f3d8604b 1288
53300bf0 1289print_header "Build modules against kernel headers"
5a196804 1290build_modules "${LINUX_HDROBJ_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
f3d8604b 1291
53300bf0
MJ
1292
1293print_header "Check for built modules in install directory"
1294
5a196804
MJ
1295# Make sure some modules were actually built
1296tree "${MODULES_OUTPUT_KSRC_DIR}"
1297if [ "x$(find "${MODULES_OUTPUT_KSRC_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
7e02032c
MJ
1298 echo "No modules built!"
1299 exit 1
1300fi
1301
5a196804
MJ
1302tree "${MODULES_OUTPUT_KHDR_DIR}"
1303if [ "x$(find "${MODULES_OUTPUT_KHDR_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
7e02032c
MJ
1304 echo "No modules built!"
1305 exit 1
1306fi
b214d997 1307
f3d8604b 1308# EOF
This page took 0.095877 seconds and 5 git commands to generate.