crossbuild-core 1.0.0

Core types, models, and traits for the cargo-crossbuild ecosystem
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
//! Host and target platform detection, capability assessment, and toolchain routing.

use std::collections::BTreeMap;

use crate::model::{
    Abi, HostDetectError, HostInfo, LinkerHint, OperatingSystem, SysrootHint,
    TargetFamily, TargetInfo, TargetSupport, TargetTriple, ToolchainHint,
};

/// Detects the host platform information.
pub fn detect_host() -> Result<HostInfo, HostDetectError> {
    HostInfo::detect()
}

/// Assesses target support tier and provides toolchain hints.
pub fn assess_target(target: &TargetTriple, host: &HostInfo) -> TargetInfo {
    let is_native = target.triple == host.host_triple.triple;
    let requires_cross = !is_native;

    let supported = determine_support_tier(target);
    let toolchain_hint = suggest_toolchain_provider(target, host, &supported);
    let sysroot_hint = suggest_sysroot_provider(target, host, &supported);
    let linker_hint = suggest_linker(target, host);

    TargetInfo {
        triple: target.clone(),
        is_native,
        requires_cross,
        supported,
        toolchain_hint,
        sysroot_hint,
        linker_hint,
    }
}

/// Determines the Rust support tier for a target.
fn determine_support_tier(target: &TargetTriple) -> TargetSupport {
    // Tier 1 targets (guaranteed to build and pass tests)
    const TIER1: &[&str] = &[
        "x86_64-unknown-linux-gnu",
        "x86_64-pc-windows-msvc",
        "aarch64-unknown-linux-gnu",
        "aarch64-apple-darwin",
        "x86_64-apple-darwin",
    ];

    // Tier 2 targets (guaranteed to build, tests may not run)
    const TIER2: &[&str] = &[
        "x86_64-unknown-linux-musl",
        "aarch64-unknown-linux-musl",
        "x86_64-unknown-freebsd",
        "aarch64-unknown-freebsd",
        "x86_64-unknown-netbsd",
        "aarch64-unknown-netbsd",
        "x86_64-unknown-openbsd",
        "x86_64-unknown-illumos",
        "powerpc64le-unknown-linux-gnu",
        "s390x-unknown-linux-gnu",
        "riscv64gc-unknown-linux-gnu",
        "x86_64-pc-windows-gnu",
        "i686-pc-windows-msvc",
        "i686-pc-windows-gnu",
        "aarch64-pc-windows-msvc",
        "wasm32-wasi",
        "wasm32-unknown-unknown",
        "wasm32-unknown-emscripten",
    ];

    if TIER1.contains(&target.triple.as_str()) {
        TargetSupport::Tier1
    } else if TIER2.contains(&target.triple.as_str()) {
        TargetSupport::Tier2
    } else if is_known_tier3(target) {
        TargetSupport::Tier3
    } else {
        TargetSupport::Unsupported
    }
}

/// Checks if a target is a known tier 3 target.
fn is_known_tier3(target: &TargetTriple) -> bool {
    let triple = target.triple.as_str();

    // Known tier 3 patterns
    const TIER3_PATTERNS: &[&str] = &[
        "mips",
        "mips64",
        "sparc",
        "sparc64",
        "hexagon",
        "avr",
        "xtensa",
        "csky",
        "loongarch",
        "wasm64",
        "nvptx",
        "spirv",
        "bpf",
    ];

    TIER3_PATTERNS.iter().any(|p| triple.contains(p))
}

/// Suggests the best toolchain provider for a target.
fn suggest_toolchain_provider(
    target: &TargetTriple,
    host: &HostInfo,
    support: &TargetSupport,
) -> ToolchainHint {
    // Native builds use host toolchain
    if target.triple == host.host_triple.triple {
        return ToolchainHint::Rustup;
    }

    match support {
        TargetSupport::Tier1 | TargetSupport::Tier2 => {
            // For tier 1/2, prefer rustup if available
            if rustup_target_available(target) {
                ToolchainHint::Rustup
            } else if target.is_wasm() {
                ToolchainHint::Rustup
            } else if target.is_bare_metal() {
                ToolchainHint::Rustup
            } else {
                ToolchainHint::Zig
            }
        }
        TargetSupport::Tier3 => ToolchainHint::Zig,
        TargetSupport::Unsupported => ToolchainHint::Custom,
    }
}

