vesper-player-cli 0.5.0

The vesper command-line tool for authoring, validating, and installing plugins.
Documentation
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
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
// Plugin requests remain parseable on every host while build internals are
// compiled only on macOS.
#![cfg_attr(not(target_os = "macos"), allow(dead_code))]

use std::collections::BTreeMap;
use std::ffi::OsString;
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::ios::IosError;

pub(crate) const IOS_PLUGIN_SPECS: [IosPluginSpec; 4] = [
    IosPluginSpec {
        id: IosPluginId::RemuxFfmpeg,
        crate_name: "player-remux-ffmpeg",
        dylib_name: "libvesper_remux_ffmpeg.dylib",
        framework_name: "VesperPlayerRemuxFfmpegPlugin",
        bundle_identifier: "io.github.umbrella22.vesper.player.remux-ffmpeg-plugin",
        build_directory: "player-remux-ffmpeg-plugin",
        release_ffmpeg_profile: Some("default"),
        uses_ffmpeg: true,
        link_headerpad: true,
        description: "remux plugin",
    },
    IosPluginSpec {
        id: IosPluginId::SourceNormalizerFfmpeg,
        crate_name: "player-source-normalizer-ffmpeg",
        dylib_name: "libvesper_source_normalizer_ffmpeg.dylib",
        framework_name: "VesperPlayerSourceNormalizerFfmpegPlugin",
        bundle_identifier: "io.github.umbrella22.vesper.player.source-normalizer-ffmpeg-plugin",
        build_directory: "player-source-normalizer-ffmpeg-plugin",
        release_ffmpeg_profile: Some("source-normalizer"),
        uses_ffmpeg: true,
        link_headerpad: false,
        description: "source normalizer plugin",
    },
    IosPluginSpec {
        id: IosPluginId::DecoderVideoToolbox,
        crate_name: "player-decoder-videotoolbox",
        dylib_name: "libvesper_decoder_videotoolbox.dylib",
        framework_name: "VesperPlayerDecoderVideoToolboxPlugin",
        bundle_identifier: "io.github.umbrella22.vesper.player.decoder-videotoolbox-plugin",
        build_directory: "player-decoder-videotoolbox-plugin",
        release_ffmpeg_profile: None,
        uses_ffmpeg: false,
        link_headerpad: false,
        description: "VideoToolbox decoder plugin",
    },
    IosPluginSpec {
        id: IosPluginId::FrameProcessorDiagnostic,
        crate_name: "player-frame-processor-diagnostic",
        dylib_name: "libvesper_frame_processor_diagnostic.dylib",
        framework_name: "VesperPlayerFrameProcessorDiagnosticPlugin",
        bundle_identifier: "io.github.umbrella22.vesper.player.frame-processor-diagnostic-plugin",
        build_directory: "player-frame-processor-diagnostic-plugin",
        release_ffmpeg_profile: None,
        uses_ffmpeg: false,
        link_headerpad: false,
        description: "frame processor plugin",
    },
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum IosPluginId {
    RemuxFfmpeg,
    SourceNormalizerFfmpeg,
    DecoderVideoToolbox,
    FrameProcessorDiagnostic,
}

impl IosPluginId {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::RemuxFfmpeg => "remux-ffmpeg",
            Self::SourceNormalizerFfmpeg => "source-normalizer-ffmpeg",
            Self::DecoderVideoToolbox => "decoder-videotoolbox",
            Self::FrameProcessorDiagnostic => "frame-processor-diagnostic",
        }
    }

    pub(crate) fn spec(self) -> &'static IosPluginSpec {
        IOS_PLUGIN_SPECS
            .iter()
            .find(|spec| spec.id == self)
            .unwrap_or_else(|| unreachable!("every iOS plugin ID has a descriptor"))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct IosPluginSpec {
    pub(crate) id: IosPluginId,
    pub(crate) crate_name: &'static str,
    pub(crate) dylib_name: &'static str,
    pub(crate) framework_name: &'static str,
    pub(crate) bundle_identifier: &'static str,
    pub(crate) build_directory: &'static str,
    pub(crate) release_ffmpeg_profile: Option<&'static str>,
    pub(crate) uses_ffmpeg: bool,
    pub(crate) link_headerpad: bool,
    pub(crate) description: &'static str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum IosPluginBuildProfile {
    Debug,
    Release,
}

impl IosPluginBuildProfile {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Debug => "debug",
            Self::Release => "release",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum IosPluginSlice {
    DeviceArm64,
    SimulatorArm64,
}

impl IosPluginSlice {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::DeviceArm64 => "ios-arm64",
            Self::SimulatorArm64 => "ios-simulator-arm64",
        }
    }

    pub(crate) const fn rust_target(self) -> &'static str {
        match self {
            Self::DeviceArm64 => "aarch64-apple-ios",
            Self::SimulatorArm64 => "aarch64-apple-ios-sim",
        }
    }
}

#[derive(Debug)]
pub(crate) struct IosPluginBuildRequest {
    pub(crate) plugin_id: IosPluginId,
    pub(crate) output_directory: PathBuf,
    pub(crate) arguments: Vec<OsString>,
    pub(crate) environment: IosPluginBuildEnvironment,
}

#[derive(Debug, Default)]
pub(crate) struct IosPluginBuildEnvironment {
    pub(crate) ios_deployment_target: Option<String>,
    pub(crate) declared_ffmpeg_profile: Option<String>,
    pub(crate) declared_ffmpeg_platform: Option<String>,
    pub(crate) ffmpeg_output_directory: Option<PathBuf>,
    pub(crate) ffmpeg_input_fingerprints: BTreeMap<IosPluginSlice, String>,
    pub(crate) skip_ffmpeg_prebuilds: Option<bool>,
    pub(crate) ffmpeg_overlays_resolved: bool,
}

pub(crate) fn ensure_supported_host() -> Result<(), IosError> {
    if cfg!(target_os = "macos") {
        Ok(())
    } else {
        Err(IosError::compatibility(
            "building iOS plugin libraries requires macOS",
        ))
    }
}

pub(crate) fn build(
    root: &Path,
    request: IosPluginBuildRequest,
    output: &mut dyn Write,
    diagnostics: &mut dyn Write,
) -> Result<(), IosError> {
    ensure_supported_host()?;

    #[cfg(target_os = "macos")]
    {
        implementation::build(root, request, output, diagnostics)
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = (root, request, output, diagnostics);
        unreachable!("the host gate rejects non-macOS iOS plugin builds")
    }
}

#[cfg(target_os = "macos")]
#[derive(Debug)]
pub(crate) struct IosPluginBuildGuard {
    inner: implementation::IosPluginBuildLock,
}

#[cfg(target_os = "macos")]
pub(crate) fn acquire_build_guard(root: &Path) -> Result<IosPluginBuildGuard, IosError> {
    implementation::IosPluginBuildLock::acquire(root).map(|inner| IosPluginBuildGuard { inner })
}

#[cfg(target_os = "macos")]
pub(crate) fn validate_build_guard(
    root: &Path,
    guard: &IosPluginBuildGuard,
) -> Result<(), IosError> {
    guard.inner.validate_root(root)
}

#[cfg(target_os = "macos")]
pub(crate) fn build_in_deferral_holding_lock(
    root: &Path,
    request: IosPluginBuildRequest,
    output: &mut dyn Write,
    diagnostics: &mut dyn Write,
    cancellation: &crate::external_process::InterruptDeferral,
    guard: &IosPluginBuildGuard,
) -> Result<(), IosError> {
    implementation::build_in_deferral_holding_lock(
        root,
        request,
        output,
        diagnostics,
        cancellation,
        &guard.inner,
    )
}

#[cfg(target_os = "macos")]
pub(crate) fn resolve_ffmpeg_output_directory_in_deferral(
    root: &Path,
    slices: &[IosPluginSlice],
    diagnostics: &mut dyn Write,
    cancellation: &crate::external_process::InterruptDeferral,
) -> Result<PathBuf, IosError> {
    implementation::resolve_ffmpeg_output_directory_in_deferral(
        root,
        slices,
        diagnostics,
        cancellation,
    )
}

#[cfg(target_os = "macos")]
mod implementation {
    use std::collections::BTreeSet;
    use std::env;
    use std::ffi::{OsStr, OsString};
    use std::fs::{self, File, OpenOptions, TryLockError};
    use std::io::{self, Read, Write};
    use std::os::unix::ffi::{OsStrExt, OsStringExt};
    use std::path::{Component, Path, PathBuf};
    use std::process::{Command, ExitStatus, Stdio};

    use serde::{Deserialize, Serialize};
    use sha2::{Digest, Sha256};

    use crate::external_process::{self, BoundedProcessOutput, ExternalProcessErrorKind};

    use super::*;

    const DEFAULT_SLICES: [IosPluginSlice; 2] =
        [IosPluginSlice::DeviceArm64, IosPluginSlice::SimulatorArm64];
    const MAX_PROCESS_OUTPUT_BYTES: usize = 32 * 1024 * 1024;
    pub(super) const MAX_FFMPEG_PROVENANCE_BYTES: u64 = 1024 * 1024;
    const MAX_DYLIB_BYTES: u64 = 512 * 1024 * 1024;
    const COPY_BUFFER_BYTES: usize = 1024 * 1024;
    const OUTPUT_MARKER_NAME: &str = ".vesper-ios-plugin-output";
    const OUTPUT_MARKER_FORMAT: &str = "vesper-ios-plugin-output";
    const MAX_OUTPUT_MARKER_BYTES: u64 = 16 * 1024;

    #[derive(Debug)]
    struct RequiredTools {
        cargo: PathBuf,
        rustc: PathBuf,
        install_name_tool: PathBuf,
        otool: PathBuf,
        lipo: PathBuf,
        xcrun: PathBuf,
    }

    #[derive(Debug)]
    struct ParsedBuildRequest {
        output_directory: PathBuf,
        profile: IosPluginBuildProfile,
        slices: Vec<IosPluginSlice>,
        deployment_target: String,
        ffmpeg: Option<FfmpegResolution>,
    }

