alef 0.36.2

Opinionated polyglot binding generator for Rust libraries
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
//! Rust target triple parsing and per-language platform name mapping.

use crate::core::config::extras::Language;
use anyhow::{Result, bail};
use std::fmt;

/// CPU architecture.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Arch {
    X86_64,
    Aarch64,
    Arm,
    Wasm32,
}

impl fmt::Display for Arch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Arch::X86_64 => write!(f, "x86_64"),
            Arch::Aarch64 => write!(f, "aarch64"),
            Arch::Arm => write!(f, "arm"),
            Arch::Wasm32 => write!(f, "wasm32"),
        }
    }
}

/// Operating system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Os {
    Linux,
    MacOs,
    Windows,
    Unknown,
}

impl fmt::Display for Os {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Os::Linux => write!(f, "linux"),
            Os::MacOs => write!(f, "macos"),
            Os::Windows => write!(f, "windows"),
            Os::Unknown => write!(f, "unknown"),
        }
    }
}

/// C runtime / ABI environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Env {
    Gnu,
    Musl,
    Msvc,
    GnuEabihf,
    None,
}

/// A parsed Rust target triple with helpers for per-language platform naming.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RustTarget {
    /// The original Rust target triple (e.g. `x86_64-unknown-linux-gnu`).
    pub triple: String,
    pub arch: Arch,
    pub os: Os,
    pub env: Env,
}

impl RustTarget {
    /// Parse a Rust target triple string.
    pub fn parse(triple: &str) -> Result<Self> {
        let parts: Vec<&str> = triple.split('-').collect();
        if parts.len() < 2 {
            bail!("invalid target triple: {triple}");
        }

        let arch = match parts[0] {
            "x86_64" => Arch::X86_64,
            "aarch64" => Arch::Aarch64,
            "arm" | "armv7" => Arch::Arm,
            "wasm32" => Arch::Wasm32,
            other => bail!("unsupported architecture: {other}"),
        };

        let os = if triple.contains("linux") {
            Os::Linux
        } else if triple.contains("apple") || triple.contains("darwin") {
            Os::MacOs
        } else if triple.contains("windows") || triple.contains("pc-windows") {
            Os::Windows
        } else if triple.contains("wasm") {
            Os::Unknown
        } else {
            bail!("unsupported OS in target triple: {triple}");
        };

        let env = if triple.contains("gnueabihf") {
            Env::GnuEabihf
        } else if triple.contains("musl") {
            Env::Musl
        } else if triple.contains("gnu") {
            Env::Gnu
        } else if triple.contains("msvc") {
            Env::Msvc
        } else {
            Env::None
        };

        Ok(Self {
            triple: triple.to_string(),
            arch,
            os,
            env,
        })
    }

    /// Return the platform string for a given language.
    ///
    /// Each language ecosystem uses different platform naming conventions.
    /// This method maps the Rust target triple to the correct convention.
    pub fn platform_for(&self, lang: Language) -> String {
        match lang {
            Language::Go | Language::Java => self.go_java_platform(),
            Language::Zig => self.triple.clone(),
            Language::Csharp => self.csharp_rid(),
            Language::Node => self.node_platform(),
            Language::Ruby => self.ruby_platform(),
            Language::Elixir | Language::Ffi | Language::Rust => self.triple.clone(),
            Language::Python => self.python_platform(),
            Language::Php => self.go_java_platform(),
            Language::Wasm => "wasm32".to_string(),
            Language::R => self.triple.clone(),
            Language::Kotlin
            | Language::KotlinAndroid
            | Language::Swift
            | Language::Dart
            | Language::Gleam
            | Language::C
            | Language::Jni => self.triple.clone(),
        }
    }

