cargo-cross 1.5.0

A cargo subcommand for cross-compilation, no need docker!
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
//! Target configuration database for cargo-cross

use std::collections::HashMap;

/// Supported glibc versions
pub const SUPPORTED_GLIBC_VERSIONS: &[&str] = &[
    "2.17", "2.27", "2.28", "2.29", "2.30", "2.31", "2.32", "2.33", "2.34", "2.35", "2.36", "2.37",
    "2.38", "2.39", "2.40", "2.41", "2.42", "2.43",
];

/// Default glibc version (empty means no version suffix)
pub const DEFAULT_GLIBC_VERSION: &str = "";

/// Supported iPhone SDK versions (for Linux cross-compilation)
pub const SUPPORTED_IPHONE_SDK_VERSIONS: &[&str] = &[
    "17.0", "17.2", "17.4", "17.5", "18.0", "18.1", "18.2", "18.4", "18.5", "26.0", "26.1", "26.2",
];

/// Default iPhone SDK version
pub const DEFAULT_IPHONE_SDK_VERSION: &str = "26.2";

/// Supported macOS SDK versions (for Linux cross-compilation)
pub const SUPPORTED_MACOS_SDK_VERSIONS: &[&str] = &[
    "14.0", "14.2", "14.4", "14.5", "15.0", "15.1", "15.2", "15.4", "15.5", "26.0", "26.1", "26.2",
];

/// Default macOS SDK version
pub const DEFAULT_MACOS_SDK_VERSION: &str = "26.2";

/// Supported FreeBSD versions
pub const SUPPORTED_FREEBSD_VERSIONS: &[&str] = &["13", "14", "15"];

/// Default FreeBSD version
pub const DEFAULT_FREEBSD_VERSION: &str = "13";

/// Default cross-compiler make version
pub const DEFAULT_CROSS_MAKE_VERSION: &str = "v0.7.7";

/// Default Android NDK version (LTS)
pub const DEFAULT_NDK_VERSION: &str = "r27d";

/// Default QEMU version
pub const DEFAULT_QEMU_VERSION: &str = "v10.2.0";

/// Format supported versions as comma-separated string
#[must_use]
pub fn supported_glibc_versions_str() -> String {
    SUPPORTED_GLIBC_VERSIONS.join(", ")
}

/// Format supported FreeBSD versions as comma-separated string
#[must_use]
pub fn supported_freebsd_versions_str() -> String {
    SUPPORTED_FREEBSD_VERSIONS.join(", ")
}

/// Format supported iPhone SDK versions as comma-separated string
#[must_use]
pub fn supported_iphone_sdk_versions_str() -> String {
    SUPPORTED_IPHONE_SDK_VERSIONS.join(", ")
}

/// Format supported macOS SDK versions as comma-separated string
#[must_use]
pub fn supported_macos_sdk_versions_str() -> String {
    SUPPORTED_MACOS_SDK_VERSIONS.join(", ")
}

/// Operating system type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Os {
    Linux,
    Windows,
    FreeBsd,
    NetBsd,
    Darwin,
    Ios,
    IosSim,
    Android,
}

impl Os {
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Linux => "linux",
            Self::Windows => "windows",
            Self::FreeBsd => "freebsd",
            Self::NetBsd => "netbsd",
            Self::Darwin => "darwin",
            Self::Ios => "ios",
            Self::IosSim => "ios-sim",
            Self::Android => "android",
        }
    }
}

/// Architecture type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Arch {
    Aarch64,
    Aarch64Be,
    Arm64e,
    Armv5,
    Armv6,
    Armv7,
    I586,
    I686,
    Loongarch64,
    Mips,
    Mipsel,
    Mipsisa32r6,
    Mipsisa32r6el,
    Mipsisa64r6,
    Mipsisa64r6el,
    Mips64,
    Mips64el,
    Powerpc64,
    Powerpc64le,
    Riscv32,
    Riscv64,
    S390x,
    X86_64,
    X86_64h,
}

