summaryrefslogtreecommitdiff
path: root/libre/linux-libre-64/PKGBUILD
blob: 12c9425411dfcd534896b7401dd2730e5076680b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# Contributor: David P. <megver83@parabola.nu>
# Maintainer (arch32): Erich Eckner <deep42thought@archlinux32.org>
# Maintainer (arch32): Andreas Baumann <mail@andreasbaumann.cc>
# Maintainer (archarm): Kevin Mihelich <kevin@archlinuxarm.org>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: fauno <fauno@parabola.nu>
# Contributor: Sorin-Mihai Vârgolici <smv@yobicore.org>
# Contributor: Michał Masłowski <mtjm@mtjm.eu>
# Contributor: Luke R. <g4jc@openmailbox.org>
# Contributor: Andreas Grapentin <andreas@grapentin.org>
# Maintainer: Parabola Hackers <dev@lists.parabola.nu>

# Based on linux package

# Rationale: This package enables to run 64bit kernels with 32bit rootfs.
#
# For armv7k/aarch64, we have no aarch64 port yet, and the 32bit
# kernels don't work for 64bit computers (no 64bit DTB files, etc).
#
# In turn if we can boot Parabola on these computers, it enables
# people to work on an aarch64 port more easily as chrooting in 64bit
# rootfs from 32bit rootfs work when we have a 64bit kernel (tested on
# an x86_64 computer with a Parabola i686 rootfs and a Parabola x86_64
# chroot).
#
# This enable to make rootfs that work on both 64bit and 32bit
# computers. In that case the user will still need to select the right
# kernel at boot.
#
# Generally speaking it also enable users to benefit from many of the
# improvements of 64bit kernels (ability to chroot in 64bit rootfs,
# better security, 4GiB address space per process instead of 3GiB for
# 32bit rootfs, etc) without having to reinstall everything. In
# addition, there was and is still no conversion guide to convert a
# 32bit Parabola installation to 64bit), and in some cases (lot of
# self-built software in /usr/local, PostgreSQL database, etc) the
# migration to 64bit requires some work.
#
# As for using i686 for new installation on x86_64 computers, it can
# enable people doing that to find and fix bugs on Parabola i686 to
# enable people with 32bit-only computers (like Libreboot compatible
# I945 Thinkpads) to still be able to use Parabola.
#
# Having both architectures (arm and x86) in the same PKGBUILD enables
# to more widely test this package (if we find issues on x86, they'll
# most likely apply to arm as well). Though it can become a problem if
# we need different kernel versions for both architectures.
#
# Status:
# - Tested on both arm and x86.
# - The build is currently broken on ARM due to a missing cross
#   compiler for armv7h (it was accidentally removed and it doesn't
#   build anymore).
# - External modules (like Wireguard or tp_smapi) also need to be
#   cross compiled. So modules for linux-libre-64 need to be added
#   (like tp_smapi-64) and dkms packages need to be adapted to also
#   work for this kernel.
# - Some software (like building Guix) break with 32bit rootfs and
#   64bit kernels due to wrong assumption about the number of bits,
#   through other software (like building rust with mrustc) work
#   better (here because we have 3GiB instead of 3GiB of memory per
#   process). Most software is not affected though.

# WIP: This PKGBUILD is derived from the 'linux-libre' PKGBUILD,
#      and includes a lot of unused cruft, to minimize the diff.
#      Eventually, these differences can be merged into linux-libre,
#      once parabola has an 'aarch64' build target and repos.
#
#      For now, 'aarch64' is not yet a valid parabola target.
#      This PKGBUILD exists only for making an 'aarch64' kernel for use with
#      parabola armv7h systems. This PKGBUILD is intended to be cross-compiled
#      only from an armv7h librechroot. The per-arch mechanism for armv7h
#      is over-ridden for the special purpose of the cross-compilation.
#
#      The makedepends_armv7h, source_armv7h, and sha512sums_armv7h arrays
#      (also source_aarch64 and sha512sums_aarch64 obviously), and the comments:
#        '# cross-compile over-ride' and
#        '# TODO: merge into 'linux-libre' PKGBUILD',
#      indicate code that would eventually be deleted, merged into,
#      or associated with $CARCH: 'aarch64', if/when aarch64 becomes a valid target.
#      All other LOCs of consequence in this file,
#      should be identical to the linux-libre PKGBUILD.
#      In this way, the diff is minimal against the linux-libre PKGBUILD;
#      and it is obvious which parts are intended to become aarch64-specific,
#      or exist only for cross-compiling the 64-bit foreign kernels for armv7h and i686.
#      After that, this PKGBUILD could be simplified, to focus only on the cross-compiled kernels.
# comments:
# TODO: repalce in 'linux-libre' PKGBUILD
# cross-compile over-ride
# TODO: merge into 'linux-libre' PKGBUILD


