ling-lang 2030.1.39

Ling - The Omniglot Systems Language
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
// src/main.rs — ling CLI entry point
use std::path::{Path, PathBuf};
use std::process::Command;

/// True for any recognized Ling source-file extension — `.ling` plus the
/// localized single-glyph aliases lingfu scaffolds (`灵符.toml`'s entry is
/// `启.灵`, not `启.ling`). Keep in sync with `ling-fu/src/normalize.rs`'s
/// own list.
fn is_ling_source(name: &str) -> bool {
    name.ends_with(".ling")
        || name.ends_with(".灵")
        || name.ends_with(".霊")
        || name.ends_with(".령")
        || name.ends_with(".ลิง")
}

fn main() {
    let args: Vec<String> = std::env::args().collect();

    match args.get(1).map(|s| s.as_str()) {
        Some("visualize") => {
            let file = args.get(2).map(|s| s.as_str()).unwrap_or_else(|| {
                eprintln!("Usage: ling visualize <file.ling>");
                std::process::exit(1);
            });
            let source = std::fs::read_to_string(file).unwrap_or_else(|e| {
                eprintln!("error reading '{}': {}", file, e);
                std::process::exit(1);
            });
            let program = ling::parser::parse(&source).unwrap_or_else(|e| {
                eprintln!("parse error: {}", e);
                std::process::exit(1);
            });
            print!("{}", ling::visualize::render(file, &program));
        },
        Some("run") => {
            // `--wasm` builds the file to a WebAssembly bundle, serves it
            // locally, and opens it in the browser to play.
            // The Cranelift JIT is the default backend; `--interp` opts into the
            // tree-walker (kept as the semantic reference / fallback).
            let wasm = args.iter().any(|a| a == "--wasm" || a == "--web");
            let use_interp = args.iter().any(|a| a == "--interp");
            let file = args[2..]
                .iter()
                .map(|s| s.as_str())
                .find(|a| is_ling_source(a))
                .unwrap_or_else(|| {
                    eprintln!("Usage: ling run [--wasm|--interp] <file.ling>");
                    std::process::exit(1);
                });
            if wasm {
                run_wasm(file);
            } else if use_interp {
                run_file(file);
            } else {
                // Server programs (http_serve) run on the tree-walker: their
                // handlers are closures passed as call arguments, which the
                // Cranelift JIT currently lowers to Unit (known bug — JIT
                // closure-argument support). The interpreter is the semantic
                // reference and handles them correctly, and a web service is
                // I/O-bound anyway, so JIT throughput isn't the bottleneck.
                let is_server = std::fs::read_to_string(file)
                    .map(|s| s.contains("http_serve") || s.contains("เว็บเสิร์ฟ"))
                    .unwrap_or(false);
                if is_server {
                    run_file(file);
                } else {
                    run_file_jit(file);
                }
            }
        },

        Some("convert") => {
            // ling convert <asset> [-o out.ling] [--no-compression]
            std::process::exit(ling::convert::run(&args[1..]));
        },

        Some("ast") => {
            // ling ast [path] [--technical] [--artwork] [--ling] [--all] [--out <dir>]
            std::process::exit(run_ast(&args[2..]));
        },

        Some("build") => {
            let target = args.get(2).map(|s| s.as_str()).unwrap_or(".");
            let out = flag_value(&args, "--out").unwrap_or_else(|| "dist".into());
            let platforms = collect_platforms(&args);
            let icon = flag_value(&args, "--icon").map(PathBuf::from);
            let pack = args.iter().any(|a| a == "--pack");
            let aot = args.iter().any(|a| a == "--aot");
            run_build(target, &out, &platforms, icon, pack, aot);
        },
        Some(file) if is_ling_source(file) => run_file_jit(file),
        _ => {
            println!("ling {} — The Omniglot Systems Language", ling::VERSION);
            println!("Usage:");
            println!("  ling run <file.ling>                run using the Cranelift JIT backend (default)");
            println!(
                "  ling run --interp <file.ling>       run using the tree-walking interpreter"
            );
            println!("  ling run --wasm <file.ling>         build to WebAssembly, serve, open in browser");
            println!("  ling visualize <file.ling>          emit SVG AST to stdout");
            println!("  ling build <file.ling|dir> [opts]   compile to distributable");
            println!("    --out <dir>                       output folder (default: dist)");
            println!("    --platform <targets>              web win lin mac all (comma-sep)");
            println!("    --icon <file.svg|png|ico>         app icon (overrides manifest icon)");
            println!(
                "    --pack                            embed [includes] resources into the exe"
            );
            println!("    --aot                             compile to native code via AOT");
            println!("  ling ast [path] [--technical|--artwork|--ling|--all]");
            println!(
                "                                      project-wide AST → SVG in ./AST/ (300 dpi)"
            );
            println!("  ling convert <asset> [opts]         transcode an asset → importable .ling");
            println!("    -o <out.ling>                     output path (default: <asset>.ling)");
            println!("    --no-compression                  emit plain arrays instead of blobs");
            println!("    (.gltf .glb .wav .ogg .flac .mid .svg .blend)");
        },
    }
}

fn run_file(path: &str) {
    let resolved = std::path::Path::new(path);
    if !resolved.exists() {
        eprintln!("[ling] error: file does not exist: {}", resolved.display());
        std::process::exit(1);
    }

    let source = std::fs::read_to_string(path).unwrap_or_else(|e| {
        eprintln!("error reading '{}': {}", path, e);
        std::process::exit(1);
    });

    let lang = ling::detect_language(&source);
    if lang != "English" {
        eprintln!("[detected language: {}]", lang);
    }
    let src_dir = resolved.parent().map(|p| p.to_path_buf());
    if let Err(e) = ling::run_named(&source, src_dir, Some(path)) {
        eprintln!("{e}");
        std::process::exit(1);
    }
}