/// Checks if a target is available via rustup.
pub fn rustup_target_available(target: &TargetTriple) -> bool {
    // Check common rustup targets
    const RUSTUP_TARGETS: &[&str] = &[
        "x86_64-unknown-linux-gnu",
        "x86_64-unknown-linux-musl",
        "aarch64-unknown-linux-gnu",
        "aarch64-unknown-linux-musl",
        "x86_64-pc-windows-msvc",
        "x86_64-pc-windows-gnu",
        "aarch64-pc-windows-msvc",
        "i686-pc-windows-msvc",
        "i686-pc-windows-gnu",
        "x86_64-apple-darwin",
        "aarch64-apple-darwin",
        "wasm32-wasi",
        "wasm32-unknown-unknown",
        "wasm32-unknown-emscripten",
        "x86_64-unknown-freebsd",
        "aarch64-unknown-freebsd",
        "powerpc64le-unknown-linux-gnu",
        "s390x-unknown-linux-gnu",
        "riscv64gc-unknown-linux-gnu",
    ];

    RUSTUP_TARGETS.contains(&target.triple.as_str())
}

/// Suggests the best sysroot provider for a target.
fn suggest_sysroot_provider(
    target: &TargetTriple,
    host: &HostInfo,
    support: &TargetSupport,
) -> SysrootHint {
    if target.triple == host.host_triple.triple {
        return SysrootHint::None;
    }

    match support {
        TargetSupport::Tier1 | TargetSupport::Tier2 => {
            if target.is_wasm() {
                SysrootHint::None // wasm doesn't need sysroot
            } else if rustup_target_available(target) {
                SysrootHint::Rustup
            } else if target.is_bare_metal() {
                SysrootHint::None
            } else {
                SysrootHint::Zig
            }
        }
        TargetSupport::Tier3 => SysrootHint::Zig,
        TargetSupport::Unsupported => SysrootHint::Custom,
    }
}

/// Suggests the best linker for a target.
fn suggest_linker(target: &TargetTriple, host: &HostInfo) -> LinkerHint {
    match (target.family(), target.os.clone(), target.abi.clone()) {
        // Windows targets
        (TargetFamily::Windows, _, Abi::Msvc) => LinkerHint::MSVC,
        (TargetFamily::Windows, _, Abi::Gnu) => LinkerHint::Lld,

        // Linux targets
        (TargetFamily::Linux, OperatingSystem::Linux, Abi::Gnu) => {
            if host.os == OperatingSystem::Linux {
                LinkerHint::SystemDefault
            } else {
                LinkerHint::Lld
            }
        }
        (TargetFamily::Linux, OperatingSystem::Linux, Abi::Musl) => LinkerHint::Lld,
        (TargetFamily::Linux, OperatingSystem::Android, _) => LinkerHint::Lld,

        // macOS targets
        (TargetFamily::MacOs, OperatingSystem::MacOs, _) => LinkerHint::Lld,
        (TargetFamily::MacOs, OperatingSystem::Ios | OperatingSystem::TvOS | OperatingSystem::WatchOS, _) => {
            LinkerHint::Lld
        }

        // WASM targets
        (TargetFamily::Wasm, _, _) => LinkerHint::Lld,

        // BSD targets
        (TargetFamily::Other, OperatingSystem::FreeBSD, _) => LinkerHint::Lld,
        (TargetFamily::Other, OperatingSystem::NetBSD, _) => LinkerHint::Lld,
        (TargetFamily::Other, OperatingSystem::OpenBSD, _) => LinkerHint::Lld,

        // Bare metal
        (TargetFamily::BareMetal, _, _) => LinkerHint::Lld,

        // Default to LLD for cross-compilation
        _ => LinkerHint::Lld,
    }
}

/// Capability matrix for host/target combinations.
#[derive(Debug, Clone)]
pub struct CapabilityMatrix {
    pub host: HostInfo,
    pub targets: BTreeMap<TargetTriple, TargetInfo>,
}

impl CapabilityMatrix {
    /// Creates a new capability matrix for the current host.
    pub fn new() -> Result<Self, HostDetectError> {
        let host = detect_host()?;
        Ok(Self {
            host,
            targets: BTreeMap::new(),
        })
    }

    /// Assesses a target and adds it to the matrix.
    pub fn assess(&mut self, target: &TargetTriple) -> &TargetInfo {
        let info = assess_target(target, &self.host);
        self.targets.insert(target.clone(), info);
        self.targets.get(target).unwrap()
    }

    /// Gets the assessment for a target, computing it if necessary.
    pub fn get(&mut self, target: &TargetTriple) -> &TargetInfo {
        if !self.targets.contains_key(target) {
            self.assess(target);
        }
        self.targets.get(target).unwrap()
    }

    /// Returns all assessed targets.
    pub fn all_targets(&self) -> &BTreeMap<TargetTriple, TargetInfo> {
        &self.targets
    }

    /// Checks if a target can be built natively.
    pub fn is_native(&self, target: &TargetTriple) -> bool {
        target.triple == self.host.host_triple.triple
    }

    /// Checks if cross-compilation is required.
    pub fn requires_cross(&self, target: &TargetTriple) -> bool {
        !self.is_native(target)
    }