# TODO: This is a bug (in the 'linux-libre' PKGBUILD) for migrations from archlinuxarm.
#       _replacesarchkernel assumes that the upstream kernel packages are named like:
#         - 'linux'          'linux-headers'
#         - 'linux-hardened' 'linux-hardened-headers'
#         - 'linux-lts'      'linux-lts-headers'
#       That is true for arch and arch32; but archlinuxarm has no packages by those names.
#       Archlinuxarm systems would have a kernel package named like:
#         - 'linux-armv7'            'linux-armv7-headers'
#         - 'linux-armv7-chromebook'
#         - 'linux-veyron'           'linux-veyron-headers'
#         - 'linux-aarch64'          'linux-aarch64-headers'
#       so Parabola armv7h and aarch64 kernels would not replace any archlinuxarm kernel.
_replacesarchkernel=('linux%') # '%' gets replaced with kernel suffix
_replacesoldkernels=() # '%' gets replaced with kernel suffix
_replacesoldmodules=() # '%' gets replaced with kernel suffix

pkgbase=linux-libre-64 # cross-compile over-ride
pkgver=5.18.5
pkgrel=2
pkgdesc="The Linux-libre kernel and modules" # TODO: merge into 'linux-libre' PKGBUILD
rcnver=5.18.2
rcnrel=armv7-x6
url='https://linux-libre.fsfla.org/'
arch=(i686 x86_64 armv7h)
arch=(armv7h i686) # cross-compile over-ride
license=(GPL2)
conflicts_armv7h=(linux-libre-aarch64) # cross-compile over-ride
replaces_armv7h=( linux-libre-aarch64) # cross-compile over-ride
conflicts_i686=(  linux-libre-x86_64 ) # cross-compile over-ride
replaces_i686=(   linux-libre-x86_64 ) # cross-compile over-ride
makedepends=(
  bc libelf pahole cpio perl tar xz
  xmlto python-sphinx python-sphinx_rtd_theme graphviz imagemagick texlive-latexextra
)
makedepends_i686+=('x86_64-pc-linux-gnu-gcc')
makedepends_armv7h=(kmod uboot-tools vboot-utils dtc) # required by linux-libre-chromebook
makedepends_armv7h+=(aarch64-linux-gnu-gcc-initial) # cross-compile over-ride
options=('!strip')
_srcname=linux-5.18
source=(
  "https://linux-libre.fsfla.org/pub/linux-libre/releases/${_srcname##*-}-gnu/linux-libre-${_srcname##*-}-gnu.tar.xz"{,.sign}
  "https://linux-libre.fsfla.org/pub/linux-libre/releases/$pkgver-gnu/patch-${_srcname##*-}-gnu-$pkgver-gnu.xz"{,.sign}
  "https://repo.parabola.nu/other/linux-libre/logos/logo_linux_"{clut224.ppm,vga16.ppm,mono.pbm}{,.sig}
  # TODO: Move (in linux-libre) config.i686 config.armv7h, linux-armv7h.preset,
  #       and "kernel"{.its,.keyblock,_data_key.vbprivk}
  #       into their respective source_<CARCH> arrays.
  #       Rename linux-armv7h.preset to linux-arm.preset, or add a new one for aarch64.
  config.i686 config.x86_64 config.armv7h    # the main kernel config files
  linux-armv7h.preset                        # armv7h preset file for mkinitcpio ramdisk
  "kernel"{.its,.keyblock,_data_key.vbprivk} # files for signing Chromebooks kernels

  # maintain the TTY over USB disconnects
  # http://www.coreboot.org/EHCI_Gadget_Debug
  0001-usb-serial-gadget-no-TTY-hangup-on-USB-disconnect-WI.patch
  # fix Atmel maXTouch touchscreen support
  # https://labs.parabola.nu/issues/877
  # http://www.fsfla.org/pipermail/linux-libre/2015-November/003202.html
  0002-fix-Atmel-maXTouch-touchscreen-support.patch
  # Arch Linux patches
  0001-ZEN-Add-sysctl-and-CONFIG-to-disallow-unprivileged-C.patch
  0002-HID-apple-Properly-handle-function-keys-on-Keychron-.patch
)
source_i686=(
  # avoid using zstd compression in ultra mode (exhausts virtual memory)
  # Arch Linux 32 patches
  no-ultra-zstd.patch
)
source_armv7h=(
  # RCN patch (CM3 firmware deblobbed and bloatware removed)
  "https://repo.parabola.nu/other/rcn-libre/patches/$rcnver/rcn-libre-$rcnver-$rcnrel.patch"{,.sig}
  # Arch Linux ARM patches (armv7h)
  0001-ARM-atags-add-support-for-Marvell-s-u-boot.patch
  0002-ARM-atags-fdt-retrieve-MAC-addresses-from-Marvell-bo.patch
  0003-fix-mvsdio-eMMC-timing.patch
  0004-net-smsc95xx-Allow-mac-address-to-be-set-as-a-parame.patch
  0005-set-default-cubietruck-led-triggers.patch
  0006-exynos4412-odroid-set-higher-minimum-buck2-regulator.patch
  0007-USB-Armory-MkII-support.patch
  # ChromiumOS patches
  0001-CHROMIUM-block-partitions-efi-Add-support-for-IGNORE.patch
)
source_aarch64=(
  config.aarch64
  # Arch Linux ARM patches (aarch64)
  0001-net-smsc95xx-Allow-mac-address-to-be-set-as-a-parame.patch
  0002-arm64-dts-rockchip-disable-pwm0-on-rk3399-firefly.patch
)
source_armv7h=( ${source_aarch64[*]} ) # cross-compile over-ride
validpgpkeys=(
  '474402C8C582DAFBE389C427BCB7CF877E7D47A7' # Alexandre Oliva
  '6DB9C4B4F0D8C0DC432CF6E4227CA7C556B2BA78' # David P.
)
sha512sums=('13be3762fffd74c63eeb23b0d34b994a3e5198bfdbda4f013b38f8d3edd24b9bbebe5a4bfde0f5191aa1cf2678e4517f3b5540a40b30ebc05da1f6708cbb98bb'
            'SKIP'
            '9355c0f22606c0401e8a01d6e22f0fa6e97b69a9fce6d991235da6430907fb6a788fca30bd48e5cbb743cc4a211a40806c896f660db7432e7bc85ba7634b5d06'
            'SKIP'
            '13cb5bc42542e7b8bb104d5f68253f6609e463b6799800418af33eb0272cc269aaa36163c3e6f0aacbdaaa1d05e2827a4a7c4a08a029238439ed08b89c564bb3'
            'SKIP'
            '7a3716bfe3b9f546da309c7492f3e08f8f506813afeb1c737a474c83313d5c313cf4582b65215c2cfce3b74d9d1021c96e8badafe8f6e5b01fe28d2b5c61ae78'
            'SKIP'
            '267295aa0cea65684968420c68b32f1a66a22d018b9d2b2c1ef14267bcf4cb68aaf7099d073cbfefe6c25c8608bdcbbd45f7ac8893fdcecbf1e621abdfe9ecc1'
            'SKIP'
            'bcec71d3bd1daaa27bdf4c9a10f0b302b915e8236ef47b5e6d6eaf65bfd0b34644d8d3ff6c25d894ae21dedbece3c1f1ff152fbf8525b77100bc790d0541f3f1'
            'e141c387e8045030278d683a1294bfd3d13197bf3466a9c89c4f72a53aafed606acc8e42e528479527c638af895bce2303944b5d8b8a29040f5aae8a9581dd5a'
            '47d16ffc94510d4a8773146a46cfb35aca8cfdae38d17283334cd62d92de36250fbec90e9892357033398ecc7d970127b1a41b703a8372972422ca4af7c90c70'
            '53103bf55b957b657039510527df0df01279dec59cda115a4d6454e4135025d4546167fa30bdc99107f232561c1e096d8328609ab5a876cf7017176f92ad3e0b'
            'f10af02f0cb2d31259d9633e1ba845f555f525789f750fc2ddc51bd18c5ff64fcdd242dae801623887f5ce5cdb5528bce890459f0fab9fd31a28868bb7f6bba5'
            'bb6718984a7357c9b00c37e4788480e5b8b75018c172ecc1441bc3fc5d2d42444eb5d8c7f9d2e3a7d6fed6d03acb565e3c0559486e494c40a7fe6bd0570c9ede'
            '143dea30c6da00e504c99984a98a0eb2411f558fcdd9dfa7f607d6c14e9e7dffff9cb00121d9317044b07e3e210808286598c785ee854084b993ec9cb14d8232'
            '02af4dd2a007e41db0c63822c8ab3b80b5d25646af1906dc85d0ad9bb8bbf5236f8e381d7f91cf99ed4b0978c50aee37cb9567cdeef65b7ec3d91b882852b1af'
            'b8fe56e14006ab866970ddbd501c054ae37186ddc065bb869cf7d18db8c0d455118d5bda3255fb66a0dde38b544655cfe9040ffe46e41d19830b47959b2fb168'
            '1b6c3108d5628ca6db20c2bc3431e560ab2a274aaf863e7a796f1931a35982d175247f47cbfbb7d643ffc6b742442806755bb10339e5b0577c3f232b38288891'
            'bf6d9a66be49c2cc67b0a0f3cfb61734adf7a3b032b5ed133e1e0afbf4a6ddbd3e8231e4b85fef2e3aeb7274a60f7572a4469c057ff13d81134613f01767d3b2')