fn run_file_jit(path: &str) {
    let resolved = std::path::Path::new(path);
    if !resolved.exists() {
        eprintln!("[ling] error: file does not exist: {}", resolved.display());
        std::process::exit(1);
    }

    let source = std::fs::read_to_string(path).unwrap_or_else(|e| {
        eprintln!("error reading '{}': {}", path, e);
        std::process::exit(1);
    });

    let lang = ling::detect_language(&source);
    if lang != "English" {
        eprintln!("[detected language: {}]", lang);
    }

    // A program with no entry point is a library; let the interpreter run it so
    // it emits the same "no entry point" diagnostic the user expects.
    let has_entry = ling::parser::parse(&source)
        .map(|p| ling::entry::entry_name(&p.items).is_some())
        .unwrap_or(true);
    if !has_entry {
        let src_dir = resolved.parent().map(|p| p.to_path_buf());
        if let Err(e) = ling::run_named(&source, src_dir, Some(path)) {
            eprintln!("{e}");
            std::process::exit(1);
        }
        return;
    }

    use ling::CompilerConfig;
    let config = CompilerConfig::default();
    let compiler = ling::LingCompiler::new(config);
    if let Err(e) = compiler.compile_and_run_jit(path) {
        use ling::core::LingError;
        match e {
            // Invalid program — render the same diagnostic the interpreter does.
            LingError::Parse(m) => {
                let out_lang = ling::diag::OutputLang::from_env();
                eprintln!(
                    "{}",
                    ling::diag::render_parse(&m, &source, Some(path), out_lang)
                );
                std::process::exit(1);
            },
            // The JIT executed and failed mid-run; output may already be on screen.
            LingError::Mir(m) => {
                eprintln!("{m}");
                std::process::exit(1);
            },
            // The JIT could not compile this program (an unsupported construct).
            // Nothing ran yet, so fall back to the full-language tree-walker.
            _ => {
                let src_dir = resolved.parent().map(|p| p.to_path_buf());
                if let Err(e) = ling::run_named(&source, src_dir, Some(path)) {
                    eprintln!("{e}");
                    std::process::exit(1);
                }
            },
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// AST visualisation — `ling ast`
// ═══════════════════════════════════════════════════════════════════════════════

/// `ling ast [path] [--technical|--artwork|--ling|--all] [--out <dir>]`
///
/// Treats the whole project (a directory of `.ling` files, or a single file) as
/// one program while preserving file scope, then writes the requested SVG
/// style(s) to `<out>/` (default `./AST/`). Returns a process exit code.
fn run_ast(args: &[String]) -> i32 {
    use ling::astviz::AstStyle;

    let out_dir = flag_value(&Vec::from(args), "--out").unwrap_or_else(|| "AST".into());
    let mut styles: Vec<AstStyle> = Vec::new();
    let all = args.iter().any(|a| a == "--all");
    if all || args.iter().any(|a| a == "--technical") {
        styles.push(AstStyle::Technical);
    }
    if all || args.iter().any(|a| a == "--artwork") {
        styles.push(AstStyle::Artwork);
    }
    if all || args.iter().any(|a| a == "--ling") {
        styles.push(AstStyle::Ling);
    }
    if styles.is_empty() {
        // No style flag → produce all three.
        styles = vec![AstStyle::Technical, AstStyle::Artwork, AstStyle::Ling];
    }

    // First non-flag argument is the path (skipping the --out value); default ".".
    let path = pick_ast_path(args).unwrap_or_else(|| ".".into());

    let (proj_name, files) = gather_project(&path);
    if files.is_empty() {
        eprintln!("ling ast: no parseable .ling files found in '{path}'");
        return 1;
    }

    if let Err(e) = std::fs::create_dir_all(&out_dir) {
        eprintln!("ling ast: cannot create '{out_dir}': {e}");
        return 1;
    }

    let n_fns: usize = files
        .iter()
        .map(|(_, p)| {
            p.items
                .iter()
                .filter(|i| matches!(i, ling::parser::ast::Item::Fn(_)))
                .count()
        })
        .sum();
    println!(
        "ling ast: '{proj_name}' — {} file(s), {n_fns} fn(s)",
        files.len()
    );

    for style in &styles {
        let svg = ling::astviz::render(*style, &proj_name, &files);
        let dst = std::path::Path::new(&out_dir).join(format!("{proj_name}.{}.svg", style.slug()));
        match std::fs::write(&dst, svg.as_bytes()) {
            Ok(()) => println!("{}", dst.display()),
            Err(e) => {
                eprintln!("{}: {e}", dst.display());
                return 1;
            },
        }
    }
    0
}

/// First non-flag argument that is not the value of `--out`.
fn pick_ast_path(args: &[String]) -> Option<String> {
    let mut i = 0;
    while i < args.len() {
        let a = &args[i];
        if a == "--out" {
            i += 2;
            continue;
        }
        if a.starts_with('-') {
            i += 1;
            continue;
        }
        return Some(a.clone());
    }
    None
}

/// Gather the project as `(project_name, [(file_label, Program)])`. A directory is
/// walked recursively (skipping build/output trees); a single file yields one entry.
/// Files that fail to parse are skipped with a warning.
fn gather_project(path: &str) -> (String, Vec<(String, ling::parser::ast::Program)>) {
    let p = Path::new(path);
    let mut files = Vec::new();

    if p.is_file() {
        let name = p
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| "program".into());
        if let Some(prog) = parse_one(p) {
            files.push((
                p.file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .into_owned(),
                prog,
            ));
        }
        return (sanitise_name(&name), files);
    }

    // Directory: recurse for .ling files.
    let mut paths = Vec::new();
    collect_ling_files(p, &mut paths);
    paths.sort();
    let base = p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
    for fp in &paths {
        if let Some(prog) = parse_one(fp) {
            let label = fp
                .strip_prefix(&base)
                .unwrap_or(fp)
                .to_string_lossy()
                .into_owned();
            files.push((label, prog));
        }
    }
    let proj = p
        .canonicalize()
        .ok()
        .and_then(|c| c.file_name().map(|n| n.to_string_lossy().into_owned()))
        .or_else(|| p.file_name().map(|n| n.to_string_lossy().into_owned()))
        .unwrap_or_else(|| "project".into());
    (sanitise_name(&proj), files)
}

fn parse_one(path: &Path) -> Option<ling::parser::ast::Program> {
    let src = std::fs::read_to_string(path).ok()?;
    match ling::parser::parse(&src) {
        Ok(prog) => Some(prog),
        Err(e) => {
            eprintln!("  [skip] {}: parse error: {e}", path.display());
            None
        },
    }
}

fn collect_ling_files(dir: &Path, out: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else { return };
    for entry in entries.flatten() {
        let path = entry.path();
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if path.is_dir() {
            if matches!(
                name.as_ref(),
                ".ling-build" | "灵碑" | "target" | "dist" | ".git" | "node_modules" | "AST"
            ) {
                continue;
            }
            collect_ling_files(&path, out);
        } else if matches!(
            path.extension().and_then(|e| e.to_str()),
            Some("ling" | "" | "" | "" | "ลิง")
        ) {
            out.push(path);
        }
    }
}

// ── arg helpers ───────────────────────────────────────────────────────────────

fn flag_value(args: &[String], flag: &str) -> Option<String> {
    args.windows(2).find(|w| w[0] == flag).map(|w| w[1].clone())
}

fn collect_platforms(args: &[String]) -> Vec<String> {
    let mut platforms: Vec<String> = args
        .windows(2)
        .filter(|w| w[0] == "--platform")
        .flat_map(|w| {
            w[1].split(',')
                .map(|s| s.trim().to_lowercase())
                .collect::<Vec<_>>()
        })
        .collect();
    // Expand "all"
    if platforms.iter().any(|p| p == "all") {
        return vec!["win".into(), "lin".into(), "mac".into(), "web".into()];
    }
    if platforms.is_empty() {
        // Default: current native platform + web
        platforms.push(native_platform().into());
        platforms.push("web".into());
    }
    platforms
}

fn native_platform() -> &'static str {
    if cfg!(target_os = "windows") {
        "win"
    } else if cfg!(target_os = "macos") {
        "mac"
    } else {
        "lin"
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Project discovery
// ═══════════════════════════════════════════════════════════════════════════════

/// Whether this is a graphical/windowed program (not headless).
#[derive(Debug, Clone, PartialEq)]
enum ProjKind {
    Bin,
    Web,
    Game,
    Ui,
    Ai,
    Crypto,
    Lib,
    Polyglot,
    Kernel,
}

impl ProjKind {
    #[allow(dead_code)]
    fn default_platforms(&self) -> Vec<&'static str> {
        match self {
            ProjKind::Web => vec!["web"],
            _ => vec![native_platform(), "web"],
        }
    }
}

struct LingProject {
    name: String,
    version: String,
    kind: ProjKind,
    entry: PathBuf,        // absolute path to the entry .ling file
    source_dir: PathBuf,   // directory that contains the .ling sources
    build_dir: PathBuf,    // where the temp Rust build project lives
    icon: Option<PathBuf>, // app icon source from the manifest (svg/png/ico)
    includes: Vec<String>, // resource globs from the manifest [includes] block
    /// `[project] graphics = true` (kernel targets only): request a
    /// Multiboot2 linear framebuffer instead of leaving VGA in text mode
    /// (see `ling-kernel`'s `request_framebuffer` Cargo feature). Off by
    /// default — enabling it for a target with no framebuffer-based
    /// renderer would blank the screen (confirmed the hard way once).
    graphics: bool,
}

fn discover_project(target: &str) -> LingProject {
    let raw = Path::new(target);
    let path = raw.canonicalize().unwrap_or_else(|_| raw.to_path_buf());

    if path.is_file() {
        // Single .ling file
        let name = sanitise_name(&path.file_stem().unwrap_or_default().to_string_lossy());
        let source_dir = path.parent().unwrap_or(Path::new(".")).to_path_buf();
        let build_dir = source_dir.join(".ling-build").join(&name);
        LingProject {
            name,
            version: "0.1.0".into(),
            kind: ProjKind::Bin,
            entry: path,
            source_dir,
            build_dir,
            icon: None,
            includes: Vec::new(),
            graphics: false,
        }
    } else if path.is_dir() {
        // ling-fu project: 灵符.toml
        let lf = path.join("灵符.toml");
        if lf.exists() {
            return parse_lingfu_toml(&lf, &path);
        }

        // Simple project: Ling.toml
        let lt = path.join("Ling.toml");
        if lt.exists() {
            return parse_ling_toml(&lt, &path);
        }

        // Bare directory: auto-detect entry
        let entry = auto_entry(&path).unwrap_or_else(|| {
            eprintln!("error: no .ling file found in '{}'", path.display());
            std::process::exit(1);
        });
        let name = path
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| "app".into());
        let build_dir = path.join(".ling-build").join(&name);
        LingProject {
            name,
            version: "0.1.0".into(),
            kind: ProjKind::Bin,
            entry,
            source_dir: path,
            build_dir,
            icon: None,
            includes: Vec::new(),
            graphics: false,
        }
    } else {
        eprintln!("error: '{}' is not a .ling file or directory", target);
        std::process::exit(1);
    }
}

/// Parse a ling-fu `灵符.toml` manifest.
/// Expected shape:
///   [灵符]
///   名 = "my-app"
///   型 = "bin"
///   版 = "0.1.0"
fn parse_lingfu_toml(toml: &Path, base: &Path) -> LingProject {
    let text = std::fs::read_to_string(toml).unwrap_or_else(|e| {
        eprintln!("read 灵符.toml: {e}");
        std::process::exit(1);
    });

    let mut name = base
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| "app".into());
    let mut version = "0.1.0".into();
    let mut kind = ProjKind::Bin;
    let mut icon: Option<PathBuf> = None;

    for line in text.lines() {
        let line = line.trim();
        // 名 = "value"  or  名 = value
        if let Some(v) = toml_kv(line, "") {
            name = sanitise_name(&v);
        }
        if let Some(v) = toml_kv(line, "") {
            version = v;
        }
        if let Some(v) = toml_kv(line, "") {
            kind = parse_kind(&v);
        }
        // 图标 = "logo.svg"  (also accept the English `icon`), relative to project root.
        if let Some(v) = toml_kv(line, "图标").or_else(|| toml_kv(line, "icon")) {
            icon = Some(base.join(v));
        }
    }

    // Entry point: 灵源/启.灵  (ling-fu convention)
    let entry = ["灵源/启.灵", "灵源/main.ling", "main.ling"]
        .iter()
        .map(|p| base.join(p))
        .find(|p| p.exists())
        .unwrap_or_else(|| {
            auto_entry(base).unwrap_or_else(|| {
                eprintln!("error: cannot find entry point for project '{}'", name);
                std::process::exit(1);
            })
        });

    let source_dir = base.to_path_buf();
    // ling-fu uses 灵碑/ for build artifacts
    let build_dir = base.join("灵碑").join(&name);
    let includes = parse_includes(&text);
    LingProject {
        name,
        version,
        kind,
        entry,
        source_dir,
        build_dir,
        icon,
        includes,
        graphics: false,
    }
}