    /// Go / Java / PHP platform label (e.g. `linux-x86_64`, `macos-arm64`).
    fn go_java_platform(&self) -> String {
        let os = match self.os {
            Os::Linux => "linux",
            Os::MacOs => "macos",
            Os::Windows => "windows",
            Os::Unknown => "unknown",
        };
        let arch = match (self.os, self.arch) {
            (Os::MacOs, Arch::Aarch64) => "arm64",
            (_, Arch::X86_64) => "x86_64",
            (_, Arch::Aarch64) => "aarch64",
            (_, Arch::Arm) => "arm",
            (_, Arch::Wasm32) => "wasm32",
        };
        let suffix = match self.env {
            Env::Musl => "-musl",
            _ => "",
        };
        format!("{os}-{arch}{suffix}")
    }

    /// C# Runtime Identifier (e.g. `linux-x64`, `osx-arm64`, `win-x64`).
    fn csharp_rid(&self) -> String {
        let os = match self.os {
            Os::Linux => "linux",
            Os::MacOs => "osx",
            Os::Windows => "win",
            Os::Unknown => "unknown",
        };
        let arch = match self.arch {
            Arch::X86_64 => "x64",
            Arch::Aarch64 => "arm64",
            Arch::Arm => "arm",
            Arch::Wasm32 => "wasm32",
        };
        let suffix = match self.env {
            Env::Musl if self.os == Os::Linux => format!("-musl-{arch}"),
            _ => format!("-{arch}"),
        };
        format!("{os}{suffix}")
    }

    /// Node / npm platform label (e.g. `linux-x64-gnu`, `darwin-arm64`, `win32-x64-msvc`).
    fn node_platform(&self) -> String {
        let os = match self.os {
            Os::Linux => "linux",
            Os::MacOs => "darwin",
            Os::Windows => "win32",
            Os::Unknown => "unknown",
        };
        let arch = match self.arch {
            Arch::X86_64 => "x64",
            Arch::Aarch64 => "arm64",
            Arch::Arm => "arm",
            Arch::Wasm32 => "wasm32",
        };
        let env = match self.env {
            Env::Gnu => "-gnu",
            Env::Musl => "-musl",
            Env::Msvc => "-msvc",
            Env::GnuEabihf => "-gnueabihf",
            Env::None => "",
        };
        format!("{os}-{arch}{env}")
    }

    /// Ruby platform label (e.g. `x86_64-linux`, `arm64-darwin`).
    fn ruby_platform(&self) -> String {
        let arch = match self.arch {
            Arch::X86_64 => "x86_64",
            Arch::Aarch64 => "aarch64",
            Arch::Arm => "arm",
            Arch::Wasm32 => "wasm32",
        };
        let os = match self.os {
            Os::Linux => "linux",
            Os::MacOs => "darwin",
            Os::Windows => "mingw-ucrt",
            Os::Unknown => "unknown",
        };
        let arch_display = if self.arch == Arch::Aarch64 && self.os == Os::MacOs {
            "arm64"
        } else {
            arch
        };
        let suffix = match self.env {
            Env::Musl if self.os == Os::Linux => "-musl",
            _ => "",
        };
        format!("{arch_display}-{os}{suffix}")
    }

    /// Python platform tag fragment (e.g. `linux-x86_64`, `macos-arm64`).
    fn python_platform(&self) -> String {
        self.go_java_platform()
    }

    /// Return the shared library filename for an FFI crate on this target.
    pub fn shared_lib_name(&self, lib_name: &str) -> String {
        match self.os {
            Os::Linux => format!("lib{lib_name}.so"),
            Os::MacOs => format!("lib{lib_name}.dylib"),
            Os::Windows => format!("{lib_name}.dll"),
            Os::Unknown => format!("lib{lib_name}.so"),
        }
    }

    /// Return the static library filename for this target.
    pub fn static_lib_name(&self, lib_name: &str) -> String {
        match self.os {
            Os::Windows => format!("{lib_name}.lib"),
            _ => format!("lib{lib_name}.a"),
        }
    }

    /// Return the appropriate archive extension for this target.
    pub fn archive_ext(&self) -> &str {
        match self.os {
            Os::Windows => "zip",
            _ => "tar.gz",
        }
    }