sha512sums_i686=('bca15cc96f64c38adcd13a46752866b5b30555ac21e19b3f7afcd20fcb7ec585c9d990fe8f842f44d5f69d477d72867fe6a9102729f26f93f5a80b372e41ce85')
sha512sums_armv7h=('5a75b12dd386940a0bf1be630d45a514ef3c32289ec5976988764baa8483b254e5dcc879337556bfa041b6dbf9ac16debbe4b57bf86db30089661e9536ffaa0a'
                   'SKIP'
                   '8da996a42249672893fa532ccbd096347580a0dc1698c45e9c865646e2765789553b1bb42793e721de30aea70340fdc116d2e4a50580fef999ca5fc627aaf4c3'
                   '0e6ddc24011d77a2e422b642c4507317fc2d26b20f5649818a2f11acac165ccab2cf2e64ab50d44ce7affcfe12c2ef5158790e499058831e7995400b2087df78'
                   '5d2f228151148358b07744a9c09cf6e2420c4284735d4ce7dc925a1deb3970298cc95dd766ee13e094e2853847b016c3ebc2d61bb423e370b4bc33938367ebda'
                   'a206c3dc678839831d72eefee997cbe1bb877af7f6f90dfe5cab0bd7d9686a3b13a2aba74c8fbe5533872e12306ac9ea1b5eede18ef3cf10b8d01361afd67e9c'
                   'a1072dd808c63592178cc01ea3c36de946f277fb451b13c34f51a5ac134cde4a8a4d57487132af1c6d7b5820842cdda9dc4edb3da85d33bd02bf87a032c3263a'
                   '85b2e16a930b8066990c42f973d386dc3c6d62fdf3d1289bbb51df3df296c26f30051162fba49a42d38f71f05de926aa8c065097f14bd8e9f8e28a52949ead00'
                   '2e99582d8b670b6c6b27add14b60e957cbcf9cd6aae40491bd9dea6ac455c0ade0bbee21e3bdfb5e06ce83ad27c5a788404b05bf7ac93831ca18d2e60a67017c'
                   '5b77c587cf2ffb60acf1d9eb43330983548c7e81a53e0ffd2b04962a99441a0000d631ff77c245c7062afc2c8368d996a2456496d42dcb658f330e5083e5e029')