/// Parse a simple English-key `Ling.toml`.
/// [project]
/// name = "my-app"
/// version = "0.1.0"
/// entry = "main.ling"
/// kind = "bin"
fn parse_ling_toml(toml: &Path, base: &Path) -> LingProject {
    let text = std::fs::read_to_string(toml).unwrap_or_else(|e| {
        eprintln!("read Ling.toml: {e}");
        std::process::exit(1);
    });

    let mut name = base
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| "app".into());
    let mut version = "0.1.0".to_string();
    let mut entry_name = "main.ling".to_string();
    let mut kind = ProjKind::Bin;
    let mut icon: Option<PathBuf> = None;
    let mut graphics = false;

    for line in text.lines() {
        let line = line.trim();
        if let Some(v) = toml_kv(line, "name") {
            name = sanitise_name(&v);
        }
        if let Some(v) = toml_kv(line, "version") {
            version = v;
        }
        if let Some(v) = toml_kv(line, "entry") {
            entry_name = v;
        }
        if let Some(v) = toml_kv(line, "kind") {
            kind = parse_kind(&v);
        }
        // icon = "images/logo.svg"  (svg/png/ico), relative to the project root.
        if let Some(v) = toml_kv(line, "icon") {
            icon = Some(base.join(v));
        }
        // graphics = true (kernel targets only) -- request a Multiboot2
        // linear framebuffer instead of VGA text mode.
        if let Some(v) = toml_kv(line, "graphics") {
            graphics = v == "true";
        }
    }

    let entry = base.join(&entry_name);
    if !entry.exists() {
        eprintln!("error: entry '{}' not found", entry.display());
        std::process::exit(1);
    }
    let build_dir = base.join(".ling-build").join(&name);
    let includes = parse_includes(&text);
    LingProject {
        name,
        version,
        kind,
        entry,
        source_dir: base.to_path_buf(),
        build_dir,
        icon,
        includes,
        graphics,
    }
}

fn auto_entry(dir: &Path) -> Option<PathBuf> {
    for name in &["main.ling", "start.ling", "启.灵"] {
        let p = dir.join(name);
        if p.exists() {
            return Some(p);
        }
    }
    // Try source sub-directory (ling-fu: 灵源/)
    for subdir in &["灵源", "src"] {
        let sub = dir.join(subdir);
        if let Some(e) = auto_entry(&sub) {
            return Some(e);
        }
    }
    // Fall back to any .ling file
    std::fs::read_dir(dir)
        .ok()?
        .flatten()
        .find(|e| e.path().extension().is_some_and(|x| x == "ling"))
        .map(|e| e.path())
}

fn parse_kind(s: &str) -> ProjKind {
    match s.to_lowercase().as_str() {
        "web" | "网灵" => ProjKind::Web,
        "game" | "游灵" => ProjKind::Game,
        "ui" | "显灵" => ProjKind::Ui,
        "ai" | "智灵" => ProjKind::Ai,
        "crypto" | "密灵" => ProjKind::Crypto,
        "lib" | "共修" => ProjKind::Lib,
        "polyglot" | "万言" => ProjKind::Polyglot,
        "kernel" | "内核" => ProjKind::Kernel,
        _ => ProjKind::Bin,
    }
}

fn toml_kv(line: &str, key: &str) -> Option<String> {
    // key = "value"  or  key = value  (key may be unicode)
    let pat = format!("{key} =");
    if !line.starts_with(&pat) {
        return None;
    }
    let v = line[pat.len()..].trim().trim_matches('"').to_string();
    if v.is_empty() {
        None
    } else {
        Some(v)
    }
}

fn sanitise_name(s: &str) -> String {
    // Cargo package names: alphanumeric + hyphen/underscore, must not start with a digit
    let out: String = s
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '-'
            }
        })
        .collect();
    let out = out.trim_matches('-').to_string();
    let out = if out.is_empty() { "app".into() } else { out };
    if out.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        format!("r{}", out)
    } else {
        out
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Build orchestration
// ═══════════════════════════════════════════════════════════════════════════════

fn run_build(
    target: &str,
    out: &str,
    platforms: &[String],
    icon_override: Option<PathBuf>,
    pack: bool,
    aot: bool,
) {
    let project = discover_project(target);
    println!(
        "Building '{}' v{} ({:?})",
        project.name, project.version, project.kind
    );

    std::fs::create_dir_all(out).unwrap_or_else(|e| {
        eprintln!("create dist dir '{}': {e}", out);
        std::process::exit(1);
    });

    let icon = icon_override.as_deref();
    for platform in platforms {
        match platform.as_str() {
            "web" => build_web(&project, out),
            "win" | "windows" => {
                build_native(&project, out, NativePlatform::Windows, icon, pack, aot)
            },
            "lin" | "linux" => build_native(&project, out, NativePlatform::Linux, icon, pack, aot),
            "mac" | "macos" | "darwin" => {
                build_native(&project, out, NativePlatform::Mac, icon, pack, aot)
            },
            "kernel" | "bare" => {
                build_native(&project, out, NativePlatform::BareMetal, icon, pack, aot)
            },
            "rpi" | "raspberrypi" => {
                build_native(&project, out, NativePlatform::Rpi, icon, pack, aot)
            },
            other => {
                eprintln!("unknown platform '{}' — use web|win|lin|mac|kernel|rpi|all", other);
                std::process::exit(1);
            },
        }
    }

    println!("\nOutputs written to: {out}/");
}

// ── Web build ─────────────────────────────────────────────────────────────────

fn build_web(project: &LingProject, out: &str) {
    println!("  [web] building WebGL bundle…");

    // Delegate to `lingc webgl` — it lives next to this binary.
    let lingc = sibling_binary("lingc");
    let web_out = Path::new(out).join("web");

    // lingc webgl <entry.ling> --out <dist/web>
    let status = Command::new(&lingc)
        .arg("webgl")
        .arg(&project.entry)
        .arg("--out")
        .arg(&web_out)
        .status()
        .unwrap_or_else(|e| {
            eprintln!("  lingc not found ({e}). Build lingc first: cargo build --bin lingc");
            std::process::exit(1);
        });

    if !status.success() {
        eprintln!("  [web] build failed");
        std::process::exit(1);
    }
    println!("  [web] → {}/web/", out);
}

// ── `ling run --wasm` — build to WebAssembly, serve, and open in the browser ──

fn run_wasm(file: &str) {
    if !Path::new(file).exists() {
        eprintln!("[ling] error: file does not exist: {file}");
        std::process::exit(1);
    }

    // 1. Build the WebGL/WASM bundle next to the source (reuses `lingc webgl`).
    let out = std::env::temp_dir().join(format!("ling-wasm-{}", std::process::id()));
    let _ = std::fs::create_dir_all(&out);
    let lingc = sibling_binary("lingc");
    println!("[ling] building WebAssembly bundle (first run compiles the runtime, ~1 min)…");
    let status = Command::new(&lingc)
        .arg("webgl")
        .arg(file)
        .arg("--out")
        .arg(&out)
        .status()
        .unwrap_or_else(|e| {
            eprintln!("[ling] lingc not found ({e}); build it first: cargo build --bin lingc");
            std::process::exit(1);
        });
    if !status.success() {
        eprintln!("[ling] wasm build failed");
        std::process::exit(1);
    }

    // 2. Serve the folder locally and open the browser.
    serve_and_open(&out);
}

/// Minimal static file server (no deps) — serves `root` and opens the browser.
/// Blocks until Ctrl+C. WASM is served as `application/wasm` so the browser can
/// stream-compile it.
fn serve_and_open(root: &Path) {
    use std::io::{Read, Write};
    use std::net::TcpListener;

    let listener = TcpListener::bind("127.0.0.1:8080")
        .or_else(|_| TcpListener::bind("127.0.0.1:0"))
        .unwrap_or_else(|e| {
            eprintln!("[ling] could not bind a local port: {e}");
            std::process::exit(1);
        });
    let port = listener.local_addr().map(|a| a.port()).unwrap_or(8080);
    let url = format!("http://localhost:{port}/index.html");

    println!("[ling] serving {} ", root.display());
    println!("[ling] ▶ {url}");
    println!("[ling] press Ctrl+C to stop.");
    open_browser(&url);

    for stream in listener.incoming() {
        let Ok(mut stream) = stream else { continue };
        let root = root.to_path_buf();
        std::thread::spawn(move || {
            let mut buf = [0u8; 4096];
            let n = stream.read(&mut buf).unwrap_or(0);
            let req = String::from_utf8_lossy(&buf[..n]);
            let raw = req
                .lines()
                .next()
                .and_then(|l| l.split_whitespace().nth(1))
                .unwrap_or("/");
            let path = raw.split('?').next().unwrap_or("/");
            let rel = if path == "/" {
                "index.html"
            } else {
                path.trim_start_matches('/')
            };

            // Resolve under root and reject path traversal.
            let target = root.join(rel);
            let safe = target.canonicalize().ok().filter(|p| {
                root.canonicalize()
                    .map(|r| p.starts_with(r))
                    .unwrap_or(false)
            });

            let (status_line, body, ctype) = match safe.and_then(|p| std::fs::read(p).ok()) {
                Some(bytes) => ("200 OK", bytes, mime_for(rel)),
                None => ("404 Not Found", b"404 not found".to_vec(), "text/plain"),
            };
            let header = format!(
                "HTTP/1.1 {status_line}\r\nContent-Type: {ctype}\r\nContent-Length: {}\r\nCross-Origin-Opener-Policy: same-origin\r\nCross-Origin-Embedder-Policy: require-corp\r\nCross-Origin-Resource-Policy: cross-origin\r\nConnection: close\r\n\r\n",
                body.len()
            );
            let _ = stream.write_all(header.as_bytes());
            let _ = stream.write_all(&body);
        });
    }
}

/// Content-Type for a served path (WASM must be `application/wasm`).
fn mime_for(path: &str) -> &'static str {
    let ext = path.rsplit('.').next().unwrap_or("");
    match ext {
        "html" => "text/html; charset=utf-8",
        "js" | "mjs" => "text/javascript; charset=utf-8",
        "wasm" => "application/wasm",
        "json" => "application/json",
        "css" => "text/css; charset=utf-8",
        "ling" => "text/plain; charset=utf-8",
        "png" => "image/png",
        "svg" => "image/svg+xml",
        "wav" => "audio/wav",
        "ogg" => "audio/ogg",
        _ => "application/octet-stream",
    }
}