    /// Return the binary extension for this target.
    pub fn binary_ext(&self) -> &str {
        match self.os {
            Os::Windows => ".exe",
            _ => "",
        }
    }

    /// PIE OS family string: `"linux"` | `"darwin"` | `"windows"`.
    ///
    /// Returns an error for `Os::Unknown` (e.g. wasm32 targets).
    pub fn pie_os_family(&self) -> Result<&'static str> {
        match self.os {
            Os::Linux => Ok("linux"),
            Os::MacOs => Ok("darwin"),
            Os::Windows => Ok("windows"),
            Os::Unknown => bail!("unsupported OS for PIE packaging: {}", self.triple),
        }
    }

    /// PIE architecture string: `"x86_64"` | `"arm64"` | `"x86"`.
    ///
    /// Maps `Aarch64` → `"arm64"` on all platforms (unlike `go_java_platform`
    /// which uses `"aarch64"` on Linux). Returns an error for wasm32.
    pub fn pie_arch(&self) -> Result<&'static str> {
        match self.arch {
            Arch::X86_64 => Ok("x86_64"),
            Arch::Aarch64 => Ok("arm64"),
            Arch::Arm => bail!("arm32 is not supported by PIE; PIE supports x86, x86_64, arm64"),
            Arch::Wasm32 => bail!("wasm32 is not supported for PIE packaging"),
        }
    }

    /// PIE libc string: `"glibc"` (Linux+Gnu) | `"musl"` (Linux+Musl) | `"bsdlibc"` (macOS).
    ///
    /// Returns an error on Windows (use the Windows filename scheme instead) or unknown OS.
    pub fn pie_libc(&self) -> Result<&'static str> {
        match self.os {
            Os::Linux => match self.env {
                Env::Musl => Ok("musl"),
                _ => Ok("glibc"),
            },
            Os::MacOs => Ok("bsdlibc"),
            Os::Windows => bail!("pie_libc is not applicable on Windows; use the Windows filename scheme"),
            Os::Unknown => bail!("unsupported OS for PIE libc: {}", self.triple),
        }
    }

    /// The canonical `[targets]` opt-out key for this triple, if it belongs to a
    /// toggleable target family.
    ///
    /// Windows matches by `(arch, os)` regardless of ABI, so `windows_x64` covers
    /// the msvc / gnu / mingw variants alike; Linux distinguishes musl from glibc.
    /// Returns `None` for `arm` / `wasm32` / unknown targets, which are never
    /// filtered by the toggle.
    pub fn canonical_target_key(&self) -> Option<&'static str> {
        match (self.arch, self.os, self.env) {
            (Arch::X86_64, Os::MacOs, _) => Some("mac_intel"),
            (Arch::Aarch64, Os::MacOs, _) => Some("mac_arm"),
            (Arch::X86_64, Os::Linux, Env::Musl) => Some("linux_x64_musl"),
            (Arch::Aarch64, Os::Linux, Env::Musl) => Some("linux_arm64_musl"),
            (Arch::X86_64, Os::Linux, _) => Some("linux_x64"),
            (Arch::Aarch64, Os::Linux, _) => Some("linux_arm64"),
            (Arch::X86_64, Os::Windows, _) => Some("windows_x64"),
            (Arch::Aarch64, Os::Windows, _) => Some("windows_arm64"),
            _ => None,
        }
    }
}

/// Canonical friendly keys accepted in the workspace `[targets]` opt-out table.
///
/// Each key selects a family of build targets by `(arch, os, libc)`; setting a
/// key to `false` drops every matching triple from every language's generated
/// target list (napi platforms, nif targets, C# RIDs, ruby cross-platforms,
/// dart native RIDs). Keys default to enabled, so an absent or empty table
/// leaves generated output byte-identical.
pub const CANONICAL_TARGET_KEYS: &[&str] = &[
    "linux_x64",
    "linux_arm64",
    "linux_x64_musl",
    "linux_arm64_musl",
    "mac_intel",
    "mac_arm",
    "windows_x64",
    "windows_arm64",
];