sha512sums_aarch64=('0510d1ad02d0ed0dbc03084fa36ae27b869eec8efe74dbe063b76a0b5ff09828c728baa17b89d20d092b310f3142656bcd9c66f006a6b7800ecd36550b6e15be'
                    '69e81aff3ac5aada75f7330e8c82d0a6aa229585b3f442b2e1a9b86681b0eadc925408d5e2377026599fb0dee72d29043eca4d072b0e2606adc34c872e95c7d6'
                    '4224779106cc520dc2c431322d9693cc968476706854ae9630611cf2896c48a34ca11bf31a4c3995573a128cf5838a6763bac86eb4cbaf509ade9b6a83a24369')
sha512sums_armv7h=( ${sha512sums_aarch64[*]} ) # cross-compile over-ride
_replacesarchkernel=("${_replacesarchkernel[@]/\%/${pkgbase#linux-libre}}")
_replacesoldkernels=("${_replacesoldkernels[@]/\%/${pkgbase#linux-libre}}")
_replacesoldmodules=("${_replacesoldmodules[@]/\%/${pkgbase#linux-libre}}")
_replacesarchkernel=("${_replacesarchkernel[@]/-64/}") # cross-compile over-ride

case "$CARCH" in
  armv7h) KARCH=arm64 ; CROSS_COMPILE=aarch64-linux-gnu- ;; # cross-compile over-ride
  aarch64) KARCH=arm64;; # TODO: merge into 'linux-libre' PKGBUILD
  i686) KARCH=x86;CROSS_COMPILE=x86_64-pc-linux-gnu-;; # cross-compile over-ride
  i686|x86_64) KARCH=x86;;
  armv7h) KARCH=arm;;