/// Open `url` in the default browser (best-effort, per platform).
fn open_browser(url: &str) {
    #[cfg(target_os = "windows")]
    let _ = Command::new("cmd").args(["/C", "start", "", url]).spawn();
    #[cfg(target_os = "macos")]
    let _ = Command::new("open").arg(url).spawn();
    #[cfg(all(unix, not(target_os = "macos")))]
    let _ = Command::new("xdg-open").arg(url).spawn();
}

// ── Native build ──────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy)]
enum NativePlatform {
    Windows,
    Linux,
    Mac,
    BareMetal,
    /// Raspberry Pi (aarch64), bare-metal — see `RPI_LINKER_SCRIPT`.
    Rpi,
}

impl NativePlatform {
    fn dir_name(self) -> &'static str {
        match self {
            Self::Windows => "windows",
            Self::Linux => "linux",
            Self::Mac => "macos",
            Self::BareMetal => "kernel",
            Self::Rpi => "rpi",
        }
    }

    /// Best triple for the platform, given where we're compiling FROM.
    fn triple(self) -> &'static str {
        match self {
            Self::Windows => {
                if cfg!(target_os = "windows") {
                    "x86_64-pc-windows-msvc"
                } else {
                    "x86_64-pc-windows-gnu"
                }
            },
            Self::Linux => {
                if cfg!(target_os = "linux") {
                    "x86_64-unknown-linux-gnu"
                } else {
                    "x86_64-unknown-linux-musl"
                }
            },
            Self::Mac => {
                // On Apple Silicon build arm64; everywhere else (including cross) use x86_64
                if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
                    "aarch64-apple-darwin"
                } else {
                    "x86_64-apple-darwin"
                }
            },
            Self::BareMetal => "x86_64-unknown-none",
            Self::Rpi => "aarch64-unknown-none",
        }
    }

    fn exe_suffix(self) -> &'static str {
        match self {
            Self::Windows => ".exe",
            Self::BareMetal | Self::Rpi => "",
            _ => "",
        }
    }

    fn is_current_host(self) -> bool {
        match self {
            Self::Windows => cfg!(target_os = "windows"),
            Self::Linux => cfg!(target_os = "linux"),
            Self::Mac => cfg!(target_os = "macos"),
            Self::BareMetal | Self::Rpi => false,
        }
    }

    /// Bare architecture name (no OS/vendor) for `CraneliftBackend::new_for_arch`.
    /// Using the bare arch — not the host's full triple — is what makes the
    /// AOT object come out as ELF even when `ling` itself runs on Windows or
    /// macOS (whose *native* triple would otherwise emit COFF/Mach-O, which
    /// the bare-metal linker step can't read).
    fn cranelift_arch(self) -> &'static str {
        match self {
            Self::BareMetal => "x86_64",
            Self::Rpi => "aarch64",
            _ => unreachable!("cranelift_arch only used for kernel targets"),
        }
    }
}