impl Arch {
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Aarch64 => "aarch64",
            Self::Aarch64Be => "aarch64_be",
            Self::Arm64e => "arm64e",
            Self::Armv5 => "armv5",
            Self::Armv6 => "armv6",
            Self::Armv7 => "armv7",
            Self::I586 => "i586",
            Self::I686 => "i686",
            Self::Loongarch64 => "loongarch64",
            Self::Mips => "mips",
            Self::Mipsel => "mipsel",
            Self::Mipsisa32r6 => "mipsisa32r6",
            Self::Mipsisa32r6el => "mipsisa32r6el",
            Self::Mipsisa64r6 => "mipsisa64r6",
            Self::Mipsisa64r6el => "mipsisa64r6el",
            Self::Mips64 => "mips64",
            Self::Mips64el => "mips64el",
            Self::Powerpc64 => "powerpc64",
            Self::Powerpc64le => "powerpc64le",
            Self::Riscv32 => "riscv32",
            Self::Riscv64 => "riscv64",
            Self::S390x => "s390x",
            Self::X86_64 => "x86_64",
            Self::X86_64h => "x86_64h",
        }
    }

    /// Get the QEMU binary name for this architecture
    #[must_use]
    pub const fn qemu_binary_name(&self) -> Option<&'static str> {
        match self {
            Self::Aarch64 | Self::Aarch64Be => Some("qemu-aarch64"),
            Self::Armv5 | Self::Armv6 | Self::Armv7 => Some("qemu-arm"),
            Self::I586 | Self::I686 => Some("qemu-i386"),
            Self::Loongarch64 => Some("qemu-loongarch64"),
            Self::Mips | Self::Mipsisa32r6 => Some("qemu-mips"),
            Self::Mipsel | Self::Mipsisa32r6el => Some("qemu-mipsel"),
            Self::Mips64 | Self::Mipsisa64r6 => Some("qemu-mips64"),
            Self::Mips64el | Self::Mipsisa64r6el => Some("qemu-mips64el"),
            Self::Powerpc64 => Some("qemu-ppc64"),
            Self::Powerpc64le => Some("qemu-ppc64le"),
            Self::Riscv32 => Some("qemu-riscv32"),
            Self::Riscv64 => Some("qemu-riscv64"),
            Self::S390x => Some("qemu-s390x"),
            Self::X86_64 => Some("qemu-x86_64"),
            _ => None,
        }
    }
}

/// C library type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Libc {
    Musl,
    Gnu,
    Msvc,
}

impl Libc {
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Musl => "musl",
            Self::Gnu => "gnu",
            Self::Msvc => "msvc",
        }
    }
}

/// ABI type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Abi {
    Eabi,
    Eabihf,
    X32,
    Gnusf,
    Gnuspe,
}

impl Abi {
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Eabi => "eabi",
            Self::Eabihf => "eabihf",
            Self::X32 => "x32",
            Self::Gnusf => "gnusf",
            Self::Gnuspe => "gnuspe",
        }
    }

    /// Check if this ABI should be used with gnu libc
    #[must_use]
    pub const fn is_gnu_abi_variant(&self) -> bool {
        matches!(self, Self::Gnusf | Self::Gnuspe)
    }
}

/// Target configuration
#[derive(Debug, Clone)]
pub struct TargetConfig {
    pub target: &'static str,
    pub os: Os,
    pub arch: Arch,
    pub libc: Option<Libc>,
    pub abi: Option<Abi>,
}

impl TargetConfig {
    const fn new(target: &'static str, os: Os, arch: Arch) -> Self {
        Self {
            target,
            os,
            arch,
            libc: None,
            abi: None,
        }
    }

    const fn with_libc(mut self, libc: Libc) -> Self {
        self.libc = Some(libc);
        self
    }

    const fn with_abi(mut self, abi: Abi) -> Self {
        self.abi = Some(abi);
        self
    }
}