/// Whether a target triple is enabled given the resolved `[targets]` toggle map.
///
/// A triple is disabled only when its [`RustTarget::canonical_target_key`] is
/// present in `toggles` with an explicit `false`. Triples with no canonical key
/// (arm/wasm32) and keys absent from the map are always enabled.
pub fn target_triple_enabled(toggles: &std::collections::BTreeMap<String, bool>, triple: &str) -> bool {
    if toggles.is_empty() {
        return true;
    }
    match RustTarget::parse(triple).ok().and_then(|t| t.canonical_target_key()) {
        Some(key) => *toggles.get(key).unwrap_or(&true),
        None => true,
    }
}

impl fmt::Display for RustTarget {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.triple)
    }
}

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

    #[test]
    fn parse_linux_gnu() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.arch, Arch::X86_64);
        assert_eq!(t.os, Os::Linux);
        assert_eq!(t.env, Env::Gnu);
    }

    #[test]
    fn parse_darwin() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.arch, Arch::Aarch64);
        assert_eq!(t.os, Os::MacOs);
        assert_eq!(t.env, Env::None);
    }

    #[test]
    fn parse_windows_msvc() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.arch, Arch::X86_64);
        assert_eq!(t.os, Os::Windows);
        assert_eq!(t.env, Env::Msvc);
    }

    #[test]
    fn parse_musl() {
        let t = RustTarget::parse("x86_64-unknown-linux-musl").unwrap();
        assert_eq!(t.arch, Arch::X86_64);
        assert_eq!(t.os, Os::Linux);
        assert_eq!(t.env, Env::Musl);
    }

    #[test]
    fn parse_arm_gnueabihf() {
        let t = RustTarget::parse("arm-unknown-linux-gnueabihf").unwrap();
        assert_eq!(t.arch, Arch::Arm);
        assert_eq!(t.os, Os::Linux);
        assert_eq!(t.env, Env::GnuEabihf);
    }

    #[test]
    fn parse_wasm() {
        let t = RustTarget::parse("wasm32-unknown-unknown").unwrap();
        assert_eq!(t.arch, Arch::Wasm32);
        assert_eq!(t.os, Os::Unknown);
    }

    #[test]
    fn parse_invalid() {
        assert!(RustTarget::parse("invalid").is_err());
    }

    #[test]
    fn go_java_platform_linux_x86() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Go), "linux-x86_64");
        assert_eq!(t.platform_for(Language::Java), "linux-x86_64");
    }

    #[test]
    fn go_java_platform_macos_arm64() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.platform_for(Language::Go), "macos-arm64");
        assert_eq!(t.platform_for(Language::Java), "macos-arm64");
    }

    #[test]
    fn go_java_platform_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.platform_for(Language::Go), "windows-x86_64");
    }

    #[test]
    fn go_java_platform_linux_aarch64() {
        let t = RustTarget::parse("aarch64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Go), "linux-aarch64");
        assert_eq!(t.platform_for(Language::Java), "linux-aarch64");
    }

    #[test]
    fn csharp_rid_linux_x64() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Csharp), "linux-x64");
    }

    #[test]
    fn csharp_rid_osx_arm64() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.platform_for(Language::Csharp), "osx-arm64");
    }

    #[test]
    fn csharp_rid_win_x64() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.platform_for(Language::Csharp), "win-x64");
    }

    #[test]
    fn node_platform_linux_x64_gnu() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Node), "linux-x64-gnu");
    }

    #[test]
    fn node_platform_darwin_arm64() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.platform_for(Language::Node), "darwin-arm64");
    }

    #[test]
    fn node_platform_win32_x64_msvc() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.platform_for(Language::Node), "win32-x64-msvc");
    }

    #[test]
    fn node_platform_linux_musl() {
        let t = RustTarget::parse("x86_64-unknown-linux-musl").unwrap();
        assert_eq!(t.platform_for(Language::Node), "linux-x64-musl");
    }

    #[test]
    fn ruby_platform_x86_64_linux() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Ruby), "x86_64-linux");
    }

    #[test]
    fn ruby_platform_arm64_darwin() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.platform_for(Language::Ruby), "arm64-darwin");
    }

    #[test]
    fn ruby_platform_aarch64_linux() {
        let t = RustTarget::parse("aarch64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Ruby), "aarch64-linux");
    }

    #[test]
    fn elixir_uses_rust_triple() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.platform_for(Language::Elixir), "x86_64-unknown-linux-gnu");
    }

    #[test]
    fn ffi_uses_rust_triple() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.platform_for(Language::Ffi), "aarch64-apple-darwin");
    }

    #[test]
    fn shared_lib_linux() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.shared_lib_name("demo_markup_ffi"), "libdemo_markup_ffi.so");
    }

    #[test]
    fn shared_lib_macos() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.shared_lib_name("demo_markup_ffi"), "libdemo_markup_ffi.dylib");
    }

    #[test]
    fn shared_lib_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.shared_lib_name("demo_markup_ffi"), "demo_markup_ffi.dll");
    }

    #[test]
    fn static_lib_unix() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.static_lib_name("demo_markup_ffi"), "libdemo_markup_ffi.a");
    }

    #[test]
    fn static_lib_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.static_lib_name("demo_markup_ffi"), "demo_markup_ffi.lib");
    }

    #[test]
    fn archive_ext_unix() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.archive_ext(), "tar.gz");
    }

    #[test]
    fn archive_ext_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.archive_ext(), "zip");
    }

    #[test]
    fn binary_ext_unix() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.binary_ext(), "");
    }

    #[test]
    fn binary_ext_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.binary_ext(), ".exe");
    }

    #[test]
    fn pie_os_family_linux() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.pie_os_family().unwrap(), "linux");
    }

    #[test]
    fn pie_os_family_darwin() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.pie_os_family().unwrap(), "darwin");
    }

    #[test]
    fn pie_os_family_windows() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert_eq!(t.pie_os_family().unwrap(), "windows");
    }

    #[test]
    fn pie_arch_x86_64() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.pie_arch().unwrap(), "x86_64");
    }

    #[test]
    fn pie_arch_aarch64_linux_maps_to_arm64() {
        let t = RustTarget::parse("aarch64-unknown-linux-gnu").unwrap();
        assert_eq!(t.pie_arch().unwrap(), "arm64");
    }

    #[test]
    fn pie_arch_aarch64_darwin_maps_to_arm64() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.pie_arch().unwrap(), "arm64");
    }

    #[test]
    fn pie_arch_arm32_errors() {
        let t = RustTarget::parse("armv7-unknown-linux-gnueabihf").unwrap();
        assert!(t.pie_arch().is_err());
    }

    #[test]
    fn pie_libc_linux_gnu_is_glibc() {
        let t = RustTarget::parse("x86_64-unknown-linux-gnu").unwrap();
        assert_eq!(t.pie_libc().unwrap(), "glibc");
    }

    #[test]
    fn pie_libc_linux_musl_is_musl() {
        let t = RustTarget::parse("x86_64-unknown-linux-musl").unwrap();
        assert_eq!(t.pie_libc().unwrap(), "musl");
    }

    #[test]
    fn pie_libc_darwin_is_bsdlibc() {
        let t = RustTarget::parse("aarch64-apple-darwin").unwrap();
        assert_eq!(t.pie_libc().unwrap(), "bsdlibc");
    }

    #[test]
    fn pie_libc_windows_errors() {
        let t = RustTarget::parse("x86_64-pc-windows-msvc").unwrap();
        assert!(t.pie_libc().is_err());
    }

    fn key_of(triple: &str) -> Option<&'static str> {
        RustTarget::parse(triple).unwrap().canonical_target_key()
    }

    #[test]
    fn canonical_target_key_maps_each_family() {
        assert_eq!(key_of("x86_64-apple-darwin"), Some("mac_intel"));
        assert_eq!(key_of("aarch64-apple-darwin"), Some("mac_arm"));
        assert_eq!(key_of("x86_64-unknown-linux-gnu"), Some("linux_x64"));
        assert_eq!(key_of("aarch64-unknown-linux-gnu"), Some("linux_arm64"));
        assert_eq!(key_of("x86_64-unknown-linux-musl"), Some("linux_x64_musl"));
        assert_eq!(key_of("aarch64-unknown-linux-musl"), Some("linux_arm64_musl"));
    }

    #[test]
    fn canonical_target_key_windows_ignores_abi() {
        // msvc, gnu, and mingw windows-x64 all fold under one key.
        assert_eq!(key_of("x86_64-pc-windows-msvc"), Some("windows_x64"));
        assert_eq!(key_of("x86_64-pc-windows-gnu"), Some("windows_x64"));
        assert_eq!(key_of("aarch64-pc-windows-msvc"), Some("windows_arm64"));
    }

    #[test]
    fn canonical_target_key_none_for_untoggleable() {
        assert_eq!(key_of("arm-unknown-linux-gnueabihf"), None);
        assert_eq!(key_of("wasm32-unknown-unknown"), None);
    }

    #[test]
    fn every_canonical_key_is_reachable() {
        // Guard against a key in CANONICAL_TARGET_KEYS that no triple maps to.
        let reachable: std::collections::BTreeSet<&str> = [
            "x86_64-apple-darwin",
            "aarch64-apple-darwin",
            "x86_64-unknown-linux-gnu",
            "aarch64-unknown-linux-gnu",
            "x86_64-unknown-linux-musl",
            "aarch64-unknown-linux-musl",
            "x86_64-pc-windows-msvc",
            "aarch64-pc-windows-msvc",
        ]
        .iter()
        .filter_map(|t| key_of(t))
        .collect();
        for key in CANONICAL_TARGET_KEYS {
            assert!(reachable.contains(key), "no triple maps to canonical key `{key}`");
        }
    }

    #[test]
    fn target_triple_enabled_empty_map_is_all_on() {
        let toggles = std::collections::BTreeMap::new();
        assert!(target_triple_enabled(&toggles, "x86_64-apple-darwin"));
        assert!(target_triple_enabled(&toggles, "aarch64-unknown-linux-gnu"));
    }

    #[test]
    fn target_triple_enabled_disables_only_matching_family() {
        let mut toggles = std::collections::BTreeMap::new();
        toggles.insert("mac_intel".to_string(), false);
        assert!(!target_triple_enabled(&toggles, "x86_64-apple-darwin"));
        // Sibling families stay enabled.
        assert!(target_triple_enabled(&toggles, "aarch64-apple-darwin"));
        assert!(target_triple_enabled(&toggles, "x86_64-unknown-linux-gnu"));
        // Untoggleable triples stay enabled.
        assert!(target_triple_enabled(&toggles, "wasm32-unknown-unknown"));
    }

    #[test]
    fn target_triple_enabled_windows_toggle_covers_all_abis() {
        let mut toggles = std::collections::BTreeMap::new();
        toggles.insert("windows_x64".to_string(), false);
        assert!(!target_triple_enabled(&toggles, "x86_64-pc-windows-msvc"));
        assert!(!target_triple_enabled(&toggles, "x86_64-pc-windows-gnu"));
    }

    #[test]
    fn target_triple_enabled_explicit_true_is_on() {
        let mut toggles = std::collections::BTreeMap::new();
        toggles.insert("mac_intel".to_string(), true);
        assert!(target_triple_enabled(&toggles, "x86_64-apple-darwin"));
    }
}