esac
pkgdesc+=" 64-bit (${CROSS_COMPILE%%-*}) for Parabola ${CARCH}" # cross-compile over-ride

export KBUILD_BUILD_HOST=parabola
export KBUILD_BUILD_USER=$pkgbase
export KBUILD_BUILD_USER=${pkgbase/-64/} # cross-compile over-ride
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

prepare() {
  cd $_srcname

  if [ "${_srcname##*-}" != "$pkgver" ]; then
    echo "Applying upstream patch..."
    patch -Np1 < "../patch-${_srcname##*-}-gnu-$pkgver-gnu"
  fi

  echo "Adding freedo as boot logo..."
  install -m644 -t drivers/video/logo \
    ../logo_linux_{clut224.ppm,vga16.ppm,mono.pbm}

  echo "Setting version..."
  scripts/setlocalversion --save-scmversion
  echo "-$pkgrel" > localversion.10-pkgrel
  echo "${pkgbase#linux-libre}" > localversion.20-pkgname

  if [ "$CARCH" = "armv7h" ]; then
    local src_armv7h
    for src_armv7h in "${source_armv7h[@]}"; do
      src_armv7h="${src_armv7h%%::*}"
      src_armv7h="${src_armv7h##*/}"
      [[ $src_armv7h = *.patch ]] || continue
      echo "Applying patch $src_armv7h..."
      patch -Np1 < "../$src_armv7h"
    done
  fi

  if [ "$CARCH" = "i686" ]; then
    local src_i686
    for src_i686 in "${source_i686[@]}"; do
      src_i686="${src_i686%%::*}"
      src_i686="${src_i686##*/}"
      [[ $src_i686 = *.patch ]] || continue
      echo "Applying patch $src_i686..."
      patch -Np1 < "../$src_i686"
    done
  fi

  if [ "$CARCH" = "aarch64" ]; then # TODO: merge into 'linux-libre' PKGBUILD
                                    # TODO: better yet, all of these per-arch patching routines should be DRY-ed out
    local src_aarch64
    for src_aarch64 in "${source_aarch64[@]}"; do
      src_aarch64="${src_aarch64%%::*}"
      src_aarch64="${src_aarch64##*/}"
      [[ $src_aarch64 = *.patch ]] || continue
      echo "Applying patch $src_aarch64..."
      patch -Np1 < "../$src_aarch64"
    done
  fi

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  echo "Setting config..."
  cp ../config.$CARCH .config
  # config.aarch64 comes straight from Arch Linux ARM and doesn't have the
  # compatibility for 32bit rootfs enabled. This makes maintenance faster than
  # having to modify the configuration file by hand at each new kernel release.
  if [[ "$KARCH" == 'arm64' ]]                                             # cross-compile over-ride
  then cp ../config.aarch64 .config ; echo 'CONFIG_COMPAT=y' >> .config ;  # cross-compile over-ride
  fi                                                                       # cross-compile over-ride
  make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE olddefconfig               # cross-compile over-ride
  diff -u ../config.$CARCH .config || :

  make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE -s kernelrelease > version # cross-compile over-ride
  echo "Prepared $pkgbase version $(<version)"
}