/// All supported target configurations
pub static TARGETS: std::sync::LazyLock<HashMap<&'static str, TargetConfig>> =
    std::sync::LazyLock::new(|| {
        let configs = vec![
            // Linux musl targets
            TargetConfig::new("aarch64-unknown-linux-musl", Os::Linux, Arch::Aarch64)
                .with_libc(Libc::Musl),
            TargetConfig::new("arm-unknown-linux-musleabi", Os::Linux, Arch::Armv6)
                .with_libc(Libc::Musl)
                .with_abi(Abi::Eabi),
            TargetConfig::new("arm-unknown-linux-musleabihf", Os::Linux, Arch::Armv6)
                .with_libc(Libc::Musl)
                .with_abi(Abi::Eabihf),
            TargetConfig::new("armv5te-unknown-linux-musleabi", Os::Linux, Arch::Armv5)
                .with_libc(Libc::Musl)
                .with_abi(Abi::Eabi),
            TargetConfig::new("armv7-unknown-linux-musleabi", Os::Linux, Arch::Armv7)
                .with_libc(Libc::Musl)
                .with_abi(Abi::Eabi),
            TargetConfig::new("armv7-unknown-linux-musleabihf", Os::Linux, Arch::Armv7)
                .with_libc(Libc::Musl)
                .with_abi(Abi::Eabihf),
            TargetConfig::new("i586-unknown-linux-musl", Os::Linux, Arch::I586)
                .with_libc(Libc::Musl),
            TargetConfig::new("i686-unknown-linux-musl", Os::Linux, Arch::I686)
                .with_libc(Libc::Musl),
            TargetConfig::new(
                "loongarch64-unknown-linux-musl",
                Os::Linux,
                Arch::Loongarch64,
            )
            .with_libc(Libc::Musl),
            TargetConfig::new("mips-unknown-linux-musl", Os::Linux, Arch::Mips)
                .with_libc(Libc::Musl),
            TargetConfig::new("mipsel-unknown-linux-musl", Os::Linux, Arch::Mipsel)
                .with_libc(Libc::Musl),
            TargetConfig::new("mips64-unknown-linux-muslabi64", Os::Linux, Arch::Mips64)
                .with_libc(Libc::Musl),
            TargetConfig::new("mips64-openwrt-linux-musl", Os::Linux, Arch::Mips64)
                .with_libc(Libc::Musl),
            TargetConfig::new(
                "mips64el-unknown-linux-muslabi64",
                Os::Linux,
                Arch::Mips64el,
            )
            .with_libc(Libc::Musl),
            TargetConfig::new("powerpc64-unknown-linux-musl", Os::Linux, Arch::Powerpc64)
                .with_libc(Libc::Musl),
            TargetConfig::new(
                "powerpc64le-unknown-linux-musl",
                Os::Linux,
                Arch::Powerpc64le,
            )
            .with_libc(Libc::Musl),
            TargetConfig::new("riscv64gc-unknown-linux-musl", Os::Linux, Arch::Riscv64)
                .with_libc(Libc::Musl),
            TargetConfig::new("s390x-unknown-linux-musl", Os::Linux, Arch::S390x)
                .with_libc(Libc::Musl),
            TargetConfig::new("x86_64-unknown-linux-musl", Os::Linux, Arch::X86_64)
                .with_libc(Libc::Musl),
            // Additional Linux musl targets supported by v0.7.7
            TargetConfig::new("aarch64_be-unknown-linux-musl", Os::Linux, Arch::Aarch64Be)
                .with_libc(Libc::Musl),
            TargetConfig::new("riscv32gc-unknown-linux-musl", Os::Linux, Arch::Riscv32)
                .with_libc(Libc::Musl),
            // Linux gnu targets
            TargetConfig::new("aarch64-unknown-linux-gnu", Os::Linux, Arch::Aarch64)
                .with_libc(Libc::Gnu),
            TargetConfig::new("arm-unknown-linux-gnueabi", Os::Linux, Arch::Armv6)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::Eabi),
            TargetConfig::new("arm-unknown-linux-gnueabihf", Os::Linux, Arch::Armv6)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::Eabihf),
            TargetConfig::new("armv5te-unknown-linux-gnueabi", Os::Linux, Arch::Armv5)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::Eabi),
            TargetConfig::new("armv7-unknown-linux-gnueabi", Os::Linux, Arch::Armv7)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::Eabi),
            TargetConfig::new("armv7-unknown-linux-gnueabihf", Os::Linux, Arch::Armv7)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::Eabihf),
            TargetConfig::new("i586-unknown-linux-gnu", Os::Linux, Arch::I586).with_libc(Libc::Gnu),
            TargetConfig::new("i686-unknown-linux-gnu", Os::Linux, Arch::I686).with_libc(Libc::Gnu),
            TargetConfig::new(
                "loongarch64-unknown-linux-gnu",
                Os::Linux,
                Arch::Loongarch64,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new("mips-unknown-linux-gnu", Os::Linux, Arch::Mips).with_libc(Libc::Gnu),
            TargetConfig::new("mipsel-unknown-linux-gnu", Os::Linux, Arch::Mipsel)
                .with_libc(Libc::Gnu),
            TargetConfig::new("mips64-unknown-linux-gnuabi64", Os::Linux, Arch::Mips64)
                .with_libc(Libc::Gnu),
            TargetConfig::new("mips64el-unknown-linux-gnuabi64", Os::Linux, Arch::Mips64el)
                .with_libc(Libc::Gnu),
            TargetConfig::new("powerpc64-unknown-linux-gnu", Os::Linux, Arch::Powerpc64)
                .with_libc(Libc::Gnu),
            TargetConfig::new(
                "powerpc64le-unknown-linux-gnu",
                Os::Linux,
                Arch::Powerpc64le,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new("riscv64gc-unknown-linux-gnu", Os::Linux, Arch::Riscv64)
                .with_libc(Libc::Gnu),
            TargetConfig::new("s390x-unknown-linux-gnu", Os::Linux, Arch::S390x)
                .with_libc(Libc::Gnu),
            TargetConfig::new("x86_64-unknown-linux-gnu", Os::Linux, Arch::X86_64)
                .with_libc(Libc::Gnu),
            TargetConfig::new("x86_64-unknown-linux-gnux32", Os::Linux, Arch::X86_64)
                .with_libc(Libc::Gnu)
                .with_abi(Abi::X32),
            // Additional Linux gnu targets supported by v0.7.7
            TargetConfig::new("aarch64_be-unknown-linux-gnu", Os::Linux, Arch::Aarch64Be)
                .with_libc(Libc::Gnu),
            TargetConfig::new(
                "mipsisa32r6-unknown-linux-gnu",
                Os::Linux,
                Arch::Mipsisa32r6,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new(
                "mipsisa32r6el-unknown-linux-gnu",
                Os::Linux,
                Arch::Mipsisa32r6el,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new(
                "mipsisa64r6-unknown-linux-gnuabi64",
                Os::Linux,
                Arch::Mipsisa64r6,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new(
                "mipsisa64r6el-unknown-linux-gnuabi64",
                Os::Linux,
                Arch::Mipsisa64r6el,
            )
            .with_libc(Libc::Gnu),
            TargetConfig::new("riscv32gc-unknown-linux-gnu", Os::Linux, Arch::Riscv32)
                .with_libc(Libc::Gnu),
            // Windows GNU targets
            TargetConfig::new("i686-pc-windows-gnu", Os::Windows, Arch::I686).with_libc(Libc::Gnu),
            TargetConfig::new("x86_64-pc-windows-gnu", Os::Windows, Arch::X86_64)
                .with_libc(Libc::Gnu),
            // FreeBSD targets
            TargetConfig::new("x86_64-unknown-freebsd", Os::FreeBsd, Arch::X86_64),
            TargetConfig::new("aarch64-unknown-freebsd", Os::FreeBsd, Arch::Aarch64),
            TargetConfig::new("powerpc64-unknown-freebsd", Os::FreeBsd, Arch::Powerpc64),
            TargetConfig::new(
                "powerpc64le-unknown-freebsd",
                Os::FreeBsd,
                Arch::Powerpc64le,
            ),
            TargetConfig::new("riscv64gc-unknown-freebsd", Os::FreeBsd, Arch::Riscv64),
            // NetBSD targets
            TargetConfig::new("x86_64-unknown-netbsd", Os::NetBsd, Arch::X86_64),
            // Darwin (macOS) targets
            TargetConfig::new("x86_64-apple-darwin", Os::Darwin, Arch::X86_64),
            TargetConfig::new("x86_64h-apple-darwin", Os::Darwin, Arch::X86_64h),
            TargetConfig::new("aarch64-apple-darwin", Os::Darwin, Arch::Aarch64),
            TargetConfig::new("arm64e-apple-darwin", Os::Darwin, Arch::Arm64e),
            // iOS targets
            TargetConfig::new("x86_64-apple-ios", Os::Ios, Arch::X86_64),
            TargetConfig::new("aarch64-apple-ios", Os::Ios, Arch::Aarch64),
            TargetConfig::new("aarch64-apple-ios-sim", Os::IosSim, Arch::Aarch64),
            // Android targets
            TargetConfig::new("aarch64-linux-android", Os::Android, Arch::Aarch64),
            TargetConfig::new("arm-linux-androideabi", Os::Android, Arch::Armv7),
            TargetConfig::new("armv7-linux-androideabi", Os::Android, Arch::Armv7),
            TargetConfig::new("i686-linux-android", Os::Android, Arch::I686),
            TargetConfig::new("riscv64-linux-android", Os::Android, Arch::Riscv64),
            TargetConfig::new("x86_64-linux-android", Os::Android, Arch::X86_64),
        ];

        configs.into_iter().map(|c| (c.target, c)).collect()
    });

/// Get target configuration by name
pub fn get_target_config(target: &str) -> Option<&'static TargetConfig> {
    TARGETS.get(target)
}

/// Get all supported targets
pub fn all_targets() -> impl Iterator<Item = &'static str> {
    TARGETS.keys().copied()
}

