summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 093e07be45daea9571b995514efb645cab3ddc4f (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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
# Maintainer: Toolybird <toolybird at tuta dot io>
#
# WORK IN PROGRESS
#
# <rant>This thing is a packaging nightmare!! The upstream build system is
# *especially* distro unfriendly (build on Centos 7...WTF?). Building from
# source to proper Arch standards is "interesting" to say the least.</rant>
#
# NOTE: upstream uses losetup (root) for disk image preparation and GRUB
# installation. Solved by utilizing libguestfs/QEMU.
#
# NOT INCLUDED (compared to upstream): ARM64, MIPS64, LiveCD.
#
# PROBLEMS: FIXME
#
# - ancient pkg versions used in the build
# - includes bundled / vendored sources
# - some third party / pre-compiled / downloaded binaries are used
#
# This PKGBUILD is based on "INSTALL/all_in_one.sh". The upstream build
# environment is Centos 7 (as root!). See "DOC/BuildVentoyFromSource.txt" and
# GitHub CI/docker files. The Ventoy code is unfortunately built upon very old
# and outdated pkgs. In order to achieve an initial working build, I've simply
# tried to replicate upstream procedures as much as possible. Some patches are
# required to successfully build on a modern Arch system. The main components of
# Ventoy are heavily modified versions of:
#
# - grub (Jul 2019) (2.04)
# - ipxe (Sep 2019) (3fe683e)
# - edk2 (Dec 2019) (201911)
#
# Please see the "License" folder for full details of various other bits that
# have been modified.

pkgname=ventoy
pkgver=1.0.99
_grub_ver=2.04                  # (Jul 2019)
#_unifont_ver=15.0.01            # FIXME see NOTE below
_ipxe_ver=3fe683e               # (Sep 29 2019)
_edk2_ver=stable201911          # (Dec 2019)
_diet_ver=0.34                  # FIXME AUR pkg needs a patch, see below, build our own (64/32)
_musl_ver=1.2.5                 # upstream uses 1.2.1, use repo for 64-bit, build our own 32-bit
_kern_hdrs_musl_ver=4.19.88     # for busybox 32-bit
_fuse_ver=2.9.9                 # need a static lib built against musl, build our own (64/32)
_exfat_ver=1.3.0                # (Sep 2018) old! FIXME see comments below for why we build our own
_lz4_ver=1.8.1.2                # (Jan 2018) old! FIXME statically linked into unsquashfs
_xz_ver=5.0.4                   # (Jun 2012) old! FIXME statically linked into unsquashfs
_lzo_ver=2.08                   # (Jun 2014) old! FIXME statically linked into unsquashfs
_zstd_ver=1.4.4                 # (Nov 2019) old! FIXME statically linked into unsquashfs
_zlib_ver=1.3.1                 # need a static 32-bit lib for unsquashfs, build our own
_squash_ver=4.4                 # (Aug 2019) old! FIXME for unsquashfs
_dm_ver=1.02.28                 # (Sep 2008) old! FIXME for dmsetup
_zstd1_ver=1.0.0                # (Sep 2016) old! FIXME for standalone zstdcat
_xz_embed_ver=20130513          # (May 2013) old! FIXME for xzminidec
_busybox_ver=1.32.0             # (Jun 2020) old! FIXME
_crypt_ver=1.7.5                # (Apr 2017) old! FIXME for veritysetup
_lunzip_ver=1.11                # (Jan 2019) old! FIXME
_wimboot_ver=2.7.3              # (Apr 2021) old! FIXME
pkgrel=3
pkgdesc="A new bootable USB solution"
arch=(x86_64)
url="https://www.ventoy.net/"
license=(GPL-3.0-or-later)
depends=(bash dosfstools)
makedepends=(
  acpica                # edk2
  cdrtools              # EfiISO
  cpio                  # IMG/USB prep
  device-mapper         # grub2
  freetype2             # grub2
  fuse2                 # grub2 exfat
  git                   # ipxe
  gtk3                  # GUI
  kernel-headers-musl   # busybox
  lib32-gcc-libs        # 32-bit squashfs-tools lz4 xz lzo zstd vblade
  lib32-glibc           # 32-bit dietlibc vtoytool dmsetup zstdcat xzminidec busybox lunzip
  libguestfs            # IMG/USB prep
  linux                 # libguestfs
  musl                  # vtoycli fuse fuseiso xzminidec busybox
  nasm                  # edk2
  python                # grub2 edk2
  python-setuptools     # edk2 (python-3.12+)
  qt5-base              # GUI
  ttf-dejavu            # grub2
)
optdepends=(
  'gtk3: for GTK GUI'
  'parted: for preferred CLI partitioner'
  'polkit: for GUI privilege escalation'
  'qt5-base: for Qt GUI'
)
conflicts=(ventoy-bin)
# A regression in latest pacman unsets MAKEFLAGS when using !buildflags.
# https://gitlab.archlinux.org/archlinux/packaging/packages/pacman/-/issues/25
# Workaround it for now by manually unsetting VARS. See (way down) below in build() function.
#options=(!buildflags)
source=(
  "$pkgname-$pkgver.tar.gz::https://github.com/ventoy/Ventoy/archive/refs/tags/v$pkgver.tar.gz"
  https://ftp.gnu.org/gnu/grub/grub-"$_grub_ver".tar.xz
  ventoy-grub-fix-build-with-binutils-2.36.patch::https://git.savannah.gnu.org/cgit/grub.git/patch/configure.ac?id=b9827513
  # https://ftp.gnu.org/gnu/unifont/unifont-"$_unifont_ver/unifont-$_unifont_ver".bdf.gz
  git+https://github.com/ipxe/ipxe.git#commit="$_ipxe_ver"
  https://github.com/tianocore/edk2/archive/refs/tags/edk2-"$_edk2_ver".zip
  ventoy-fix-ucs-2-lookup-on-python-3.9.patch::https://github.com/tianocore/edk2/commit/5d864834.patch
  ventoy-fix-array.array.tostring-removal-in-python-3.9.patch::https://github.com/tianocore/edk2/commit/43bec9ea.patch
  https://www.fefe.de/dietlibc/dietlibc-"$_diet_ver".tar.xz
  dietlibc-headers-fix.patch::https://salsa.debian.org/debian/dietlibc/-/raw/master/debian/patches/bugfixes/newer-linux-headers.diff
  https://musl.libc.org/releases/musl-"$_musl_ver".tar.gz
  kernel-headers-musl-"$_kern_hdrs_musl_ver".tar.gz::https://github.com/sabotage-linux/kernel-headers/archive/v"$_kern_hdrs_musl_ver".tar.gz
  https://github.com/libfuse/libfuse/releases/download/fuse-"$_fuse_ver/fuse-$_fuse_ver".tar.gz
  exfat-"$_exfat_ver".tar.gz::https://github.com/relan/exfat/archive/refs/tags/v"$_exfat_ver".tar.gz
  https://github.com/madler/zlib/releases/download/v"$_zlib_ver/zlib-$_zlib_ver".tar.xz
  squashfs-tools-"$_squash_ver".tar.gz::https://github.com/plougher/squashfs-tools/archive/refs/tags/"$_squash_ver".tar.gz
  http://vault.centos.org/5.3/os/SRPMS/device-mapper-"$_dm_ver"-2.el5.src.rpm
  zstd-"$_zstd1_ver".tar.gz::https://github.com/facebook/zstd/archive/refs/tags/v"$_zstd1_ver".tar.gz
  https://tukaani.org/xz/xz-embedded-"$_xz_embed_ver".tar.gz
  https://busybox.net/downloads/busybox-"$_busybox_ver".tar.bz2
  https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/v"${_crypt_ver%.*}"/cryptsetup-"$_crypt_ver".tar.xz
  wimboot-"$_wimboot_ver".tar.gz::https://github.com/ipxe/wimboot/archive/v"$_wimboot_ver".tar.gz
  wimboot-binutils-2.42-fix.patch
)
noextract=(
  grub-"$_grub_ver".tar.xz
  edk2-"$_edk2_ver".zip
  fuse-"$_fuse_ver".tar.gz
  exfat-"$_exfat_ver".tar.gz
  zlib-"$_zlib_ver".tar.xz
  squashfs-tools-"$_squash_ver".tar.gz
  device-mapper-"$_dm_ver"-2.el5.src.rpm
  zstd-"$_zstd1_ver".tar.gz
  xz-embedded-"$_xz_embed_ver".tar.gz
  busybox-"$_busybox_ver".tar.bz2
  cryptsetup-"$_crypt_ver".tar.xz
  wimboot-"$_wimboot_ver".tar.gz
)
sha256sums=('2557ccdeaf8b0e517087afb63f65c4e2a32ef3f15bd196b7f828d93d586ca7b9'
            'e5292496995ad42dabe843a0192cf2a2c502e7ffcc7479398232b10a472df77d'
            '789851a70038c9ed2db61181cd1c20243b41b246a8efe77f2af8a41fb22a52c9'
            '5ee49d23d376aeea24269f7605fcaa7fbd326c04cda4e31b8eb7fa15a540ef44'
            'c6f691aa91afbaab811a369fe729f61d8e5b58bb5ad79a45446c9ee849c1a60b'
            'SKIP' # Cannot rely on GitHub to maintain a stable patch checksum
            'SKIP' # Cannot rely on GitHub to maintain a stable patch checksum
            '7994ad5a63d00446da2e95da1f3f03355b272f096d7eb9830417ab14393b3ace'
            '313aa962c7f80a02f41758d90d6f67687c77c74a6126b060337f248bc1b637f6'
            'a9a118bbe84d8764da0ea0d28b3ab3fae8477fc7e4085d90102b8596fc7c75e4'
            'd104397fc657ffb0f0bda46f54fd182b76a9ebc324149c183a4ff8c86a8db53d'
            'd0e69d5d608cc22ff4843791ad097f554dd32540ddc9bed7638cc6fea7c1b4b5'
            '689bcb4a639acd2d45e6fa0ff455f7f18edb2421d4f4f42909943775adc0e375'
            '38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32'
            'a7fa4845e9908523c38d4acf92f8a41fdfcd19def41bd5090d7ad767a6dc75c3'
            '599a630fdf020282e27c66aa2b4f3d624d731bd150749a8d7b74f544be03b2bb'
            '197e6ef74da878cbf72844f38461bb18129d144fd5221b3598e973ecda6f5963'
            '19577e9f68a2d4e08bb5564e3946e35c6323276cb6749c101c86e26505e3bf0e'
            'c35d87f1d04b2b153d33c275c2632e40d388a88f19a9e71727e0bbbff51fe689'
            '2b30cd1d0dd606a53ac77b406e1d37798d4b0762fa89de6ea546201906a251bd'
            '3cf04ca4a5b4466e624570d980638f8ab72feaed9b94106dd6ed2bed674a4cdf'
            '8121a64145ff317693de80148fbdca6cb73d3f2ed92f66b946949750ab71afe9')

# Some components below are notated as follows:
#
# 'HOST' -> a tool that runs on Arch itself. e.g. it might be part of the
# install routine to set up (install) a Ventoy USB drive. Usually a "normal"
# x86_64 binary.
#
# 'IMG/USB' -> a tool that is "embedded" into a Ventoy USB drive. The tool is
# stored inside a cpio archive on the ESP and is typically involved in the
# process of booting ISO's. There will usually be an additional 32-bit binary.
# These tools are meant to be small and self-contained and are often statically
# linked against dietlibc or musl.

prepare() {
  cd Ventoy-$pkgver
  : "${_DIFF:=0}" # "1" to generate diffs for easier inspection of Ventoy mod's.

  # Create our own INSTALL trees
  mv -v INSTALL{,.upstream}
  mv -v IMG/cpio_x86{,.upstream}

  (
    # Refer "GRUB2/buildgrub.sh"
    cd GRUB2
    mkdir -v SRC NBP PXE
    tar -xf "$srcdir"/grub-$_grub_ver.tar.xz -C SRC
    cp -av MOD_SRC/grub-$_grub_ver SRC

    if ((_DIFF)); then
      tar -xf "$srcdir"/grub-$_grub_ver.tar.xz -C SRC --xform "s|\(grub-$_grub_ver\)|\1.orig|"
      diff -urN SRC/grub-$_grub_ver{.orig,} > ventoy-$pkgver-grub-$_grub_ver.patch || :
    fi

    # Fix build for newer toolchain.
    patch -d SRC/grub-$_grub_ver -Np1 -i "$srcdir"/ventoy-grub-fix-build-with-binutils-2.36.patch

    # Tweak font path.
    sed -i 's|\(/usr/share/fonts/dejavu\)|\1 /usr/share/fonts/TTF|g' SRC/grub-$_grub_ver/configure.ac

    # NOTE: Upstream builds don't include this. Yet they ship their own
    # {ascii,unicode}.pf2 font files...how are they derived? FIXME
    # gzip -cd "$srcdir"/unifont-$_unifont_ver.bdf.gz > SRC/grub-$_grub_ver/unifont.bdf
  )

  (
    # Refer "IPXE/buildipxe.sh"
    cd IPXE
    tar -xf ipxe_org_code/ipxe-$_ipxe_ver.tar.bz2
    cp -av ipxe_mod_code/ipxe-$_ipxe_ver .

    if ((_DIFF)); then
      # The bundled tarball has been independently verified as being identical
      # to a git checkout. But might as well use the git version for comparison.
      diff -urN -x .git "$srcdir"/ipxe ipxe-$_ipxe_ver > ventoy-$pkgver-ipxe-$_ipxe_ver.patch || :
    fi

    rm -rfv ipxe-$_ipxe_ver/src/{bin,drivers}
  )

  (
    # Refer "EDK2/buildedk.sh"
    cd EDK2
    bsdtar -xf "$srcdir"/edk2-$_edk2_ver.zip
    cp -av edk2_mod/edk2-edk2-$_edk2_ver .

    if ((_DIFF)); then
      bsdtar -xf "$srcdir"/edk2-$_edk2_ver.zip -s "|edk2-edk2-$_edk2_ver|~.orig|"
      diff -urN edk2-edk2-$_edk2_ver{.orig,} > ventoy-$pkgver-edk2-$_edk2_ver.patch || :
    fi

    cd edk2-edk2-$_edk2_ver

    # Remove -Werror for successful build (as per main Arch repo).
    sed -i 's/ -Werror//g' BaseTools/Conf/*.template BaseTools/Source/C/Makefiles/*.makefile

    # Fix build with newer toolchain.
    sed -i 's/GCC48/GCC5/' ../build.sh

    # Fix build against recent python.
    patch -Np1 -i "$srcdir"/ventoy-fix-ucs-2-lookup-on-python-3.9.patch
    patch -Np1 -i "$srcdir"/ventoy-fix-array.array.tostring-removal-in-python-3.9.patch
  )

  (
    cd "$srcdir"/dietlibc-$_diet_ver

    # Fix from Debian. Avoid errors when compiling apps against recent kernel headers. FIXME
    patch -Np1 -i "$srcdir"/dietlibc-headers-fix.patch

    # <cpuid.h> compile fix
    sed -i 's/__leaf/__LEAF/' include/sys/cdefs.h
  )

  (
    cd SQUASHFS
    if ((_DIFF)); then
      tar -xf "$srcdir"/squashfs-tools-$_squash_ver.tar.gz --xform "s|\(squashfs-tools-$_squash_ver\)|\1.orig|"
      diff -urN squashfs-tools-$_squash_ver{.orig,} > ventoy-$pkgver-squashfs-tools-$_squash_ver.patch || :
    fi
  )

  (
    cd Ventoy2Disk
    if ((_DIFF)); then
      tar -xf "$srcdir"/xz-embedded-$_xz_embed_ver.tar.gz -C Ventoy2Disk --xform "s|\(xz-embedded-$_xz_embed_ver\)|\1.orig|"
      diff -urN Ventoy2Disk/xz-embedded-$_xz_embed_ver{.orig,} > ventoy-$pkgver-xz-embedded-$_xz_embed_ver.patch || :
    fi
  )

  (
    cd wimboot
    # Some *.S files are missing from the bundled source. We will grab them from the tarball.
    tar -xf "$srcdir"/wimboot-$_wimboot_ver.tar.gz --xform "s|\(wimboot-$_wimboot_ver\)|\1.orig|"

    if ((_DIFF)); then
      diff -ur wimboot-$_wimboot_ver{.orig,}/src > ventoy-$pkgver-wimboot-$_wimboot_ver.patch || :
    fi

    # Fix build with recent binutils.
    cd wimboot-$_wimboot_ver.orig
    patch -Np1 -i "$srcdir"/wimboot-binutils-2.42-fix.patch
  )
}

_build_grub() (
  echo ":: grub"
  # Refer "GRUB2/buildgrub.sh"
  cd Ventoy-$pkgver/GRUB2
  local _VT_GRUB_DIR=$PWD

  (
    cd SRC/grub-$_grub_ver
    ./autogen.sh
  )

  cp -a SRC/grub-$_grub_ver SRC/grub-x86_64-efi
  cp -a SRC/grub-$_grub_ver SRC/grub-i386-efi
  cp -a SRC/grub-$_grub_ver SRC/grub-i386-pc

  mkdir -pv ../INSTALL/{EFI/BOOT,grub/i386-pc}
  local _conf_args=(--prefix="$_VT_GRUB_DIR"/INSTALL --disable-werror)

  _build_grub-x86_64-efi() (
    cd SRC/grub-x86_64-efi
    ./configure --with-platform=efi "${_conf_args[@]}"
    make
    sh install.sh uefi
  )

  _build_grub-i386-efi() (
    cd SRC/grub-i386-efi
    ./configure --target=i386 --with-platform=efi "${_conf_args[@]}"
    make
    sh install.sh i386efi
  )

  _build_grub-i386-pc() (
    cd SRC/grub-i386-pc
    ./configure --target=i386 --with-platform=pc "${_conf_args[@]}"
    make
    sh install.sh
  )

  _build_grub-x86_64-efi
  _build_grub-i386-efi
  _build_grub-i386-pc

  # Copy over the ancillary stuff.
  local _d
  for _d in distro fonts help menu themes; do
    cp -av ../INSTALL.upstream/grub/$_d ../INSTALL/grub
  done
  cp -av ../INSTALL.upstream/grub/*.cfg ../INSTALL/grub
)

_build_ipxe() (
  echo ":: ipxe"
  # Refer "IPXE/buildipxe.sh"
  cd Ventoy-$pkgver/IPXE/ipxe-$_ipxe_ver/src

  make bin/ipxe.lkrn NO_WERROR=1 V=1
  install -Dv bin/ipxe.lkrn "$srcdir"/Ventoy-$pkgver/INSTALL/ventoy/ipxe.krn
)

_build_edk2() (
  echo ":: edk2"
  # Refer "EDK2/buildedk.sh"
  cd Ventoy-$pkgver/EDK2

  (
    cd edk2-edk2-$_edk2_ver
    make -C BaseTools
  )

  mkdir -pv ../INSTALL/ventoy
  sh ./build.sh ia32
  sh ./build.sh
)

_build_dietlibc() (
  echo ":: dietlibc"
  # Refer "DOC/installdietlibc.sh"
  cd dietlibc-$_diet_ver
  make
  make i386
)

_build_musl32() (
  echo ":: musl32"
  # Refer "DOC/BuildVentoyFromSource.txt" Section 2.3
  (
    cd musl-$_musl_ver

    CFLAGS=-m32 \
      ./configure --prefix="$srcdir"/musl32 --syslibdir="$srcdir"/musl32/lib \
      --target=i386 --build=i386
    make
    make install
  )

  cd kernel-headers-$_kern_hdrs_musl_ver
  make ARCH=x86 prefix="$srcdir"/musl32 install
)

# IMG/USB
_build_vtoytool() (
  echo ":: vtoytool"
  # Refer "VtoyTool/build.sh"
  cd Ventoy-$pkgver/VtoyTool

  "$srcdir"/dietlibc-$_diet_ver/bin-x86_64/diet -Os gcc -DVTOY_X86_64 -D_FILE_OFFSET_BITS=64 ./*.c BabyISO/*.c \
    -IBabyISO -Wall -DBUILD_VTOY_TOOL -DUSE_DIET_C -o vtoytool_64

  "$srcdir"/dietlibc-$_diet_ver/bin-i386/diet -Os gcc -DVTOY_I386 -D_FILE_OFFSET_BITS=64 -m32 ./*.c BabyISO/*.c \
    -IBabyISO -Wall -DBUILD_VTOY_TOOL -DUSE_DIET_C -o vtoytool_32

  strip --strip-all vtoytool_{64,32}
  rm -rfv vtoytool
  install -Dvt vtoytool/00 vtoytool_{64,32}
)

# HOST
_build_vtoycli() (
  echo ":: vtoycli"
  # Refer "vtoycli/fat_io_lib/buildlib.sh" and "vtoycli/build.sh"
  #
  # Upstream builds small and static here, but this is a "host" binary so we
  # don't really need that, nor do we need a 32-bit binary. A "normal" GCC
  # binary would do. FIXME

  cd Ventoy-$pkgver/vtoycli

  (
    cd fat_io_lib/release
    rm -rfv ../{include,lib}

    # GCC-14 build fix
    sed -i '/fat_cache.h/a #include "fat_format.h"' fat_filelib.c

    musl-gcc -O2 -D_FILE_OFFSET_BITS=64 fat*.c -c
    ar -rc libfat_io_64.a ./*.o
    rm -fv ./*.o

    # gcc -m32 -O2 -D_FILE_OFFSET_BITS=64 fat*.c -c
    # ar -rc libfat_io_32.a ./*.o
    # rm -fv ./*.o

    mkdir -v ../{include,lib}
    mv -v ./*.a ../lib/
    cp -av ./*.h ../include/
  )

  local _SRCS=(vtoycli.c vtoyfat.c vtoygpt.c crc32.c partresize.c)

  musl-gcc -Os -static -D_FILE_OFFSET_BITS=64 \
    "${_SRCS[@]}" -Ifat_io_lib/include fat_io_lib/lib/libfat_io_64.a -o vtoycli_64

  # "$srcdir"/dietlibc-$_diet_ver/bin-i386/diet -Os gcc -D_FILE_OFFSET_BITS=64 -m32 \
  #   "${_SRCS[@]}" -Ifat_io_lib/include fat_io_lib/lib/libfat_io_32.a -o vtoycli_32

  strip --strip-all vtoycli_64
  # strip --strip-all vtoycli_32
  install -Dv vtoycli_64 ../INSTALL/tool/x86_64/vtoycli
  # install -Dv vtoycli_32 ../INSTALL/tool/i386/vtoycli
)

# IMG/USB
_build_fuseiso() (
  echo ":: fuseiso"
  # Refer "FUSEISO/build.sh"
  cd Ventoy-$pkgver/FUSEISO

  __build_libfuse_static() (
    # Refer "FUSEISO/build_libfuse.sh"
    tar -xf "$srcdir"/fuse-$_fuse_ver.tar.gz
    cd fuse-$_fuse_ver
    mkdir -v build{64,32}

    local _conf_args=(
      --disable-shared
      --disable-util
      --disable-example
      CFLAGS=-Os
    )

    (
      cd build64
      CC=musl-gcc \
        ../configure "${_conf_args[@]}"
      make V=1
    )

    (
      cd build32
      CC="$srcdir/musl32/bin/musl-gcc -m32" \
        LDFLAGS="-Wl,-melf_i386" \
        ../configure "${_conf_args[@]}"
      make V=1
    )
  )

  __build_libfuse_static

  rm -fv vtoy_fuse_iso_*

  musl-gcc -static -O2 -D_FILE_OFFSET_BITS=64 vtoy_fuse_iso.c -Ifuse-$_fuse_ver/include \
    fuse-$_fuse_ver/build64/lib/.libs/libfuse.a -o vtoy_fuse_iso_64

  "$srcdir"/musl32/bin/musl-gcc -m32 -static -O2 -D_FILE_OFFSET_BITS=64 \
    vtoy_fuse_iso.c -Ifuse-$_fuse_ver/include -Wl,-melf_i386 \
    fuse-$_fuse_ver/build32/lib/.libs/libfuse.a -o vtoy_fuse_iso_32

  strip --strip-all vtoy_fuse_iso_{64,32}
)

# HOST
_build_exfat() (
  echo ":: exfat"
  # Refer "ExFAT/buidexfat.sh"
  #
  # This is the FUSE based pkg. The version in the repo "exfat-utils" is going
  # away in favor of the kernel based "exfatprogs" [1]. Ventoy is apparently not
  # yet ready for the newer "exfatprogs" tools. Upstream builds want to link
  # "libfuse.a" statically, however this is a "host" binary so there's no need.
  # Additionally, Ventoy modifies it slightly with the sed below. Although, it
  # appears the mod is only applicable to "mount.exfat-fuse" which we don't need
  # (see below). FIXME
  #
  # [1] https://bugs.archlinux.org/task/72629

  cd Ventoy-$pkgver/ExFAT

  tar -xf "$srcdir"/exfat-$_exfat_ver.tar.gz
  cd exfat-$_exfat_ver

  sed "/printf.*VERSION/a\\\tif (access(\"/etc/initrd-release\", F_OK) >= 0) argv[0][0] = '@';" \
    -i fuse/main.c
  autoreconf -i
  ./configure
  make

  # NOTE: AFAICT "mount.exfat-fuse" is used only in the LiveCD which is of no use to us.
  # If it turns out that it is required, a runtime dep on "fuse2" will be needed.

  # strip --strip-all fuse/mount.exfat-fuse
  strip --strip-all mkfs/mkexfatfs

  # install -Dvt ../../INSTALL/tool/x86_64 fuse/mount.exfat-fuse
  install -Dvt ../../INSTALL/tool/x86_64 mkfs/mkexfatfs
)

# IMG/USB
_build_unsquashfs() (
  echo ":: unsquashfs"
  # Refer "SQUASHFS/build.txt"
  cd Ventoy-$pkgver/SQUASHFS/SRC
  CFLAGS=-Os
  local _libdir="$srcdir"/Ventoy-$pkgver/SQUASHFS/LIB

  __build_lz4_static() (
    # Refer "SQUASHFS/SRC/build_lz4.sh"
    # The bundled lz4 tarball (ver 1.8.1.2 Jan 2018) has been independently
    # verified as being identical to the one available on GitHub.

    tar -xf lz4-$_lz4_ver.tar.gz
    sed -i "s/@\$(CC)/\$(CC)/" lz4-$_lz4_ver/lib/Makefile
    cp -a lz4-$_lz4_ver{,-32}

    (
      cd lz4-$_lz4_ver
      make -C lib
      make -C lib install PREFIX="$_libdir"/LZ4
    )

    (
      cd lz4-$_lz4_ver-32
      CC="gcc -m32" \
        make -C lib
      make -C lib install PREFIX="$_libdir"32/LZ4
    )
  )

  __build_xz_static() (
    # Refer "SQUASHFS/SRC/build_lzma.sh"
    # The bundled liblzma/xz zip file contents have been independently verified
    # as being identical to (ver 5.0.4 Jun 2012) available on [1], apart from
    # the addition of a single file "liblzma-builder.sh".
    #
    # [1] https://tukaani.org/xz/old.html

    bsdtar -xf liblzma-master.zip
    cp -a liblzma-master{,-32}

    local _conf_args=(
      --disable-shared
      --disable-xz
      --disable-xzdec
      --disable-lzmadec
      --disable-lzmainfo
      --enable-small
    )

    (
      cd liblzma-master
      ./configure --prefix="$_libdir"/LZMA "${_conf_args[@]}"
      make
      make install
    )

    (
      cd liblzma-master-32
      CC="gcc -m32" \
        ./configure --prefix="$_libdir"32/LZMA "${_conf_args[@]}"
      make
      make install
    )
  )

  __build_lzo_static() (
    # Refer "SQUASHFS/SRC/build_lzo.sh"
    # The bundled lzo tarball (ver 2.08 Jun 2014) contents have been
    # independently verified as being identical to the 2.08 tarball available
    # on [1], apart from a 1 line change in "configure{,.ac}" and a minor change
    # in "include/lzo/lzodefs.h" but these appear to be tweaks by lzo upstream.
    #
    # [1] https://www.oberhumer.com/opensource/lzo/download/lzo-2.08.tar.gz

    tar -xf lzo-$_lzo_ver.tar.gz
    cp -a lzo-$_lzo_ver{,-32}

    (
      cd lzo-$_lzo_ver
      ./configure --prefix="$_libdir"/LZO
      make V=1
      make install
    )

    (
      cd lzo-$_lzo_ver-32
      CC="gcc -m32" \
        ./configure --prefix="$_libdir"32/LZO
      make V=1
      make install
    )
  )

  __build_zstd_static() (
    # Refer "SQUASHFS/SRC/build_zstd.sh"
    # The bundled zstd tarball (ver 1.4.4 Nov 2019) has been independently
    # verified as being identical to the one available on GitHub.

    tar -xf zstd-$_zstd_ver.tar.gz
    cp -a zstd-$_zstd_ver{,-32}

    (
      cd zstd-$_zstd_ver
      ZSTD_LIB_COMPRESSION=0 make -C lib
      make -C lib install PREFIX="$_libdir"/ZSTD
    )

    (
      cd zstd-$_zstd_ver-32
      export CC="gcc -m32"
      ZSTD_LIB_COMPRESSION=0 make -C lib
      make -C lib install PREFIX="$_libdir"32/ZSTD
    )
  )

  __build_zlib_static-32() (
    tar -xf "$srcdir"/zlib-$_zlib_ver.tar.xz
    cd zlib-$_zlib_ver

    CC="gcc -m32" \
      ./configure --static
    make
  )

  __build_lz4_static
  __build_xz_static
  __build_lzo_static
  __build_zstd_static
  __build_zlib_static-32

  cd "$srcdir"/Ventoy-$pkgver/SQUASHFS
  unset CFLAGS
  cp -a squashfs-tools-4.4/squashfs-tools{,-32}
  rm -fv unsquashfs_*

  (
    cd squashfs-tools-4.4/squashfs-tools
    sed -i 's|LIBS) -o|LIBS) /usr/lib/libz.a -o|' Makefile
    make unsquashfs LZ4_LIBDIR="$_libdir"/LZ4 LZMA_LIBDIR="$_libdir"/LZMA \
      LZO_LIBDIR="$_libdir"/LZO ZSTD_LIBDIR="$_libdir"/ZSTD

    strip --strip-all unsquashfs
    cp -av unsquashfs ../../unsquashfs_64
  )

  (
    cd squashfs-tools-4.4/squashfs-tools-32
    sed -i "s|LIBS) -o|LIBS) ../../SRC/zlib-$_zlib_ver/libz.a -o|" Makefile
    CC="gcc -m32" \
      make unsquashfs LZ4_LIBDIR="$_libdir"32/LZ4 LZMA_LIBDIR="$_libdir"32/LZMA \
      LZO_LIBDIR="$_libdir"32/LZO ZSTD_LIBDIR="$_libdir"32/ZSTD

    strip --strip-all unsquashfs
    cp -av unsquashfs ../../unsquashfs_32
  )
)

# IMG/USB
_build_vblade() (
  echo ":: vblade"
  # Refer "VBLADE/vblade-master/build.sh"
  cd Ventoy-$pkgver/VBLADE/vblade-master
  rm -fv vblade_*

  # Need -fcommon for build to succeed FIXME
  gcc linux.c aoe.c ata.c bpf.c -Os -o vblade_64 -fcommon
  gcc linux.c aoe.c ata.c bpf.c -Os -m32 -o vblade_32 -fcommon

  strip --strip-all vblade_{64,32}
)

# IMG/USB
_build_dmsetup() (
  echo ":: dmsetup"
  # Refer "DMSETUP/build.txt"
  cd Ventoy-$pkgver/DMSETUP
  rm -fv dmsetup*
  bsdtar -O -xvf "$srcdir"/device-mapper*.rpm device-mapper*.tgz | tar -xzf -

  tee -a device-mapper.$_dm_ver/include/configure.h.in > /dev/null << _EOF_
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
#endif

#ifndef UINT64_C
#define UINT64_C(c) c ## ULL
#endif
_EOF_

  # GCC-14 build fixes
  sed -i '/ctype.h/a #include <strings.h>' device-mapper.$_dm_ver/lib/libdm-report.c
  sed -i '/langinfo.h/a #include <strings.h>' device-mapper.$_dm_ver/dmsetup/dmsetup.c

  cp -a device-mapper.$_dm_ver{,-32}

  (
    cd device-mapper.$_dm_ver
    CC="$srcdir/dietlibc-$_diet_ver/bin-x86_64/diet gcc" \
      ./configure
    sed -i '/rpl_malloc/d' include/configure.h
    make
    strip --strip-all dmsetup/dmsetup
    cp -av dmsetup/dmsetup ../dmsetup64
  )

  (
    cd device-mapper.$_dm_ver-32
    CC="$srcdir/dietlibc-$_diet_ver/bin-i386/diet gcc -m32" \
      ./configure
    sed -i '/rpl_malloc/d' include/configure.h
    make
    strip --strip-all dmsetup/dmsetup
    cp -av dmsetup/dmsetup ../dmsetup32
  )
)

# IMG/USB
_build_zstdcat() (
  echo ":: zstdcat"
  # Refer "ZSTD/build.txt"
  cd Ventoy-$pkgver/ZSTD
  rm -fv zstdcat*
  tar -xf "$srcdir"/zstd-$_zstd1_ver.tar.gz
  cp -a zstd-$_zstd1_ver{,-32}

  local _compile_opts1=(
    -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -Wall -DZSTD_LEGACY_SUPPORT=1
    ../lib/decompress/zstd_decompress.c -c -o ../lib/decompress/zstd_decompress.o
  )

  local _compile_opts2=(
      -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -Wall -DZSTD_LEGACY_SUPPORT=1
      ../lib/decompress/zstd_decompress.o ../lib/decompress/huf_decompress.c ../lib/common/entropy_common.c
      ../lib/common/fse_decompress.c ../lib/common/xxhash.c ../lib/common/zstd_common.c
      ../lib/compress/zstd_compress.c ../lib/compress/fse_compress.c ../lib/compress/huf_compress.c
      ../lib/legacy/zstd_v01.c ../lib/legacy/zstd_v02.c ../lib/legacy/zstd_v03.c ../lib/legacy/zstd_v04.c
      ../lib/legacy/zstd_v05.c ../lib/legacy/zstd_v06.c ../lib/legacy/zstd_v07.c ../lib/dictBuilder/divsufsort.c
      ../lib/dictBuilder/zdict.c zstdcli.c fileio.c bench.c datagen.c dibio.c -o zstd
  )

  (
    cd zstd-$_zstd1_ver/programs
    "$srcdir"/dietlibc-$_diet_ver/bin-x86_64/diet -Os gcc -pipe -nostdinc -falign-loops=32 "${_compile_opts1[@]}"
    "$srcdir"/dietlibc-$_diet_ver/bin-x86_64/diet -Os gcc -pipe -nostdinc "${_compile_opts2[@]}"
    strip --strip-all zstd
    cp -av zstd ../../zstdcat64
  )

  (
    cd zstd-$_zstd1_ver-32/programs
    "$srcdir"/dietlibc-$_diet_ver/bin-i386/diet -Os gcc -m32 -pipe -nostdinc -falign-loops=32 "${_compile_opts1[@]}"
    "$srcdir"/dietlibc-$_diet_ver/bin-i386/diet -Os gcc -m32 -pipe -nostdinc "${_compile_opts2[@]}"
    strip --strip-all zstd
    cp -av zstd ../../zstdcat
  )

  install -Dvt ../IMG/cpio_x86/ventoy/tool zstdcat{,64}
)

# IMG/USB
_build_xzminidec() (
  echo ":: xzminidec"
  # Refer "DOC/BuildVentoyFromSource.txt" Section(s) 4.15 and 4.16
  cd Ventoy-$pkgver/Ventoy2Disk/Ventoy2Disk/xz-embedded-20130513/userspace

  make -f ventoy_makefile CC="$srcdir/dietlibc-$_diet_ver/bin-i386/diet gcc -Os -m32 -std=gnu89"
  mv -v xzminidec{,32}
  make clean
  rm -v bytetest.o

  make -f ventoy_makefile CC="$srcdir/dietlibc-$_diet_ver/bin-x86_64/diet gcc -Os -std=gnu89"
  mv -v xzminidec{,64}
  make clean
  rm -v bytetest.o

  make -f ventoy_makefile CC="musl-gcc -Os -static -std=gnu89"
  mv -v xzminidec{,64_musl}
  make clean
  rm -v bytetest.o

  strip --strip-all xzminidec{32,64}*
  install -Dvt ../../../../IMG/cpio_x86/ventoy/busybox xzminidec{32,64}*
)

# IMG/USB
_build_busybox() (
  echo ":: busybox"
  # Refer "BUSYBOX/build.txt"
  cd Ventoy-$pkgver/BUSYBOX
  mkdir -pv ../IMG/cpio_x86/ventoy/busybox

  (
    # Refer "BUSYBOX/chmod/build.sh"
    cd chmod
    rm -vf vtchmod*{32,64}*

    "$srcdir"/dietlibc-$_diet_ver/bin-x86_64/diet gcc -Os vtchmod.c -o vtchmod64
    "$srcdir"/dietlibc-$_diet_ver/bin-i386/diet gcc -Os -m32 vtchmod.c -o vtchmod32
    musl-gcc -Os -static vtchmod.c -o vtchmod64_musl
    strip --strip-all vtchmod{64,32} vtchmod64_musl
    cp -avt ../../IMG/cpio_x86/ventoy/busybox vtchmod{64,32} vtchmod64_musl
  )

  tar -xf "$srcdir"/busybox-$_busybox_ver.tar.bz2

  cp -a busybox-$_busybox_ver{,-xzcat64}
  cp -a busybox-$_busybox_ver{,-xzcat32}
  cp -a busybox-$_busybox_ver{,-32}

  (
    cd busybox-$_busybox_ver-xzcat64
    cp -av ../x86_64_xzcat.config .config
    make CC=musl-gcc V=1
    mv -v busybox xzcat64_musl
    xz xzcat64_musl
    cp -avt ../../IMG/cpio_x86/ventoy/busybox xzcat64_musl.xz
  )

  (
    cd busybox-$_busybox_ver-xzcat32
    sed 's/# CONFIG_LFS is not set/CONFIG_LFS=y/' ../x86_64_xzcat.config > .config
    make CC="$srcdir/musl32/bin/musl-gcc -m32 -Wl,-melf_i386" V=1
    mv -v busybox xzcat32_musl
    xz xzcat32_musl
    cp -avt ../../IMG/cpio_x86/ventoy/busybox xzcat32_musl.xz
  )

  (
    cd busybox-$_busybox_ver
    make defconfig
    sed -i 's/# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config
    make CC=musl-gcc V=1
    mv -v busybox busybox64
    xz busybox64
    cp -avt ../../IMG/cpio_x86/ventoy/busybox busybox64.xz
  )

  (
    cd busybox-$_busybox_ver-32
    make defconfig
    sed -i 's/# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config
    make CC="$srcdir/musl32/bin/musl-gcc -m32 -Wl,-melf_i386" V=1
    mv -v busybox busybox32
    xz busybox32
    cp -avt ../../IMG/cpio_x86/ventoy/busybox busybox32.xz
  )
)

# IMG/USB
_build_lunzip() (
  echo ":: lunzip"
  # Refer "DOC/BuildVentoyFromSource.txt" Section 4.19
  # The bundled lunzip tarball (ver 1.11 Jan 2019) has been independently
  # verified as being identical to the one available on [1].
  #
  # [1] http://download.savannah.gnu.org/releases/lzip/lunzip/

  cd Ventoy-$pkgver/LZIP
  rm -fv lunzip*{32,64}
  tar -xf lunzip-$_lunzip_ver.tar.gz
  cp -a lunzip-$_lunzip_ver{,-32}

  (
    cd lunzip-$_lunzip_ver
    ./configure CC="$srcdir/dietlibc-$_diet_ver/bin-x86_64/diet gcc" CFLAGS=-Os
    make
    strip --strip-all lunzip
    cp -av lunzip ../lunzip64
  )

  (
    cd lunzip-$_lunzip_ver-32
    ./configure CC="$srcdir/dietlibc-$_diet_ver/bin-i386/diet gcc -m32" CFLAGS=-Os
    make
    strip --strip-all lunzip
    cp -av lunzip ../lunzip32
  )

  # Undocumented!
  rm -fv lz4cat*
  "$srcdir"/dietlibc-$_diet_ver/bin-x86_64/diet -Os gcc -D_FILE_OFFSET_BITS=64 smallz4cat.c -o lz4cat64
  strip --strip-all lz4cat64
  install -Dvt ../IMG/cpio_x86/ventoy/tool lz4cat64
)

# IMG/USB
_build_verity() (
  echo ":: verity"
  # Refer "cryptsetup/cryptsetup-build.txt"
  # Needed for *experimental* FydeOS/CloudReady (ChromeOS) support.

  cd Ventoy-$pkgver/cryptsetup
  tar -xf "$srcdir"/cryptsetup-$_crypt_ver.tar.xz
  cp -a cryptsetup-$_crypt_ver{,-32}

  # 30 Jun 2024. Ugh, this now fails to build because latest libgcrypt-1.11.0
  # has removed libgcrypt-config. Just use the upstream provided 64-bit binary
  # for now. The ChromeOS support is experimental anyway. FIXME

  # (
  #   rm -v veritysetup64
  #   cd cryptsetup-$_crypt_ver
  #   ./configure --enable-static --host=x86_64-pc-linux-gnu
  #   make
  #   cd src
  #   gcc -Wall -O2 -o veritysetup veritysetup-utils_crypt.o veritysetup-utils_loop.o veritysetup-utils_tools.o \
  #     veritysetup-veritysetup.o -lpopt -ldevmapper -lgcrypt -luuid .././lib/.libs/libcryptsetup.a
  #   strip --strip-all veritysetup
  #   cp -av veritysetup ../../veritysetup64
  # )

  # Ugh, this needs multilib (lib32-e2fsprogs lib32-popt lib32-libgcrypt) and
  # a 32-bit libdevmapper which doesn't actually exist in the repo. Just use the
  # upstream provided 32-bit binary for now. FIXME

  # rm -v veritysetup32
  # cd cryptsetup-$_crypt_ver-32
  # CC="gcc -m32" \
  # ./configure --enable-static --host=x86_64-pc-linux-gnu
  # make
  # cd src
  # gcc -m32 -Wall -O2 -o veritysetup veritysetup-utils_crypt.o veritysetup-utils_loop.o veritysetup-utils_tools.o \
  #   veritysetup-veritysetup.o -lpopt -ldevmapper -lgcrypt -luuid .././lib/.libs/libcryptsetup.a
  # strip --strip-all veritysetup
  # cp -av veritysetup ../../veritysetup32
)

# IMG/USB
_build_wimboot() (
  echo ":: wimboot"
  # Refer "wimboot/build.sh"
  cd Ventoy-$pkgver/wimboot/wimboot-$_wimboot_ver/src

  # Grab the missing *.S files
  cp -av ../../wimboot-$_wimboot_ver.orig/src/*.S .
  sed -i '/^CFLAGS/s/ -Werror//' Makefile

  # pesign (if installed) appears to make no difference, ignore.
  make wimboot.x86_64 wimboot.i386.efi
  xz wimboot.{x86_64,i386.efi}
  install -Dvt "$srcdir"/Ventoy-$pkgver/INSTALL/ventoy wimboot.{x86_64,i386.efi}.xz
)

_pack_ventoy() (
  echo ":: pack"
  # Refer "INSTALL/ventoy_pack.sh"
  # 3RD PARTY BINARIES ALERT! FIXME

  cd Ventoy-$pkgver
  local _LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now,-z,pack-relative-relocs"

  (
    cd IMG
    sed -i '/arm64 #/i exit 0' mkcpio.sh
    install -dv cpio_x86/ventoy/{busybox,tool}
    cp -avt cpio_x86/ventoy/tool cpio_x86.upstream/ventoy/tool/*.sh

    # 32-bit tiny lz4cat binary FIXME
    # Refer "DOC/BuildVentoyFromSource.txt" Section 5.1
    cp -avt cpio_x86/ventoy/tool cpio_x86.upstream/ventoy/tool/lz4cat

    # 32-bit busybox ar binary FIXME
    # Refer "DOC/BuildVentoyFromSource.txt" Section 5.2
    cp -avt cpio_x86/ventoy/tool cpio_x86.upstream/ventoy/tool/ar

    # 32-bit busybox inotifyd binary FIXME
    # Refer "DOC/BuildVentoyFromSource.txt" Section 5.3
    cp -avt cpio_x86/ventoy/tool cpio_x86.upstream/ventoy/tool/inotifyd

    # 32-bit busybox ash binary FIXME
    # Refer "DOC/BuildVentoyFromSource.txt" Section 5.4
    cp -avt cpio_x86/ventoy/busybox cpio_x86.upstream/ventoy/busybox/ash

    # 64-bit busybox ash binary FIXME
    # Refer "DOC/BuildVentoyFromSource.txt" Section 4.18
    # For some unknown reason this is the only binary built with uclibc.
    # Please see: https://github.com/ventoy/Ventoy/issues/2183
    cp -avt cpio_x86/ventoy/busybox cpio_x86.upstream/ventoy/busybox/64h

    # patch for device-mapper kernel modules FIXME
    # Refer "DMPATCH/readme.txt"
    cp -avt cpio_x86/ventoy/tool cpio_x86.upstream/ventoy/tool/dm_patch_*

    sh mkcpio.sh
    install -Dvm 644 -t ../INSTALL/ventoy ventoy.cpio ventoy_x86.cpio

    # "IMG/vtloopex" contains pre-compiled binary kernel modules (dm-mod) for
    # stuff like LibreELEC, openwrt, etc. FIXME
    sh mkloopex.sh
  )

  (
    # "IMG/Unix" contains pre-compiled binary kernel modules (geom_ventoy) for
    # various BSD's, FreeBSD, etc. FIXME
    cd Unix
    sh pack_unix.sh
  )

  (
    # HOST
    cd LinuxGUI
    sed -i '/dos2unix/d' language.sh
    sh language.sh

    mkdir -pv ../INSTALL/tool/x86_64

    sed -e '/^build.*i386/d;/^build.*aarch64/d;/^build.*mips/d;/^build.*gtk2/d;/^export/d' \
        -e "/\$XXLIB/a \        $_LDFLAGS \\\\" \
        -e "/VTOY_GUI/s/$/ $_LDFLAGS/" -i.bak build.sh build_gtk.sh
    sh build.sh
    sh build_gtk.sh

    sed -e '/^if.*CentOS8/,+6d;/^build.*aarch64$/d;/^build.*mips64el$/d' \
        -e '/QT_INC.*gcc_64/a \    QT_INC_PATH="/usr/include/qt"' \
        -e '/QT_LIB.*gcc_64/a \    QT_LIB_PATH="/usr/lib"' \
        -e "/LFLAGS.*PATH\"/a \    LFLAGS+=\" $_LDFLAGS\"" -i.bak build_qt.sh
    sh build_qt.sh
  )

  (
    # HOST
    cd Plugson
    sed -e '/^build.*i386/d;/^build.*aarch64/d;/^build.*mips/d' \
        -e "/\$XXLIB/a \        $_LDFLAGS \\\\" -i.bak build.sh
    sh build.sh
    sh pack.sh
  )

  (
    # HOST
    # Upstream builds small and static here, but this is a "host" binary so we
    # don't really need that. A "normal" GCC binary would do. FIXME
    cd Vlnk
    local _SRCS=(src/crc32.c src/main_linux.c src/vlnk.c)

    musl-gcc -Os -static -D_FILE_OFFSET_BITS=64 "${_SRCS[@]}" -Isrc -o vlnk
    strip --strip-all vlnk
    install -Dvt ../INSTALL/tool/x86_64 vlnk

    sh pack.sh
  )

  (
    cd EfiISO
    sed -i 's/-P/-publisher/' mkefiiso.sh
    sh mkefiiso.sh
  )

  # Binary EFI Secure Boot stuff FIXME
  # These files are taken from various projects. "?" denotes unverified/unknown origin

  local _efi_files=(
    BOOTIA32.EFI        # shim 32-bit  ver 15.6         # SUISBD [1] based on Fedora
    BOOTX64.EFI         # shim 64-bit  ver 15.7         # ? SUSE Linux Enterprise ?
    grub.efi            # preloader 64-bit              # SUISBD [1]
    grubia32.efi        # preloader 32-bit              # SUISBD [2]
    mmia32.efi          # MokManager 32-bit  ver 15.6   # same length as SUISBD [1] (but 261 bytes differ) based on Fedora
    MokManager.efi      # MokManager 64-bit  ver 15.7   # ? SUSE Linux Enterprise ?
  )

  # This file is also from SUISBD
  install -vm 644 INSTALL.upstream/tool/ENROLL_THIS_KEY_IN_MOKMANAGER.cer INSTALL/tool
  (
    cd INSTALL.upstream/EFI/BOOT
    install -Dvt "$srcdir"/Ventoy-$pkgver/INSTALL/EFI/BOOT "${_efi_files[@]}"
  )
  # [1] https://github.com/ValdikSS/Super-UEFIinSecureBoot-Disk/releases/download/3-4/Super-UEFIinSecureBoot-Disk_minimal_v3-4.zip
  # [2] https://github.com/ValdikSS/Super-UEFIinSecureBoot-Disk/releases/download/3-3/Super-UEFIinSecureBoot-Disk_minimal_v3-3.zip

  # More EFI stuff. Should be buildable with EDK2 FIXME
  # Refer "DOC/BuildVentoyFromSource.txt" Section 4.17
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/iso9660_{x64,ia32}.efi
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/udf_{x64,ia32}.efi

  # Windows stuff. Apparently buildable with Microsoft Visual Studio FIXME
  # Refer "DOC/BuildVentoyFromSource.txt" Section 4.4
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/vtoyjump{64,32}.exe

  # 3rd party Windows stuff FIXME
  # Refer "DOC/BuildVentoyFromSource.txt" Section(s) 5.12 and 5.8
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/7z
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/imdisk

  # Unknown Windows crap. Config data for Windows Boot Manager? Where from? FIXME
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/common_{bcd,bootmgr}.xz

  # From syslinux. Could we use our own repo version? FIXME
  # Refer "DOC/BuildVentoyFromSource.txt" Section 5.9
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/memdisk

  # DragonFly BSD stuff FIXME
  # Refer "Unix/ventoy_unix/DragonFly/mkinitrd.sh"
  cp -avt INSTALL/ventoy INSTALL.upstream/ventoy/dragonfly.mfs.xz
)

_create_img() (
  echo ":: image"
  # Refer "INSTALL/ventoy_pack.sh"
  mkdir -pv Ventoy-$pkgver/INSTALL
  cd Ventoy-$pkgver/INSTALL

  # Create a 256M Ventoy disk image, partition/format a 32M ESP
  dd if=/dev/zero of=img.bin bs=1M count=256 status=none
  echo -e 'size=223M, type=7, bootable\n size=32M, type=U\n' | sfdisk img.bin
  guestfish add img.bin : run : mkfs vfat /dev/sda2 label:VTOYEFI

  # Create a 64M "scratch" disk image / ext4 partition
  dd if=/dev/zero of=scratch.bin bs=1M count=64 status=none
  echo -e 'size=+\n' | sfdisk scratch.bin
  guestfish add scratch.bin : run : mkfs ext4 /dev/sda1 label:SCRATCH

  # Copy our GRUB stuff into the scratch disk
  tar -czvf - -C "$srcdir"/Ventoy-$pkgver/INSTALL grub/i386-pc |
    guestfish add scratch.bin : run : mount /dev/sda1 / : tar-in - / compress:gzip
  tar -czvf - -C "$srcdir"/Ventoy-$pkgver/GRUB2 INSTALL |
    guestfish add scratch.bin : run : mount /dev/sda1 / : tar-in - / compress:gzip

  # Do the GRUB install business
  #
  # XXX (ab)using `guestfish debug sh "args ..."' like this is a dirty hack. FIXME. Pls see:
  # https://www.libguestfs.org/guestfs-faq.1.html#whats-the-difference-between-guestfish-and-virt-rescue
  #
  # We should be using `guestfish sh command' or maybe `guestfish command "args ..."' but we
  # cannot do so because we don't have a proper guest filesystem.
  # https://www.libguestfs.org/guestfish.1.html#sh
  # https://www.libguestfs.org/guestfish.1.html#command
  #
  # Possible alternative for a guest image -> Arch Boxes basic qcow2...or maybe we just don't
  # use guestfish here at all and instead use something like [1] with an Arch ISO.
  #
  # [1] https://gitlab.archlinux.org/archlinux/ci-scripts/-/blob/master/scripts/build_in_archiso_vm.sh

  guestfish add img.bin : add scratch.bin : run : mount /dev/sdb1 / : debug sh \
    "/sysroot/INSTALL/sbin/grub-bios-setup --skip-fs-probe --directory=/sysroot/grub/i386-pc /dev/sda"

  (
    cd grub
    tar -czf help.tar.gz help
    rm -rf help

    vtlangtitle=$(grep VTLANG_LANGUAGE_NAME menu/zh_CN.json | awk -F\" '{print $4}')
    {
      echo "menuentry \"zh_CN  -  $vtlangtitle\" --class=menu_lang_item --class=debug_menu_lang --class=F5tool {"
      echo "    vt_load_menu_lang zh_CN"
      echo "}"
    } >> menulang.cfg

    # shellcheck disable=SC2010 # FIXME
    ls -1 menu | grep -v 'zh_CN' | sort | while read -r vtlang; do
      vtlangname=${vtlang%.*}
      vtlangtitle=$(grep VTLANG_LANGUAGE_NAME menu/"$vtlang" | awk -F\" '{print $4}')
      {
        echo "menuentry \"$vtlangname  -  $vtlangtitle\" --class=menu_lang_item --class=debug_menu_lang --class=F5tool {"
        echo "    vt_load_menu_lang $vtlangname"
        echo "}"
      } >> menulang.cfg
    done

    {
      echo "menuentry \"\$VTLANG_RETURN_PREVIOUS\" --class=vtoyret VTOY_RET {"
      echo "        echo \"Return ...\""
      echo "}"
    } >> menulang.cfg

    tar -czf menu.tar.gz menu
    rm -rf menu

    rm -fv i386-pc/*.img
  )

  # Apparently grub.cfg needs to be located at the front of the partition, copy it first..
  guestfish add img.bin : run : mount /dev/sda2 / : mkdir /grub : copy-in grub/grub.cfg /grub

  # ..and now the rest
  tar -czvf - --exclude=grub.cfg -C grub . |
    guestfish add img.bin : run : mount /dev/sda2 / : tar-in - /grub compress:gzip
  tar -czvf - ventoy EFI |
    guestfish add img.bin : run : mount /dev/sda2 / : tar-in - / compress:gzip
  guestfish add img.bin : run : mount /dev/sda2 / : copy-in tool/ENROLL_THIS_KEY_IN_MOKMANAGER.cer /

  # Just *WTF* is going on here? FIXME
  dd status=none bs=1024 count=16 if=/dev/zero of=/tmp/mount.exfat-fuse_aarch64
  guestfish add img.bin : run : mount /dev/sda2 / : mkdir /tool : copy-in /tmp/mount.exfat-fuse_aarch64 /tool

  # Prepare the final "payload" dir
  curver=$(grep 'set.*VENTOY_VERSION=' ./grub/grub.cfg | awk -F'"' '{print $2}')
  tmpdir=ventoy-$curver

  mkdir -pv "$tmpdir"/{boot,ventoy}
  echo "$curver" > "$tmpdir"/ventoy/version
  dd if=img.bin of="$tmpdir"/boot/boot.img bs=1 count=512 status=none
  dd if=img.bin of="$tmpdir"/boot/core.img bs=512 count=2047 skip=1 status=none
  xz --check=crc32 "$tmpdir"/boot/core.img

  cp -avt "$tmpdir" tool ../INSTALL.upstream/plugin ../LinuxGUI/WebUI
  sed -i 's/.*SCRIPT_DEL_THIS \(.*\)/\1/g' "$tmpdir"/WebUI/index.html

  cp -avt "$tmpdir"/tool \
    ../INSTALL.upstream/tool/{distro_gui_type.json,VentoyGTK.glade,ventoy_lib.sh,VentoyWorker.sh}

  cp -avt "$tmpdir" VentoyGUI.x86_64 VentoyVlnk.sh \
    ../INSTALL.upstream/{Ventoy2Disk,VentoyWeb,VentoyPlugson,CreatePersistentImg,ExtendPersistentImg}.sh

  # 32MB ESP image
  dd status=none if=img.bin of="$tmpdir"/ventoy/ventoy.disk.img bs=512 count=65536 skip=458752
  xz --check=crc32 "$tmpdir"/ventoy/ventoy.disk.img

  chmod -v +x "$tmpdir"{,/tool}/*.sh
  cp -avt "$tmpdir"/tool ../LANGUAGES/languages.json

  # Fix ups
  sed -e 's|./log.txt|/var/log/ventoy.log|' \
      -e "/^vtdebug()/a \    test -w /var/log && \\\\" -i "$tmpdir"/{Ventoy2Disk,tool/ventoy_lib}.sh

  sed -i 's|log.txt|/var/log/ventoy.log|' "$tmpdir/"WebUI/static/js/languages.js \
    "$tmpdir"/tool/{languages.json,VentoyWorker.sh} "$tmpdir"/VentoyWeb.sh

  sed -e '/^chmod/d' -e '/^echo.*decompress/d' \
      -e '/^echo.*2D/s|^|test -w /var/log \&\& |' \
      -e '/^date/s|^|test -w /var/log \&\& |' -i "$tmpdir"/Ventoy2Disk.sh

  sed -e 's|#!/bin/sh|#!/usr/bin/env bash|' -i "$tmpdir"/tool/{VentoyWorker.sh,ventoy_lib.sh}
)

build() {
  # Workaround for abovementioned pacman regression.
  unset CPPFLAGS CFLAGS DEBUG_CFLAGS CXXFLAGS DEBUG_CXXFLAGS LDFLAGS LTOFLAGS RUSTFLAGS DEBUG_RUSTFLAGS

  _build_grub
  _build_ipxe
  _build_edk2
  _build_dietlibc
  _build_musl32
  _build_vtoytool
  _build_vtoycli
  _build_fuseiso
  _build_exfat
  _build_unsquashfs
  _build_vblade
  _build_dmsetup
  _build_zstdcat
  _build_xzminidec
  _build_busybox
  _build_lunzip
  _build_verity
  _build_wimboot
  _pack_ventoy
  _create_img
}

package() {
  mkdir -pv "$pkgdir"/{opt,usr/{bin,share/{applications,pixmaps}}}

  cp -arv Ventoy-$pkgver/INSTALL/ventoy-$pkgver "$pkgdir"/opt/ventoy

  # Everything below this line -> Credits: https://aur.archlinux.org/packages/ventoy-bin
  tee "$pkgdir"/usr/bin/ventoy > /dev/null << _EOF_
#!/bin/sh

cd /opt/ventoy || exit 1
exec ./Ventoy2Disk.sh "\$@"
_EOF_

  tee "$pkgdir"/usr/bin/ventoygui > /dev/null << _EOF_
#!/bin/sh

cd /opt/ventoy || exit 1
./VentoyGUI.x86_64 "\$@"
_EOF_

  tee "$pkgdir"/usr/bin/ventoyplugson > /dev/null << _EOF_
#!/bin/sh

cd /opt/ventoy || exit 1
exec ./VentoyPlugson.sh "\$@"
_EOF_

  tee "$pkgdir"/usr/bin/ventoyweb > /dev/null << _EOF_
#!/bin/sh

cd /opt/ventoy || exit 1
exec ./VentoyWeb.sh "\$@"
_EOF_

  tee "$pkgdir"/usr/bin/ventoy-persistent > /dev/null << _EOF_
#!/bin/sh

exec /opt/ventoy/CreatePersistentImg.sh "\$@"
_EOF_

  tee "$pkgdir"/usr/bin/ventoy-extend-persistent > /dev/null << _EOF_
#!/bin/sh

exec /opt/ventoy/ExtendPersistentImg.sh "\$@"
_EOF_

  tee "$pkgdir"/usr/share/applications/ventoy.desktop > /dev/null << _EOF_
[Desktop Entry]
Type=Application
Icon=ventoy
Name=Ventoy
Exec=ventoygui --xdg
Terminal=false
Hidden=false
Categories=Utility
Comment=Ventoy2Disk GUI
_EOF_

  chmod -v 755 "$pkgdir"/usr/bin/*
  cp -av Ventoy-$pkgver/LinuxGUI/WebUI/static/img/VentoyLogo.png "$pkgdir"/usr/share/pixmaps/ventoy.png
}