fn build_native(
    project: &LingProject,
    out: &str,
    platform: NativePlatform,
    icon: Option<&Path>,
    pack: bool,
    aot: bool,
) {
    let triple = platform.triple();
    let is_kernel = matches!(platform, NativePlatform::BareMetal | NativePlatform::Rpi);
    let is_rpi = matches!(platform, NativePlatform::Rpi);
    println!(
        "  [{}] building {} ({triple}){}",
        platform.dir_name(),
        platform.dir_name(),
        if aot { " [AOT]" } else { "" }
    );

    let ling_root = find_ling_root().unwrap_or_else(|| {
        eprintln!(
            "  cannot find ling-lang source.\n  \
             Run from the repository or set LING_HOME=<path-to-ling-repo>."
        );
        std::process::exit(1);
    });

    // ── 1. Set up build directory ────────────────────────────────────────────
    let build_dir = &project.build_dir;
    std::fs::create_dir_all(build_dir.join("src")).unwrap_or_else(|e| {
        eprintln!("  create build dir: {e}");
        std::process::exit(1);
    });

    // Kernel builds always use AOT (compiling the .ling to a .o and linking it)
    let use_aot = aot || is_kernel;

    // Copy all .ling files from source_dir (recurse one level for ling-fu 灵源/)
    if !use_aot {
        copy_ling_sources(&project.source_dir, build_dir);
    }

    // ── 2. Write generated Cargo.toml + src/main.rs ──────────────────────────
    let entry_filename = project
        .entry
        .file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .into_owned();

    // Resources declared in the manifest [includes] block (glob-expanded).
    let resources = expand_includes(project);
    let do_pack = pack && !resources.is_empty();

    if is_kernel {
        // ── Kernel path: AOT compile + no_std runtime ──────────────────────
        println!("    AOT-compiling {} for kernel…", entry_filename);

        let mir = ling::mir::compile_path(&project.entry, ling::core::OptimizationLevel::O3)
            .unwrap_or_else(|e| {
                eprintln!("    MIR compilation failed: {e}");
                std::process::exit(1);
            });

        let mir_prog = ling_codegen::MirProgram::new(mir, entry_filename.clone());
        println!(
            "    compiling {} functions to native code…",
            mir_prog.mir.functions.len()
        );
        // Cross-target explicitly by bare arch name (not `::new()`'s
        // host-native triple): on Windows/macOS hosts, the "native" triple
        // carries the host OS and makes cranelift-object emit COFF/Mach-O,
        // which the bare-metal ELF linker step below can't read. The bare
        // arch name resolves to an "unknown" OS/vendor, which cranelift-object
        // maps to ELF — correct for every `*-unknown-none` kernel target
        // regardless of which OS `ling` itself is running on.
        let mut backend =
            ling_codegen::CraneliftBackend::new_for_arch(platform.cranelift_arch())
                .with_progress(true);
        let obj_path = build_dir.join("entry.o");
        use ling_codegen::CodegenBackend;
        backend.emit(&mir_prog, &obj_path).unwrap_or_else(|e| {
            eprintln!("    AOT codegen failed: {e}");
            std::process::exit(1);
        });

        println!("    AOT object written to {}", obj_path.display());

        // Generate kernel-specific Cargo.toml with ling-kernel dependency
        std::fs::write(
            build_dir.join("Cargo.toml"),
            gen_kernel_cargo_toml(&project.name, &project.version, &ling_root, project.graphics),
        )
        .expect("write Cargo.toml");

        // Idle-timeout VGA font (x86_64 only — a plain UART, aarch64's
        // console, has no font/color concept for the connecting terminal to
        // take direction from). Convention: walk up from the project dir
        // looking for a `font/` folder with an .otf/.ttf in it (matches
        // LingOS's own layout: kernel/x86_64/ finds ../../font/square.otf).
        let idle_font_mod = if !is_rpi {
            find_font_asset(&project.source_dir).and_then(|font_path| {
                let bytes = std::fs::read(&font_path).ok()?;
                let cjk_fallback = std::fs::read(ling_root.join("assets/fonts/NotoSansSC.ttf")).ok();
                let atlas = rasterize_vga_font(&bytes, cjk_fallback.as_deref());
                std::fs::write(build_dir.join("src/idle_font.rs"), gen_idle_font_rs(&atlas))
                    .ok()?;
                println!("    idle font: {} → src/idle_font.rs", font_path.display());
                Some("idle_font")
            })
        } else {
            None
        };

        let main_rs = if is_rpi {
            gen_rpi_kernel_main_rs()
        } else {
            gen_kernel_main_rs(idle_font_mod)
        };
        std::fs::write(build_dir.join("src/main.rs"), main_rs).expect("write src/main.rs");

        std::fs::write(build_dir.join("build.rs"), gen_kernel_build_rs())
            .expect("write build.rs");

        // Write linker script
        let linker_script = if is_rpi { RPI_LINKER_SCRIPT } else { KERNEL_LINKER_SCRIPT };
        std::fs::write(build_dir.join("linker.ld"), linker_script).expect("write linker.ld");

        println!("    kernel build files written to {}", build_dir.display());
    } else if use_aot {
        // ── AOT path: compile to native .o, link via Rust stub ──────────────
        println!("    AOT-compiling {}", entry_filename);

        let mir = ling::mir::compile_path(&project.entry, ling::core::OptimizationLevel::O3)
            .unwrap_or_else(|e| {
                eprintln!("    MIR compilation failed: {e}");
                std::process::exit(1);
            });

        let mir_prog = ling_codegen::MirProgram::new(mir, entry_filename.clone());
        println!(
            "    compiling {} functions to native code…",
            mir_prog.mir.functions.len()
        );
        let mut backend = ling_codegen::CraneliftBackend::new().with_progress(true);
        let obj_path = build_dir.join("entry.o");
        use ling_codegen::CodegenBackend;
        backend.emit(&mir_prog, &obj_path).unwrap_or_else(|e| {
            eprintln!("    AOT codegen failed: {e}");
            std::process::exit(1);
        });

        println!("    AOT object written to {}", obj_path.display());

        std::fs::write(build_dir.join("src/main.rs"), gen_aot_main_rs(do_pack))
            .expect("write src/main.rs");
        std::fs::write(build_dir.join("build.rs"), gen_aot_build_rs()).expect("write build.rs");
    } else {
        // ── Standard path: embed interpreter ────────────────────────────────
        std::fs::write(
            build_dir.join("src/main.rs"),
            gen_main_rs(&entry_filename, do_pack),
        )
        .expect("write src/main.rs");
        std::fs::write(build_dir.join("build.rs"), gen_build_rs()).expect("write build.rs");
    }

    // Pack resources into the executable (both backends self-extract them).
    if do_pack {
        write_packed_resources(build_dir, &resources);
        println!(
            "  packing {} resource(s) into the executable",
            resources.len()
        );
    } else if pack {
        println!("  --pack: no [includes] resources to pack");
    }

    // App icon (Windows): rendered to app.ico, embedded by the generated build.rs.
    if matches!(platform, NativePlatform::Windows) {
        match resolve_icon(icon, project, &ling_root) {
            Some(src) => {
                let out_ico = build_dir.join("app.ico");
                match ling_icon::write_ico(&src, &out_ico, ling_icon::DEFAULT_SIZES) {
                    Ok(()) => println!("  [windows] icon: {}", src.display()),
                    Err(e) => eprintln!("  [windows] icon skipped: {e}"),
                }
            },
            None => println!("  [windows] no icon found; building without one"),
        }
    }

    // ── 3. Ensure rustup target is installed ─────────────────────────────────
    ensure_rustup_target(triple);

    // ── 4. Build ─────────────────────────────────────────────────────────────
    let build_cmd = choose_build_tool(platform);

    let mut cargo_args: Vec<&str> = vec!["build", "--release", "--target", triple];

    // For no_std targets, we need nightly features
    if is_kernel {
        // Write .cargo/config.toml to enable build-std. The linker script and
        // entry.o are wired in separately via build.rs's
        // `cargo:rustc-link-arg-bins` (see `gen_kernel_build_rs`), so this
        // only needs the build-std bit — it used to also duplicate the
        // linker-script arg here under a hardcoded `[target.x86_64-unknown-none]`
        // section (which silently never applied to any other kernel triple,
        // e.g. `aarch64-unknown-none`) plus an invalid `-nostartfiles` raw
        // linker arg (that flag is for a cc-frontend link line, not a direct
        // ld/lld invocation, which is how rustc links `*-unknown-none`).
        // `-C relocation-model=static`: without it, rustc's default PIC/PIE
        // codegen for this target leaves runtime relocations in the ELF and
        // adds `-pie` at link time. Nothing applies those relocations at
        // boot (no dynamic linker), and GRUB's multiboot2 ELF64 loader
        // refuses to load a PIE outright ("ELF files with relocs are not
        // supported yet") — confirmed by an actual QEMU boot attempt.
        // `--cfg curve25519_dalek_backend="serial"`: forces ed25519-dalek's
        // portable (non-SIMD) backend. Its default x86_64 backend emits AVX2
        // codegen that a freestanding target can't lower ("rustc-LLVM ERROR:
        // Do not know how to split the result of this operator!", confirmed
        // by an actual build attempt without this flag) — the same
        // portable-over-fast tradeoff already made for blake3's `pure`
        // feature above, and harmless on aarch64 too since "serial" is
        // arch-generic, not x86-specific.
        let cargo_dir = build_dir.join(".cargo");
        std::fs::create_dir_all(&cargo_dir).expect("create .cargo dir");
        std::fs::write(
            cargo_dir.join("config.toml"),
            format!(
                r#"[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]

[target.{triple}]
rustflags = ["-C", "relocation-model=static", "--cfg", "curve25519_dalek_backend=\"serial\""]
"#
            ),
        )
        .ok();
        // Use nightly for build-std support
        let nightly = "cargo";
        cargo_args = vec!["+nightly", "build", "--release", "--target", triple, "-Z", "build-std=core,compiler_builtins", "-Z", "build-std-features=compiler-builtins-mem"];
        let status = Command::new(nightly)
            .args(&cargo_args)
            .current_dir(build_dir)
            .status()
            .unwrap_or_else(|e| {
                eprintln!("  {nightly}: {e}");
                std::process::exit(1);
            });
        if !status.success() {
            eprintln!("  [{}] build failed.", platform.dir_name());
            eprintln!("  Tip: ensure nightly Rust is installed: rustup toolchain install nightly");
            eprintln!("  Then install: rustup target add x86_64-unknown-none --toolchain nightly");
            eprintln!("  And: rustup component add rust-src --toolchain nightly");
            std::process::exit(1);
        }
    } else {
        let status = Command::new(build_cmd)
            .args(&cargo_args)
            .current_dir(build_dir)
            .status()
            .unwrap_or_else(|e| {
                eprintln!("  {build_cmd}: {e}");
                if build_cmd == "cross" {
                    eprintln!("  install cross: cargo install cross  (requires Docker)");
                }
                std::process::exit(1);
            });
        if !status.success() {
            eprintln!("  [{}] build failed.", platform.dir_name());
            if !platform.is_current_host() && build_cmd == "cargo" && !has_cross() {
                eprintln!("  Tip: install `cross` for cross-compilation (needs Docker):");
                eprintln!("       cargo install cross");
            }
            std::process::exit(1);
        }
    }

    // ── 5. Copy binary to dist/<platform>/ ──────────────────────────────────
    let exe = format!("{}{}", project.name, platform.exe_suffix());
    let src_bin = build_dir
        .join("target")
        .join(triple)
        .join("release")
        .join(&exe);
    let platform_dir = Path::new(out).join(platform.dir_name());
    std::fs::create_dir_all(&platform_dir).expect("create platform dir");
    let dst = platform_dir.join(&exe);
    std::fs::copy(&src_bin, &dst).unwrap_or_else(|e| {
        eprintln!("  copy binary {}: {e}", src_bin.display());
        std::process::exit(1);
    });
    println!("  [{}] → {}", platform.dir_name(), dst.display());

    // Raspberry Pi firmware boots a raw binary (`kernel8.img`), not an ELF —
    // extract one from the ELF we just built and copied.
    if is_rpi {
        let img_path = platform_dir.join("kernel8.img");
        objcopy_to_raw_binary(&dst, &img_path);
        println!("  [{}] → {}", platform.dir_name(), img_path.display());
    }

    // ── 6. Copy included resources next to the exe (unless packed inside it) ──
    if !do_pack && !resources.is_empty() {
        for (rel, abs) in &resources {
            let dst = platform_dir.join(rel);
            if let Some(parent) = dst.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            if let Err(e) = std::fs::copy(abs, &dst) {
                eprintln!("  resource copy {}: {e}", rel);
            }
        }
        println!(
            "  [{}] bundled {} resource(s)",
            platform.dir_name(),
            resources.len()
        );
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Extract a raw binary image from an ELF via `objcopy -O binary` — what
/// Raspberry Pi firmware wants for `kernel8.img` (it has no ELF loader; it
/// just copies file bytes to a fixed address). Tries `llvm-objcopy` first
/// (ships with any LLVM install, handles cross-arch ELF happily), then falls
/// back to a plain `objcopy` on PATH (e.g. a GNU binutils install, or the one
/// inside WSL).
fn objcopy_to_raw_binary(elf: &Path, out_img: &Path) {
    for tool in ["llvm-objcopy", "objcopy"] {
        let status = Command::new(tool)
            .args(["-O", "binary"])
            .arg(elf)
            .arg(out_img)
            .status();
        match status {
            Ok(s) if s.success() => return,
            Ok(s) => {
                eprintln!("  {tool} exited with {s}");
                std::process::exit(1);
            },
            Err(_) => continue, // tool not found — try the next one
        }
    }
    eprintln!(
        "  error: neither llvm-objcopy nor objcopy found on PATH; can't produce {}",
        out_img.display()
    );
    eprintln!("  install LLVM (provides llvm-objcopy) or GNU binutils (objcopy)");
    std::process::exit(1);
}

/// Walk up from `dir` looking for a `font/` subdirectory containing an
/// `.otf`/`.ttf` file — the convention a kernel project's idle-timeout font
/// is found by (e.g. `LingOS/font/square.otf`, found from
/// `LingOS/kernel/x86_64/`). Returns the first match (sorted, for
/// determinism) in the nearest ancestor that has one; `None` if nothing is
/// found within 6 levels up.
fn find_font_asset(dir: &Path) -> Option<PathBuf> {
    let mut cur = Some(dir);
    for _ in 0..6 {
        let d = cur?;
        let font_dir = d.join("font");
        if font_dir.is_dir() {
            let mut candidates: Vec<PathBuf> = std::fs::read_dir(&font_dir)
                .ok()?
                .flatten()
                .map(|e| e.path())
                .filter(|p| {
                    matches!(
                        p.extension().and_then(|e| e.to_str()).map(|e| e.to_lowercase()),
                        Some(ref e) if e == "otf" || e == "ttf"
                    )
                })
                .collect();
            candidates.sort();
            // Prefer a file literally named "square.*" — this project's
            // chosen console font — over whatever else sorts first
            // alphabetically (e.g. other font assets living alongside it).
            if let Some(square) = candidates
                .iter()
                .find(|p| p.file_stem().and_then(|s| s.to_str()) == Some("square"))
            {
                return Some(square.clone());
            }
            if let Some(first) = candidates.into_iter().next() {
                return Some(first);
            }
        }
        cur = d.parent();
    }
    None
}

/// Byte slots reserved for glyphs beyond the primary font's own coverage —
/// currently a couple of Chinese characters for the boot banner, sourced
/// from a CJK-capable fallback font (most small/display fonts, like a
/// project's chosen pixel/ASCII font, don't include CJK glyphs). 0x01/0x02/
/// 0x03 are otherwise-unused control-character slots in a 256-glyph table.
const SPECIAL_GLYPHS: &[(u8, char)] = &[
    (0x01, ''), // líng — "spirit/soul", the project's namesake character
    (0x02, ''), // nèi  — "inner"
    (0x03, ''), // hé   — "core" (as in "kernel")
];

/// Rasterize `primary` (and, for `SPECIAL_GLYPHS`, `cjk_fallback` if given)
/// into a 256-glyph, 8x16, 1bpp VGA font atlas (`ling_kernel::vga::load_font`'s
/// expected layout). Printable ASCII (0x20..=0x7E) maps directly to the same
/// Unicode codepoint; anything else is blank unless it's one of the special
/// slots above. Missing glyphs (empty rasterized bitmap) are left blank
/// rather than erroring — fonts commonly don't cover every codepoint.
fn rasterize_vga_font(primary: &[u8], cjk_fallback: Option<&[u8]>) -> [u8; 4096] {
    let mut out = [0u8; 4096];
    let Ok(primary_font) = fontdue::Font::from_bytes(primary, fontdue::FontSettings::default())
    else {
        return out;
    };
    let cjk_font = cjk_fallback
        .and_then(|b| fontdue::Font::from_bytes(b, fontdue::FontSettings::default()).ok());

    for code in 0u32..256 {
        if let Some(&(_, ch)) = SPECIAL_GLYPHS.iter().find(|(b, _)| *b as u32 == code) {
            let font = cjk_font.as_ref().unwrap_or(&primary_font);
            blit_glyph(&mut out, code as usize, font, ch);
        } else if (0x20..=0x7E).contains(&code) {
            let ch = char::from_u32(code).unwrap();
            blit_glyph(&mut out, code as usize, &primary_font, ch);
        }
    }
    out
}

/// Rasterize one glyph at a size tuned to mostly fill an 8x16 cell, centering
/// it (both axes) within the cell before thresholding coverage to 1bpp.
fn blit_glyph(out: &mut [u8; 4096], slot: usize, font: &fontdue::Font, ch: char) {
    let (metrics, bitmap) = font.rasterize(ch, 15.0);
    if metrics.width == 0 || metrics.height == 0 {
        return; // glyph not covered by this font — leave the slot blank
    }
    let y_off = ((16i32 - metrics.height as i32) / 2).max(0) as usize;
    let x_off = ((8i32 - metrics.width as i32) / 2).max(0) as usize;
    for row in 0..metrics.height.min(16) {
        let mut byte = 0u8;
        for col in 0..metrics.width.min(8) {
            if bitmap[row * metrics.width + col] > 128 {
                byte |= 1 << (7usize.saturating_sub(x_off + col));
            }
        }
        let out_row = y_off + row;
        if out_row < 16 {
            out[slot * 16 + out_row] |= byte;
        }
    }
}

/// Generate `src/idle_font.rs`: the rasterized VGA font atlas as a plain byte
/// array, `include!`d (via `mod idle_font;`) by the generated kernel
/// `main.rs` when a font asset was found.
fn gen_idle_font_rs(atlas: &[u8; 4096]) -> String {
    let mut out = String::with_capacity(4096 * 4 + 64);
    out.push_str("pub static IDLE_FONT: [u8; 4096] = [\n");
    for chunk in atlas.chunks(16) {
        out.push_str("    ");
        for b in chunk {
            out.push_str(&format!("{b},"));
        }
        out.push('\n');
    }
    out.push_str("];\n");
    out
}

/// Recursively collect all .ling files from `src` and copy them flat into `dst`.
/// Also descends into 灵源/ and src/ sub-directories (ling-fu convention).
fn copy_ling_sources(src: &Path, dst: &Path) {
    let Ok(entries) = std::fs::read_dir(src) else { return };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            let dname = path.file_name().unwrap_or_default().to_string_lossy();
            if dname == "灵源" || dname == "src" {
                copy_ling_sources(&path, dst);
            }
        } else if path.extension().is_some_and(|e| e == "ling") {
            if let Some(name) = path.file_name() {
                let _ = std::fs::copy(&path, dst.join(name));
            }
        }
    }
}