/// Expand target patterns
///
/// Supports:
/// - `all` - all targets
/// - Glob patterns (using globset):
///   - `*` - matches any sequence of characters
///   - `?` - matches any single character
///   - `[abc]` - matches any character in the set
///   - `[a-z]` - matches any character in the range
///   - `[!abc]` - matches any character NOT in the set
///   - `{a,b,c}` - matches any of the alternatives
///   - Examples: `*-linux-musl`, `aarch64-*`, `{x86_64,aarch64}-*-linux-*`
/// - Regex patterns (prefix with `~`): `~.*linux.*(musl|gnu)`, `~^x86_64-.*`
/// - Direct target name: `x86_64-unknown-linux-gnu`
pub fn expand_targets(pattern: &str) -> Vec<&'static str> {
    let pattern = pattern.trim();

    let mut targets: Vec<&'static str> = if pattern == "all" {
        TARGETS.keys().copied().collect()
    } else if let Some(regex_pattern) = pattern.strip_prefix('~') {
        // Regex mode: prefix with ~
        regex_lite::Regex::new(regex_pattern).map_or_else(
            |_| vec![],
            |re| TARGETS.keys().copied().filter(|t| re.is_match(t)).collect(),
        )
    } else if pattern.contains('*')
        || pattern.contains('?')
        || pattern.contains('[')
        || pattern.contains('{')
    {
        // Use globset for glob pattern matching
        globset::Glob::new(pattern).map_or_else(
            |_| vec![],
            |glob| {
                let matcher = glob.compile_matcher();
                TARGETS
                    .keys()
                    .copied()
                    .filter(|t| matcher.is_match(t))
                    .collect()
            },
        )
    } else {
        // Direct target name - lookup to get the static reference
        TARGETS
            .get(pattern)
            .map_or_else(std::vec::Vec::new, |config| vec![config.target])
    };

    // Sort targets for consistent output
    targets.sort_unstable();
    targets
}