    #[derive(Debug)]
    struct ParsedBuildPreflight {
        output_directory: PathBuf,
        profile: IosPluginBuildProfile,
        slices: Vec<IosPluginSlice>,
        deployment_target: String,
        ffmpeg: Option<crate::ffmpeg::AppleRawProfile>,
        environment: IosPluginBuildEnvironment,
    }

    #[derive(Debug)]
    struct FfmpegResolution {
        profile: String,
        output_directory: PathBuf,
        slices: Vec<FfmpegSliceInput>,
    }

    #[derive(Debug)]
    struct FfmpegSliceInput {
        slice: IosPluginSlice,
        ffmpeg_directory: PathBuf,
        cargo_cache_key: String,
        input_fingerprint: String,
    }

    #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
    #[serde(deny_unknown_fields)]
    struct OutputOwnershipMarker {
        format: String,
        plugin_id: String,
        cargo_profile: String,
        slices: Vec<String>,
        #[serde(default)]
        ios_deployment_target: Option<String>,
        ffmpeg_profile: Option<String>,
        ffmpeg_inputs: Vec<OutputFfmpegInputMarker>,
    }

    #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
    #[serde(deny_unknown_fields)]
    struct OutputFfmpegInputMarker {
        slice: String,
        input_fingerprint: String,
    }

    #[derive(Debug)]
    struct BuildOutcome {
        output_directory: PathBuf,
        ffmpeg_profile: Option<String>,
        ffmpeg_output_directory: Option<PathBuf>,
        slices: Vec<IosPluginSlice>,
        warnings: Vec<String>,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct FileIdentity {
        device: u64,
        inode: u64,
    }

    #[derive(Debug)]
    struct OutputTarget {
        path: PathBuf,
        parent: PathBuf,
        parent_identity: FileIdentity,
        initial_identity: Option<FileIdentity>,
    }

    #[derive(Debug)]
    pub(super) struct IosPluginBuildLock {
        _file: File,
        root_identity: FileIdentity,
    }

    pub(super) fn build(
        root: &Path,
        request: IosPluginBuildRequest,
        output: &mut dyn Write,
        diagnostics: &mut dyn Write,
    ) -> Result<(), IosError> {
        let plugin = request.plugin_id.spec();
        let cancellation = external_process::InterruptDeferral::start("iOS plugin build")
            .map_err(map_external_process_error)?;
        let result = build_transaction(root, plugin, request, diagnostics, &cancellation, None);
        let cancelled = cancellation.finish();
        report_build_result(plugin, result, cancelled, output, diagnostics)
    }

    pub(super) fn build_in_deferral_holding_lock(
        root: &Path,
        request: IosPluginBuildRequest,
        output: &mut dyn Write,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
        guard: &IosPluginBuildLock,
    ) -> Result<(), IosError> {
        let plugin = request.plugin_id.spec();
        let result = build_transaction(
            root,
            plugin,
            request,
            diagnostics,
            cancellation,
            Some(guard),
        );
        report_build_result(plugin, result, false, output, diagnostics)
    }