/// The build script written into each generated app crate. It embeds `app.ico`
/// (produced by `ling build`) into the Windows executable; on other targets, or
/// if no icon was generated, it does nothing.
fn gen_build_rs() -> String {
    r#"// Generated by `ling build` — embeds the app icon on Windows.
use std::path::Path;

fn main() {
    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
        return;
    }
    if !Path::new("app.ico").exists() {
        return;
    }
    println!("cargo:rerun-if-changed=app.ico");
    let mut res = winresource::WindowsResource::new();
    res.set_icon("app.ico");
    if let Err(e) = res.compile() {
        println!("cargo:warning=icon embed skipped ({e})");
    }
}
"#
    .to_string()
}

/// Generate Cargo.toml for kernel (no_std, no default features, links to ling-kernel).
fn gen_kernel_cargo_toml(name: &str, version: &str, ling_root: &Path, graphics: bool) -> String {
    let root_str = ling_root.display().to_string().replace('\\', "/");
    let ling_kernel_dep = if graphics {
        format!(r#"{{ path = "{root_str}/crates/ling-kernel", features = ["request_framebuffer"] }}"#)
    } else {
        format!(r#"{{ path = "{root_str}/crates/ling-kernel" }}"#)
    };
    format!(
        r#"[workspace]
[package]
name = "{name}"
version = "{version}"
edition = "2021"
build = "build.rs"

[[bin]]
name = "{name}"
path = "src/main.rs"

[dependencies]
ling-kernel = {ling_kernel_dep}

[build-dependencies]
cc = "1.0"

[profile.release]
lto = "fat"
codegen-units = 1
opt-level = 3
panic = "abort"
strip = true
"#
    )
}

/// Generate `src/main.rs` for kernel builds: no_std, no_main. The multiboot2
/// header and the real ELF entry point (`_start`) both live in
/// `ling_kernel::boot32` now, not here — GRUB's Multiboot2 handoff lands in
/// 32-bit protected mode with paging off even for a 64-bit ELF (confirmed by
/// an actual QEMU boot: jumping straight into 64-bit-compiled code from
/// there triple-faults immediately), so getting into a state where this
/// file's 64-bit-compiled `kernel_entry` is safe to run at all takes a real
/// assembly trampoline (page tables, PAE, EFER.LME, a 64-bit GDT) — see
/// `boot32.rs`'s doc comment. `_start` there calls `kernel_entry` once that's
/// done, mirroring the aarch64/Raspberry Pi boot path's own `kernel_entry`
/// convention (see `gen_rpi_kernel_main_rs`) so both platforms' generated
/// main.rs stay structurally identical.
///
/// `idle_font` is `Some` when a build-time-rasterized VGA font was found (see
/// `find_font_asset`/`rasterize_vga_font`) — it names the generated module
/// (`idle_font.rs`, written alongside this file) holding the byte array, and
/// gets registered with `ling_kernel::vga::set_idle_font` before the kernel's
/// own code runs, so `keyboard::read_char`'s idle timer can swap to it later.
fn gen_kernel_main_rs(idle_font: Option<&str>) -> String {
    // Plain string substitution (not `format!`) — the template below is full
    // of literal Rust-code braces that `format!` would otherwise try to
    // parse as placeholders.
    let idle_font_mod = idle_font.map(|m| format!("mod {m};\n")).unwrap_or_default();
    let idle_font_reg = idle_font
        .map(|m| format!("        ling_kernel::vga::set_idle_font(&{m}::IDLE_FONT);\n"))
        .unwrap_or_default();
    let template = r#"#![no_std]
#![no_main]

// Nothing below references `ling_kernel` by path — only `entry.o`'s compiled
// code calls its `#[no_mangle] extern "C"` functions, which is invisible to
// rustc's own "is this dependency used" tracking. Without this, rustc never
// links ling-kernel's rlib in at all, and the link fails with "undefined
// symbol: ling_kernel_vga_clear" (or whichever intrinsic is first referenced).
extern crate ling_kernel;

/*IDLE_FONT_MOD*/

extern "C" {
    fn __main__() -> u64;
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn kernel_entry() -> ! {
    unsafe {
/*IDLE_FONT_REG*/
        __main__();
    }
    loop {
        unsafe { core::arch::asm!("hlt"); }
    }
}
"#;
    template
        .replace("/*IDLE_FONT_MOD*/", &idle_font_mod)
        .replace("/*IDLE_FONT_REG*/", &idle_font_reg)
}

/// Generate `build.rs` for kernel builds: links entry.o, multiboot.o, and linker script.
fn gen_kernel_build_rs() -> String {
    r#"fn main() {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    // NOTE: no `-nostartfiles` here — that's a cc-frontend flag, not a raw
    // ld/lld one, and rustc links `*-unknown-none` targets by invoking the
    // linker directly (no cc frontend in between).
    println!("cargo:rustc-link-arg-bins=-static");
    println!("cargo:rustc-link-arg-bins=-n");
    println!("cargo:rustc-link-arg-bins=-T{}/linker.ld", manifest_dir);
    println!("cargo:rustc-link-arg-bins={}/entry.o", manifest_dir);
    println!("cargo:rerun-if-changed=entry.o");
    println!("cargo:rerun-if-changed=linker.ld");
}
"#
    .to_string()
}

/// Generate `src/main.rs` for Raspberry Pi (aarch64) kernel builds: no_std,
/// no_main, no multiboot header (RPi firmware loads `kernel8.img` as a raw
/// binary at a fixed address — there's no header/magic to look for). The
/// actual `_start` — parking secondary cores, setting up the stack, zeroing
/// .bss — lives in `ling_kernel::boot` since it needs real assembly before
/// any Rust code (including this generated file) can safely run; this just
/// supplies the `kernel_entry` that boot stub calls into.
fn gen_rpi_kernel_main_rs() -> String {
    r#"#![no_std]
#![no_main]

// See the equivalent comment in `gen_kernel_main_rs` — forces ling-kernel's
// rlib to actually be linked (the `zero_bss` call below would do this too,
// but keep both: it's the correct idiom and doesn't depend on this function
// body never changing).
extern crate ling_kernel;

extern "C" {
    fn __main__() -> u64;
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn kernel_entry() -> ! {
    unsafe {
        ling_kernel::boot::zero_bss();
        __main__();
    }
    loop {
        unsafe { core::arch::asm!("wfi"); }
    }
}
"#
    .to_string()
}

/// Linker script for x86_64 kernel with multiboot2 header.
const KERNEL_LINKER_SCRIPT: &str = r#"OUTPUT_FORMAT(elf64-x86-64)
ENTRY(_start)

SECTIONS {
    . = 1M;

    .multiboot : ALIGN(8) {
        KEEP(*(.ling_multiboot))
    }

    .text : ALIGN(4096) {
        *(.text .text.*)
    }

    .rodata : ALIGN(4096) {
        *(.rodata .rodata.*)
    }

    .data : ALIGN(4096) {
        *(.data .data.*)
    }

    .bss : ALIGN(4096) {
        *(.bss .bss.*)
        *(COMMON)
    }

    /DISCARD/ : {
        *(.eh_frame)
        *(.comment)
        *(.note.*)
    }
}
"#;

/// Linker script for the Raspberry Pi (aarch64) kernel. Entry at `0x80000` —
/// the standard AArch64 no-devicetree load address RPi firmware jumps to.
/// Defines `__bss_start`/`__bss_end` (zeroed by `ling_kernel::boot::zero_bss`
/// before any Rust code runs — unlike the GRUB/Multiboot2 path, raw
/// `kernel8.img` loading has no ELF program headers to zero .bss for us) and
/// a `_stack_top` a few pages past the end of .bss for the boot stub's `sp`.
const RPI_LINKER_SCRIPT: &str = r#"OUTPUT_FORMAT(elf64-littleaarch64)
ENTRY(_start)

SECTIONS {
    . = 0x80000;

    .text : ALIGN(4096) {
        KEEP(*(.text.boot))
        *(.text .text.*)
    }

    .rodata : ALIGN(4096) {
        *(.rodata .rodata.*)
    }

    .data : ALIGN(4096) {
        *(.data .data.*)
    }

    .bss (NOLOAD) : ALIGN(16) {
        __bss_start = .;
        *(.bss .bss.*)
        *(COMMON)
        __bss_end = .;
    }

    . = ALIGN(16);
    . += 0x4000; /* 16KB boot stack */
    _stack_top = .;

    /DISCARD/ : {
        *(.eh_frame)
        *(.comment)
        *(.note.*)
    }
}
"#;

/// Pick the icon source: explicit `--icon` flag, else the manifest field, else
/// the bundled default logo from the ling-lang repo. `None` if nothing is found.
fn resolve_icon(cli: Option<&Path>, project: &LingProject, ling_root: &Path) -> Option<PathBuf> {
    // Explicit --icon / manifest icon wins, but if the file is missing we warn
    // and fall through to the bundled default rather than shipping no icon.
    for candidate in [cli.map(Path::to_path_buf), project.icon.clone()]
        .into_iter()
        .flatten()
    {
        if candidate.exists() {
            return Some(candidate);
        }
        eprintln!(
            "  [icon] '{}' not found; using default",
            candidate.display()
        );
    }
    let default = ling_root.join("ling-lang.org/images/logo.svg");
    default.exists().then_some(default)
}

fn gen_main_rs(entry_file: &str, packed: bool) -> String {
    // When packing, pull in the generated resource table and self-extract it
    // before running so every path-based asset loader finds its files on disk.
    let res_mod = if packed { "mod resources;\n" } else { "" };
    let unpack = if packed {
        "    ling::unpack_resources(env!(\"CARGO_PKG_NAME\"), resources::RESOURCES);\n"
    } else {
        ""
    };
    format!(
        r#"// Built by ling build — no console window on Windows.
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
{res_mod}
fn main() {{
    const SOURCE: &str = include_str!("../{entry_file}");
{unpack}    let lang = ling::detect_language(SOURCE);
    if lang != "English" {{
        eprintln!("[language: {{}}]", lang);
    }}
    if let Err(e) = ling::run(SOURCE) {{
        eprintln!("{{e}}");
        std::process::exit(1);
    }}
}}
"#
    )
}

/// Generate `src/main.rs` for AOT builds: initializes runtime, calls compiled code.
fn gen_aot_main_rs(packed: bool) -> String {
    let res_mod = if packed { "mod resources;\n" } else { "" };
    let unpack = if packed {
        "    ling::unpack_resources(env!(\"CARGO_PKG_NAME\"), resources::RESOURCES);\n"
    } else {
        ""
    };
    format!(
        r#"// Built by ling build --aot — no console window on Windows.
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
{res_mod}
fn main() {{
    ling::runtime::init_aot_runtime();
{unpack}
    extern "C" {{
        fn __main__() -> u64;
    }}

    unsafe {{
        __main__();
    }}
}}
"#
    )
}

/// Generate `build.rs` for AOT builds: links the compiled object directly into
/// the binary (no archiver needed) and embeds the app icon on Windows.
fn gen_aot_build_rs() -> String {
    r#"// Generated by `ling build --aot`. Links the AOT-compiled object file.
use std::path::Path;

fn main() {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR");
    let obj = Path::new(&manifest_dir).join("entry.o");
    println!("cargo:rustc-link-arg={}", obj.display());
    println!("cargo:rerun-if-changed=entry.o");

    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows")
        && Path::new("app.ico").exists()
    {
        println!("cargo:rerun-if-changed=app.ico");
        let mut res = winresource::WindowsResource::new();
        res.set_icon("app.ico");
        if let Err(e) = res.compile() {
            println!("cargo:warning=icon embed skipped ({e})");
        }
    }
}
"#
    .to_string()
}

// ── [includes] resource handling ───────────────────────────────────────────────

/// Parse the manifest `[includes]` block (or an inline `includes = [...]`).
/// Accepts the section form the user writes:
///   [includes]
///   "/music/*.wav",
///   "/font/*.otf",
/// Patterns are returned verbatim (leading `/`/`./` stripped later).
fn parse_includes(text: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut in_section = false;
    for raw in text.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if line.starts_with('[') {
            // [includes] (English) or [包含] (Chinese)
            in_section = line == "[includes]" || line == "[包含]";
            continue;
        }
        // Inline array form: includes = ["a", "b"]  /  包含 = [...]
        if let Some(rest) = line
            .strip_prefix("includes")
            .or_else(|| line.strip_prefix("包含"))
            .and_then(|r| r.trim_start().strip_prefix('='))
        {
            collect_quoted(rest, &mut out);
            continue;
        }
        if in_section {
            collect_quoted(line, &mut out);
        }
    }
    out
}

/// Push every `"..."`-quoted substring of `s` into `out`.
fn collect_quoted(s: &str, out: &mut Vec<String>) {
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'"' {
            if let Some(end) = s[i + 1..].find('"') {
                let val = &s[i + 1..i + 1 + end];
                if !val.is_empty() {
                    out.push(val.to_string());
                }
                i += end + 2;
                continue;
            }
        }
        i += 1;
    }
}

/// Load ignore patterns from `ling.ignore` (or `.lingignore`) at the project
/// root. One pattern per line, `#` comments. Same glob syntax as `[includes]`;
/// a plain path (with or without a trailing `/`) excludes that file or the
/// whole subtree under a directory. Applied to `[includes]` expansion so junk
/// (recordings, saves, editor litter) never gets packed into a build — the
/// same file `lingfu publish` uses to filter the uploaded tarball.
fn load_ignore_patterns(root: &Path) -> Vec<String> {
    let mut pats = Vec::new();
    for name in ["ling.ignore", ".lingignore"] {
        if let Ok(text) = std::fs::read_to_string(root.join(name)) {
            for line in text.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') {
                    continue;
                }
                let line = line
                    .trim_start_matches("./")
                    .trim_start_matches('/')
                    .trim_end_matches('/');
                if !line.is_empty() {
                    pats.push(line.replace('\\', "/"));
                }
            }
        }
    }
    pats
}