build() {
  cd $_srcname
  make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE all      # cross-compile over-ride
  make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE htmldocs # cross-compile over-ride
}

_package() {
  pkgdesc="The $pkgdesc kernel and modules"
  depends=(coreutils kmod initramfs)
  optdepends=('wireless-regdb: to set the correct wireless channels of your country'
              'linux-libre-firmware: firmware images needed for some devices')
  provides=(VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE)
  conflicts=() # TODO: merge into 'linux-libre' PKGBUILD (conflicts array is appended before declared)
  replaces=(virtualbox-guest-modules-arch wireguard-arch)
  provides+=("${_replacesarchkernel[@]/%/=$pkgver}" "LINUX-ABI_VERSION=$pkgver")
  conflicts+=("${_replacesarchkernel[@]}" "${_replacesoldkernels[@]}" "${_replacesoldmodules[@]}")
  replaces+=("${_replacesarchkernel[@]}" "${_replacesoldkernels[@]}" "${_replacesoldmodules[@]}")
  # cross-compile over-rides
  # NOTE: Above is providing an upstream 'linux{,-*}' (eg: linux, linux-hardened),
  #       and replacing the same during migration to Parabola.
  #       Below is providing 'linux-libre' for 'parabola-base',
  #       and replacing the obsoleted parabola 'linux-libre-{aarch64,x86_64}'.
  #       These are defined analogously in _package-headers(), _package-docs(), and _package-chromebook().
  provides+=( linux-libre             )    # cross-compile over-ride
  conflicts=( ${conflicts[*]/linux*/} )    # cross-compile over-ride
  replaces=(  ${replaces[*]/linux*/}  )    # cross-compile over-ride
  conflicts_armv7h=( linux-libre-aarch64 ) # cross-compile over-ride
  replaces_armv7h=(  linux-libre-aarch64 ) # cross-compile over-ride
  conflicts_i686=(   linux-libre-x86_64  ) # cross-compile over-ride
  replaces_i686=(    linux-libre-x86_64  ) # cross-compile over-ride

  cd $_srcname
  local kernver="$(<version)"
  local modulesdir="$pkgdir/usr/lib/modules/$kernver"

  echo "Installing boot image..."
  # systemd expects to find the kernel here to allow hibernation
  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
  # The default arm64 image target is 'Image.gz' (per `make -s image_name`);
  # but some u-boot configs/versions don't support that, so we build 'Image' instead.
  # TODO: merge into 'linux-libre' PKGBUILD (actually [[ "$CARCH" == 'aarch64' ]])
  [[ "$KARCH" == 'arm64' ]]                                   && # cross-compile over-ride
  install -Dm644 arch/$KARCH/boot/Image "$modulesdir/vmlinuz" ||
  install -Dm644 "$(make -s image_name)" "$modulesdir/vmlinuz"

  # FIXME: aarch64 - the kernel is not installed under /boot
  if   [[ "$KARCH" == 'arm64' && ! -f "$pkgdir/boot/Image" ]]
  then echo "FIXME: kernel was not installed to \$pkgdir/boot"
       install -Dm644 "$modulesdir/vmlinuz" "$pkgdir/boot/vmlinuz-${basename}-64"
  fi

  # Used by mkinitcpio to name the kernel
  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

  echo "Installing modules..."
  make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 modules_install # cross-compile over-ride

  # remove build and source links
  rm "$modulesdir"/{source,build}

  if [ "$CARCH" = "armv7h" ]; then
    echo "Installing device tree binaries..."
    make ARCH=$KARCH CROSS_COMPILE=$CROSS_COMPILE INSTALL_DTBS_PATH="$pkgdir/boot/dtbs/$pkgbase" dtbs_install # cross-compile over-ride

    # armv7h presets only work with ALL_kver=$kernver
    # TODO: kernel version auto-detection does not work for ARM kernels.
    #       it may be best to not use auto-detection at all, and use the same mechanism
    #       for all arches, eg: System.map or somthing simple like this below.
    backup=("etc/mkinitcpio.d/$pkgbase.preset")
    echo "Installing mkinitcpio preset..."
    sed "s|%PKGBASE%|$pkgbase|g;s|%KERNVER%|$kernver|g" ../linux-armv7h.preset \
      | install -Dm644 /dev/stdin "$pkgdir/etc/mkinitcpio.d/$pkgbase.preset"
  fi
}