    pub(super) fn resolve_ffmpeg_output_directory_in_deferral(
        root: &Path,
        slices: &[IosPluginSlice],
        _diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<PathBuf, IosError> {
        check_cancellation(cancellation, "FFmpeg input resolution")?;
        let arguments = slices
            .iter()
            .map(|slice| OsString::from(slice.as_str()))
            .collect::<Vec<_>>();
        crate::ffmpeg::resolve_apple_raw_profile(root, &arguments, None, false)
            .map(|resolution| resolution.output_directory)
            .map_err(map_ffmpeg_error)
    }

    fn build_transaction(
        root: &Path,
        plugin: &IosPluginSpec,
        request: IosPluginBuildRequest,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
        held_lock: Option<&IosPluginBuildLock>,
    ) -> Result<BuildOutcome, IosError> {
        let owned_lock = held_lock
            .is_none()
            .then(|| IosPluginBuildLock::acquire(root))
            .transpose()?;
        let active_lock = held_lock.or(owned_lock.as_ref()).ok_or_else(|| {
            IosError::worker("iOS plugin build transaction is missing its repository lock")
        })?;
        active_lock.validate_root(root)?;
        let target = OutputTarget::preflight(root, plugin, &request.output_directory)?;
        let tools = resolve_required_tools()?;
        let preflight = parse_build_preflight(root, plugin, request)?;
        validate_required_rust_targets(&tools.rustc, &preflight.slices, cancellation)?;
        let parsed =
            hydrate_build_request(root, preflight, diagnostics, cancellation, active_lock)?;

        let staging = tempfile::Builder::new()
            .prefix(".vesper-ios-plugin-stage-")
            .tempdir_in(&target.parent)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to create iOS plugin staging directory in '{}': {error}",
                    target.parent.display()
                ))
            })?;
        for (index, slice) in parsed.slices.iter().copied().enumerate() {
            check_cancellation(cancellation, "iOS plugin build")?;
            let ffmpeg_input = parsed
                .ffmpeg
                .as_ref()
                .and_then(|resolution| resolution.slices.get(index));
            build_slice(
                root,
                plugin,
                parsed.profile,
                slice,
                ffmpeg_input,
                &parsed.deployment_target,
                staging.path(),
                &tools,
                diagnostics,
                cancellation,
            )?;
        }
        create_simulator_compatibility_copy(plugin, &parsed.slices, staging.path(), cancellation)?;
        let marker = output_ownership_marker(plugin, &parsed)?;
        write_output_marker(staging.path(), &marker)?;
        validate_staged_layout(plugin, &parsed.slices, &marker, staging.path())?;
        let warnings = promote_staged_output(staging, &target, cancellation)?;
        Ok(BuildOutcome {
            output_directory: parsed.output_directory,
            ffmpeg_profile: parsed.ffmpeg.as_ref().map(|value| value.profile.clone()),
            ffmpeg_output_directory: parsed
                .ffmpeg
                .as_ref()
                .map(|value| value.output_directory.clone()),
            slices: parsed.slices,
            warnings,
        })
    }

    fn parse_build_preflight(
        root: &Path,
        plugin: &IosPluginSpec,
        request: IosPluginBuildRequest,
    ) -> Result<ParsedBuildPreflight, IosError> {
        let IosPluginBuildRequest {
            output_directory,
            mut arguments,
            environment,
            ..
        } = request;
        let profile = match arguments.first().and_then(|value| value.to_str()) {
            Some("debug") => {
                arguments.remove(0);
                IosPluginBuildProfile::Debug
            }
            Some("release") => {
                arguments.remove(0);
                IosPluginBuildProfile::Release
            }
            _ => IosPluginBuildProfile::Debug,
        };
        let (slices, ffmpeg) = if plugin.uses_ffmpeg {
            let resolution = crate::ffmpeg::resolve_apple_raw_profile_with_source(
                root,
                &arguments,
                environment.ffmpeg_output_directory.as_deref(),
                environment.ffmpeg_overlays_resolved,
            )
            .map_err(map_ffmpeg_error)?;
            let slices = resolution
                .slices
                .iter()
                .map(|slice| parse_slice(OsStr::new(slice)))
                .collect::<Result<Vec<_>, _>>()?;
            (slices, Some(resolution))
        } else {
            (parse_non_ffmpeg_slices(&arguments)?, None)
        };
        validate_slice_selection(&slices)?;
        let deployment_target = resolve_ios_deployment_target(&environment)?;
        Ok(ParsedBuildPreflight {
            output_directory,
            profile,
            slices,
            deployment_target,
            ffmpeg,
            environment,
        })
    }

    fn hydrate_build_request(
        root: &Path,
        preflight: ParsedBuildPreflight,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
        guard: &IosPluginBuildLock,
    ) -> Result<ParsedBuildRequest, IosError> {
        let ParsedBuildPreflight {
            output_directory,
            profile,
            slices,
            deployment_target,
            ffmpeg,
            environment,
        } = preflight;
        let ffmpeg = match ffmpeg {
            Some(preflight) => {
                maybe_build_ffmpeg_prebuilts(
                    root,
                    &preflight,
                    &environment,
                    &deployment_target,
                    diagnostics,
                    cancellation,
                    guard,
                )?;
                let mut resolved = hydrate_ffmpeg_inputs(&preflight, cancellation)?;
                apply_ffmpeg_input_fingerprint_overrides(
                    &mut resolved,
                    &environment.ffmpeg_input_fingerprints,
                )?;
                Some(resolved)
            }
            None => None,
        };
        Ok(ParsedBuildRequest {
            output_directory,
            profile,
            slices,
            deployment_target,
            ffmpeg,
        })
    }

    fn hydrate_ffmpeg_inputs(
        preflight: &crate::ffmpeg::AppleRawProfile,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<FfmpegResolution, IosError> {
        let mut slices = Vec::with_capacity(preflight.slices.len());
        for value in &preflight.slices {
            check_cancellation(cancellation, "FFmpeg input provenance")?;
            let slice = parse_slice(OsStr::new(value))?;
            let ffmpeg_directory = preflight.output_directory.join(match slice {
                IosPluginSlice::DeviceArm64 => "ios",
                IosPluginSlice::SimulatorArm64 => "ios-simulator",
            });
            validate_ffmpeg_profile_hash(&ffmpeg_directory, &preflight.profile_hash)?;
            let cargo_cache_key = ffmpeg_directory_cache_key(&ffmpeg_directory)?;
            let input_fingerprint = ffmpeg_build_input_fingerprint(&ffmpeg_directory)?;
            slices.push(FfmpegSliceInput {
                slice,
                ffmpeg_directory,
                cargo_cache_key,
                input_fingerprint,
            });
        }
        Ok(FfmpegResolution {
            profile: preflight.profile.clone(),
            output_directory: preflight.output_directory.clone(),
            slices,
        })
    }

    fn validate_ffmpeg_profile_hash(
        directory: &Path,
        expected_profile_hash: &str,
    ) -> Result<(), IosError> {
        let path = directory.join("vesper-ffmpeg-build-metadata.txt");
        let metadata = read_ffmpeg_provenance_file(&path, "FFmpeg build metadata")?;
        let text = std::str::from_utf8(&metadata).map_err(|error| {
            IosError::conformance(format!(
                "FFmpeg build metadata '{}' is not UTF-8: {error}",
                path.display()
            ))
        })?;
        let mut actual_profile_hash = None;
        for line in text.lines() {
            if let Some(value) = line.strip_prefix("profile_hash=")
                && actual_profile_hash.replace(value).is_some()
            {
                return Err(IosError::conformance(format!(
                    "FFmpeg build metadata '{}' contains duplicate profile_hash records",
                    path.display()
                )));
            }
        }
        let Some(actual_profile_hash) = actual_profile_hash else {
            return Err(IosError::conformance(format!(
                "FFmpeg build metadata '{}' omits profile_hash",
                path.display()
            )));
        };
        if actual_profile_hash != expected_profile_hash {
            return Err(IosError::conformance(format!(
                "FFmpeg build metadata '{}' has profile_hash '{actual_profile_hash}', expected '{expected_profile_hash}'",
                path.display()
            )));
        }
        Ok(())
    }

    pub(super) fn ffmpeg_directory_cache_key(path: &Path) -> Result<String, IosError> {
        let bytes = path.as_os_str().as_bytes();
        let length = u64::try_from(bytes.len())
            .map_err(|_| IosError::conformance("FFmpeg input path is too long to fingerprint"))?;
        let mut digest = Sha256::new();
        digest.update(b"vesper-apple-ffmpeg-directory-v1\0");
        digest.update(length.to_le_bytes());
        digest.update(bytes);
        Ok(format!("path-{}", hex::encode(digest.finalize())))
    }

    pub(super) fn ffmpeg_build_input_fingerprint(directory: &Path) -> Result<String, IosError> {
        let metadata = read_ffmpeg_provenance_file(
            &directory.join("vesper-ffmpeg-build-metadata.txt"),
            "FFmpeg build metadata",
        )?;
        let library_checksums = read_ffmpeg_provenance_file(
            &directory.join("vesper-ffmpeg-library-sha256.txt"),
            "FFmpeg library checksum record",
        )?;
        Ok(format!(
            "{}-{}",
            hex::encode(Sha256::digest(&metadata)),
            hex::encode(Sha256::digest(&library_checksums))
        ))
    }

    fn read_ffmpeg_provenance_file(path: &Path, label: &str) -> Result<Vec<u8>, IosError> {
        use std::os::unix::fs::OpenOptionsExt;

        validate_regular_file(path, MAX_FFMPEG_PROVENANCE_BYTES, label)?;
        let file = OpenOptions::new()
            .read(true)
            .custom_flags(nix::libc::O_CLOEXEC | nix::libc::O_NOFOLLOW)
            .open(path)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to open {label} '{}': {error}",
                    path.display()
                ))
            })?;
        let metadata = file.metadata().map_err(|error| {
            IosError::storage(format!(
                "failed to inspect open {label} '{}': {error}",
                path.display()
            ))
        })?;
        if !metadata.file_type().is_file()
            || metadata.len() == 0
            || metadata.len() > MAX_FFMPEG_PROVENANCE_BYTES
        {
            return Err(IosError::conformance(format!(
                "{label} '{}' must remain a non-empty regular file within its {MAX_FFMPEG_PROVENANCE_BYTES}-byte limit",
                path.display()
            )));
        }
        let mut bytes = Vec::new();
        file.take(MAX_FFMPEG_PROVENANCE_BYTES + 1)
            .read_to_end(&mut bytes)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to read {label} '{}': {error}",
                    path.display()
                ))
            })?;
        if bytes.is_empty() || bytes.len() as u64 > MAX_FFMPEG_PROVENANCE_BYTES {
            return Err(IosError::conformance(format!(
                "{label} '{}' changed outside its non-empty {MAX_FFMPEG_PROVENANCE_BYTES}-byte limit while reading",
                path.display()
            )));
        }
        Ok(bytes)
    }

    fn apply_ffmpeg_input_fingerprint_overrides(
        resolution: &mut FfmpegResolution,
        overrides: &BTreeMap<IosPluginSlice, String>,
    ) -> Result<(), IosError> {
        if overrides.is_empty() {
            return Ok(());
        }
        if overrides.len() != resolution.slices.len() {
            return Err(IosError::conformance(
                "FFmpeg input fingerprint overrides do not match the resolved slices",
            ));
        }
        for input in &mut resolution.slices {
            let fingerprint = overrides.get(&input.slice).ok_or_else(|| {
                IosError::conformance(format!(
                    "FFmpeg input fingerprint override is missing for {}",
                    input.slice.as_str()
                ))
            })?;
            validate_cache_component(fingerprint, "FFmpeg input fingerprint")?;
            input.input_fingerprint = fingerprint.clone();
        }
        Ok(())
    }

    fn parse_non_ffmpeg_slices(arguments: &[OsString]) -> Result<Vec<IosPluginSlice>, IosError> {
        if arguments.is_empty() {
            return Ok(DEFAULT_SLICES.to_vec());
        }
        arguments
            .iter()
            .map(|argument| parse_slice(argument.as_os_str()))
            .collect()
    }

    fn parse_slice(value: &OsStr) -> Result<IosPluginSlice, IosError> {
        match value.to_str() {
            Some("ios-arm64") => Ok(IosPluginSlice::DeviceArm64),
            Some("ios-simulator-arm64") => Ok(IosPluginSlice::SimulatorArm64),
            Some(value) => Err(IosError::compatibility(format!(
                "unsupported Apple slice: {value}; expected ios-arm64 or ios-simulator-arm64"
            ))),
            None => Err(IosError::compatibility(
                "Apple slice names must be valid UTF-8",
            )),
        }
    }

    fn validate_slice_selection(slices: &[IosPluginSlice]) -> Result<(), IosError> {
        if slices.is_empty() {
            return Err(IosError::compatibility("no Apple slices were selected"));
        }
        if slices.iter().copied().collect::<BTreeSet<_>>().len() != slices.len() {
            return Err(IosError::conformance(
                "Apple slice selection contains a duplicate slice",
            ));
        }
        Ok(())
    }

    fn maybe_build_ffmpeg_prebuilts(
        root: &Path,
        preflight: &crate::ffmpeg::AppleRawProfile,
        environment: &IosPluginBuildEnvironment,
        deployment_target: &str,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
        guard: &IosPluginBuildLock,
    ) -> Result<(), IosError> {
        let skip = environment.skip_ffmpeg_prebuilds.unwrap_or_else(|| {
            env::var_os("VESPER_SKIP_APPLE_FFMPEG_PREBUILDS").as_deref() == Some(OsStr::new("1"))
        });
        if skip {
            return Ok(());
        }
        guard.validate_root(root)?;
        let mut profile = preflight.native_profile.clone();
        if let Some(declared_profile) = &environment.declared_ffmpeg_profile {
            profile.declared_profile = declared_profile.clone();
        }
        if let Some(declared_platform) = &environment.declared_ffmpeg_platform
            && declared_platform != "ios"
        {
            return Err(IosError::conformance(format!(
                "resolved Apple FFmpeg declared platform must be ios, got {declared_platform}"
            )));
        }
        let source = preflight.source.clone().ok_or_else(|| {
            IosError::conformance("resolved Apple FFmpeg profile did not carry source provenance")
        })?;
        crate::ffmpeg_apple::run_holding_repository_lock(
            root,
            &preflight.output_directory,
            &preflight.slices,
            deployment_target,
            &profile,
            &source,
            diagnostics,
            cancellation,
        )
        .map_err(map_ffmpeg_error)
    }

    fn resolve_ios_deployment_target(
        environment: &IosPluginBuildEnvironment,
    ) -> Result<String, IosError> {
        let inherited = match env::var_os("VESPER_APPLE_IOS_DEPLOYMENT_TARGET") {
            Some(value) => Some(value.into_string().map_err(|_| {
                IosError::compatibility("VESPER_APPLE_IOS_DEPLOYMENT_TARGET must be valid UTF-8")
            })?),
            None => None,
        };
        let value = environment
            .ios_deployment_target
            .clone()
            .or(inherited)
            .unwrap_or_else(|| "17.0".to_owned());
        validate_ios_deployment_target(&value)?;
        Ok(value)
    }

    pub(super) fn validate_ios_deployment_target(value: &str) -> Result<(), IosError> {
        let version =
            crate::ios::normalize_apple_version(value, "iOS deployment target").map_err(|_| {
                IosError::compatibility(format!("iOS plugin deployment target is invalid: {value}"))
            })?;
        if version < (17, 0, 0) {
            return Err(IosError::compatibility(format!(
                "iOS plugin deployment target {value} is below the supported iOS 17.0 floor"
            )));
        }
        Ok(())
    }

    fn resolve_required_tools() -> Result<RequiredTools, IosError> {
        Ok(RequiredTools {
            cargo: require_path_command("cargo")?,
            rustc: require_path_command("rustc")?,
            install_name_tool: require_path_command("install_name_tool")?,
            otool: require_path_command("otool")?,
            lipo: require_path_command("lipo")?,
            xcrun: require_path_command("xcrun")?,
        })
    }

    fn require_path_command(name: &str) -> Result<PathBuf, IosError> {
        let paths = env::var_os("PATH").unwrap_or_default();
        resolve_path_command(&paths, name)
            .ok_or_else(|| IosError::compatibility(format!("Missing required command: {name}")))
    }

    fn resolve_path_command(paths: &OsStr, name: &str) -> Option<PathBuf> {
        use nix::unistd::{AccessFlags, access};

        env::split_paths(paths).find_map(|directory| {
            let directory = if directory.as_os_str().is_empty() {
                env::current_dir().ok()?
            } else {
                directory
            };
            let candidate = directory.join(name);
            let metadata = fs::metadata(&candidate).ok()?;
            (metadata.is_file() && access(&candidate, AccessFlags::X_OK).is_ok())
                .then_some(candidate)
        })
    }

    fn validate_required_rust_targets(
        rustc: &Path,
        slices: &[IosPluginSlice],
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let mut missing = Vec::new();
        for slice in slices {
            check_cancellation(cancellation, "Rust target validation")?;
            if !rust_target_is_installed(rustc, slice.rust_target(), cancellation)? {
                missing.push(slice.rust_target());
            }
        }
        if missing.is_empty() {
            Ok(())
        } else {
            Err(IosError::compatibility(format!(
                "Required Rust Apple targets are missing:\n  {}\n\nInstall them with:\n  rustup target add {}",
                missing.join("\n  "),
                missing.join(" ")
            )))
        }
    }

    fn rust_target_is_installed(
        rustc: &Path,
        target: &str,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<bool, IosError> {
        let mut command = Command::new(rustc);
        command
            .args(["--print", "target-libdir", "--target", target])
            .stdin(Stdio::null());
        let result = run_process_capture(
            &mut command,
            &format!("Rust target query for {target}"),
            cancellation,
        )?;
        if result.status.code().is_none_or(|code| code >= 128) {
            return Err(IosError::worker(format!(
                "rustc terminated abnormally while checking target {target} ({})",
                result.status
            )));
        }
        if !result.status.success() {
            return Ok(false);
        }
        let target_libdir = PathBuf::from(OsString::from_vec(trim_ascii(&result.stdout).to_vec()));
        Ok(target_libdir.is_dir())
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "the plugin slice build boundary keeps the selected slice, toolchain, staging, diagnostics, and cancellation state explicit"
    )]
    fn build_slice(
        root: &Path,
        plugin: &IosPluginSpec,
        profile: IosPluginBuildProfile,
        slice: IosPluginSlice,
        ffmpeg_input: Option<&FfmpegSliceInput>,
        deployment_target: &str,
        staging: &Path,
        tools: &RequiredTools,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let cargo_target_directory =
            cargo_target_directory(root, plugin, ffmpeg_input, deployment_target)?;
        let mut command = Command::new(&tools.cargo);
        command
            .current_dir(root)
            .args(["build", "--locked", "--manifest-path"])
            .arg(root.join("Cargo.toml"))
            .args(["--target", slice.rust_target(), "-p", plugin.crate_name])
            .env("CARGO_TARGET_DIR", &cargo_target_directory)
            .env("IPHONEOS_DEPLOYMENT_TARGET", deployment_target)
            .stdin(Stdio::null());
        if profile == IosPluginBuildProfile::Release {
            command.arg("--release");
        }
        if let Some(input) = ffmpeg_input {
            command.env("FFMPEG_DIR", &input.ffmpeg_directory);
        }
        if plugin.link_headerpad {
            let mut rustflags = env::var_os("RUSTFLAGS").unwrap_or_default();
            if !rustflags.is_empty() {
                rustflags.push(" ");
            }
            rustflags.push("-C link-arg=-Wl,-headerpad_max_install_names");
            command.env("RUSTFLAGS", rustflags);
        }
        run_required_command(
            command,
            &format!("Cargo build for {}/{}", plugin.crate_name, slice.as_str()),
            diagnostics,
            cancellation,
        )?;
        let artifact = cargo_target_directory
            .join(slice.rust_target())
            .join(profile.as_str())
            .join(plugin.dylib_name);
        validate_regular_file(&artifact, MAX_DYLIB_BYTES, "built iOS plugin library")?;
        let destination = staged_slice_path(staging, plugin, slice);
        let parent = destination.parent().ok_or_else(|| {
            IosError::storage("staged iOS plugin library has no parent directory")
        })?;
        fs::create_dir_all(parent).map_err(|error| {
            IosError::storage(format!(
                "failed to create staged iOS plugin directory '{}': {error}",
                parent.display()
            ))
        })?;
        copy_regular_file(&artifact, &destination, cancellation)?;
        prepare_plugin_binary(plugin, &destination, tools, diagnostics, cancellation)?;
        verify_arm64_binary(&destination, tools, diagnostics, cancellation)?;
        verify_macho_build_metadata(
            &destination,
            slice,
            deployment_target,
            tools,
            diagnostics,
            cancellation,
        )
    }

    fn cargo_target_directory(
        root: &Path,
        plugin: &IosPluginSpec,
        ffmpeg_input: Option<&FfmpegSliceInput>,
        deployment_target: &str,
    ) -> Result<PathBuf, IosError> {
        let base = root
            .join("target")
            .join(format!("{}-ios", plugin.crate_name))
            .join(format!(
                "minimum-ios-{}",
                deployment_target.replace('.', "_")
            ));
        match ffmpeg_input {
            Some(input) => {
                let cache_key = &input.cargo_cache_key;
                let fingerprint = &input.input_fingerprint;
                validate_cache_component(cache_key, "Cargo cache key")?;
                validate_cache_component(fingerprint, "FFmpeg input fingerprint")?;
                Ok(base.join(cache_key).join(fingerprint))
            }
            None => Ok(base),
        }
    }

    fn validate_cache_component(value: &str, label: &str) -> Result<(), IosError> {
        let path = Path::new(value);
        let mut components = path.components();
        if value.is_empty()
            || !matches!(components.next(), Some(Component::Normal(_)))
            || components.next().is_some()
        {
            return Err(IosError::conformance(format!(
                "{label} must be one normalized path component"
            )));
        }
        Ok(())
    }

    fn staged_slice_path(staging: &Path, plugin: &IosPluginSpec, slice: IosPluginSlice) -> PathBuf {
        match slice {
            IosPluginSlice::DeviceArm64 => staging.join("iphoneos").join(plugin.dylib_name),
            IosPluginSlice::SimulatorArm64 => staging
                .join("iphonesimulator")
                .join(slice.rust_target())
                .join(plugin.dylib_name),
        }
    }

    fn create_simulator_compatibility_copy(
        plugin: &IosPluginSpec,
        slices: &[IosPluginSlice],
        staging: &Path,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        if !slices.contains(&IosPluginSlice::SimulatorArm64) {
            return Ok(());
        }
        let source = staged_slice_path(staging, plugin, IosPluginSlice::SimulatorArm64);
        let destination = staging.join("iphonesimulator").join(plugin.dylib_name);
        copy_regular_file(&source, &destination, cancellation)
    }

    fn prepare_plugin_binary(
        plugin: &IosPluginSpec,
        binary: &Path,
        tools: &RequiredTools,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let mut install_id = Command::new(&tools.install_name_tool);
        install_id
            .args(["-id", &format!("@rpath/{}", plugin.dylib_name)])
            .arg(binary);
        run_required_command(
            install_id,
            "iOS plugin install-name update",
            diagnostics,
            cancellation,
        )?;
        if !plugin.uses_ffmpeg {
            return Ok(());
        }
        let rpaths = read_rpaths(binary, tools, diagnostics, cancellation)?;
        for stale in [
            "@loader_path/VesperPlayerFfmpegRuntime.framework/Frameworks",
            "@loader_path/../VesperPlayerFfmpegRuntime.framework/Frameworks",
        ] {
            if rpaths.contains(stale) {
                let mut remove = Command::new(&tools.install_name_tool);
                remove.args(["-delete_rpath", stale]).arg(binary);
                run_required_command(
                    remove,
                    "iOS plugin stale rpath removal",
                    diagnostics,
                    cancellation,
                )?;
            }
        }
        for required in ["@loader_path", "@loader_path/.."] {
            if !rpaths.contains(required) {
                let mut add = Command::new(&tools.install_name_tool);
                add.args(["-add_rpath", required]).arg(binary);
                run_required_command(
                    add,
                    "iOS plugin loader rpath update",
                    diagnostics,
                    cancellation,
                )?;
            }
        }
        Ok(())
    }

    fn read_rpaths(
        binary: &Path,
        tools: &RequiredTools,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<BTreeSet<String>, IosError> {
        let mut command = Command::new(&tools.otool);
        command.arg("-l").arg(binary);
        let result =
            run_process_capture(&mut command, "iOS plugin rpath inspection", cancellation)?;
        diagnostics
            .write_all(&result.stderr)
            .map_err(diagnostics_error)?;
        diagnostics.flush().map_err(diagnostics_error)?;
        classify_process_status(result.status, "iOS plugin rpath inspection")?;
        let output = std::str::from_utf8(&result.stdout).map_err(|error| {
            IosError::conformance(format!(
                "otool returned non-UTF-8 iOS plugin metadata: {error}"
            ))
        })?;
        let mut rpaths = BTreeSet::new();
        for line in output.lines() {
            let mut fields = line.split_ascii_whitespace();
            if fields.next() == Some("path")
                && let Some(path) = fields.next()
            {
                rpaths.insert(path.to_owned());
            }
        }
        Ok(rpaths)
    }

    fn verify_arm64_binary(
        binary: &Path,
        tools: &RequiredTools,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let command = lipo_verify_arm64_command(&tools.lipo, binary);
        run_required_command(
            command,
            "iOS plugin arm64 architecture verification",
            diagnostics,
            cancellation,
        )
    }

    pub(super) fn lipo_verify_arm64_command(lipo: &Path, binary: &Path) -> Command {
        let mut command = Command::new(lipo);
        command.arg(binary).args(["-verify_arch", "arm64"]);
        command
    }

    fn verify_macho_build_metadata(
        binary: &Path,
        slice: IosPluginSlice,
        deployment_target: &str,
        tools: &RequiredTools,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let mut command = Command::new(&tools.xcrun);
        command
            .args(["vtool", "-show-build"])
            .arg(binary)
            .stdin(Stdio::null());
        let result = run_process_capture(
            &mut command,
            "iOS plugin Mach-O build metadata inspection",
            cancellation,
        )?;
        diagnostics
            .write_all(&result.stderr)
            .map_err(diagnostics_error)?;
        classify_process_status(result.status, "iOS plugin Mach-O build metadata inspection")?;
        let output = std::str::from_utf8(&result.stdout).map_err(|error| {
            IosError::conformance(format!(
                "xcrun vtool output is not UTF-8 for '{}': {error}",
                binary.display()
            ))
        })?;
        let metadata =
            crate::ios::parse_vtool_build_metadata(output, &binary.display().to_string())?;
        let expected_platform = match slice {
            IosPluginSlice::DeviceArm64 => "IOS",
            IosPluginSlice::SimulatorArm64 => "IOSSIMULATOR",
        };
        if metadata.platform != expected_platform {
            return Err(IosError::conformance(format!(
                "iOS plugin Mach-O platform mismatch for '{}': actual {}, expected {expected_platform}",
                binary.display(),
                metadata.platform
            )));
        }
        if crate::ios::normalize_apple_version(&metadata.minimum_os, "plugin Mach-O minimum OS")?
            != crate::ios::normalize_apple_version(deployment_target, "iOS deployment target")?
        {
            return Err(IosError::conformance(format!(
                "iOS plugin Mach-O minimum OS mismatch for '{}': actual {}, expected {}",
                binary.display(),
                metadata.minimum_os,
                deployment_target
            )));
        }
        Ok(())
    }

    fn validate_staged_layout(
        plugin: &IosPluginSpec,
        slices: &[IosPluginSlice],
        expected_marker: &OutputOwnershipMarker,
        staging: &Path,
    ) -> Result<(), IosError> {
        let mut expected_root = slices
            .iter()
            .map(|slice| match slice {
                IosPluginSlice::DeviceArm64 => OsString::from("iphoneos"),
                IosPluginSlice::SimulatorArm64 => OsString::from("iphonesimulator"),
            })
            .collect::<BTreeSet<_>>();
        expected_root.insert(OsString::from(OUTPUT_MARKER_NAME));
        if read_directory_names(staging, "iOS plugin staging directory", 3)? != expected_root {
            return Err(IosError::conformance(
                "iOS plugin staging directory has an unexpected artifact set",
            ));
        }
        let marker = read_output_marker(staging, plugin)?;
        if marker != *expected_marker {
            return Err(IosError::conformance(
                "staged iOS plugin output marker does not match its build inputs",
            ));
        }
        validate_plugin_slice_layout(plugin, slices, staging)
    }

    fn validate_plugin_slice_layout(
        plugin: &IosPluginSpec,
        slices: &[IosPluginSlice],
        directory_root: &Path,
    ) -> Result<(), IosError> {
        if slices.contains(&IosPluginSlice::DeviceArm64) {
            let directory = directory_root.join("iphoneos");
            let expected = BTreeSet::from([OsString::from(plugin.dylib_name)]);
            if read_directory_names(&directory, "iOS device plugin directory", 1)? != expected {
                return Err(IosError::conformance(
                    "iOS device plugin directory has an unexpected artifact set",
                ));
            }
            validate_regular_file(
                &directory.join(plugin.dylib_name),
                MAX_DYLIB_BYTES,
                "staged iOS device plugin library",
            )?;
        }
        if slices.contains(&IosPluginSlice::SimulatorArm64) {
            let directory = directory_root.join("iphonesimulator");
            let expected = BTreeSet::from([
                OsString::from(plugin.dylib_name),
                OsString::from(IosPluginSlice::SimulatorArm64.rust_target()),
            ]);
            if read_directory_names(&directory, "iOS Simulator plugin directory", 2)? != expected {
                return Err(IosError::conformance(
                    "iOS Simulator plugin directory has an unexpected artifact set",
                ));
            }
            let target_directory = directory.join(IosPluginSlice::SimulatorArm64.rust_target());
            let expected = BTreeSet::from([OsString::from(plugin.dylib_name)]);
            if read_directory_names(
                &target_directory,
                "iOS Simulator target plugin directory",
                1,
            )? != expected
            {
                return Err(IosError::conformance(
                    "iOS Simulator target plugin directory has an unexpected artifact set",
                ));
            }
            for binary in [
                directory.join(plugin.dylib_name),
                target_directory.join(plugin.dylib_name),
            ] {
                validate_regular_file(
                    &binary,
                    MAX_DYLIB_BYTES,
                    "staged iOS Simulator plugin library",
                )?;
            }
        }
        Ok(())
    }

    fn output_ownership_marker(
        plugin: &IosPluginSpec,
        request: &ParsedBuildRequest,
    ) -> Result<OutputOwnershipMarker, IosError> {
        let ffmpeg_inputs = match request.ffmpeg.as_ref() {
            Some(ffmpeg) => ffmpeg
                .slices
                .iter()
                .map(|input| {
                    Ok(OutputFfmpegInputMarker {
                        slice: input.slice.as_str().to_owned(),
                        input_fingerprint: input.input_fingerprint.clone(),
                    })
                })
                .collect::<Result<Vec<_>, IosError>>()?,
            None => Vec::new(),
        };
        Ok(OutputOwnershipMarker {
            format: OUTPUT_MARKER_FORMAT.to_owned(),
            plugin_id: plugin.id.as_str().to_owned(),
            cargo_profile: request.profile.as_str().to_owned(),
            slices: request
                .slices
                .iter()
                .map(|slice| slice.as_str().to_owned())
                .collect(),
            ios_deployment_target: Some(request.deployment_target.clone()),
            ffmpeg_profile: request.ffmpeg.as_ref().map(|ffmpeg| ffmpeg.profile.clone()),
            ffmpeg_inputs,
        })
    }

    fn write_output_marker(
        staging: &Path,
        marker_value: &OutputOwnershipMarker,
    ) -> Result<(), IosError> {
        let marker = staging.join(OUTPUT_MARKER_NAME);
        let mut bytes = serde_json::to_vec(marker_value).map_err(|error| {
            IosError::conformance(format!(
                "failed to serialize iOS plugin output marker: {error}"
            ))
        })?;
        bytes.push(b'\n');
        if u64::try_from(bytes.len()).map_or(true, |length| length > MAX_OUTPUT_MARKER_BYTES) {
            return Err(IosError::conformance(format!(
                "iOS plugin output marker exceeds its {MAX_OUTPUT_MARKER_BYTES}-byte schema limit"
            )));
        }
        let mut file = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(&marker)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to create iOS plugin output marker '{}': {error}",
                    marker.display()
                ))
            })?;
        file.write_all(&bytes).map_err(|error| {
            IosError::storage(format!(
                "failed to write iOS plugin output marker '{}': {error}",
                marker.display()
            ))
        })?;
        file.sync_all().map_err(|error| {
            IosError::storage(format!(
                "failed to sync iOS plugin output marker '{}': {error}",
                marker.display()
            ))
        })
    }

    fn read_output_marker(
        output: &Path,
        plugin: &IosPluginSpec,
    ) -> Result<OutputOwnershipMarker, IosError> {
        let marker = output.join(OUTPUT_MARKER_NAME);
        validate_regular_file(&marker, MAX_OUTPUT_MARKER_BYTES, "iOS plugin output marker")?;
        let file = OpenOptions::new()
            .read(true)
            .open(&marker)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to open iOS plugin output marker '{}': {error}",
                    marker.display()
                ))
            })?;
        let mut content = Vec::new();
        file.take(MAX_OUTPUT_MARKER_BYTES + 1)
            .read_to_end(&mut content)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to read iOS plugin output marker '{}': {error}",
                    marker.display()
                ))
            })?;
        if u64::try_from(content.len()).map_or(true, |length| length > MAX_OUTPUT_MARKER_BYTES) {
            return Err(IosError::compatibility(format!(
                "iOS plugin output marker '{}' exceeds its {MAX_OUTPUT_MARKER_BYTES}-byte schema limit",
                marker.display()
            )));
        }
        let value: OutputOwnershipMarker = serde_json::from_slice(&content).map_err(|error| {
            IosError::compatibility(format!(
                "iOS plugin output marker '{}' is invalid: {error}",
                marker.display()
            ))
        })?;
        validate_output_marker_fields(&value, plugin, &marker)?;
        Ok(value)
    }

    fn validate_output_marker_fields(
        marker: &OutputOwnershipMarker,
        plugin: &IosPluginSpec,
        marker_path: &Path,
    ) -> Result<(), IosError> {
        if marker.format != OUTPUT_MARKER_FORMAT || marker.plugin_id != plugin.id.as_str() {
            return Err(IosError::compatibility(format!(
                "iOS plugin output marker '{}' does not identify a compatible {} output",
                marker_path.display(),
                plugin.id.as_str()
            )));
        }
        if !matches!(marker.cargo_profile.as_str(), "debug" | "release") {
            return Err(IosError::compatibility(format!(
                "iOS plugin output marker '{}' has an unsupported Cargo profile",
                marker_path.display()
            )));
        }
        if let Some(deployment_target) = &marker.ios_deployment_target {
            validate_ios_deployment_target(deployment_target)?;
        }
        let slices = parse_marker_slices(&marker.slices, marker_path)?;
        if plugin.uses_ffmpeg {
            if marker.ffmpeg_profile.as_deref().is_none_or(str::is_empty)
                || marker.ffmpeg_inputs.len() != slices.len()
            {
                return Err(IosError::compatibility(format!(
                    "iOS plugin output marker '{}' has incomplete FFmpeg provenance",
                    marker_path.display()
                )));
            }
            let mut input_slices = BTreeSet::new();
            for input in &marker.ffmpeg_inputs {
                let slice = parse_slice(OsStr::new(&input.slice))?;
                if input.input_fingerprint.is_empty() || !input_slices.insert(slice) {
                    return Err(IosError::compatibility(format!(
                        "iOS plugin output marker '{}' has invalid FFmpeg input provenance",
                        marker_path.display()
                    )));
                }
            }
            if input_slices != slices.iter().copied().collect() {
                return Err(IosError::compatibility(format!(
                    "iOS plugin output marker '{}' has mismatched FFmpeg slices",
                    marker_path.display()
                )));
            }
        } else if marker.ffmpeg_profile.is_some() || !marker.ffmpeg_inputs.is_empty() {
            return Err(IosError::compatibility(format!(
                "iOS plugin output marker '{}' unexpectedly declares FFmpeg provenance",
                marker_path.display()
            )));
        }
        Ok(())
    }

    fn parse_marker_slices(
        values: &[String],
        marker_path: &Path,
    ) -> Result<Vec<IosPluginSlice>, IosError> {
        let slices = values
            .iter()
            .map(|value| parse_slice(OsStr::new(value)))
            .collect::<Result<Vec<_>, _>>()?;
        let unique = slices.iter().copied().collect::<BTreeSet<_>>();
        if slices.is_empty() || unique.len() != slices.len() {
            return Err(IosError::compatibility(format!(
                "iOS plugin output marker '{}' has an empty or repeated slice set",
                marker_path.display()
            )));
        }
        Ok(slices)
    }

    fn validate_existing_output_ownership(
        output: &Path,
        plugin: &IosPluginSpec,
    ) -> Result<(), IosError> {
        let marker = output.join(OUTPUT_MARKER_NAME);
        match fs::symlink_metadata(&marker) {
            Ok(_) => {
                let marker = read_output_marker(output, plugin)?;
                let slices = parse_marker_slices(&marker.slices, &output.join(OUTPUT_MARKER_NAME))?;
                let mut expected_root = slices
                    .iter()
                    .map(|slice| match slice {
                        IosPluginSlice::DeviceArm64 => OsString::from("iphoneos"),
                        IosPluginSlice::SimulatorArm64 => OsString::from("iphonesimulator"),
                    })
                    .collect::<BTreeSet<_>>();
                expected_root.insert(OsString::from(OUTPUT_MARKER_NAME));
                if read_directory_names(output, "existing iOS plugin output", 3)? != expected_root {
                    return Err(IosError::compatibility(format!(
                        "refusing to replace malformed builder-owned iOS plugin output directory '{}'",
                        output.display()
                    )));
                }
                validate_plugin_slice_layout(plugin, &slices, output).map_err(|error| {
                    IosError::compatibility(format!(
                        "refusing to replace malformed builder-owned iOS plugin output directory '{}': {error}",
                        output.display()
                    ))
                })
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => {
                validate_legacy_output_layout(output, plugin)
            }
            Err(error) => Err(IosError::storage(format!(
                "failed to inspect iOS plugin output marker '{}': {error}",
                marker.display()
            ))),
        }
    }

    fn validate_legacy_output_layout(
        output: &Path,
        plugin: &IosPluginSpec,
    ) -> Result<(), IosError> {
        let names = read_directory_names(output, "existing iOS plugin output", 2)?;
        let mut slices = Vec::new();
        if names.contains(OsStr::new("iphoneos")) {
            slices.push(IosPluginSlice::DeviceArm64);
        }
        if names.contains(OsStr::new("iphonesimulator")) {
            slices.push(IosPluginSlice::SimulatorArm64);
        }
        let expected = slices
            .iter()
            .map(|slice| match slice {
                IosPluginSlice::DeviceArm64 => OsString::from("iphoneos"),
                IosPluginSlice::SimulatorArm64 => OsString::from("iphonesimulator"),
            })
            .collect::<BTreeSet<_>>();
        if slices.is_empty() || names != expected {
            return Err(IosError::compatibility(format!(
                "refusing to replace unowned iOS plugin output directory '{}'; remove it explicitly or use a builder-owned output",
                output.display()
            )));
        }
        validate_plugin_slice_layout(plugin, &slices, output).map_err(|error| {
            IosError::compatibility(format!(
                "refusing to replace unowned iOS plugin output directory '{}': {error}",
                output.display()
            ))
        })
    }

    fn read_directory_names(
        path: &Path,
        label: &str,
        maximum_entries: usize,
    ) -> Result<BTreeSet<OsString>, IosError> {
        let mut names = BTreeSet::new();
        let entries = fs::read_dir(path).map_err(|error| {
            IosError::storage(format!(
                "failed to read {label} '{}': {error}",
                path.display()
            ))
        })?;
        for entry in entries {
            if names.len() >= maximum_entries {
                return Err(IosError::conformance(format!(
                    "{label} '{}' exceeds its {maximum_entries}-entry limit",
                    path.display()
                )));
            }
            let entry = entry.map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect {label} '{}': {error}",
                    path.display()
                ))
            })?;
            names.insert(entry.file_name());
        }
        Ok(names)
    }

    fn validate_regular_file(path: &Path, maximum_bytes: u64, label: &str) -> Result<(), IosError> {
        let metadata = fs::symlink_metadata(path).map_err(|error| {
            IosError::storage(format!(
                "failed to inspect {label} '{}': {error}",
                path.display()
            ))
        })?;
        if !metadata.file_type().is_file() || metadata.len() == 0 {
            return Err(IosError::conformance(format!(
                "{label} '{}' must be a non-empty regular non-symlink file",
                path.display()
            )));
        }
        if metadata.len() > maximum_bytes {
            return Err(IosError::conformance(format!(
                "{label} '{}' exceeds its {maximum_bytes}-byte limit",
                path.display()
            )));
        }
        Ok(())
    }

    fn copy_regular_file(
        source: &Path,
        destination: &Path,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        validate_regular_file(source, MAX_DYLIB_BYTES, "iOS plugin copy source")?;
        let mut input = File::open(source).map_err(|error| {
            IosError::storage(format!(
                "failed to open iOS plugin copy source '{}': {error}",
                source.display()
            ))
        })?;
        let mut output = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(destination)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to create staged iOS plugin library '{}': {error}",
                    destination.display()
                ))
            })?;
        let mut buffer = vec![0_u8; COPY_BUFFER_BYTES];
        loop {
            check_cancellation(cancellation, "iOS plugin library copy")?;
            let read = input.read(&mut buffer).map_err(|error| {
                IosError::storage(format!(
                    "failed to read iOS plugin library '{}': {error}",
                    source.display()
                ))
            })?;
            if read == 0 {
                break;
            }
            output.write_all(&buffer[..read]).map_err(|error| {
                IosError::storage(format!(
                    "failed to write staged iOS plugin library '{}': {error}",
                    destination.display()
                ))
            })?;
        }
        output.sync_all().map_err(|error| {
            IosError::storage(format!(
                "failed to sync staged iOS plugin library '{}': {error}",
                destination.display()
            ))
        })?;
        let permissions = fs::metadata(source)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect iOS plugin library permissions '{}': {error}",
                    source.display()
                ))
            })?
            .permissions();
        fs::set_permissions(destination, permissions).map_err(|error| {
            IosError::storage(format!(
                "failed to preserve iOS plugin library permissions '{}': {error}",
                destination.display()
            ))
        })
    }

    impl OutputTarget {
        fn preflight(
            root: &Path,
            plugin: &IosPluginSpec,
            requested: &Path,
        ) -> Result<Self, IosError> {
            let file_name = requested
                .components()
                .next_back()
                .and_then(|component| match component {
                    Component::Normal(value) => Some(value.to_os_string()),
                    _ => None,
                })
                .ok_or_else(|| {
                    IosError::compatibility("the iOS plugin output must name a child directory")
                })?;
            let absolute = if requested.is_absolute() {
                requested.to_path_buf()
            } else {
                env::current_dir()
                    .map_err(|error| {
                        IosError::storage(format!(
                            "failed to resolve the iOS plugin output directory: {error}"
                        ))
                    })?
                    .join(requested)
            };
            let parent = absolute.parent().ok_or_else(|| {
                IosError::compatibility("the iOS plugin output must have a parent directory")
            })?;
            fs::create_dir_all(parent).map_err(|error| {
                IosError::storage(format!(
                    "failed to create iOS plugin output parent '{}': {error}",
                    parent.display()
                ))
            })?;
            let parent = fs::canonicalize(parent).map_err(|error| {
                IosError::storage(format!(
                    "failed to resolve iOS plugin output parent '{}': {error}",
                    parent.display()
                ))
            })?;
            let parent_metadata = fs::symlink_metadata(&parent).map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect iOS plugin output parent '{}': {error}",
                    parent.display()
                ))
            })?;
            if !parent_metadata.file_type().is_dir() {
                return Err(IosError::compatibility(format!(
                    "iOS plugin output parent '{}' must be a regular non-symlink directory",
                    parent.display()
                )));
            }
            let path = parent.join(file_name);
            let canonical_root = fs::canonicalize(root).map_err(|error| {
                IosError::storage(format!(
                    "failed to resolve iOS plugin repository root '{}': {error}",
                    root.display()
                ))
            })?;
            if canonical_root == path || canonical_root.starts_with(&path) {
                return Err(IosError::compatibility(format!(
                    "refusing to replace repository root or ancestor as iOS plugin output: {}",
                    path.display()
                )));
            }
            let initial_identity = optional_directory_identity(&path, "iOS plugin output")?;
            if initial_identity.is_some() {
                validate_existing_output_ownership(&path, plugin)?;
            }
            Ok(Self {
                initial_identity,
                path,
                parent,
                parent_identity: file_identity(&parent_metadata),
            })
        }

        fn revalidate(&self) -> Result<bool, IosError> {
            let parent_metadata = fs::symlink_metadata(&self.parent).map_err(|error| {
                IosError::storage(format!(
                    "failed to recheck iOS plugin output parent '{}': {error}",
                    self.parent.display()
                ))
            })?;
            if !parent_metadata.file_type().is_dir()
                || file_identity(&parent_metadata) != self.parent_identity
            {
                return Err(IosError::compatibility(format!(
                    "iOS plugin output parent '{}' changed after validation",
                    self.parent.display()
                )));
            }
            let current = optional_directory_identity(&self.path, "iOS plugin output")?;
            if current != self.initial_identity {
                return Err(IosError::compatibility(format!(
                    "iOS plugin output '{}' changed after validation",
                    self.path.display()
                )));
            }
            Ok(current.is_some())
        }
    }

    fn promote_staged_output(
        staging: tempfile::TempDir,
        target: &OutputTarget,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<Vec<String>, IosError> {
        let source = staging.path().to_path_buf();
        let source_identity = directory_identity(&source, "staged iOS plugin output")?;
        check_cancellation(cancellation, "iOS plugin output promotion")?;
        let had_previous = target.revalidate()?;
        check_cancellation(cancellation, "iOS plugin output promotion")?;
        if had_previous {
            exchange_paths(&source, &target.path).map_err(|error| {
                IosError::storage(format!(
                    "failed to atomically exchange iOS plugin output '{}': {error}",
                    target.path.display()
                ))
            })?;
            let previous_identity = directory_identity(&source, "previous iOS plugin output").ok();
            let promoted_identity =
                directory_identity(&target.path, "promoted iOS plugin output").ok();
            if previous_identity != target.initial_identity
                || promoted_identity != Some(source_identity)
            {
                return Err(rollback_or_preserve(
                    staging,
                    &source,
                    target,
                    true,
                    source_identity,
                    IosError::compatibility(format!(
                        "iOS plugin output '{}' changed during atomic promotion",
                        target.path.display()
                    )),
                ));
            }
        } else {
            rename_noreplace(&source, &target.path).map_err(|error| {
                IosError::storage(format!(
                    "failed to atomically publish iOS plugin output '{}': {error}",
                    target.path.display()
                ))
            })?;
        }
        if let Err(error) = sync_directory(&target.parent).and_then(|()| {
            let promoted = directory_identity(&target.path, "promoted iOS plugin output")?;
            if promoted == source_identity {
                Ok(())
            } else {
                Err(IosError::compatibility(format!(
                    "iOS plugin output '{}' changed during promotion",
                    target.path.display()
                )))
            }
        }) {
            return Err(rollback_or_preserve(
                staging,
                &source,
                target,
                had_previous,
                source_identity,
                error,
            ));
        }
        let staging_path = staging.path().to_path_buf();
        let mut warnings = Vec::new();
        if let Err(error) = staging.close()
            && error.kind() != io::ErrorKind::NotFound
        {
            warnings.push(format!(
                "iOS plugin staging cleanup failed for '{}': {error}",
                staging_path.display()
            ));
        }
        Ok(warnings)
    }

    fn rollback_or_preserve(
        staging: tempfile::TempDir,
        source: &Path,
        target: &OutputTarget,
        had_previous: bool,
        source_identity: FileIdentity,
        error: IosError,
    ) -> IosError {
        let current_target = optional_directory_identity(
            &target.path,
            "published iOS plugin output during rollback",
        );
        if current_target.as_ref().ok() != Some(&Some(source_identity)) {
            return preserve_staging_quarantine(
                staging,
                error,
                "published output ownership changed before rollback",
            );
        }
        if had_previous {
            let current_previous =
                optional_directory_identity(source, "previous iOS plugin output during rollback");
            if current_previous.as_ref().ok() != Some(&target.initial_identity) {
                return preserve_staging_quarantine(
                    staging,
                    error,
                    "previous output ownership changed before rollback",
                );
            }
        }
        let rollback = if had_previous {
            exchange_paths(source, &target.path)
        } else {
            rename_noreplace(&target.path, source)
        };
        match rollback {
            Ok(()) => match staging.close() {
                Ok(()) => error,
                Err(cleanup) => append_error(error, format!("rollback cleanup failed: {cleanup}")),
            },
            Err(rollback_error) => preserve_staging_quarantine(
                staging,
                error,
                format!("rollback failed: {rollback_error}"),
            ),
        }
    }

    fn preserve_staging_quarantine(
        staging: tempfile::TempDir,
        error: IosError,
        reason: impl std::fmt::Display,
    ) -> IosError {
        let preserved = staging.keep();
        append_error(
            error,
            format!(
                "rollback skipped: {reason}; staging quarantine was preserved under '{}'",
                preserved.display()
            ),
        )
    }

    fn optional_directory_identity(
        path: &Path,
        label: &str,
    ) -> Result<Option<FileIdentity>, IosError> {
        match fs::symlink_metadata(path) {
            Ok(metadata) if metadata.file_type().is_dir() => Ok(Some(file_identity(&metadata))),
            Ok(_) => Err(IosError::compatibility(format!(
                "{label} '{}' must be a regular non-symlink directory",
                path.display()
            ))),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
            Err(error) => Err(IosError::storage(format!(
                "failed to inspect {label} '{}': {error}",
                path.display()
            ))),
        }
    }

    fn directory_identity(path: &Path, label: &str) -> Result<FileIdentity, IosError> {
        let metadata = fs::symlink_metadata(path).map_err(|error| {
            IosError::storage(format!(
                "failed to inspect {label} '{}': {error}",
                path.display()
            ))
        })?;
        if !metadata.file_type().is_dir() {
            return Err(IosError::conformance(format!(
                "{label} '{}' must be a regular non-symlink directory",
                path.display()
            )));
        }
        Ok(file_identity(&metadata))
    }

    fn file_identity(metadata: &fs::Metadata) -> FileIdentity {
        use std::os::unix::fs::MetadataExt;

        FileIdentity {
            device: metadata.dev(),
            inode: metadata.ino(),
        }
    }

    fn exchange_paths(left: &Path, right: &Path) -> io::Result<()> {
        use rustix::fs::{CWD, RenameFlags, renameat_with};

        renameat_with(CWD, left, CWD, right, RenameFlags::EXCHANGE).map_err(io::Error::from)
    }

    fn rename_noreplace(source: &Path, target: &Path) -> io::Result<()> {
        use rustix::fs::{CWD, RenameFlags, renameat_with};

        renameat_with(CWD, source, CWD, target, RenameFlags::NOREPLACE).map_err(io::Error::from)
    }

    fn sync_directory(path: &Path) -> Result<(), IosError> {
        File::open(path)
            .and_then(|directory| directory.sync_all())
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to sync iOS plugin directory '{}': {error}",
                    path.display()
                ))
            })
    }

    impl IosPluginBuildLock {
        pub(super) fn acquire(root: &Path) -> Result<Self, IosError> {
            use std::os::unix::fs::OpenOptionsExt;

            let metadata = fs::symlink_metadata(root).map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect iOS plugin repository root '{}': {error}",
                    root.display()
                ))
            })?;
            if !metadata.file_type().is_dir() {
                return Err(IosError::compatibility(format!(
                    "iOS plugin repository root '{}' must be a regular non-symlink directory",
                    root.display()
                )));
            }
            let path = root.join(".vesper-ios-plugin.lock");
            let mut create = OpenOptions::new();
            create
                .create_new(true)
                .read(true)
                .write(true)
                .custom_flags(nix::libc::O_CLOEXEC | nix::libc::O_NOFOLLOW);
            let file = match create.open(&path) {
                Ok(file) => file,
                Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
                    let mut open = OpenOptions::new();
                    open.read(true)
                        .write(true)
                        .custom_flags(nix::libc::O_CLOEXEC | nix::libc::O_NOFOLLOW);
                    open.open(&path).map_err(|open_error| {
                        IosError::compatibility(format!(
                            "failed to open regular non-symlink iOS plugin build lock '{}': {open_error}",
                            path.display()
                        ))
                    })?
                }
                Err(error) => {
                    return Err(IosError::storage(format!(
                        "failed to create iOS plugin build lock '{}': {error}",
                        path.display()
                    )));
                }
            };
            if !file
                .metadata()
                .map_err(|error| {
                    IosError::storage(format!(
                        "failed to inspect iOS plugin build lock '{}': {error}",
                        path.display()
                    ))
                })?
                .file_type()
                .is_file()
            {
                return Err(IosError::compatibility(format!(
                    "iOS plugin build lock '{}' must be a regular non-symlink file",
                    path.display()
                )));
            }
            match file.try_lock() {
                Ok(()) => Ok(Self {
                    _file: file,
                    root_identity: file_identity(&metadata),
                }),
                Err(TryLockError::WouldBlock) => Err(IosError::compatibility(format!(
                    "another iOS plugin build is already active for '{}'",
                    root.display()
                ))),
                Err(TryLockError::Error(error)) => Err(IosError::storage(format!(
                    "failed to lock iOS plugin build for '{}': {error}",
                    root.display()
                ))),
            }
        }

        pub(super) fn validate_root(&self, root: &Path) -> Result<(), IosError> {
            let metadata = fs::symlink_metadata(root).map_err(|error| {
                IosError::storage(format!(
                    "failed to recheck iOS plugin repository root '{}': {error}",
                    root.display()
                ))
            })?;
            if !metadata.file_type().is_dir() || file_identity(&metadata) != self.root_identity {
                return Err(IosError::compatibility(format!(
                    "iOS plugin build guard does not belong to repository root '{}'",
                    root.display()
                )));
            }
            Ok(())
        }
    }

    fn report_build_result(
        plugin: &IosPluginSpec,
        result: Result<BuildOutcome, IosError>,
        cancelled: bool,
        output: &mut dyn Write,
        diagnostics: &mut dyn Write,
    ) -> Result<(), IosError> {
        match result {
            Ok(outcome) => {
                for warning in outcome.warnings {
                    let _ = writeln!(diagnostics, "warning: {warning}");
                }
                if cancelled {
                    diagnostics.flush().map_err(diagnostics_error)?;
                    return Err(IosError::worker(format!(
                        "iOS {} build was cancelled after the output was committed at '{}'",
                        plugin.description,
                        outcome.output_directory.display()
                    )));
                }
                writeln!(output).map_err(output_error)?;
                writeln!(
                    output,
                    "Built iOS {} plugin libraries into:",
                    plugin.crate_name
                )
                .map_err(output_error)?;
                writeln!(output, "  {}", outcome.output_directory.display())
                    .map_err(output_error)?;
                if let (Some(directory), Some(profile)) =
                    (outcome.ffmpeg_output_directory, outcome.ffmpeg_profile)
                {
                    writeln!(output, "Using Apple FFmpeg prebuilts:").map_err(output_error)?;
                    writeln!(output, "  {}", directory.display()).map_err(output_error)?;
                    writeln!(output, "FFmpeg profile:").map_err(output_error)?;
                    writeln!(output, "  {profile}").map_err(output_error)?;
                    writeln!(output, "Selected slices:").map_err(output_error)?;
                    for slice in outcome.slices {
                        writeln!(output, "  {}", slice.as_str()).map_err(output_error)?;
                    }
                    writeln!(
                        output,
                        "This dylib is an intermediate build input; package it as {}.framework for app distribution.",
                        plugin.framework_name
                    )
                    .map_err(output_error)?;
                }
                output.flush().map_err(output_error)
            }
            Err(error) if cancelled => Err(IosError::worker(format!(
                "iOS {} build was cancelled; {error}",
                plugin.description
            ))),
            Err(error) => Err(error),
        }
    }

    fn run_required_command(
        mut command: Command,
        label: &str,
        diagnostics: &mut dyn Write,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<(), IosError> {
        let result = run_process_capture(&mut command, label, cancellation)?;
        diagnostics
            .write_all(&result.stdout)
            .map_err(diagnostics_error)?;
        diagnostics
            .write_all(&result.stderr)
            .map_err(diagnostics_error)?;
        diagnostics.flush().map_err(diagnostics_error)?;
        classify_process_status(result.status, label)
    }

    fn run_process_capture(
        command: &mut Command,
        label: &str,
        cancellation: &external_process::InterruptDeferral,
    ) -> Result<BoundedProcessOutput, IosError> {
        external_process::run_interruptible_capture_in_deferral(
            command,
            label,
            MAX_PROCESS_OUTPUT_BYTES,
            MAX_PROCESS_OUTPUT_BYTES,
            cancellation,
        )
        .map_err(map_external_process_error)
    }

    fn classify_process_status(status: ExitStatus, label: &str) -> Result<(), IosError> {
        if status.success() {
            Ok(())
        } else if status.code().is_none_or(|code| code >= 128) {
            Err(IosError::worker(format!(
                "{label} terminated abnormally ({status})"
            )))
        } else {
            Err(IosError::conformance(format!(
                "{label} exited unsuccessfully ({status})"
            )))
        }
    }

    fn check_cancellation(
        cancellation: &external_process::InterruptDeferral,
        label: &str,
    ) -> Result<(), IosError> {
        if cancellation.is_cancelled() {
            Err(IosError::worker(format!("{label} was cancelled")))
        } else {
            Ok(())
        }
    }

    fn trim_ascii(bytes: &[u8]) -> &[u8] {
        let start = bytes
            .iter()
            .position(|byte| !byte.is_ascii_whitespace())
            .unwrap_or(bytes.len());
        let end = bytes
            .iter()
            .rposition(|byte| !byte.is_ascii_whitespace())
            .map_or(start, |index| index + 1);
        &bytes[start..end]
    }

    fn append_error(error: IosError, suffix: impl std::fmt::Display) -> IosError {
        let message = format!("{error}; {suffix}");
        match error.kind() {
            crate::ios::IosErrorKind::Storage => IosError::storage(message),
            crate::ios::IosErrorKind::Compatibility => IosError::compatibility(message),
            crate::ios::IosErrorKind::Conformance => IosError::conformance(message),
            crate::ios::IosErrorKind::Worker => IosError::worker(message),
        }
    }

    fn map_external_process_error(
        error: crate::external_process::ExternalProcessError,
    ) -> IosError {
        match error.kind() {
            ExternalProcessErrorKind::Compatibility => IosError::compatibility(error.to_string()),
            ExternalProcessErrorKind::Worker | ExternalProcessErrorKind::Cancelled => {
                IosError::worker(error.to_string())
            }
        }
    }

    fn map_ffmpeg_error(error: crate::ffmpeg::FfmpegError) -> IosError {
        match error.kind() {
            crate::ffmpeg::FfmpegErrorKind::Storage => IosError::storage(error.to_string()),
            crate::ffmpeg::FfmpegErrorKind::Compatibility => {
                IosError::compatibility(error.to_string())
            }
            crate::ffmpeg::FfmpegErrorKind::Conformance => IosError::conformance(error.to_string()),
            crate::ffmpeg::FfmpegErrorKind::Worker => IosError::worker(error.to_string()),
        }
    }

    fn diagnostics_error(error: io::Error) -> IosError {
        IosError::storage(format!("failed to write iOS plugin diagnostics: {error}"))
    }

    fn output_error(error: io::Error) -> IosError {
        IosError::storage(format!("failed to write iOS plugin output: {error}"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn plugin_registry_preserves_raw_and_release_profile_boundaries() {
        let remux = IosPluginId::RemuxFfmpeg.spec();
        assert_eq!(remux.release_ffmpeg_profile, Some("default"));
        assert!(remux.uses_ffmpeg);
        assert!(remux.link_headerpad);

        let normalizer = IosPluginId::SourceNormalizerFfmpeg.spec();
        assert_eq!(normalizer.release_ffmpeg_profile, Some("source-normalizer"));
        assert!(normalizer.uses_ffmpeg);
        assert!(!normalizer.link_headerpad);

        for plugin in [
            IosPluginId::DecoderVideoToolbox,
            IosPluginId::FrameProcessorDiagnostic,
        ] {
            let spec = plugin.spec();
            assert_eq!(spec.release_ffmpeg_profile, None);
            assert!(!spec.uses_ffmpeg);
            assert!(!spec.link_headerpad);
        }
    }

    #[test]
    fn plugin_registry_has_unique_ids_and_distribution_names() {
        let ids = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.id.as_str())
            .collect::<std::collections::BTreeSet<_>>();
        let crates = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.crate_name)
            .collect::<std::collections::BTreeSet<_>>();
        let dylibs = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.dylib_name)
            .collect::<std::collections::BTreeSet<_>>();
        let frameworks = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.framework_name)
            .collect::<std::collections::BTreeSet<_>>();
        let bundles = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.bundle_identifier)
            .collect::<std::collections::BTreeSet<_>>();
        let build_directories = IOS_PLUGIN_SPECS
            .iter()
            .map(|spec| spec.build_directory)
            .collect::<std::collections::BTreeSet<_>>();

        for values in [ids, crates, dylibs, frameworks, bundles, build_directories] {
            assert_eq!(values.len(), IOS_PLUGIN_SPECS.len());
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn lipo_architecture_verification_places_the_binary_before_the_operation() {
        let command = implementation::lipo_verify_arm64_command(
            Path::new("/usr/bin/lipo"),
            Path::new("Fixture.framework/Fixture"),
        );
        assert_eq!(
            command.get_args().collect::<Vec<_>>(),
            [
                std::ffi::OsStr::new("Fixture.framework/Fixture"),
                std::ffi::OsStr::new("-verify_arch"),
                std::ffi::OsStr::new("arm64"),
            ]
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn ios_plugin_deployment_target_enforces_the_product_floor() {
        for value in ["17.0", "17.0.1", "26.5"] {
            implementation::validate_ios_deployment_target(value)
                .expect("supported deployment target");
        }
        for value in ["", "16.9", "17.beta", "17.0.0.0"] {
            let error = implementation::validate_ios_deployment_target(value)
                .expect_err("unsupported deployment target must fail");
            assert_eq!(error.kind(), crate::ios::IosErrorKind::Compatibility);
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn ffmpeg_provenance_hashes_exact_bytes_and_paths_without_loss() {
        use sha2::{Digest, Sha256};

        let directory = tempfile::tempdir().expect("create FFmpeg provenance fixture");
        let metadata = [b'm', b'e', b't', b'a', 0, 0xff];
        let checksums = b"avutil_sha256=fixture\n";
        std::fs::write(
            directory.path().join("vesper-ffmpeg-build-metadata.txt"),
            metadata,
        )
        .expect("write exact FFmpeg metadata bytes");
        std::fs::write(
            directory.path().join("vesper-ffmpeg-library-sha256.txt"),
            checksums,
        )
        .expect("write exact FFmpeg checksum bytes");
        let fingerprint = implementation::ffmpeg_build_input_fingerprint(directory.path())
            .expect("fingerprint FFmpeg provenance");
        assert_eq!(
            fingerprint,
            format!(
                "{}-{}",
                hex::encode(Sha256::digest(metadata)),
                hex::encode(Sha256::digest(checksums))
            )
        );

        let spaced = implementation::ffmpeg_directory_cache_key(Path::new("/tmp/a b"))
            .expect("fingerprint spaced path");
        let underscored = implementation::ffmpeg_directory_cache_key(Path::new("/tmp/a_b"))
            .expect("fingerprint underscored path");
        assert_ne!(spaced, underscored);
        assert!(spaced.starts_with("path-") && spaced.len() == 69);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn ffmpeg_provenance_rejects_empty_symlink_and_oversized_inputs() {
        use std::os::unix::fs::symlink;

        let directory = tempfile::tempdir().expect("create invalid FFmpeg provenance fixture");
        let metadata = directory.path().join("vesper-ffmpeg-build-metadata.txt");
        let checksums = directory.path().join("vesper-ffmpeg-library-sha256.txt");
        std::fs::write(&metadata, []).expect("write empty FFmpeg metadata");
        std::fs::write(&checksums, b"checksums\n").expect("write FFmpeg checksums");
        let error = implementation::ffmpeg_build_input_fingerprint(directory.path())
            .expect_err("reject empty FFmpeg provenance");
        assert_eq!(error.kind(), crate::ios::IosErrorKind::Conformance);

        std::fs::write(&metadata, b"metadata\n").expect("write valid FFmpeg metadata");
        std::fs::remove_file(&checksums).expect("remove FFmpeg checksum file");
        let target = directory.path().join("checksum-target.txt");
        std::fs::write(&target, b"checksums\n").expect("write checksum symlink target");
        symlink(&target, &checksums).expect("create checksum symlink");
        let error = implementation::ffmpeg_build_input_fingerprint(directory.path())
            .expect_err("reject symlink FFmpeg provenance");
        assert_eq!(error.kind(), crate::ios::IosErrorKind::Conformance);

        std::fs::remove_file(&checksums).expect("remove checksum symlink");
        std::fs::write(
            &checksums,
            vec![b'x'; implementation::MAX_FFMPEG_PROVENANCE_BYTES as usize + 1],
        )
        .expect("write oversized FFmpeg checksum record");
        let error = implementation::ffmpeg_build_input_fingerprint(directory.path())
            .expect_err("reject oversized FFmpeg provenance");
        assert_eq!(error.kind(), crate::ios::IosErrorKind::Conformance);
    }
}