/// True when `rel` (forward-slash relative path) matches an ignore pattern.
fn is_ignored(pats: &[String], rel: &str) -> bool {
    pats.iter().any(|p| {
        if p.contains('*') || p.contains('?') {
            glob_match(p, rel)
        } else {
            rel == p || rel.starts_with(&format!("{p}/"))
        }
    })
}

/// Expand the manifest's include patterns against the project root into a list of
/// `(relative-path-with-forward-slashes, absolute-path)` files. Files matching
/// `ling.ignore` are excluded.
fn expand_includes(project: &LingProject) -> Vec<(String, PathBuf)> {
    let root = &project.source_dir;
    let mut all: Vec<(String, PathBuf)> = Vec::new();
    let mut seen = std::collections::HashSet::new();
    let files = walk_files(root); // (rel, abs) for the whole tree, once
    let ignore = load_ignore_patterns(root);
    let mut skipped = 0usize;

    for pat in &project.includes {
        let pat = pat.trim().trim_start_matches("./").trim_start_matches('/');
        if pat.is_empty() {
            continue;
        }
        let has_glob = pat.contains('*') || pat.contains('?');
        if !has_glob {
            let abs = root.join(pat);
            if abs.is_dir() {
                // A bare directory means "everything under it".
                let prefix = format!("{}/", pat.replace('\\', "/"));
                for (rel, abs) in &files {
                    if rel.starts_with(&prefix) {
                        if is_ignored(&ignore, rel) {
                            skipped += 1;
                            continue;
                        }
                        if seen.insert(rel.clone()) {
                            all.push((rel.clone(), abs.clone()));
                        }
                    }
                }
            } else if abs.is_file() {
                let rel = pat.replace('\\', "/");
                if is_ignored(&ignore, &rel) {
                    skipped += 1;
                } else if seen.insert(rel.clone()) {
                    all.push((rel, abs));
                }
            } else {
                eprintln!("  [includes] no match for '{pat}'");
            }
            continue;
        }
        let mut matched = false;
        for (rel, abs) in &files {
            if glob_match(pat, rel) {
                if is_ignored(&ignore, rel) {
                    skipped += 1;
                    matched = true; // pattern did match; the file is just excluded
                    continue;
                }
                if seen.insert(rel.clone()) {
                    all.push((rel.clone(), abs.clone()));
                    matched = true;
                }
            }
        }
        if !matched {
            eprintln!("  [includes] no match for '{pat}'");
        }
    }
    if skipped > 0 {
        println!("  ling.ignore: excluded {skipped} file(s)");
    }
    all
}