/// Host platform information
#[derive(Debug, Clone)]
pub struct HostPlatform {
    pub os: &'static str,
    pub arch: &'static str,
    pub triple: String,
}

impl HostPlatform {
    /// Detect current host platform
    #[must_use]
    pub fn detect() -> Self {
        let os = if cfg!(target_os = "linux") {
            "linux"
        } else if cfg!(target_os = "macos") {
            "darwin"
        } else if cfg!(target_os = "windows") {
            "windows"
        } else if cfg!(target_os = "freebsd") {
            "freebsd"
        } else if cfg!(target_os = "netbsd") {
            "netbsd"
        } else {
            "unknown"
        };

        let arch = if cfg!(target_arch = "x86_64") {
            "x86_64"
        } else if cfg!(target_arch = "aarch64") {
            "aarch64"
        } else if cfg!(target_arch = "arm") {
            "armv7"
        } else if cfg!(target_arch = "x86") {
            "i686"
        } else if cfg!(target_arch = "s390x") {
            "s390x"
        } else if cfg!(target_arch = "riscv64") {
            "riscv64"
        } else if cfg!(target_arch = "loongarch64") {
            "loongarch64"
        } else if cfg!(all(target_arch = "powerpc64", target_endian = "big")) {
            "powerpc64"
        } else if cfg!(all(target_arch = "powerpc64", target_endian = "little")) {
            "powerpc64le"
        } else if cfg!(all(target_arch = "mips64", target_endian = "big")) {
            "mips64"
        } else if cfg!(all(target_arch = "mips64", target_endian = "little")) {
            "mips64el"
        } else {
            "unknown"
        };

        // Get the host triple from rustc
        let triple = std::process::Command::new("rustc")
            .args(["-vV"])
            .output()
            .ok()
            .and_then(|output| {
                String::from_utf8(output.stdout).ok().and_then(|s| {
                    s.lines()
                        .find(|line| line.starts_with("host:"))
                        .map(|line| line.trim_start_matches("host:").trim().to_string())
                })
            })
            .unwrap_or_else(|| format!("{arch}-unknown-{os}"));

        Self { os, arch, triple }
    }

