| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2016-2023 Michael Jeanson <mjeanson@efficios.com> |
| 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 | |
| 18 | set -exu |
| 19 | |
| 20 | # Parameters |
| 21 | platform=${platforms:-} |
| 22 | cross_arch=${cross_arch:-} |
| 23 | ktag=${ktag:-} |
| 24 | kgitrepo=${kgitrepo:-} |
| 25 | mversion=${mversion:-} |
| 26 | mgitrepo=${mgitrepo:-} |
| 27 | make_args=() |
| 28 | |
| 29 | DEBUG=${DEBUG:-} |
| 30 | |
| 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]}" |
| 37 | else |
| 38 | arch="${platform:-}" |
| 39 | fi |
| 40 | fi |
| 41 | |
| 42 | |
| 43 | ## FUNCTIONS ## |
| 44 | |
| 45 | # Kernel version compare functions |
| 46 | verlte() { |
| 47 | [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ] |
| 48 | } |
| 49 | |
| 50 | verlt() { |
| 51 | # shellcheck disable=SC2015 |
| 52 | [ "$1" = "$2" ] && return 1 || verlte "$1" "$2" |
| 53 | } |
| 54 | |
| 55 | vergte() { |
| 56 | [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -n1)" ] |
| 57 | } |
| 58 | |
| 59 | vergt() { |
| 60 | # shellcheck disable=SC2015 |
| 61 | [ "$1" = "$2" ] && return 1 || vergte "$1" "$2" |
| 62 | } |
| 63 | |
| 64 | print_header() { |
| 65 | set +x |
| 66 | |
| 67 | local message=" $1 " |
| 68 | local message_len |
| 69 | local padding_len |
| 70 | |
| 71 | message_len="${#message}" |
| 72 | padding_len=$(( (80 - (message_len)) / 2 )) |
| 73 | |
| 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' |
| 79 | |
| 80 | set -x |
| 81 | } |
| 82 | |
| 83 | git_clone_modules_sources() { |
| 84 | mkdir -p "$MODULES_GIT_DIR" |
| 85 | |
| 86 | # If the version starts with "refs/", checkout the specific git ref, otherwise treat it |
| 87 | # as a branch name. |
| 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) |
| 91 | else |
| 92 | git clone --no-tags --depth=1 -b "${mversion}" "${mgitrepo}" "$MODULES_GIT_DIR" |
| 93 | fi |
| 94 | } |
| 95 | |
| 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" |
| 100 | } |
| 101 | |
| 102 | |
| 103 | # Export the kernel sources from the git repo |
| 104 | git_export_linux_sources() { |
| 105 | cd "$LINUX_GIT_DIR" |
| 106 | git archive "${ktag}" | tar -x -C "$LINUX_SRCOBJ_DIR" |
| 107 | } |
| 108 | |
| 109 | |
| 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" |
| 114 | } |
| 115 | |
| 116 | |
| 117 | extract_archive_obj() { |
| 118 | tar -xf "$WORKSPACE/$obj_name" -C "$LINUX_OBJ_DIR" -I pbzip2 |
| 119 | rm -f "$WORKSPACE/$obj_name" |
| 120 | } |
| 121 | |
| 122 | |
| 123 | tar_archive_obj() { |
| 124 | cd "$LINUX_OBJ_DIR" |
| 125 | tar -cf "$WORKSPACE/$obj_name" -I pbzip2 . |
| 126 | cd - |
| 127 | } |
| 128 | |
| 129 | # List all GCC versions present in the PATH |
| 130 | list_gccs() { |
| 131 | local gccs |
| 132 | gccs=() |
| 133 | IFS=: read -r -a path_array <<< "$PATH" |
| 134 | while read -r gcc ; do |
| 135 | gccs+=("$gcc") |
| 136 | done < <(find "${path_array[@]}" -maxdepth 1 -regex '.*/gcc-[0-9\.]+$' -printf '%f\n' | sort -t- -k2 -V -r) |
| 137 | echo "${gccs[@]}" |
| 138 | } |
| 139 | |
| 140 | # Find the most recent GCC version supported by the kernel sources |
| 141 | select_compiler() { |
| 142 | |
| 143 | # Enter linux source dir |
| 144 | cd "$LINUX_SRCOBJ_DIR" |
| 145 | |
| 146 | kversion=$(make -s kernelversion) |
| 147 | |
| 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) |
| 152 | else |
| 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 |
| 162 | continue |
| 163 | fi |
| 164 | selected_cc="$cc" |
| 165 | selected_cc_version="$cc_version" |
| 166 | break |
| 167 | fi |
| 168 | done |
| 169 | fi |
| 170 | |
| 171 | if [ -z "$selected_cc" ]; then |
| 172 | echo "Found no suitable compiler." |
| 173 | exit 1 |
| 174 | fi |
| 175 | |
| 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" |
| 180 | exit 1 |
| 181 | fi |
| 182 | |
| 183 | cd - |
| 184 | } |
| 185 | |
| 186 | export_kbuild_flags() { |
| 187 | local _KAFLAGS |
| 188 | local _KCFLAGS |
| 189 | local _KCPPFLAGS |
| 190 | local _HOSTCFLAGS |
| 191 | |
| 192 | _KAFLAGS=() |
| 193 | _KCFLAGS=() |
| 194 | _KCPPFLAGS=() |
| 195 | _HOSTCFLAGS=() |
| 196 | |
| 197 | if [ "$selected_cc" != "gcc-4.8" ]; then |
| 198 | # Older kernel Makefiles do not expect the compiler to default to PIE |
| 199 | _KAFLAGS+=(-fno-pie) |
| 200 | _KCFLAGS+=( |
| 201 | -fno-pie |
| 202 | -no-pie |
| 203 | -fno-stack-protector |
| 204 | ) |
| 205 | _KCPPFLAGS+=(-fno-pie) |
| 206 | fi |
| 207 | |
| 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) |
| 214 | fi |
| 215 | |
| 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) |
| 222 | fi |
| 223 | |
| 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) |
| 228 | fi |
| 229 | |
| 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}" |
| 236 | |
| 237 | make_args=( |
| 238 | CC="${CC}" |
| 239 | HOSTCC="${HOSTCC}" |
| 240 | HOSTCFLAGS="${HOSTCFLAGS:-}" |
| 241 | ) |
| 242 | |
| 243 | if [ -n "${DEBUG}" ] ; then |
| 244 | make_args+=( |
| 245 | V=1 |
| 246 | ) |
| 247 | fi |
| 248 | } |
| 249 | |
| 250 | patch_linux_kernel() { |
| 251 | local commit_hash |
| 252 | commit_hash="$1" |
| 253 | |
| 254 | # Show the title of the patch in the build log |
| 255 | git -C "${LINUX_GIT_REF_REPO_DIR}" show --oneline -s "${commit_hash}" |
| 256 | |
| 257 | # Apply patch, don't fail if it doesn't apply cleanly |
| 258 | set +e |
| 259 | git -C "${LINUX_GIT_REF_REPO_DIR}" format-patch -n1 --stdout "${commit_hash}" | patch -p1 |
| 260 | set -e |
| 261 | |
| 262 | if [ "$?" -gt 1 ] ; then |
| 263 | echo "Serious issue patching" |
| 264 | exit 1 |
| 265 | fi |
| 266 | } |
| 267 | |
| 268 | build_linux_kernel() { |
| 269 | cd "$LINUX_SRCOBJ_DIR" |
| 270 | |
| 271 | kversion=$(make -s kernelversion "${make_args[@]}") |
| 272 | pahole_version="$(pahole --version | tr -d 'v')" |
| 273 | |
| 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' |
| 278 | else |
| 279 | vanilla_config='mx51_defconfig' |
| 280 | fi |
| 281 | fi |
| 282 | |
| 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' |
| 286 | fi |
| 287 | |
| 288 | # Generate kernel configuration |
| 289 | case "$ktag" in |
| 290 | Ubuntu*) |
| 291 | if [ "${cross_arch}" = "powerpc" ] && vergte "${kversion}" "4.10"; then |
| 292 | echo "Ubuntu removed big endian powerpc configuration from kernel >= 4.10. Don't try to build it." |
| 293 | exit 0 |
| 294 | fi |
| 295 | |
| 296 | if [ "${cross_arch}" = "riscv64" ] && verlt "${kversion}" "6.2"; then |
| 297 | echo "Ubuntu added RISC-V config to their 'regular' kernels in v6.2. Don't try to build it." |
| 298 | exit 0 |
| 299 | fi |
| 300 | |
| 301 | fakeroot debian/rules clean KW_DEFCONFIG_DIR=. |
| 302 | |
| 303 | # Hack for kernel Ubuntu-hwe-5.8 |
| 304 | # The debian/control file is produced by the clean target above, so this |
| 305 | # check needs to happen afterwards. |
| 306 | if [ ! -f "debian/compat" ] && ! grep -q debhelper-compat debian/control; then |
| 307 | echo "10" > "debian/compat" |
| 308 | fi |
| 309 | |
| 310 | # genconfigs check can fail in certain cases, eg. when a more recent |
| 311 | # compiler exposes kernel configuration flags which aren't set in the |
| 312 | # Ubuntu annotations. |
| 313 | # In any case, the configuration will be updated with any missing values |
| 314 | # later in our build script. |
| 315 | fakeroot debian/rules genconfigs KW_DEFCONFIG_DIR=. do_skip_checks=true |
| 316 | cp CONFIGS/"${ubuntu_config}" .config |
| 317 | ;; |
| 318 | |
| 319 | *) |
| 320 | # Force 32bit build on i386, default is 64bit |
| 321 | if [ "$arch" = "i386" ]; then |
| 322 | export ARCH="i386" |
| 323 | fi |
| 324 | |
| 325 | make "${vanilla_config}" "${make_args[@]}" |
| 326 | ;; |
| 327 | esac |
| 328 | |
| 329 | if [ "${vanilla_config}" = "pseries_defconfig" ] && [ "${cross_arch}" = "ppc64el" ] ; then |
| 330 | # @see diff <(git show v3.13:arch/powerpc/configs/pseries_defconfig) <(git show v3.13:arch/powerpc/configs/pseries_le_defconfig) |
| 331 | scripts/config --enable CONFIG_CPU_LITTLE_ENDIAN |
| 332 | scripts/config --enable CONFIG_CMA |
| 333 | scripts/config --disable CONFIG_XMON_DEFAULT |
| 334 | # scripts/config --disable CONFIG_VIRTUALIZATION |
| 335 | # scripts/config --disable CONFIG_KVM_BOOK3S_64 |
| 336 | scripts/config --disable CONFIG_KVM_BOOK3S_64_HV |
| 337 | fi |
| 338 | |
| 339 | # oldnoconfig was renamed in 4.19 |
| 340 | if vergte "$kversion" "4.19"; then |
| 341 | update_conf_target="olddefconfig" |
| 342 | else |
| 343 | update_conf_target="oldnoconfig" |
| 344 | fi |
| 345 | |
| 346 | # Fix 'defined(@array)' was removed from recent perl |
| 347 | if [ -f "kernel/timeconst.pl" ]; then |
| 348 | sed -i 's/defined(\@\(.*\))/@\1/' kernel/timeconst.pl |
| 349 | fi |
| 350 | |
| 351 | # Fix syntax of inline assembly which is confused with C++11 raw strings on gcc >= 5 |
| 352 | if [ "$HOSTCC" != "gcc-4.8" ]; then |
| 353 | if [ -f "arch/x86/kvm/svm.c" ]; then |
| 354 | sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/svm.c |
| 355 | fi |
| 356 | |
| 357 | if [ -f "arch/x86/kvm/vmx.c" ]; then |
| 358 | sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/vmx.c |
| 359 | fi |
| 360 | fi |
| 361 | |
| 362 | if { verlt "${kversion}" "5.11"; } && { vergte "${kversion}" "4.10"; } ; then |
| 363 | # Binutils > 2.35 strips empty symbol tables, causing obltool to fail |
| 364 | # in certain cases when files are empty. |
| 365 | # @see https://gitlab.com/linux-kernel/stable/-/commit/1d489151e9f9d1647110277ff77282fe4d96d09b |
| 366 | # |
| 367 | # There doesn't seem to be any LD/AS/AR flags to control this behaviour, |
| 368 | # therefore patching tools/objtool/elf.c is attempted. |
| 369 | patch_linux_kernel 1d489151e9f9d1647110277ff77282fe4d96d09b |
| 370 | if { verlt "${kversion}" "4.18"; } ; then |
| 371 | patch_linux_kernel e81e0724432542af8d8c702c31e9d82f57b1ff31 |
| 372 | fi |
| 373 | fi |
| 374 | |
| 375 | if { vergt "${selected_cc_version}" "7"; } && { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.17"; } ; then |
| 376 | # Builds fail due to -Werror=restrict in pager.o and str_error_r.o |
| 377 | if { verlt "${kversion}" "4.16"; } ; then |
| 378 | # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS |
| 379 | # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ad343a98e74e85aa91d844310e797f96fee6983b |
| 380 | patch_linux_kernel ad343a98e74e85aa91d844310e797f96fee6983b |
| 381 | fi |
| 382 | if { verlt "${kversion}" "4.17"; } ; then |
| 383 | # This is patched since objtool's Makefile doesn't respect HOSTCFLAGS |
| 384 | # @see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=854e55ad289ef8888e7991f0ada85d5846f5afb9 |
| 385 | patch_linux_kernel 854e55ad289ef8888e7991f0ada85d5846f5afb9 |
| 386 | fi |
| 387 | |
| 388 | fi |
| 389 | |
| 390 | if { vergt "${selected_cc_version}" "9"; } && { verlt "${kversion}" "5.6"; } ; then |
| 391 | # Duplicate decalarations of __force_order |
| 392 | # @see https://gitlab.com/linux-kernel/stable/-/commit/df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc |
| 393 | # However, kaslr_64.c doesn't exit in 4.15, 4.16, it's named pagetable.c |
| 394 | if [ -f arch/x86/boot/compressed/pagetable.c ] ; then |
| 395 | sed -i '/^unsigned long __force_order;$/d' arch/x86/boot/compressed/pagetable.c |
| 396 | fi |
| 397 | if [ -f arch/x86/boot/compressed/kaslr_64.c ] ; then |
| 398 | patch_linux_kernel df6d4f9db79c1a5d6f48b59db35ccd1e9ff9adfc |
| 399 | fi |
| 400 | fi |
| 401 | |
| 402 | if { vergte "${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then |
| 403 | # In some cases, compiling net/bpfilter can fail with the following error: |
| 404 | # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory |
| 405 | # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1 |
| 406 | # |
| 407 | # While the issue is potentially in a number of old versions, it has only |
| 408 | # been observed in v4.18-rt |
| 409 | # |
| 410 | patch_linux_kernel 303a339f30a9441c4695d3d2cc78f1b33cd959ff |
| 411 | fi |
| 412 | |
| 413 | if { vergte "${kversion}" "4.18"; } && { verlt "${kversion}" "4.19"; } ; then |
| 414 | # In some cases, compiling net/bpfilter can fail with the following error: |
| 415 | # net/bpfilter/main.c:9:10: fatal error: include/uapi/linux/bpf.h: No such file or directory |
| 416 | # make[2]: *** [scripts/Makefile.host:107: net/bpfilter/main.o] Error 1 |
| 417 | # |
| 418 | # While the issue is potentially in a number of old versions, it has only |
| 419 | # been observed in v4.18-rt |
| 420 | # |
| 421 | patch_linux_kernel 303a339f30a9441c4695d3d2cc78f1b33cd959ff |
| 422 | fi |
| 423 | |
| 424 | if { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ; then |
| 425 | # Some old kernels fail to build when make is too new |
| 426 | # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583 |
| 427 | patch_linux_kernel 9564a8cf422d7b58f6e857e3546d346fa970191e |
| 428 | fi |
| 429 | |
| 430 | if { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.55"; } ; then |
| 431 | # Some old kernels fail to build when make is too new |
| 432 | # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583 |
| 433 | patch_linux_kernel e82885490a611f2b75a6c27cd7bb09665c1740be |
| 434 | fi |
| 435 | |
| 436 | if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.18"; } ) || \ |
| 437 | ( { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.56"; } ) ; then |
| 438 | # Some old kernels fail to build when make is too new |
| 439 | # @see https://gitlab.com/linux-kernel/stable/-/commit/9feeb638cde083c737e295c0547f1b4f28e99583 |
| 440 | patch_linux_kernel 9feeb638cde083c737e295c0547f1b4f28e99583 |
| 441 | fi |
| 442 | |
| 443 | if ( { vergte "${kversion}" "4.12"; } && { verlt "${kversion}" "4.20.17"; } ) || \ |
| 444 | ( { vergte "${kversion}" "5.0"; } && { verlt "${kversion}" "5.0.12"; } ) ; then |
| 445 | # Old kernels can fail to build while on newer host kernels with errors |
| 446 | # such as: |
| 447 | # In file included from scripts/selinux/genheaders/genheaders.c:19: |
| 448 | # ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map. |
| 449 | # @see https://gitlab.com/linux-kernel/stable/-/commit/dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e |
| 450 | # |
| 451 | patch_linux_kernel dfbd199a7cfe3e3cd8531e1353cdbd7175bfbc5e |
| 452 | fi |
| 453 | |
| 454 | # Compatibility with binutils >= ~ 2.31 |
| 455 | if { vergte "${kversion}" "3.19"; } && { verlt "${kversion}" "4.16"; } ; then |
| 456 | patch_linux_kernel b21ebf2fb4cde1618915a97cc773e287ff49173e |
| 457 | fi |
| 458 | if { vergte "${kversion}" "3.17"; } && { verlt "${kversion}" "3.18.69"; } ; then |
| 459 | patch_linux_kernel edb9d2d5e647e7a8521b0d35f8452deb02dfd138 |
| 460 | fi |
| 461 | if { vergte "${kversion}" "3.17"; } && { verlt "${kversion}" "3.18.100"; } ; then |
| 462 | patch_linux_kernel 3be6583f0b6f1bf1ee650ebf473d9dee36836527 |
| 463 | patch_linux_kernel 12d839211d080f6a9c370398c41a260365d34c62 |
| 464 | fi |
| 465 | if { vergte "${kversion}" "3.16"; } && { verlt "${kversion}" "3.16.82"; } ; then |
| 466 | patch_linux_kernel ad10e6d464796f2a481de4039a43b9cfca034e1c |
| 467 | fi |
| 468 | |
| 469 | if ( { vergte "${kversion}" "3.14"; } && { verlt "${kversion}" "4.4"; } ) || |
| 470 | ( { vergte "${kversion}" "4.8"; } && { verlt "${kversion}" "4.18"; } ); then |
| 471 | # While the original motivation of this patch is for fixing builds using |
| 472 | # clang, the same error occurs between linux >= 3.14 and < 4.4, and in |
| 473 | # 4.15, 4.16. |
| 474 | # For rt-linux, the error has been observed in 4.8, 4.11, and 4.13. |
| 475 | # |
| 476 | # This patch only partially applies due to changes in kernel/Makefile, |
| 477 | # so a supplementary patch is needed |
| 478 | # |
| 479 | # Without this patch, builds fail with |
| 480 | # Cannot find symbol for section 2: .text. |
| 481 | # kernel/elfcore.o: failed |
| 482 | # |
| 483 | # @see https://github.com/linuxppc/issues/issues/388 |
| 484 | # @see https://lore.kernel.org/lkml/20201204165742.3815221-2-arnd@kernel.org/ |
| 485 | # |
| 486 | patch_linux_kernel 6e7b64b9dd6d96537d816ea07ec26b7dedd397b9 |
| 487 | if grep -q elfcore.o kernel/Makefile ; then |
| 488 | sed -i '/^.* += elfcore.o$/d' kernel/Makefile |
| 489 | fi |
| 490 | fi |
| 491 | # Same as above for the v4.4 branch |
| 492 | if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.257"; } ); then |
| 493 | patch_linux_kernel 3140b0740b31cc63cf2ee08bc3f746b423eb068d |
| 494 | if grep -q elfcore.o kernel/Makefile ; then |
| 495 | sed -i '/^.* += elfcore.o$/d' kernel/Makefile |
| 496 | fi |
| 497 | fi |
| 498 | |
| 499 | if { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ; then |
| 500 | # Kernels between v4.5 and v4.8 built with gcc >= 8 on arm will hit an |
| 501 | # assembler error : |
| 502 | # |
| 503 | # kernel/.tmp_fork.s: Assembler messages: |
| 504 | # kernel/.tmp_fork.s:1790: Error: .err encountered |
| 505 | # |
| 506 | # @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85745 |
| 507 | # |
| 508 | patch_linux_kernel 9f73bd8bb445e0cbe4bcef6d4cfc788f1e184007 |
| 509 | fi |
| 510 | |
| 511 | if ( { vergte "${kversion}" "4.4"; } && { verlt "${kversion}" "4.4.136"; } ) || |
| 512 | ( { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.8"; } ); then |
| 513 | # Hacky patch to deal with the following build error: |
| 514 | # Cannot find symbol for section 7: .text.unlikely. |
| 515 | # kernel/kexec_file.o: failed |
| 516 | # make[1]: *** [scripts/Makefile.build:291: kernel/kexec_file.o] Error 1 |
| 517 | # |
| 518 | # This error happens with binutils 2.36 and 2.37, but should probably not |
| 519 | # be an issue with binutils 2.38. |
| 520 | # @see https://github.com/linuxppc/issues/issues/388 |
| 521 | # @see https://github.com/bminor/binutils-gdb/commit/c09c8b42021180eee9495bd50d8b35e683d3901b |
| 522 | # |
| 523 | # There is some sort of config (unspecified in past discussions) which |
| 524 | # provokes the error, and there was never a potential fix merged in |
| 525 | # this discussion, in part because the build systems of the kernel |
| 526 | # switched to objtool instead. |
| 527 | # |
| 528 | # @see https://lore.kernel.org/all/20210215162209.5e2a475b@gandalf.local.home/ |
| 529 | # |
| 530 | sed -i 's/return txtname;/return shdr0->sh_size ? txtname : NULL;/' scripts/recordmcount.h |
| 531 | |
| 532 | # After applying the above patch, the build continues but fails with |
| 533 | # head64.c:(.text.exit+0x5): undefined reference to `__gcov_exit' |
| 534 | # |
| 535 | scripts/config --disable CONFIG_GCOV_KERNEL |
| 536 | fi |
| 537 | |
| 538 | if { vergte "${kversion}" "4.5"; } && { verlt "${kversion}" "4.5.5"; } ; then |
| 539 | # drivers/staging/wilc1000/wilc_spi.c:123:34: error: storage size of ‘wilc1000_spi_ops’ isn’t known |
| 540 | patch_linux_kernel ce7b516f3f9e11fe4ee06fad0d7e853bb6e8f160 |
| 541 | fi |
| 542 | |
| 543 | # Newer binutils don't accept 3 operand 'cmp' instructions on ppc64 |
| 544 | # Convert them to 'cmpw' which was previously done silently |
| 545 | if verlt "$kversion" "4.9"; then |
| 546 | 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/" |
| 547 | 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/" |
| 548 | sed -i "s/\$pie \-o \"\$ofile\"/\$pie --no-dynamic-linker -o \"\$ofile\"/" arch/powerpc/boot/wrapper |
| 549 | fi |
| 550 | |
| 551 | if [ "$(scripts/config --state CONFIG_EXTCON_ADC_JACK)" != "n" ] && |
| 552 | ( { vergte "${kversion}" "4.2"; } && { verlt "${kversion}" "4.12"; } ); then |
| 553 | # 73b6ecdb93e8e77752cae9077c424fcdc6f23c39 introduced a change where |
| 554 | # extcon-adc-jack.h has an incompatible pointer type. |
| 555 | # In GCC >= 5 this will provoke a warning and build failure. |
| 556 | # Eg. |
| 557 | # drivers/extcon/extcon-adc-jack.c: In function ‘adc_jack_probe’: |
| 558 | # 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] |
| 559 | # make[2]: *** [scripts/Makefile.build:295: drivers/extcon/extcon-adc-jack.o] Error 1 |
| 560 | # |
| 561 | # 8a522bf2d4f788306443d36b26b54f0aedcdfdbe (in 4.11) has a fix for this warning |
| 562 | # |
| 563 | patch_linux_kernel 8a522bf2d4f788306443d36b26b54f0aedcdfdbe |
| 564 | fi |
| 565 | |
| 566 | if ( { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.15.2"; } ) || \ |
| 567 | ( { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.18"; } ) ; then |
| 568 | # Fix an objtool Segmentation fault |
| 569 | patch_linux_kernel dd12561854824fd1f05baf2a1b794faa046e2425 |
| 570 | fi |
| 571 | |
| 572 | if { vergte "${kversion}" "4.14"; } && { verlt "${kversion}" "4.14.268"; } ; then |
| 573 | # Builds fail due to -Werror=use-after-free in sigchain.o and help.o |
| 574 | patch_linux_kernel e89bb266b710ce056f141f29f091fd468a4a8185 |
| 575 | fi |
| 576 | |
| 577 | # Fix a typo in v2.6.36.x |
| 578 | if [ -f "arch/x86/kernel/entry_64.S" ]; then |
| 579 | sed -i 's/END(do_hypervisor_callback)/END(xen_do_hypervisor_callback)/' arch/x86/kernel/entry_64.S |
| 580 | fi |
| 581 | |
| 582 | # Fix compiler switch in vdso Makefile for 2.6.36 to 2.6.36.2 |
| 583 | if { vergte "$kversion" "2.6.36" && verlte "$kversion" "2.6.36.3"; }; then |
| 584 | sed -i 's/-m elf_x86_64/-m64/' arch/x86/vdso/Makefile |
| 585 | sed -i 's/-m elf_i386/-m32/' arch/x86/vdso/Makefile |
| 586 | fi |
| 587 | |
| 588 | # Fix kernel < 3.0 with gcc >= 4.7 |
| 589 | if verlt "$kversion" "3.0"; then |
| 590 | sed -i '/linux\/compiler.h/a #include <linux\/linkage.h> \/* For asmregparm *\/' arch/x86/include/asm/ptrace.h |
| 591 | sed -i 's/extern long syscall_trace_enter/extern asmregparm long syscall_trace_enter/' arch/x86/include/asm/ptrace.h |
| 592 | sed -i 's/extern void syscall_trace_leave/extern asmregparm void syscall_trace_leave/' arch/x86/include/asm/ptrace.h |
| 593 | echo "header-y += linkage.h" >> include/linux/Kbuild |
| 594 | fi |
| 595 | |
| 596 | if [ "${cross_arch}" = "powerpc" ] ; then |
| 597 | if { vergte "${kversion}" "4.15"; } && { verlt "${kversion}" "4.16"; } ; then |
| 598 | # Avoid register errors such as |
| 599 | # aes_generic.c:(.text+0x4e0): undefined reference to `_restgpr_31_x' |
| 600 | # @see https://gitlab.com/linux-kernel/stable/-/commit/148b974deea927f5dbb6c468af2707b488bfa2de |
| 601 | make_args+=( |
| 602 | CFLAGS_aes_generic.o='' |
| 603 | ) |
| 604 | fi |
| 605 | fi |
| 606 | |
| 607 | if [ "$(scripts/config --state CONFIG_DEBUG_INFO_BTF)" == "y" ] && |
| 608 | { vergte "${pahole_version}" "1.24"; } && |
| 609 | { vergte "${kversion}" "5.10"; } && { verlt "${kversion}" "6.0"; } ;then |
| 610 | # Some kernels Eg. Ubuntu-hwe-5.13-5.13.0-52.59_20.04.1 |
| 611 | # fail with the following error: |
| 612 | # BTFIDS vmlinux |
| 613 | # FAILED: load BTF from vmlinux: Invalid argument |
| 614 | # |
| 615 | # When CONFIG_DEBUG_INFO_BTF is set, certain versions of pahole require |
| 616 | # `--skip_encoding_btf_enum64` to be passed as the kernel doesn't define |
| 617 | # BTF_KIND_ENUM64. |
| 618 | # |
| 619 | # Introduced in 341dfcf8d78eaa3a2dc96dea06f0392eb2978364 (~v5.10) |
| 620 | # @see https://lore.kernel.org/bpf/20220825171620.cioobudss6ovyrkc@altlinux.org/t/ |
| 621 | # |
| 622 | if [ -f "scripts/pahole-flags.sh" ] ; then |
| 623 | # shellcheck disable=SC2016 |
| 624 | sed -i 's/ -J ${PAHOLE_FLAGS} / -J ${PAHOLE_FLAGS} --skip_encoding_btf_enum64 /' scripts/link-vmlinux.sh |
| 625 | else |
| 626 | # shellcheck disable=SC2016 |
| 627 | sed -i 's/ -J ${extra_paholeopt} / -J ${extra_paholeopt} --skip_encoding_btf_enum64 /' scripts/link-vmlinux.sh |
| 628 | fi |
| 629 | fi |
| 630 | |
| 631 | # GCC 4.8 |
| 632 | if [ "$HOSTCC" == "gcc-4.8" ]; then |
| 633 | scripts/config --disable CONFIG_CC_STACKPROTECTOR_STRONG |
| 634 | scripts/config --disable CONFIG_PPC_OF_BOOT_TRAMPOLINE |
| 635 | fi |
| 636 | |
| 637 | # Don't try to sign modules |
| 638 | scripts/config --disable CONFIG_MODULE_SIG |
| 639 | |
| 640 | # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails |
| 641 | scripts/config --disable CONFIG_STACK_VALIDATION |
| 642 | |
| 643 | # Cause problems with inline assembly on i386 |
| 644 | scripts/config --disable CONFIG_DEBUG_SECTION_MISMATCH |
| 645 | |
| 646 | # Don't build samples, they are broken on some kernel releases |
| 647 | scripts/config --disable CONFIG_SAMPLES |
| 648 | scripts/config --disable CONFIG_BUILD_DOCSRC |
| 649 | |
| 650 | # Disable kcov |
| 651 | scripts/config --disable CONFIG_KCOV |
| 652 | |
| 653 | # Broken on some RT kernels |
| 654 | scripts/config --disable CONFIG_HYPERV |
| 655 | |
| 656 | # Broken drivers |
| 657 | scripts/config --disable CONFIG_RAPIDIO_TSI721 |
| 658 | scripts/config --disable CONFIG_SGI_XP |
| 659 | scripts/config --disable CONFIG_MFD_WM8994 |
| 660 | scripts/config --disable CONFIG_DRM_RADEON |
| 661 | scripts/config --disable CONFIG_SND_SOC_WM5100 |
| 662 | # More recent compiler optimizations (from gcc 8 onwards )expose build errors |
| 663 | # with netronome on older kernels. |
| 664 | # Observed in 4.11-rt, 4.15-4.17, 5.0-rt - 5.16-rt |
| 665 | # It seems easier to disable the driver than to attempt patching. |
| 666 | # Eg. |
| 667 | # In function ‘ur_load_imm_any’, |
| 668 | # inlined from ‘jeq_imm’ at drivers/net/ethernet/netronome/nfp/bpf/jit.c:3146:13: |
| 669 | # ./include/linux/compiler.h:350:45: error: call to ‘__compiletime_assert_1062’ declared with attribute error: FIELD_FIT: value too large for the field |
| 670 | # 350 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) |
| 671 | # |
| 672 | scripts/config --disable CONFIG_NET_VENDOR_NETRONOME |
| 673 | # Eg. |
| 674 | # In function ‘memcpy’, |
| 675 | # inlined from ‘kszphy_get_strings’ at drivers/net/phy/micrel.c:664:3: |
| 676 | # ./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 |
| 677 | # 305 | __read_overflow2(); |
| 678 | # | ^~~~~~~~~~~~~~~~~~ |
| 679 | # make[3]: *** [scripts/Makefile.build:308: drivers/net/phy/micrel.o] Error 1 |
| 680 | # |
| 681 | scripts/config --disable CONFIG_MICREL_PHY |
| 682 | |
| 683 | |
| 684 | # IGBVF won't build with recent gcc on 2.6.38.x |
| 685 | if { vergte "$kversion" "2.6.37" && verlt "$kversion" "2.6.38"; }; then |
| 686 | scripts/config --disable CONFIG_IGBVF |
| 687 | fi |
| 688 | |
| 689 | # Don't fail the build on warnings |
| 690 | scripts/config --disable CONFIG_WERROR |
| 691 | scripts/config --enable CONFIG_PPC_DISABLE_WERROR |
| 692 | |
| 693 | # Set required options |
| 694 | scripts/config --enable CONFIG_TRACEPOINTS |
| 695 | scripts/config --enable CONFIG_KALLSYMS |
| 696 | scripts/config --enable CONFIG_HIGH_RES_TIMERS |
| 697 | scripts/config --enable CONFIG_KPROBES |
| 698 | scripts/config --enable CONFIG_FTRACE |
| 699 | scripts/config --enable CONFIG_BLK_DEV_IO_TRACE |
| 700 | scripts/config --enable CONFIG_KALLSYMS_ALL |
| 701 | scripts/config --enable CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 702 | scripts/config --enable CONFIG_PERF_EVENTS |
| 703 | scripts/config --enable CONFIG_EVENT_TRACING |
| 704 | scripts/config --enable CONFIG_KRETPROBES |
| 705 | |
| 706 | if [ -n "${DEBUG}" ] ; then |
| 707 | cat .config |
| 708 | fi |
| 709 | |
| 710 | make "$update_conf_target" "${make_args[@]}" |
| 711 | make -j"$NPROC" "${make_args[@]}" |
| 712 | |
| 713 | krelease=$(make -s kernelrelease "${make_args[@]}") |
| 714 | |
| 715 | # Save the kernel and modules |
| 716 | mkdir -p "$LINUX_INSTOBJ_DIR/boot" |
| 717 | make INSTALL_MOD_PATH="$LINUX_INSTOBJ_DIR" INSTALL_MOD_STRIP=1 modules_install "${make_args[@]}" |
| 718 | make INSTALL_MOD_PATH="$LINUX_INSTOBJ_DIR" INSTALL_PATH="$LINUX_INSTOBJ_DIR/boot" install "${make_args[@]}" |
| 719 | rm -f "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/build" |
| 720 | ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" |
| 721 | ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" |
| 722 | } |
| 723 | |
| 724 | |
| 725 | extract_distro_headers() { |
| 726 | |
| 727 | # Enter linux source dir |
| 728 | cd "${LINUX_SRCOBJ_DIR}" |
| 729 | |
| 730 | |
| 731 | # For RT kernels, copy version file |
| 732 | if [ -s localversion-rt ]; then |
| 733 | cp -a localversion-rt "${LINUX_HDROBJ_DIR}" |
| 734 | fi |
| 735 | |
| 736 | # Copy all Makefile related stuff |
| 737 | find . -path './include/*' -prune \ |
| 738 | -o -path './scripts/*' -prune -o -type f \ |
| 739 | \( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \ |
| 740 | -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \ |
| 741 | -print | cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}" |
| 742 | |
| 743 | # Copy base scripts and include dirs |
| 744 | cp -a scripts include "${LINUX_HDROBJ_DIR}" |
| 745 | |
| 746 | # Copy arch includes |
| 747 | (find arch -name include -type d -print0 | \ |
| 748 | xargs -0 -n1 -I '{}' find '{}' -type f) | \ |
| 749 | cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}" |
| 750 | |
| 751 | # Copy arch scripts |
| 752 | (find arch -name scripts -type d -print0 | \ |
| 753 | xargs -0 -n1 -I '{}' find '{}' -type f) | \ |
| 754 | cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}" |
| 755 | |
| 756 | # Cleanup scripts |
| 757 | rm -f "${LINUX_HDROBJ_DIR}/scripts/*.o" |
| 758 | rm -f "${LINUX_HDROBJ_DIR}/scripts/*/*.o" |
| 759 | |
| 760 | # On powerpc 32bits this object is required to link modules |
| 761 | if [ "${karch}" = "powerpc" ]; then |
| 762 | if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte "${kversion}" "5.4"; then |
| 763 | : |
| 764 | else |
| 765 | cp -a --parents arch/powerpc/lib/crtsavres.[So] "${LINUX_HDROBJ_DIR}/" |
| 766 | fi |
| 767 | fi |
| 768 | |
| 769 | # On arm64 between 4.13 and 1.15 this object is required to build with ftrace support |
| 770 | if [ "${karch}" = "arm64" ]; then |
| 771 | if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then |
| 772 | cp -a --parents arch/arm64/kernel/ftrace-mod.[So] "${LINUX_HDROBJ_DIR}/" |
| 773 | fi |
| 774 | fi |
| 775 | |
| 776 | # Newer kernels need objtool to build modules when CONFIG_STACK_VALIDATION=y |
| 777 | if [ -f tools/objtool/objtool ]; then |
| 778 | cp -a --parents tools/objtool/objtool "${LINUX_HDROBJ_DIR}/" |
| 779 | fi |
| 780 | |
| 781 | if [ -f "arch/x86/kernel/macros.s" ]; then |
| 782 | cp -a --parents arch/x86/kernel/macros.s "${LINUX_HDROBJ_DIR}/" |
| 783 | fi |
| 784 | |
| 785 | # Copy modules related stuff, if available |
| 786 | if [ -s Module.symvers ]; then |
| 787 | cp Module.symvers "${LINUX_HDROBJ_DIR}" |
| 788 | fi |
| 789 | |
| 790 | if [ -s System.map ]; then |
| 791 | cp System.map "${LINUX_HDROBJ_DIR}" |
| 792 | fi |
| 793 | |
| 794 | if [ -s Module.markers ]; then |
| 795 | cp Module.markers "${LINUX_HDROBJ_DIR}" |
| 796 | fi |
| 797 | |
| 798 | # Copy config file |
| 799 | cp .config "${LINUX_HDROBJ_DIR}" |
| 800 | |
| 801 | # Make sure the Makefile and version.h have a matching timestamp so that |
| 802 | # external modules can be built |
| 803 | if [ -s "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h" ]; then |
| 804 | touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h" |
| 805 | elif [ -s "${LINUX_HDROBJ_DIR}/include/linux/version.h" ]; then |
| 806 | touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/linux/version.h" |
| 807 | else |
| 808 | echo "Missing version.h" |
| 809 | exit 1 |
| 810 | fi |
| 811 | touch -r "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/generated/autoconf.h" |
| 812 | |
| 813 | # Copy .config to include/config/auto.conf so "make prepare" is unnecessary. |
| 814 | if [ ! -f "${LINUX_HDROBJ_DIR}/include/config/auto.conf" ]; then |
| 815 | cp "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/config/auto.conf" |
| 816 | fi |
| 817 | |
| 818 | # Finally clean the object files from the full source tree |
| 819 | make clean |
| 820 | |
| 821 | # And regen the modules support files |
| 822 | make modules_prepare "${make_args[@]}" |
| 823 | |
| 824 | # On powerpc 32bits this object is required to link modules |
| 825 | if [ "${karch}" = "powerpc" ]; then |
| 826 | if [ "$(scripts/config -s CONFIG_PPC64)" = "y" ] && vergte "${kversion}" "5.4"; then |
| 827 | : |
| 828 | else |
| 829 | make arch/powerpc/lib/crtsavres.o "${make_args[@]}" |
| 830 | fi |
| 831 | fi |
| 832 | |
| 833 | # On arm64 between 4.13 and 4.15 this object is required to build with ftrace support |
| 834 | if [ "${karch}" = "arm64" ]; then |
| 835 | if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then |
| 836 | make arch/arm64/kernel/ftrace-mod.o "${make_args[@]}" |
| 837 | fi |
| 838 | fi |
| 839 | |
| 840 | # Version specific tasks |
| 841 | case "$ktag" in |
| 842 | Ubuntu*) |
| 843 | # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code |
| 844 | ABINUM="$(echo "$ktag" | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)')" |
| 845 | echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> include/generated/utsrelease.h |
| 846 | echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> "${LINUX_HDROBJ_DIR}/include/generated/utsrelease.h" |
| 847 | ;; |
| 848 | esac |
| 849 | } |
| 850 | |
| 851 | |
| 852 | build_modules() { |
| 853 | |
| 854 | local kdir="$1" |
| 855 | local outdir="$2" |
| 856 | local kversion |
| 857 | |
| 858 | kversion=$(make -C "$LINUX_HDROBJ_DIR" -s kernelversion) |
| 859 | |
| 860 | # Try to catch some compatibility problems by turning some |
| 861 | # warnings into errors. |
| 862 | #export KCFLAGS="$KCFLAGS -Wall -Werror" |
| 863 | |
| 864 | # Enter lttng-modules source dir |
| 865 | cd "${MODULES_GIT_DIR}" |
| 866 | |
| 867 | # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the |
| 868 | # timekeeping subsystem. We want those build to fail. |
| 869 | if { vergte "$kversion" "3.10" && verlte "$kversion" "3.10.13"; } || \ |
| 870 | { vergte "$kversion" "3.11" && verlte "$kversion" "3.11.2"; }; then |
| 871 | |
| 872 | set +e |
| 873 | |
| 874 | # Build modules |
| 875 | KERNELDIR="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}" |
| 876 | ret=$? |
| 877 | |
| 878 | set -e |
| 879 | |
| 880 | # We expect this build to fail, if it doesn't, fail the job. |
| 881 | if [ "$ret" -eq 0 ]; then |
| 882 | echo "This build should have failed." |
| 883 | exit 1 |
| 884 | fi |
| 885 | |
| 886 | # We have to publish at least one file or the build will fail |
| 887 | echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${outdir}/BROKEN.txt.ko" |
| 888 | |
| 889 | KERNELDIR="${kdir}" make clean |
| 890 | |
| 891 | else # Regular build |
| 892 | |
| 893 | # Build modules against full kernel sources |
| 894 | KERNELDIR="${kdir}" make -j"${NPROC}" V=1 "${make_args[@]}" |
| 895 | |
| 896 | # Install modules to build dir |
| 897 | KERNELDIR="${kdir}" make INSTALL_MOD_PATH="${outdir}" modules_install |
| 898 | |
| 899 | # Clean build dir |
| 900 | KERNELDIR="${kdir}" make clean |
| 901 | fi |
| 902 | } |
| 903 | |
| 904 | |
| 905 | ## MAIN ## |
| 906 | |
| 907 | # Use all CPU cores |
| 908 | NPROC=$(nproc) |
| 909 | |
| 910 | MODULES_GIT_DIR="${WORKSPACE}/src/lttng-modules" |
| 911 | LINUX_GIT_DIR="${WORKSPACE}/src/linux" |
| 912 | |
| 913 | LINUX_OBJ_DIR="${WORKSPACE}/linux" |
| 914 | LINUX_SRCOBJ_DIR="${LINUX_OBJ_DIR}/sources" |
| 915 | LINUX_HDROBJ_DIR="${LINUX_OBJ_DIR}/headers" |
| 916 | LINUX_INSTOBJ_DIR="${LINUX_OBJ_DIR}/install" |
| 917 | |
| 918 | MODULES_OUTPUT_KSRC_DIR="${WORKSPACE}/build/lttng-modules-ksrc" |
| 919 | MODULES_OUTPUT_KHDR_DIR="${WORKSPACE}/build/lttng-modules-khdr" |
| 920 | |
| 921 | LINUX_GIT_REF_REPO_DIR="$HOME/gitcache/linux-stable.git/" |
| 922 | |
| 923 | OBJ_STORE_URL="s3://jenkins" |
| 924 | |
| 925 | cd "$WORKSPACE" |
| 926 | |
| 927 | # Create build directories |
| 928 | mkdir -p "${LINUX_SRCOBJ_DIR}" "${LINUX_HDROBJ_DIR}" "${LINUX_INSTOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}" "${MODULES_OUTPUT_KHDR_DIR}" |
| 929 | |
| 930 | print_header "Clone LTTng-modules sources" |
| 931 | git_clone_modules_sources |
| 932 | |
| 933 | # Setup cross compile env if available |
| 934 | if [ "x${cross_arch}" != "x" ]; then |
| 935 | |
| 936 | case "$cross_arch" in |
| 937 | "armhf") |
| 938 | karch="arm" |
| 939 | cross_compile="arm-linux-gnueabihf-" |
| 940 | vanilla_config="imx_v6_v7_defconfig" |
| 941 | ubuntu_config="armhf-config.flavour.generic" |
| 942 | ;; |
| 943 | |
| 944 | "arm64") |
| 945 | karch="arm64" |
| 946 | cross_compile="aarch64-linux-gnu-" |
| 947 | vanilla_config="defconfig" |
| 948 | ubuntu_config="arm64-config.flavour.generic" |
| 949 | ;; |
| 950 | |
| 951 | "powerpc") |
| 952 | karch="powerpc" |
| 953 | cross_compile="powerpc-linux-gnu-" |
| 954 | vanilla_config="ppc44x_defconfig" |
| 955 | ubuntu_config="powerpc-config.flavour.powerpc-smp" |
| 956 | ;; |
| 957 | |
| 958 | "ppc64el") |
| 959 | karch="powerpc" |
| 960 | cross_compile="powerpc64le-linux-gnu-" |
| 961 | vanilla_config="pseries_le_defconfig" |
| 962 | ubuntu_config="ppc64el-config.flavour.generic" |
| 963 | ;; |
| 964 | |
| 965 | *) |
| 966 | echo "Unsupported cross arch $cross_arch" |
| 967 | exit 1 |
| 968 | ;; |
| 969 | esac |
| 970 | |
| 971 | # Export variables used by Kbuild for cross compilation |
| 972 | export ARCH="${karch}" |
| 973 | export CROSS_COMPILE="${cross_compile}" |
| 974 | |
| 975 | # Set arch specific values if we are not cross compiling |
| 976 | elif [ "x${arch}" != "x" ]; then |
| 977 | |
| 978 | case "$arch" in |
| 979 | "i386") |
| 980 | karch="x86" |
| 981 | vanilla_config="allmodconfig" |
| 982 | ubuntu_config="i386-config.flavour.generic" |
| 983 | ;; |
| 984 | |
| 985 | "amd64") |
| 986 | karch="x86" |
| 987 | vanilla_config="allmodconfig" |
| 988 | ubuntu_config="amd64-config.flavour.generic" |
| 989 | ;; |
| 990 | |
| 991 | "armhf") |
| 992 | karch="arm" |
| 993 | vanilla_config="allmodconfig" |
| 994 | ubuntu_config="armhf-config.flavour.generic" |
| 995 | ;; |
| 996 | |
| 997 | "arm64") |
| 998 | karch="arm64" |
| 999 | vanilla_config="allmodconfig" |
| 1000 | ubuntu_config="arm64-config.flavour.generic" |
| 1001 | ;; |
| 1002 | |
| 1003 | "powerpc") |
| 1004 | karch="powerpc" |
| 1005 | vanilla_config="allmodconfig" |
| 1006 | ubuntu_config="powerpc-config.flavour.powerpc-smp" |
| 1007 | ;; |
| 1008 | |
| 1009 | "ppc64el") |
| 1010 | karch="powerpc" |
| 1011 | vanilla_config="allmodconfig" |
| 1012 | ubuntu_config="ppc64el-config.flavour.generic" |
| 1013 | ;; |
| 1014 | |
| 1015 | *) |
| 1016 | echo "Unsupported arch $arch" |
| 1017 | exit 1 |
| 1018 | ;; |
| 1019 | esac |
| 1020 | else |
| 1021 | echo "No arch or cross_arch specified" |
| 1022 | exit 1 |
| 1023 | fi |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | # First get the kernel build from the object store, or build it, if it's |
| 1028 | # not available. |
| 1029 | |
| 1030 | set +x |
| 1031 | echo "# Setup endpoint |
| 1032 | host_base = obj.internal.efficios.com |
| 1033 | host_bucket = obj.internal.efficios.com |
| 1034 | bucket_location = us-east-1 |
| 1035 | use_https = True |
| 1036 | |
| 1037 | # Setup access keys |
| 1038 | access_key = jenkins |
| 1039 | secret_key = echo123456 |
| 1040 | |
| 1041 | # Enable S3 v4 signature APIs |
| 1042 | signature_v2 = False" > "$WORKSPACE/.s3cfg" |
| 1043 | set -x |
| 1044 | |
| 1045 | url_hash="$(echo -n "$kgitrepo" | md5sum | awk '{ print $1 }')" |
| 1046 | obj_name="linux.tar.bz2" |
| 1047 | |
| 1048 | if [ -z "${cross_arch}" ]; then |
| 1049 | obj_url_prefix="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/$arch/native" |
| 1050 | else |
| 1051 | obj_url_prefix="$OBJ_STORE_URL/linux-build/$url_hash/$ktag/platform-${platform}/${cross_arch}" |
| 1052 | fi |
| 1053 | |
| 1054 | obj_url="$obj_url_prefix/$obj_name" |
| 1055 | |
| 1056 | set +e |
| 1057 | # In s3cmd 2.3, the return code of get when an object does not exist (64) |
| 1058 | # is different than in 2.2 (12). The return codes of 's3cmd info' are |
| 1059 | # consistent between 2.2 and 2.3. |
| 1060 | s3cmd -c "$WORKSPACE/.s3cfg" info "$obj_url" |
| 1061 | ret=$? |
| 1062 | set -e |
| 1063 | |
| 1064 | case "$ret" in |
| 1065 | "0") |
| 1066 | print_header "Get sources and prebuilt kernel" |
| 1067 | s3cmd -c "$WORKSPACE/.s3cfg" get "$obj_url" |
| 1068 | extract_archive_obj |
| 1069 | |
| 1070 | print_header "Select compiler and set build flags" |
| 1071 | select_compiler |
| 1072 | export_kbuild_flags |
| 1073 | ;; |
| 1074 | |
| 1075 | "12") |
| 1076 | print_header "Clone kernel sources" |
| 1077 | |
| 1078 | # Build all the things and upload |
| 1079 | # then finish the module build... |
| 1080 | |
| 1081 | git_clone_linux_sources |
| 1082 | git_export_linux_sources |
| 1083 | |
| 1084 | print_header "Select compiler and set build flags" |
| 1085 | select_compiler |
| 1086 | export_kbuild_flags |
| 1087 | |
| 1088 | ## PREPARE FULL LINUX SOURCE TREE |
| 1089 | print_header "Build kernel from source" |
| 1090 | build_linux_kernel |
| 1091 | |
| 1092 | ## EXTRACT DISTRO STYLE KERNEL HEADERS / DEVEL |
| 1093 | extract_distro_headers |
| 1094 | |
| 1095 | print_header "Upload kernel to object storage" |
| 1096 | tar_archive_obj |
| 1097 | upload_archive_obj |
| 1098 | |
| 1099 | ;; |
| 1100 | |
| 1101 | *) |
| 1102 | echo "Unknown error? Abort" |
| 1103 | exit 1 |
| 1104 | ;; |
| 1105 | esac |
| 1106 | |
| 1107 | |
| 1108 | ## BUILD modules |
| 1109 | # Either we downloaded a pre-build kernel or we built it and uploaded |
| 1110 | # the archive for future builds. |
| 1111 | |
| 1112 | cd "$WORKSPACE" |
| 1113 | |
| 1114 | print_header "Build modules against full kernel sources" |
| 1115 | build_modules "${LINUX_SRCOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}" |
| 1116 | |
| 1117 | print_header "Build modules against kernel headers" |
| 1118 | build_modules "${LINUX_HDROBJ_DIR}" "${MODULES_OUTPUT_KHDR_DIR}" |
| 1119 | |
| 1120 | |
| 1121 | print_header "Check for built modules in install directory" |
| 1122 | |
| 1123 | # Make sure some modules were actually built |
| 1124 | tree "${MODULES_OUTPUT_KSRC_DIR}" |
| 1125 | if [ "x$(find "${MODULES_OUTPUT_KSRC_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then |
| 1126 | echo "No modules built!" |
| 1127 | exit 1 |
| 1128 | fi |
| 1129 | |
| 1130 | tree "${MODULES_OUTPUT_KHDR_DIR}" |
| 1131 | if [ "x$(find "${MODULES_OUTPUT_KHDR_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then |
| 1132 | echo "No modules built!" |
| 1133 | exit 1 |
| 1134 | fi |
| 1135 | |
| 1136 | # EOF |