_package-headers() {
  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
  depends=(pahole)
  provides=("${_replacesarchkernel[@]/%/-headers=$pkgver}")
  conflicts=("${_replacesarchkernel[@]/%/-headers}" "${_replacesoldkernels[@]/%/-headers}")
  replaces=("${_replacesarchkernel[@]/%/-headers}" "${_replacesoldkernels[@]/%/-headers}")
  # cross-compile over-rides - see note in _package()
  conflicts=()                                     # cross-compile over-ride
  replaces=()                                      # cross-compile over-ride
  conflicts_armv7h=( linux-libre-aarch64-headers ) # cross-compile over-ride
  replaces_armv7h=(  linux-libre-aarch64-headers ) # cross-compile over-ride

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  echo "Installing build files..."
  install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
    localversion.* version vmlinux
  install -Dt "$builddir/kernel" -m644 kernel/Makefile
  install -Dt "$builddir/arch/$KARCH" -m644 arch/$KARCH/Makefile
  if [[ $CARCH = i686 ]]; then
    install -Dt "$builddir/arch/$KARCH" -m644 arch/$KARCH/Makefile_32.cpu
  fi
  cp -t "$builddir" -a scripts

  # required when STACK_VALIDATION is enabled
  if [[ -e tools/objtool/objtool ]]; then
    install -Dt "$builddir/tools/objtool" tools/objtool/objtool
  fi

  # required when DEBUG_INFO_BTF_MODULES is enabled
  if [[ -e tools/bpf/resolve_btfids/resolve_btfids ]]; then
    install -Dt "$builddir/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids
  fi

  echo "Installing headers..."
  cp -t "$builddir" -a include
  cp -t "$builddir/arch/$KARCH" -a arch/$KARCH/include
  install -Dt "$builddir/arch/$KARCH/kernel" -m644 arch/$KARCH/kernel/asm-offsets.s

  # from linux-armv7
  [[ "$KARCH" != 'arm64' ]] && # cross-compile  over-ride
  if [ "$CARCH" = "armv7h" ]; then
    for i in dove omap2; do
      mkdir -p "$builddir/arch/$KARCH/mach-$i"
      cp -t "$builddir/arch/$KARCH/mach-$i/" -a arch/$KARCH/mach-$i/include
    done
    for i in omap orion versatile; do
      mkdir -p "$builddir/arch/$KARCH/plat-$i"
      cp -t "$builddir/arch/$KARCH/plat-$i/" -a arch/$KARCH/plat-$i/include
    done
  fi

  install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
  install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

  # https://bugs.archlinux.org/task/13146
  install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

  # https://bugs.archlinux.org/task/20402
  install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
  install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
  install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

  # https://bugs.archlinux.org/task/71392
  install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

  echo "Installing KConfig files..."
  find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

  echo "Removing unneeded architectures..."
  local arch
  for arch in "$builddir"/arch/*/; do
    [[ $arch = */$KARCH/ ]] && continue
    echo "Removing $(basename "$arch")"
    rm -r "$arch"
  done

  echo "Removing documentation..."
  rm -r "$builddir/Documentation"

  echo "Removing broken symlinks..."
  find -L "$builddir" -type l -printf 'Removing %P\n' -delete

  echo "Removing loose objects..."
  find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

  strip() { ${CROSS_COMPILE}strip "${@}" ; } ; # cross-compile over-ride

  echo "Stripping build tools..."
  local file
  while read -rd '' file; do
    case "$(file -bi "$file")" in
      application/x-sharedlib\;*)      # Libraries (.so)
        strip -v $STRIP_SHARED "$file" ;;
      application/x-archive\;*)        # Libraries (.a)
        strip -v $STRIP_STATIC "$file" ;;
      application/x-executable\;*)     # Binaries
        strip -v $STRIP_BINARIES "$file" ;;
      application/x-pie-executable\;*) # Relocatable binaries
        strip -v $STRIP_SHARED "$file" ;;
    esac
  done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

  echo "Stripping vmlinux..."
  strip -v $STRIP_STATIC "$builddir/vmlinux"

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/src"
  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
}