/// Recursively list every file under `root` as `(relative-forward-slash, absolute)`,
/// skipping build/output directories.
fn walk_files(root: &Path) -> Vec<(String, PathBuf)> {
    fn rec(base: &Path, dir: &Path, out: &mut Vec<(String, PathBuf)>) {
        let Ok(entries) = std::fs::read_dir(dir) else { return };
        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name();
            let name = name.to_string_lossy();
            if path.is_dir() {
                // Don't descend into generated/output trees.
                if matches!(
                    name.as_ref(),
                    ".ling-build" | "灵碑" | "target" | "dist" | ".git"
                ) {
                    continue;
                }
                rec(base, &path, out);
            } else if let Ok(rel) = path.strip_prefix(base) {
                out.push((rel.to_string_lossy().replace('\\', "/"), path.clone()));
            }
        }
    }
    let mut out = Vec::new();
    rec(root, root, &mut out);
    out
}

/// Glob match with `/`-aware segments: `*`/`?` stay within a path segment,
/// while a `**` segment matches across any number of segments.
fn glob_match(pattern: &str, path: &str) -> bool {
    let pat: Vec<&str> = pattern.split('/').collect();
    let seg: Vec<&str> = path.split('/').collect();
    seg_match(&pat, &seg)
}

fn seg_match(pat: &[&str], seg: &[&str]) -> bool {
    match pat.first() {
        None => seg.is_empty(),
        Some(&"**") => {
            // Match zero or more path segments.
            (0..=seg.len()).any(|i| seg_match(&pat[1..], &seg[i..]))
        },
        Some(p) => match seg.first() {
            Some(s) if wildcard(p.as_bytes(), s.as_bytes()) => seg_match(&pat[1..], &seg[1..]),
            _ => false,
        },
    }
}

/// `*` (any run) and `?` (one char) matching within a single segment.
fn wildcard(pat: &[u8], s: &[u8]) -> bool {
    let (mut pi, mut si) = (0, 0);
    let (mut star, mut mark) = (usize::MAX, 0);
    while si < s.len() {
        if pi < pat.len() && (pat[pi] == b'?' || pat[pi] == s[si]) {
            pi += 1;
            si += 1;
        } else if pi < pat.len() && pat[pi] == b'*' {
            star = pi;
            mark = si;
            pi += 1;
        } else if star != usize::MAX {
            pi = star + 1;
            mark += 1;
            si = mark;
        } else {
            return false;
        }
    }
    while pi < pat.len() && pat[pi] == b'*' {
        pi += 1;
    }
    pi == pat.len()
}

/// Copy each resource into `<build_dir>/res/<rel>` and generate
/// `src/resources.rs` mapping every relative path to its `include_bytes!`.
fn write_packed_resources(build_dir: &Path, resources: &[(String, PathBuf)]) {
    let res_root = build_dir.join("res");
    let mut entries = String::new();
    for (rel, abs) in resources {
        let dst = res_root.join(rel);
        if let Some(parent) = dst.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        if let Err(e) = std::fs::copy(abs, &dst) {
            eprintln!("  pack copy {rel}: {e}");
            continue;
        }
        // include_bytes! path is relative to src/resources.rs → ../res/<rel>.
        entries.push_str(&format!(
            "    ({rel:?}, include_bytes!(\"../res/{rel}\")),\n"
        ));
    }
    let module = format!(
        "// Generated by `ling build --pack`. Embedded resource table.\n\
         pub static RESOURCES: &[(&str, &[u8])] = &[\n{entries}];\n"
    );
    let _ = std::fs::write(build_dir.join("src/resources.rs"), module);
}

fn ensure_rustup_target(triple: &str) {
    let Ok(out) = Command::new("rustup")
        .args(["target", "list", "--installed"])
        .output()
    else {
        return;
    };
    let installed = String::from_utf8_lossy(&out.stdout);
    if !installed.contains(triple) {
        println!("    installing target {triple}");
        let _ = Command::new("rustup")
            .args(["target", "add", triple])
            .status();
    }
}

/// Use `cross` for cross-compilation when available; otherwise fall back to `cargo`.
fn choose_build_tool(platform: NativePlatform) -> &'static str {
    if !platform.is_current_host() && has_cross() {
        "cross"
    } else {
        "cargo"
    }
}

fn has_cross() -> bool {
    Command::new("cross").arg("--version").output().is_ok()
}

/// Return the path to a sibling binary in the same directory as this executable.
fn sibling_binary(name: &str) -> PathBuf {
    if let Ok(exe) = std::env::current_exe() {
        let candidate =
            exe.parent()
                .unwrap_or(Path::new("."))
                .join(if cfg!(target_os = "windows") {
                    format!("{name}.exe")
                } else {
                    name.to_string()
                });
        if candidate.exists() {
            return candidate;
        }
    }
    PathBuf::from(name)
}

/// Locate the ling-lang repository root (the directory containing the root Cargo.toml).
fn find_ling_root() -> Option<PathBuf> {
    // 1. Explicit env var
    if let Ok(home) = std::env::var("LING_HOME") {
        let p = PathBuf::from(home);
        if p.join("Cargo.toml").exists() {
            return Some(p);
        }
    }
    // 2. Relative to this binary: target/{debug,release}/ling → 3 levels up
    if let Ok(exe) = std::env::current_exe() {
        if let Some(repo) = exe
            .parent()
            .and_then(|p| p.parent())
            .and_then(|p| p.parent())
        {
            if repo.join("Cargo.toml").exists() {
                return Some(repo.to_path_buf());
            }
        }
    }
    // 3. Current working directory
    let cwd = std::env::current_dir().ok()?;
    if cwd.join("Cargo.toml").exists() {
        return Some(cwd);
    }
    None
}

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

    #[test]
    fn parses_includes_section_form() {
        let toml = "\
[project]
name = \"game\"

[includes]
\"/music/*.wav\",
\"/font/*.otf\",
\"data.bin\"
";
        let inc = parse_includes(toml);
        assert_eq!(inc, vec!["/music/*.wav", "/font/*.otf", "data.bin"]);
    }

    #[test]
    fn parses_includes_inline_array() {
        let inc = parse_includes("includes = [\"a/*.png\", \"b.txt\"]\n");
        assert_eq!(inc, vec!["a/*.png", "b.txt"]);
    }

    #[test]
    fn includes_section_stops_at_next_header() {
        let toml = "[includes]\n\"a.txt\"\n[other]\n\"b.txt\"\n";
        assert_eq!(parse_includes(toml), vec!["a.txt"]);
    }

    #[test]
    fn glob_matches_within_and_across_segments() {
        assert!(glob_match("music/*.wav", "music/song.wav"));
        assert!(!glob_match("music/*.wav", "music/sub/song.wav")); // * doesn't cross '/'
        assert!(glob_match("music/**/*.wav", "music/a/b/song.wav")); // ** crosses
        assert!(glob_match("**/*.otf", "font/deep/x.otf"));
        assert!(glob_match("data?.bin", "data7.bin"));
        assert!(!glob_match("*.wav", "song.mp3"));
        assert!(glob_match("font/x.otf", "font/x.otf")); // exact
    }
}