    /// Gets the recommended toolchain provider for a target.
    pub fn toolchain_hint(&mut self, target: &TargetTriple) -> ToolchainHint {
        self.get(target).toolchain_hint
    }

    /// Gets the recommended sysroot provider for a target.
    pub fn sysroot_hint(&mut self, target: &TargetTriple) -> SysrootHint {
        self.get(target).sysroot_hint
    }

    /// Gets the recommended linker for a target.
    pub fn linker_hint(&mut self, target: &TargetTriple) -> LinkerHint {
        self.get(target).linker_hint
    }
}

impl Default for CapabilityMatrix {
    fn default() -> Self {
        Self::new().expect("failed to detect host")
    }
}

/// Known target triples organized by tier.
pub struct KnownTargets;

impl KnownTargets {
    /// Returns all tier 1 target triples.
    pub fn tier1() -> &'static [&'static str] {
        &[
            "x86_64-unknown-linux-gnu",
            "x86_64-pc-windows-msvc",
            "aarch64-unknown-linux-gnu",
            "aarch64-apple-darwin",
            "x86_64-apple-darwin",
        ]
    }

    /// Returns all tier 2 target triples.
    pub fn tier2() -> &'static [&'static str] {
        &[
            "x86_64-unknown-linux-musl",
            "aarch64-unknown-linux-musl",
            "x86_64-unknown-freebsd",
            "aarch64-unknown-freebsd",
            "x86_64-unknown-netbsd",
            "aarch64-unknown-netbsd",
            "x86_64-unknown-openbsd",
            "x86_64-unknown-illumos",
            "powerpc64le-unknown-linux-gnu",
            "s390x-unknown-linux-gnu",
            "riscv64gc-unknown-linux-gnu",
            "x86_64-pc-windows-gnu",
            "i686-pc-windows-msvc",
            "i686-pc-windows-gnu",
            "aarch64-pc-windows-msvc",
            "wasm32-wasi",
            "wasm32-unknown-unknown",
            "wasm32-unknown-emscripten",
        ]
    }

    /// Checks if a target is a known tier 1 target.
    pub fn is_tier1(target: &str) -> bool {
        Self::tier1().contains(&target)
    }

    /// Checks if a target is a known tier 2 target.
    pub fn is_tier2(target: &str) -> bool {
        Self::tier2().contains(&target)
    }

    /// Returns all known targets (tier 1 + tier 2).
    pub fn all_known() -> Vec<&'static str> {
        let mut targets = Vec::new();
        targets.extend_from_slice(Self::tier1());
        targets.extend_from_slice(Self::tier2());
        targets
    }
}

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

    #[test]
    fn detects_host() {
        let host = detect_host().expect("host detection should work");
        assert!(!host.host_triple.triple.is_empty());
        assert!(host.rustc_version.is_some());
        assert!(host.cargo_version.is_some());
    }

    #[test]
    fn assesses_tier1_targets() {
        let host = detect_host().unwrap();
        let targets = KnownTargets::tier1();
        for t in targets {
            let target = TargetTriple::parse(t).unwrap();
            let info = assess_target(&target, &host);
            assert_eq!(info.supported, TargetSupport::Tier1);
        }
    }

    #[test]
    fn assesses_tier2_targets() {
        let host = detect_host().unwrap();
        let targets = KnownTargets::tier2();
        for t in targets {
            let target = TargetTriple::parse(t).unwrap();
            let info = assess_target(&target, &host);
            assert_eq!(info.supported, TargetSupport::Tier2);
        }
    }

    #[test]
    fn suggests_rustup_for_native() {
        let host = detect_host().unwrap();
        let target = TargetTriple::parse(&host.host_triple.triple).unwrap();
        let info = assess_target(&target, &host);
        assert!(info.is_native);
        assert!(!info.requires_cross);
        assert_eq!(info.toolchain_hint, ToolchainHint::Rustup);
    }

    #[test]
    fn suggests_linker_for_windows_msvc() {
        let host = detect_host().unwrap();
        let target = TargetTriple::parse("x86_64-pc-windows-msvc").unwrap();
        let info = assess_target(&target, &host);
        assert_eq!(info.linker_hint, LinkerHint::MSVC);
    }

    #[test]
    fn suggests_lld_for_cross_linux() {
        let host = detect_host().unwrap();
        let target = TargetTriple::parse("x86_64-unknown-linux-gnu").unwrap();
        let info = assess_target(&target, &host);
        assert_eq!(info.linker_hint, LinkerHint::Lld);
    }

    #[test]
    fn capability_matrix_works() {
        let mut matrix = CapabilityMatrix::new().unwrap();
        let target = TargetTriple::parse("x86_64-unknown-linux-gnu").unwrap();
        let info = matrix.assess(&target);
        assert_eq!(info.supported, TargetSupport::Tier1);
    }
}