_package-docs() {
  pkgdesc="Documentation for the $pkgdesc kernel"
  provides=("${_replacesarchkernel[@]/%/-docs=$pkgver}")
  conflicts=("${_replacesarchkernel[@]/%/-docs}" "${_replacesoldkernels[@]/%/-docs}")
  replaces=("${_replacesarchkernel[@]/%/-docs}" "${_replacesoldkernels[@]/%/-docs}")
  # cross-compile over-rides - see note in _package()
  conflicts=()                                # cross-compile over-ride
  replaces=()                                 # cross-compile over-ride
  conflicts_armv7h=(linux-libre-aarch64-docs) # cross-compile over-ride
  replaces_armv7h=( linux-libre-aarch64-docs) # cross-compile over-ride

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  echo "Installing documentation..."
  local src dst
  while read -rd '' src; do
    dst="${src#Documentation/}"
    dst="$builddir/Documentation/${dst#output/}"
    install -Dm644 "$src" "$dst"
  done < <(find Documentation -name '.*' -prune -o ! -type d -print0)

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/share/doc"
  ln -sr "$builddir/Documentation" "$pkgdir/usr/share/doc/$pkgbase"
}

_package-chromebook() {
  pkgdesc="$pkgdesc kernel sign for Veyron Chromebooks"
  depends=(linux-libre=$pkgver)
  provides=("${_replacesarchkernel[@]/%/-armv7-chromebook=$pkgver}")
  conflicts=("${_replacesarchkernel[@]/%/-armv7-chromebook}" "${_replacesoldkernels[@]/%/-armv7-chromebook}")
  replaces=("${_replacesarchkernel[@]/%/-armv7-chromebook}" "${_replacesoldkernels[@]/%/-armv7-chromebook}")
  install=$pkgbase-chromebook.install
  # NOTE: when aarch64 becomes a supported target, 'armv7' above should be changed to "${CARCH}"
  depends=(   ${depends[*]/linux-libre/${pkgbase}} ) # cross-compile over-ride
  provides=(  ${provides[*]/armv7/aarch64}         ) # cross-compile over-ride
  conflicts=( ${conflicts[*]/armv7/aarch64}        ) # cross-compile over-ride
  replaces=(  ${replaces[*]/armv7/aarch64}         ) # cross-compile over-ride

  cd $_srcname

  cp ../kernel.its .
  mkimage -D "-I dts -O dtb -p 2048" -f kernel.its kernel.signed
  dd if=/dev/zero of=bootloader.bin bs=512 count=1
  echo 'console=tty0 init=/sbin/init root=PARTUUID=%U/PARTNROFF=1 rootwait rw noinitrd' > cmdline

  # TODO: merge into 'linux-libre' PKGBUILD (--arch ${KARCH})
  echo "Creating kernel sign..."
  vbutil_kernel \
    --pack vmlinux.kpart \
    --version 1 \
    --vmlinuz kernel.signed \
    --arch ${KARCH} \
    --keyblock ../kernel.keyblock \
    --signprivate ../kernel_data_key.vbprivk \
    --config cmdline \
    --bootloader bootloader.bin

  echo "Installing kernel sign..."
  mkdir -p "$pkgdir/boot"
  cp vmlinux.kpart "$pkgdir/boot"
}

pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-docs")
# The chromebook package is disabled until the following error is fixed:
# |  ==> Starting package_linux-libre-64-chromebook()...
# |  FATAL ERROR: Couldn't open "arch/arm/boot/zImage": No such file or directory
# |  mkimage: Can't read kernel.signed.tmp: Invalid argument
# [[ $CARCH = armv7h ]] && pkgname+=("$pkgbase-chromebook")
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done

# vim:set ts=8 sts=2 sw=2 et: