3 # Copyright (C) 2016-2023 Michael Jeanson <mjeanson@efficios.com>
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.
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.
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/>.
21 platform
=${platforms:-}
22 cross_arch
=${cross_arch:-}
24 kgitrepo
=${kgitrepo:-}
25 mversion
=${mversion:-}
26 mgitrepo
=${mgitrepo:-}
31 # Derive arch from label if it isn't set
32 if [ -z "${arch:-}" ] ; then
33 # Labels may be platform specific, eg. jammy-amd64, deb12-armhf
34 regex
='[[:alnum:]]+-([[:alnum:]]+)'
35 if [[ "${platform}" =~
${regex} ]] ; then
36 arch
="${BASH_REMATCH[1]}"
45 # Kernel version compare functions
47 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ]
51 # shellcheck disable=SC2015
52 [ "$1" = "$2" ] && return 1 || verlte
"$1" "$2"
56 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -n1)" ]
60 # shellcheck disable=SC2015
61 [ "$1" = "$2" ] && return 1 || vergte
"$1" "$2"
71 message_len
="${#message}"
72 padding_len
=$
(( (80 - (message_len
)) / 2 ))
74 printf '\n'; printf -- '#%.0s' {1.
.80}; printf '\n'
75 printf -- '-%.0s' {1.
.80}; printf '\n'
76 printf -- '#%.0s' $
(seq 1 $padding_len); printf '%s' "$message"; printf -- '#%.0s' $
(seq 1 $padding_len); printf '\n'
77 printf -- '-%.0s' {1.
.80}; printf '\n'
78 printf -- '#%.0s' {1.
.80}; printf '\n\n'
83 git_clone_modules_sources
() {
84 mkdir
-p "$MODULES_GIT_DIR"
86 # If the version starts with "refs/", checkout the specific git ref, otherwise treat it
88 if [ "${mversion:0:5}" = "refs/" ]; then
89 git clone
--no-tags --depth=1 "${mgitrepo}" "$MODULES_GIT_DIR"
90 (cd "$MODULES_GIT_DIR" && git fetch origin
"${mversion}" && git checkout FETCH_HEAD
)
92 git clone
--no-tags --depth=1 -b "${mversion}" "${mgitrepo}" "$MODULES_GIT_DIR"
96 # Checkout a shallow kernel tree of the specified tag
97 git_clone_linux_sources
() {
98 mkdir
-p "$LINUX_GIT_DIR"
99 git clone
--depth=1 -b "${ktag}" --reference "$LINUX_GIT_REF_REPO_DIR" "${kgitrepo}" "$LINUX_GIT_DIR"
103 # Export the kernel sources from the git repo
104 git_export_linux_sources
() {
106 git archive
"${ktag}" |
tar -x -C "$LINUX_SRCOBJ_DIR"
110 # Upload the tar archive to the object store
111 upload_archive_obj
() {
112 s3cmd
-c "$WORKSPACE/.s3cfg" put
"$WORKSPACE/$obj_name" "$obj_url_prefix/"
113 rm -f "$WORKSPACE/$obj_name"
117 extract_archive_obj
() {
118 tar -xf "$WORKSPACE/$obj_name" -C "$LINUX_OBJ_DIR" -I pbzip2
119 rm -f "$WORKSPACE/$obj_name"
125 tar -cf "$WORKSPACE/$obj_name" -I pbzip2 .
129 # List all GCC versions present in the PATH
133 IFS
=: read -r -a path_array
<<< "$PATH"
134 while read -r gcc
; do
136 done < <(find "${path_array[@]}" -maxdepth 1 -regex '.*/gcc-[0-9\.]+$' -printf '%f\n' |
sort -t- -k2 -V -r)
140 # Find the most recent GCC version supported by the kernel sources
143 # Enter linux source dir
144 cd "$LINUX_SRCOBJ_DIR"
146 kversion
=$
(make -s kernelversion
)
148 if { verlt
"$kversion" "4.4"; }; then
149 # Force gcc-4.8 for kernels before 4.4
150 selected_cc
='gcc-4.8'
151 selected_cc_version
=$
(echo "${selected_cc}" | cut
-d'-' -f2)
153 for cc
in $
(list_gccs
) ; do
154 if "${CROSS_COMPILE:-}${cc}" -I include
/ -D__LINUX_COMPILER_H -D__LINUX_COMPILER_TYPES_H -E include
/linux
/compiler-gcc.h
; then
155 cc_version
=$
(echo "${cc}" | cut
-d'-' -f2)
156 if { verlt
"${kversion}" "5.17"; } && { vergt "${cc_version}" "11"; } ; then
157 # Using gcc-12+ with '-Wuse-after-free' breaks the build of older
158 # kernels (in particular, objtool). Some releases on LTS
159 # branches between 4.x and 5.15 can be built with gcc-12.
160 # @see https://lore.kernel.org/lkml/20494.1643237814@turing-police/
161 # @see https://gitlab.com/linux-kernel/stable/-/commit/52a9dab6d892763b2a8334a568bd4e2c1a6fde66
165 selected_cc_version="$cc_version"
171 if [ -z "$selected_cc" ]; then
172 echo "Found no suitable compiler.
"
176 # lttng-modules requires gcc >= 5.1 for aarch64
177 # @see https://github.com/lttng/lttng-modules/commit/be06402dbdbea2f3394e60ec15c5d3356e2be416
178 if { verlt "${selected_cc_version}" "5.1"; } && [ "${cross_arch}" = "arm64
" ] ; then
179 echo "Building lttng-modules on aarch64 requires gcc
>= 5.1"
186 export_kbuild_flags() {
197 if [ "$selected_cc" != "gcc-4.8
" ]; then
198 # Older kernel Makefiles do not expect the compiler to default to PIE
205 _KCPPFLAGS+=(-fno-pie)
208 if { vergte "${selected_cc_version}" "10"; } && { verlt "${kversion}" "5.10"; } ; then
209 # gcc-10 changed the default from '-fcommon' to '-fno-common', which
210 # causes a linker failure. '-fcommon' can be set on the HOSTCFLAGS
211 # to avoid the issue.
212 # @see https://gitlab.com/linux-kernel/stable/-/commit/e33a814e772cdc36436c8c188d8c42d019fda639
213 _HOSTCFLAGS
+=(-fcommon)
216 if { verlt
"${kversion}" "5.14"; } && [ "${cross_arch:-}" == "armhf" ] ; then
217 # Work-around for producing instructions that aren't valid for the
218 # default architectures.
219 # Eg. Error: selected processor does not support `cpsid i' in ARM mode
220 _KCFLAGS
+=(-march=armv7-a
-mfpu=vfpv3-d16
)
221 _KCPPFLAGS
+=(-march=armv7-a
-mfpu=vfpv3-d16
)
224 if { vergt
"${selected_cc_version}" "8"; } && { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.17"; } ; then
225 # This was added to -Wall in gcc 9 but some kernels do not include the fixes
226 # @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88583
227 _KCFLAGS+=(-Wno-error=packed-not-aligned)
230 export KAFLAGS="${_KAFLAGS[*]}"
231 export KCFLAGS="${_KCFLAGS[*]}"
232 export KCPPFLAGS="${_KCPPFLAGS[*]}"
233 export HOSTCFLAGS="${_HOSTCFLAGS[*]}"
234 export CC="${CROSS_COMPILE:-}${selected_cc}"
235 export HOSTCC="${selected_cc}"
240 HOSTCFLAGS="${HOSTCFLAGS:-}"
243 if [ -n "${DEBUG}" ] ; then
250 patch_linux_kernel() {
254 # Show the title of the patch in the build log
255 git -C "${LINUX_GIT_REF_REPO_DIR}" show --oneline -s "${commit_hash}"
257 # Apply patch, don't fail if it doesn't apply cleanly
259 git -C "${LINUX_GIT_REF_REPO_DIR}" format-patch -n1 --stdout "${commit_hash}" | patch -p1
262 if [ "$?
" -gt 1 ] ; then
263 echo "Serious issue patching
"
268 build_linux_kernel() {
269 cd "$LINUX_SRCOBJ_DIR"
271 kversion=$(make -s kernelversion "${make_args[@]}")
272 pahole_version="$
(pahole
--version |
tr -d 'v')"
274 if { verlt "${kversion}" "3.3"; } && [ "${vanilla_config}" = "imx_v6_v7_defconfig
" ] ; then
275 # imx_v6_v7 didn't exist before 06965c39b4c63933fa0a1cde2237ef85477c5655
276 if { verlt "${kversion}" "3.2"; } ; then
277 vanilla_config='mx5_defconfig'
279 vanilla_config='mx51_defconfig'
283 if { verlt "${kversion}" "3.13"; } && [ "${vanilla_config}" = "pseries_le_defconfig
" ] ; then
284 # pseries_le_deconfig was introduced in f53e462e907cbaed29c49c0f10f5b8f614e1bf1d
285 vanilla_config='pseries_defconfig'
288 # Generate kernel configuration
291 if [ "${cross_arch}" = "powerpc
" ]; then
292 if vergte "${kversion}" "4.10"; then
293 echo "Ubuntu removed big endian powerpc configuration from kernel
>= 4.10. Don
't try to build it."
298 fakeroot debian/rules clean KW_DEFCONFIG_DIR=.
300 # Hack for kernel Ubuntu-hwe-5.8
301 # The debian/control file is produced by the clean target above, so this
302 # check needs to happen afterwards.
303 if [ ! -f "debian/compat" ] && ! grep -q debhelper-compat debian/control; then
304 echo "10" > "debian/compat"
307 # genconfigs check can fail in certain cases, eg. when a more recent
308 # compiler exposes kernel configuration flags which aren't
set in the
309 # Ubuntu annotations.
310 # In any case, the configuration will be updated with any missing values
311 # later in our build script.
312 fakeroot debian
/rules genconfigs KW_DEFCONFIG_DIR
=. do_skip_checks
=true
313 cp CONFIGS
/"${ubuntu_config}" .config
317 # Force 32bit build on i386, default is 64bit
318 if [ "$arch" = "i386" ]; then
322 make "${vanilla_config}" "${make_args[@]}"
326 if [ "${vanilla_config}" = "pseries_defconfig" ] && [ "${cross_arch}" = "ppc64el" ] ; then
327 # @see diff <(git show v3.13:arch/powerpc/configs/pseries_defconfig) <(git show v3.13:arch/powerpc/configs/pseries_le_defconfig)
328 scripts
/config
--enable CONFIG_CPU_LITTLE_ENDIAN
329 scripts
/config
--enable CONFIG_CMA
330 scripts
/config
--disable CONFIG_XMON_DEFAULT
331 # scripts/config --disable CONFIG_VIRTUALIZATION
332 # scripts/config --disable CONFIG_KVM_BOOK3S_64
333 scripts
/config
--disable CONFIG_KVM_BOOK3S_64_HV
336 # oldnoconfig was renamed in 4.19
337 if vergte
"$kversion" "4.19"; then
338 update_conf_target
="olddefconfig"
340 update_conf_target
="oldnoconfig"
343 # Fix 'defined(@array)' was removed from recent perl
344 if [ -f "kernel/timeconst.pl" ]; then
345 sed -i 's/defined(\@\(.*\))/@\1/' kernel
/timeconst.pl
348 # Fix syntax of inline assembly which is confused with C++11 raw strings on gcc >= 5
349 if [ "$HOSTCC" != "gcc-4.8" ]; then
350 if [ -f "arch/x86/kvm/svm.c" ]; then
351 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch
/x86
/kvm
/svm.c
354 if [ -f "arch/x86/kvm/vmx.c" ]; then
355 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch
/x86
/kvm
/vmx.c
359 if { verlt
"${kversion}" "5.11"; } && { vergte "${kversion}" "4.10"; } ; then
360 # Binutils > 2.35 strips empty symbol tables, causing obltool to fail
361 # in certain cases when files are empty.
362 # @see https://gitlab.com/linux-kernel/stable/-/commit/1d489151e9f9d1647110277ff77282fe4d96d09b
364 # There doesn't seem to be any LD/AS/AR flags to control this behaviour,
365 # therefore patching tools/objtool/elf.c is attempted.
366 patch_linux_kernel 1d489151e9f9d1647110277ff77282fe4d96d09b
367 if { verlt "${kversion}" "4.18"; } ; then
368 patch_linux_kernel e81e0724432542af8d8c702c31e9d82f57b1ff31
372 if { vergt "${selected_cc_version}" "7"; } && { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.17"; } ; then
373 # Builds fail due to -Werror=restrict in pager.o and str_error_r.o
374 if { verlt
"${kversion}" "4.16"; } ; then
375 # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS
376 # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ad343a98e74e85aa91d844310e797f96fee6983b
377 patch_linux_kernel ad343a98e74e85aa91d844310e797f96fee6983b
379 if { verlt
"${kversion}" "4.17"; } ; then
380 # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS
381 # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=854e55ad289ef8888e7991f0ada85d5846f5afb9
382 patch_linux_kernel
854e55ad289ef8888e7991f0ada85d5846f5afb9
387 if { vergt
"${selected_cc_version}" "9"; } && { verlt "${kversion}" "5.6"; } ; then
388 # Duplicate decalarations of __force_order
389 # @see https://gitlab.com/linux-kernel/stable/-/commit/df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc
390 # However, kaslr_64.c doesn't exit in 4.15, 4.16, it's named pagetable.c
391 if [ -f arch/x86/boot/compressed/pagetable.c ] ; then
392 sed -i '/^unsigned long __force_order;$/d' arch/x86/boot/compressed/pagetable.c
394 if [ -f arch/x86/boot/compressed/kaslr_64.c ] ; then
395 patch_linux_kernel df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc
399 if { vergte "${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then
400 # In some cases, compiling net/bpfilter can fail with the following error:
401 # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory
402 # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1
404 # While the issue is potentially in a number of old versions, it has only
405 # been observed in v4.18-rt
407 patch_linux_kernel
303a339f30a9441c4695d3d2cc78f1b33cd959ff
410 if { vergte
"${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then
411 # In some cases, compiling net/bpfilter can fail with the following error:
412 # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory
413 # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1
415 # While the issue is potentially in a number of old versions, it has only
416 # been observed in v4.18-rt
418 patch_linux_kernel 303a339f30a9441c4695d3d2cc78f1b33cd959ff
421 if { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ; then
422 # Some old kernels fail to build when make is too new
423 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
424 patch_linux_kernel
9564a8cf422d7b58f6e857e3546d346fa970191e
427 if { vergte
"${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.55"; } ; then
428 # Some old kernels fail to build when make is too new
429 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
430 patch_linux_kernel e82885490a611f2b75a6c27cd7bb09665c1740be
433 if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ) || \
434 ( { vergte
"${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.56"; } ) ; then
435 # Some old kernels fail to build when make is too new
436 # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583
437 patch_linux_kernel 9feeb638cde083c737e295c0547f1b4f28e99583
440 if ( { vergte "${kversion}" "4.12"; } && { verlt "${kversion}" "4.20.17"; } ) || \
441 ( { vergte
"${kversion}" "5.0"; } && { verlt "${kversion}" "5.0.12"; } ) ; then
442 # Old kernels can fail to build while on newer host kernels with errors
444 # In file included from scripts/selinux/genheaders/genheaders.c:19:
445 # ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map.
446 # @see https://gitlab.com/linux-kernel/stable/-/commit/dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e
448 patch_linux_kernel dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e
451 if { vergte "${kversion}" "3.18"; } && { verlt "${kversion}" "4.16"; } ; then
452 # Compatibility with binutils >= ~ 2.31
453 patch_linux_kernel b21ebf2fb4cde1618915a97cc773e287ff49173e
456 # The above patch only partially applies linux 3.17, and has been, so a
457 # rebased version is used instead.
458 if { vergte
"${kversion}" "3.17"; } && { verlt "${kversion}" "3.18"; } ; then
459 cat <<'EOF' | patch -p1
460 diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
461 index 48598105..0652c5b6 100644
462 --- a/arch/x86/kernel/machine_kexec_64.c
463 +++ b/arch/x86/kernel/machine_kexec_64.c
464 @@ -516,6 +516,7 @@ int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
468 + case R_X86_64_PLT32:
469 value -= (u64)address;
470 *(u32 *)location = value;
472 diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
473 index e69f9882..7c6bc9fe 100644
474 --- a/arch/x86/kernel/module.c
475 +++ b/arch/x86/kernel/module.c
476 @@ -180,6 +180,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
480 + case R_X86_64_PLT32:
484 diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
485 index bbb1d225..8deeacbc 100644
486 --- a/arch/x86/tools/relocs.c
487 +++ b/arch/x86/tools/relocs.c
488 @@ -763,6 +763,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
492 + case R_X86_64_PLT32:
494 * NONE can be ignored and PC relative relocations don't
495 * need to be adjusted.
499 if ( { vergte "${kversion}" "3.14"; } && { verlt "${kversion}" "4.4"; } ) ||
500 ( { vergte
"${kversion}" "4.8"; } && { verlt "${kversion}" "4.18"; } ); then
501 # While the original motivation of this patch is for fixing builds using
502 # clang, the same error occurs between linux >= 3.14 and < 4.4, and in
504 # For rt-linux, the error has been observed in 4.8, 4.11, and 4.13.
506 # This patch only partially applies due to changes in kernel/Makefile,
507 # so a supplementary patch is needed
509 # Without this patch, builds fail with
510 # Cannot find symbol for section 2: .text.
511 # kernel/elfcore.o: failed
513 # @see https://github.com/linuxppc/issues/issues/388
514 # @see https://lore.kernel.org/lkml/20201204165742.3815221-2-arnd@kernel.org/
516 patch_linux_kernel 6e7b64b9dd6d96537d816ea07ec26b7dedd397b9
517 if grep -q elfcore.o kernel/Makefile ; then
518 sed -i '/^.* += elfcore.o$/d' kernel/Makefile
521 # Same as above for the v4.4 branch
522 if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.257"; } ); then
523 patch_linux_kernel
3140b0740b31cc63cf2ee08bc3f746b423eb068d
524 if grep -q elfcore.o kernel
/Makefile
; then
525 sed -i '/^.* += elfcore.o$/d' kernel
/Makefile
529 if { vergte
"${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ; then
530 # Kernels between v4.5 and v4.8 built with gcc >= 8 on arm will hit an
533 # kernel/.tmp_fork.s: Assembler messages:
534 # kernel/.tmp_fork.s:1790: Error: .err encountered
536 # @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85745
538 patch_linux_kernel 9f73bd8bb445e0cbe4bcef6d4cfc788f1e184007
541 if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.136"; } ) ||
542 ( { vergte
"${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ); then
543 # Hacky patch to deal with the following build error:
544 # Cannot find symbol for section 7: .text.unlikely.
545 # kernel/kexec_file.o: failed
546 # make[1]: *** [scripts/Makefile.build:291: kernel/kexec_file.o] Error 1
548 # This error happens with binutils 2.36 and 2.37, but should probably not
549 # be an issue with binutils 2.38.
550 # @see https://github.com/linuxppc/issues/issues/388
551 # @see https://github.com/bminor/binutils-gdb/commit/c09c8b42021180eee9495bd50d8b35e683d3901b
553 # There is some sort of config (unspecified in past discussions) which
554 # provokes the error, and there was never a potential fix merged in
555 # this discussion, in part because the build systems of the kernel
556 # switched to objtool instead.
558 # @see https://lore.kernel.org/all/20210215162209.5e2a475b@gandalf.local.home/
560 sed -i 's/return txtname;/return shdr0->sh_size ? txtname : NULL;/' scripts/recordmcount.h
562 # After applying the above patch, the build continues but fails with
563 # head64.c:(.text.exit+0x5): undefined reference to `__gcov_exit'
565 scripts/config --disable CONFIG_GCOV_KERNEL
568 if { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.5.5"; } ; then
569 # drivers/staging/wilc1000/wilc_spi.c:123:34: error: storage size of ‘wilc1000_spi_ops’ isn’t known
570 patch_linux_kernel ce7b516f3f9e11fe4ee06fad0d7e853bb6e8f160
573 # Newer binutils don't accept 3 operand 'cmp' instructions on ppc64
574 # Convert them to 'cmpw' which was previously done silently
575 if verlt
"$kversion" "4.9"; then
576 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/"
577 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/"
578 sed -i "s/\$pie \-o \"\$ofile\"/\$pie --no-dynamic-linker -o \"\$ofile\"/" arch
/powerpc
/boot
/wrapper
581 if [ "$(scripts/config --state CONFIG_EXTCON_ADC_JACK)" != "n" ] &&
582 ( { vergte
"${kversion}" "4.2"; } && { verlt "${kversion}" "4.12"; } ); then
583 # 73b6ecdb93e8e77752cae9077c424fcdc6f23c39 introduced a change where
584 # extcon-adc-jack.h has an incompatible pointer type.
585 # In GCC >= 5 this will provoke a warning and build failure.
587 # drivers/extcon/extcon-adc-jack.c: In function ‘adc_jack_probe’:
588 # 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]
589 # make[2]: *** [scripts/Makefile.build:295: drivers/extcon/extcon-adc-jack.o] Error 1
591 # 8a522bf2d4f788306443d36b26b54f0aedcdfdbe (in 4.11) has a fix for this warning
593 patch_linux_kernel 8a522bf2d4f788306443d36b26b54f0aedcdfdbe
596 if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.15.2"; } ) || \
597 ( { vergte
"${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.18"; } ) ; then
598 # Fix an objtool Segmentation fault
599 patch_linux_kernel dd12561854824fd1f05baf2a1b794faa046e2425
602 if { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.268"; } ; then
603 # Builds fail due to -Werror=use-after-free in sigchain.o and help.o
604 patch_linux_kernel e89bb266b710ce056f141f29f091fd468a4a8185
607 # Fix a typo in v2.6.36.x
608 if [ -f "arch/x86/kernel/entry_64.S" ]; then
609 sed -i 's/END(do_hypervisor_callback)/END(xen_do_hypervisor_callback)/' arch
/x86
/kernel
/entry_64.S
612 # Fix compiler switch in vdso Makefile for 2.6.36 to 2.6.36.2
613 if { vergte
"$kversion" "2.6.36" && verlte
"$kversion" "2.6.36.3"; }; then
614 sed -i 's/-m elf_x86_64/-m64/' arch
/x86
/vdso
/Makefile
615 sed -i 's/-m elf_i386/-m32/' arch
/x86
/vdso
/Makefile
618 # Fix kernel < 3.0 with gcc >= 4.7
619 if verlt
"$kversion" "3.0"; then
620 sed -i '/linux\/compiler.h/a #include <linux\/linkage.h> \/* For asmregparm *\/' arch
/x86
/include
/asm
/ptrace.h
621 sed -i 's/extern long syscall_trace_enter/extern asmregparm long syscall_trace_enter/' arch
/x86
/include
/asm
/ptrace.h
622 sed -i 's/extern void syscall_trace_leave/extern asmregparm void syscall_trace_leave/' arch
/x86
/include
/asm
/ptrace.h
623 echo "header-y += linkage.h" >> include
/linux
/Kbuild
626 if [ "${cross_arch}" = "powerpc" ] ; then
627 if { vergte
"${kversion}" "4.15"; } && { verlt "${kversion}" "4.16"; } ; then
628 # Avoid register errors such as
629 # aes_generic.c:(.text+0x4e0): undefined reference to `_restgpr_31_x'
630 # @see https://gitlab.com/linux-kernel/stable/-/commit/148b974deea927f5dbb6c468af2707b488bfa2de
632 CFLAGS_aes_generic.o=''
637 if [ "$
(scripts
/config
--state CONFIG_DEBUG_INFO_BTF
)" == "y
" ] &&
638 { vergte "${pahole_version}" "1.24"; } &&
639 { vergte "${kversion}" "5.10"; } && { verlt "${kversion}" "6.0"; } ;then
640 # Some kernels Eg. Ubuntu-hwe-5.13-5.13.0-52.59_20.04.1
641 # fail with the following error:
643 # FAILED: load BTF from vmlinux: Invalid argument
645 # When CONFIG_DEBUG_INFO_BTF is set, certain versions of pahole require
646 # `--skip_encoding_btf_enum64` to be passed as the kernel doesn't define
649 # Introduced in 341dfcf8d78eaa3a2dc96dea06f0392eb2978364 (~v5.10)
650 # @see https://lore.kernel.org/bpf/20220825171620.cioobudss6ovyrkc@altlinux.org/t/
652 if [ -f "scripts/pahole-flags.sh" ] ; then
653 # shellcheck disable=SC2016
654 sed -i 's/ -J ${PAHOLE_FLAGS} / -J ${PAHOLE_FLAGS} --skip_encoding_btf_enum64 /' scripts
/link-vmlinux.sh
656 # shellcheck disable=SC2016
657 sed -i 's/ -J ${extra_paholeopt} / -J ${extra_paholeopt} --skip_encoding_btf_enum64 /' scripts
/link-vmlinux.sh
662 if [ "$HOSTCC" == "gcc-4.8" ]; then
663 scripts
/config
--disable CONFIG_CC_STACKPROTECTOR_STRONG
664 scripts
/config
--disable CONFIG_PPC_OF_BOOT_TRAMPOLINE
667 # Don't try to sign modules
668 scripts
/config
--disable CONFIG_MODULE_SIG
670 # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails
671 scripts
/config
--disable CONFIG_STACK_VALIDATION
673 # Cause problems with inline assembly on i386
674 scripts
/config
--disable CONFIG_DEBUG_SECTION_MISMATCH
676 # Don't build samples, they are broken on some kernel releases
677 scripts
/config
--disable CONFIG_SAMPLES
678 scripts
/config
--disable CONFIG_BUILD_DOCSRC
681 scripts
/config
--disable CONFIG_KCOV
683 # Broken on some RT kernels
684 scripts
/config
--disable CONFIG_HYPERV
687 scripts
/config
--disable CONFIG_RAPIDIO_TSI721
688 scripts
/config
--disable CONFIG_SGI_XP
689 scripts
/config
--disable CONFIG_MFD_WM8994
690 scripts
/config
--disable CONFIG_DRM_RADEON
691 scripts
/config
--disable CONFIG_SND_SOC_WM5100
692 # More recent compiler optimizations (from gcc 8 onwards )expose build errors
693 # with netronome on older kernels.
694 # Observed in 4.11-rt, 4.15-4.17, 5.0-rt - 5.16-rt
695 # It seems easier to disable the driver than to attempt patching.
697 # In function ‘ur_load_imm_any’,
698 # inlined from ‘jeq_imm’ at drivers/net/ethernet/netronome/nfp/bpf/jit.c:3146:13:
699 # ./include/linux/compiler.h:350:45: error: call to ‘__compiletime_assert_1062’ declared with attribute error: FIELD_FIT: value too large for the field
700 # 350 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
702 scripts
/config
--disable CONFIG_NET_VENDOR_NETRONOME
704 # In function ‘memcpy’,
705 # inlined from ‘kszphy_get_strings’ at drivers/net/phy/micrel.c:664:3:
706 # ./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
707 # 305 | __read_overflow2();
708 # | ^~~~~~~~~~~~~~~~~~
709 # make[3]: *** [scripts/Makefile.build:308: drivers/net/phy/micrel.o] Error 1
711 scripts
/config
--disable CONFIG_MICREL_PHY
714 # IGBVF won't build with recent gcc on 2.6.38.x
715 if { vergte
"$kversion" "2.6.37" && verlt
"$kversion" "2.6.38"; }; then
716 scripts
/config
--disable CONFIG_IGBVF
719 # Don't fail the build on warnings
720 scripts
/config
--disable CONFIG_WERROR
721 scripts
/config
--enable CONFIG_PPC_DISABLE_WERROR
723 # Set required options
724 scripts
/config
--enable CONFIG_TRACEPOINTS
725 scripts
/config
--enable CONFIG_KALLSYMS
726 scripts
/config
--enable CONFIG_HIGH_RES_TIMERS
727 scripts
/config
--enable CONFIG_KPROBES
728 scripts
/config
--enable CONFIG_FTRACE
729 scripts
/config
--enable CONFIG_BLK_DEV_IO_TRACE
730 scripts
/config
--enable CONFIG_KALLSYMS_ALL
731 scripts
/config
--enable CONFIG_HAVE_SYSCALL_TRACEPOINTS
732 scripts
/config
--enable CONFIG_PERF_EVENTS
733 scripts
/config
--enable CONFIG_EVENT_TRACING
734 scripts
/config
--enable CONFIG_KRETPROBES
736 if [ -n "${DEBUG}" ] ; then
740 make "$update_conf_target" "${make_args[@]}"
741 make -j"$NPROC" "${make_args[@]}"
743 krelease
=$
(make -s kernelrelease
"${make_args[@]}")
745 # Save the kernel and modules
746 mkdir
-p "$LINUX_INSTOBJ_DIR/boot"
747 make INSTALL_MOD_PATH
="$LINUX_INSTOBJ_DIR" INSTALL_MOD_STRIP
=1 modules_install
"${make_args[@]}"
748 make INSTALL_MOD_PATH
="$LINUX_INSTOBJ_DIR" INSTALL_PATH
="$LINUX_INSTOBJ_DIR/boot" install "${make_args[@]}"
749 rm -f "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/build"
750 ln -s ..
/..
/..
/..
/sources
"$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
751 ln -s ..
/..
/..
/..
/sources
"$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
755 extract_distro_headers
() {
757 # Enter linux source dir
758 cd "${LINUX_SRCOBJ_DIR}"
761 # For RT kernels, copy version file
762 if [ -s localversion-rt
]; then
763 cp -a localversion-rt
"${LINUX_HDROBJ_DIR}"
766 # Copy all Makefile related stuff
767 find .
-path './include/*' -prune \
768 -o -path './scripts/*' -prune -o -type f \
769 \
( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
770 -name '*.sh' -o -name '*.pl' -o -name '*.lds' \
) \
771 -print |
cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
773 # Copy base scripts and include dirs
774 cp -a scripts include
"${LINUX_HDROBJ_DIR}"
777 (find arch
-name include
-type d
-print0 | \
778 xargs -0 -n1 -I '{}' find '{}' -type f
) | \
779 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
782 (find arch
-name scripts
-type d
-print0 | \
783 xargs -0 -n1 -I '{}' find '{}' -type f
) | \
784 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
787 rm -f "${LINUX_HDROBJ_DIR}/scripts/*.o"
788 rm -f "${LINUX_HDROBJ_DIR}/scripts/*/*.o"
790 # On powerpc 32bits this object is required to link modules
791 if [ "${karch}" = "powerpc" ]; then
792 if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte
"${kversion}" "5.4"; then
795 cp -a --parents arch
/powerpc
/lib
/crtsavres.
[So
] "${LINUX_HDROBJ_DIR}/"
799 # On arm64 between 4.13 and 1.15 this object is required to build with ftrace support
800 if [ "${karch}" = "arm64" ]; then
801 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
802 cp -a --parents arch
/arm64
/kernel
/ftrace-mod.
[So
] "${LINUX_HDROBJ_DIR}/"
806 # Newer kernels need objtool to build modules when CONFIG_STACK_VALIDATION=y
807 if [ -f tools
/objtool
/objtool
]; then
808 cp -a --parents tools
/objtool
/objtool
"${LINUX_HDROBJ_DIR}/"
811 if [ -f "arch/x86/kernel/macros.s" ]; then
812 cp -a --parents arch
/x86
/kernel
/macros.s
"${LINUX_HDROBJ_DIR}/"
815 # Copy modules related stuff, if available
816 if [ -s Module.symvers
]; then
817 cp Module.symvers
"${LINUX_HDROBJ_DIR}"
820 if [ -s System.map
]; then
821 cp System.map
"${LINUX_HDROBJ_DIR}"
824 if [ -s Module.markers
]; then
825 cp Module.markers
"${LINUX_HDROBJ_DIR}"
829 cp .config
"${LINUX_HDROBJ_DIR}"
831 # Make sure the Makefile and version.h have a matching timestamp so that
832 # external modules can be built
833 if [ -s "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h" ]; then
834 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h"
835 elif [ -s "${LINUX_HDROBJ_DIR}/include/linux/version.h" ]; then
836 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/linux/version.h"
838 echo "Missing version.h"
841 touch -r "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/generated/autoconf.h"
843 # Copy .config to include/config/auto.conf so "make prepare" is unnecessary.
844 if [ ! -f "${LINUX_HDROBJ_DIR}/include/config/auto.conf" ]; then
845 cp "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/config/auto.conf"
848 # Finally clean the object files from the full source tree
851 # And regen the modules support files
852 make modules_prepare
"${make_args[@]}"
854 # On powerpc 32bits this object is required to link modules
855 if [ "${karch}" = "powerpc" ]; then
856 if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte
"${kversion}" "5.4"; then
859 make arch
/powerpc
/lib
/crtsavres.o
"${make_args[@]}"
863 # On arm64 between 4.13 and 4.15 this object is required to build with ftrace support
864 if [ "${karch}" = "arm64" ]; then
865 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
866 make arch
/arm64
/kernel
/ftrace-mod.o
"${make_args[@]}"
870 # Version specific tasks
873 # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code
874 ABINUM
="$(echo "$ktag" | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)')"
875 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> include
/generated
/utsrelease.h
876 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> "${LINUX_HDROBJ_DIR}/include/generated/utsrelease.h"
888 kversion
=$
(make -C "$LINUX_HDROBJ_DIR" -s kernelversion
)
890 # Try to catch some compatibility problems by turning some
891 # warnings into errors.
892 #export KCFLAGS="$KCFLAGS -Wall -Werror"
894 # Enter lttng-modules source dir
895 cd "${MODULES_GIT_DIR}"
897 # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the
898 # timekeeping subsystem. We want those build to fail.
899 if { vergte
"$kversion" "3.10" && verlte
"$kversion" "3.10.13"; } || \
900 { vergte
"$kversion" "3.11" && verlte
"$kversion" "3.11.2"; }; then
905 KERNELDIR
="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}"
910 # We expect this build to fail, if it doesn't, fail the job.
911 if [ "$ret" -eq 0 ]; then
912 echo "This build should have failed."
916 # We have to publish at least one file or the build will fail
917 echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${outdir}/BROKEN.txt.ko"
919 KERNELDIR
="${kdir}" make clean
923 # Build modules against full kernel sources
924 KERNELDIR
="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}"
926 # Install modules to build dir
927 KERNELDIR
="${kdir}" make INSTALL_MOD_PATH
="${outdir}" modules_install
930 KERNELDIR
="${kdir}" make clean
940 MODULES_GIT_DIR
="${WORKSPACE}/src/lttng-modules"
941 LINUX_GIT_DIR
="${WORKSPACE}/src/linux"
943 LINUX_OBJ_DIR
="${WORKSPACE}/linux"
944 LINUX_SRCOBJ_DIR
="${LINUX_OBJ_DIR}/sources"
945 LINUX_HDROBJ_DIR
="${LINUX_OBJ_DIR}/headers"
946 LINUX_INSTOBJ_DIR
="${LINUX_OBJ_DIR}/install"
948 MODULES_OUTPUT_KSRC_DIR
="${WORKSPACE}/build/lttng-modules-ksrc"
949 MODULES_OUTPUT_KHDR_DIR
="${WORKSPACE}/build/lttng-modules-khdr"
951 LINUX_GIT_REF_REPO_DIR
="$HOME/gitcache/linux-stable.git/"
953 OBJ_STORE_URL
="s3://jenkins"
957 # Create build directories
958 mkdir
-p "${LINUX_SRCOBJ_DIR}" "${LINUX_HDROBJ_DIR}" "${LINUX_INSTOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
960 print_header
"Clone LTTng-modules sources"
961 git_clone_modules_sources
963 # Setup cross compile env if available
964 if [ "x${cross_arch}" != "x" ]; then
966 case "$cross_arch" in
969 cross_compile
="arm-linux-gnueabihf-"
970 vanilla_config
="imx_v6_v7_defconfig"
971 ubuntu_config
="armhf-config.flavour.generic"
976 cross_compile
="aarch64-linux-gnu-"
977 vanilla_config
="defconfig"
978 ubuntu_config
="arm64-config.flavour.generic"
983 cross_compile
="powerpc-linux-gnu-"
984 vanilla_config
="ppc44x_defconfig"
985 ubuntu_config
="powerpc-config.flavour.powerpc-smp"
990 cross_compile
="powerpc64le-linux-gnu-"
991 vanilla_config
="pseries_le_defconfig"
992 ubuntu_config
="ppc64el-config.flavour.generic"
996 echo "Unsupported cross arch $cross_arch"
1001 # Export variables used by Kbuild for cross compilation
1002 export ARCH
="${karch}"
1003 export CROSS_COMPILE
="${cross_compile}"
1005 # Set arch specific values if we are not cross compiling
1006 elif [ "x${arch}" != "x" ]; then
1011 vanilla_config
="allmodconfig"
1012 ubuntu_config
="i386-config.flavour.generic"
1017 vanilla_config
="allmodconfig"
1018 ubuntu_config
="amd64-config.flavour.generic"
1023 vanilla_config
="allmodconfig"
1024 ubuntu_config
="armhf-config.flavour.generic"
1029 vanilla_config
="allmodconfig"
1030 ubuntu_config
="arm64-config.flavour.generic"
1035 vanilla_config
="allmodconfig"
1036 ubuntu_config
="powerpc-config.flavour.powerpc-smp"
1041 vanilla_config
="allmodconfig"
1042 ubuntu_config
="ppc64el-config.flavour.generic"
1046 echo "Unsupported arch $arch"
1051 echo "No arch or cross_arch specified"
1057 # First get the kernel build from the object store, or build it, if it's
1061 echo "# Setup endpoint
1062 host_base = obj.internal.efficios.com
1063 host_bucket = obj.internal.efficios.com
1064 bucket_location = us-east-1
1068 access_key = jenkins
1069 secret_key = echo123456
1071 # Enable S3 v4 signature APIs
1072 signature_v2 = False" > "$WORKSPACE/.s3cfg"
1075 url_hash
="$(echo -n "$kgitrepo" | md5sum | awk '{ print $1 }')"
1076 obj_name
="linux.tar.bz2"
1078 if [ -z "${cross_arch}" ]; then
1079 obj_url_prefix
="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/$arch/native"
1081 obj_url_prefix
="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/${cross_arch}"
1084 obj_url
="$obj_url_prefix/$obj_name"
1087 # In s3cmd 2.3, the return code of get when an object does not exist (64)
1088 # is different than in 2.2 (12). The return codes of 's3cmd info' are
1089 # consistent between 2.2 and 2.3.
1090 s3cmd
-c "$WORKSPACE/.s3cfg" info
"$obj_url"
1096 print_header
"Get sources and prebuilt kernel"
1097 s3cmd
-c "$WORKSPACE/.s3cfg" get
"$obj_url"
1100 print_header
"Select compiler and set build flags"
1106 print_header
"Clone kernel sources"
1108 # Build all the things and upload
1109 # then finish the module build...
1111 git_clone_linux_sources
1112 git_export_linux_sources
1114 print_header
"Select compiler and set build flags"
1118 ## PREPARE FULL LINUX SOURCE TREE
1119 print_header
"Build kernel from source"
1122 ## EXTRACT DISTRO STYLE KERNEL HEADERS / DEVEL
1123 extract_distro_headers
1125 print_header
"Upload kernel to object storage"
1132 echo "Unknown error? Abort"
1139 # Either we downloaded a pre-build kernel or we built it and uploaded
1140 # the archive for future builds.
1144 print_header
"Build modules against full kernel sources"
1145 build_modules
"${LINUX_SRCOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}"
1147 print_header
"Build modules against kernel headers"
1148 build_modules
"${LINUX_HDROBJ_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
1151 print_header
"Check for built modules in install directory"
1153 # Make sure some modules were actually built
1154 tree
"${MODULES_OUTPUT_KSRC_DIR}"
1155 if [ "x$(find "${MODULES_OUTPUT_KSRC_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
1156 echo "No modules built!"
1160 tree
"${MODULES_OUTPUT_KHDR_DIR}"
1161 if [ "x$(find "${MODULES_OUTPUT_KHDR_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
1162 echo "No modules built!"