    /// Get platform string for downloads (e.g., "linux-x86_64")
    #[must_use]
    pub fn download_platform(&self) -> String {
        format!("{}-{}", self.os, self.arch)
    }

    /// Check if host can natively run the target architecture
    #[must_use]
    pub fn can_run_natively(&self, target_arch: Arch) -> bool {
        match self.arch {
            "x86_64" => matches!(target_arch, Arch::X86_64 | Arch::I686 | Arch::I586),
            "aarch64" => matches!(
                target_arch,
                Arch::Aarch64 | Arch::Armv5 | Arch::Armv6 | Arch::Armv7
            ),
            "i686" | "i586" => matches!(target_arch, Arch::I686 | Arch::I586),
            _ => self.arch == target_arch.as_str(),
        }
    }

    /// Check if running on Windows
    #[must_use]
    pub fn is_windows(&self) -> bool {
        self.os == "windows"
    }

    /// Check if running on macOS/Darwin
    #[must_use]
    pub fn is_darwin(&self) -> bool {
        self.os == "darwin"
    }

    /// Check if running on Linux
    #[must_use]
    pub fn is_linux(&self) -> bool {
        self.os == "linux"
    }

    /// Get PATH separator for this platform
    #[must_use]
    pub fn path_separator(&self) -> &'static str {
        if self.is_windows() {
            ";"
        } else {
            ":"
        }
    }
}

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

    #[test]
    fn test_target_lookup() {
        assert!(get_target_config("x86_64-unknown-linux-musl").is_some());
        assert!(get_target_config("invalid-target").is_none());
    }

    #[test]
    fn test_expand_all() {
        let targets = expand_targets("all");
        assert!(targets.len() > 50);
    }

    #[test]
    fn test_expand_pattern() {
        let targets = expand_targets("*-linux-musl");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.ends_with("-linux-musl") || target.contains("-linux-musl"));
        }
    }

    #[test]
    fn test_expand_direct() {
        let targets = expand_targets("x86_64-unknown-linux-gnu");
        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0], "x86_64-unknown-linux-gnu");
    }

    #[test]
    fn test_glibc_versions() {
        assert!(SUPPORTED_GLIBC_VERSIONS.contains(&"2.28"));
        assert!(SUPPORTED_GLIBC_VERSIONS.contains(&"2.31"));
    }

    #[test]
    fn test_os_as_str() {
        assert_eq!(Os::Linux.as_str(), "linux");
        assert_eq!(Os::Windows.as_str(), "windows");
        assert_eq!(Os::Darwin.as_str(), "darwin");
        assert_eq!(Os::FreeBsd.as_str(), "freebsd");
        assert_eq!(Os::NetBsd.as_str(), "netbsd");
        assert_eq!(Os::Ios.as_str(), "ios");
        assert_eq!(Os::Android.as_str(), "android");
    }

    #[test]
    fn test_arch_as_str() {
        assert_eq!(Arch::X86_64.as_str(), "x86_64");
        assert_eq!(Arch::Aarch64.as_str(), "aarch64");
        assert_eq!(Arch::Armv7.as_str(), "armv7");
        assert_eq!(Arch::Riscv64.as_str(), "riscv64");
    }

    #[test]
    fn test_arch_qemu_binary_name() {
        assert_eq!(Arch::Aarch64.qemu_binary_name(), Some("qemu-aarch64"));
        assert_eq!(Arch::X86_64.qemu_binary_name(), Some("qemu-x86_64"));
        assert_eq!(Arch::Armv7.qemu_binary_name(), Some("qemu-arm"));
        assert_eq!(Arch::Riscv64.qemu_binary_name(), Some("qemu-riscv64"));
    }

    #[test]
    fn test_libc_as_str() {
        assert_eq!(Libc::Musl.as_str(), "musl");
        assert_eq!(Libc::Gnu.as_str(), "gnu");
    }

    #[test]
    fn test_abi_as_str() {
        assert_eq!(Abi::Eabi.as_str(), "eabi");
        assert_eq!(Abi::Eabihf.as_str(), "eabihf");
        assert_eq!(Abi::X32.as_str(), "x32");
        assert_eq!(Abi::Gnusf.as_str(), "gnusf");
        assert_eq!(Abi::Gnuspe.as_str(), "gnuspe");
    }

    #[test]
    fn test_expand_invalid_target() {
        let targets = expand_targets("totally-invalid-target-name");
        assert!(targets.is_empty());
    }

    #[test]
    fn test_expand_freebsd_pattern() {
        let targets = expand_targets("*-freebsd");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.ends_with("-freebsd"));
        }
    }

    #[test]
    fn test_expand_netbsd_pattern() {
        let targets = expand_targets("*-netbsd");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.ends_with("-netbsd"));
        }
    }

    #[test]
    fn test_expand_darwin_pattern() {
        let targets = expand_targets("*-apple-darwin");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.ends_with("-apple-darwin"));
        }
    }

    #[test]
    fn test_expand_regex_pattern() {
        // Regex mode with ~ prefix
        let targets = expand_targets("~.*linux.*(musl|gnu)");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.contains("linux"));
            assert!(target.contains("musl") || target.contains("gnu"));
        }
    }

    #[test]
    fn test_expand_regex_anchored() {
        // Anchored regex
        let targets = expand_targets("~^x86_64-.*-linux-musl$");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.starts_with("x86_64-"));
            assert!(target.ends_with("-linux-musl"));
        }
    }

    #[test]
    fn test_expand_glob_question_mark() {
        // ? matches single character
        let targets = expand_targets("x86_64-unknown-linux-???");
        assert!(targets.contains(&"x86_64-unknown-linux-gnu"));
        // Test with architecture - ?686 matches i686
        let targets2 = expand_targets("?686-unknown-linux-gnu");
        assert!(targets2.contains(&"i686-unknown-linux-gnu"));
    }

    #[test]
    fn test_expand_glob_brackets() {
        // [abc] character class
        let targets = expand_targets("[xi]86_64-unknown-linux-gnu");
        assert!(targets.contains(&"x86_64-unknown-linux-gnu"));
    }

    #[test]
    fn test_expand_glob_braces() {
        // {a,b} alternatives
        let targets = expand_targets("{x86_64,aarch64}-unknown-linux-gnu");
        assert!(targets.contains(&"x86_64-unknown-linux-gnu"));
        assert!(targets.contains(&"aarch64-unknown-linux-gnu"));
        assert_eq!(targets.len(), 2);
    }

    #[test]
    fn test_expand_glob_braces_with_wildcard() {
        // Combine braces with wildcards
        let targets = expand_targets("{x86_64,aarch64}-*-linux-musl");
        assert!(!targets.is_empty());
        for target in &targets {
            assert!(target.starts_with("x86_64-") || target.starts_with("aarch64-"));
            assert!(target.ends_with("-linux-musl"));
        }
    }

    #[test]
    fn test_all_targets_count() {
        let count = all_targets().count();
        assert!(count > 50, "Expected more than 50 targets, got {count}");
    }

    #[test]
    fn test_target_config_linux_musl() {
        let config = get_target_config("aarch64-unknown-linux-musl").unwrap();
        assert_eq!(config.os, Os::Linux);
        assert_eq!(config.arch, Arch::Aarch64);
        assert_eq!(config.libc, Some(Libc::Musl));
    }

    #[test]
    fn test_target_config_windows() {
        let config = get_target_config("x86_64-pc-windows-gnu").unwrap();
        assert_eq!(config.os, Os::Windows);
        assert_eq!(config.arch, Arch::X86_64);
    }

    #[test]
    fn test_target_config_darwin() {
        let config = get_target_config("aarch64-apple-darwin").unwrap();
        assert_eq!(config.os, Os::Darwin);
        assert_eq!(config.arch, Arch::Aarch64);
    }

    #[test]
    fn test_target_config_android() {
        let config = get_target_config("aarch64-linux-android").unwrap();
        assert_eq!(config.os, Os::Android);
        assert_eq!(config.arch, Arch::Aarch64);
    }

    #[test]
    fn test_x32_targets() {
        // Test x32 gnu target
        let config = get_target_config("x86_64-unknown-linux-gnux32").unwrap();
        assert_eq!(config.os, Os::Linux);
        assert_eq!(config.arch, Arch::X86_64);
        assert_eq!(config.libc, Some(Libc::Gnu));
        assert_eq!(config.abi, Some(Abi::X32));

        // x32 musl is not supported
        let config = get_target_config("x86_64-unknown-linux-muslx32");
        assert!(config.is_none());
    }
}