arm-targets 0.4.1

Compile-time feature detection for Arm processors
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
//! Useful cfg helpers for when you are building Arm code
//!
//! Hopefully Rust will stabilise these kinds of target features in the
//! future, and this won't be required. But until this, arm-targets is here to
//! help you conditionally compile your code based on the specific Arm
//! platform you are compiling for.
//!
//! In your application, do something like this:
//!
//! ```console
//! $ cargo add --build arm-targets
//! $ cat > build.rs << EOF
//! fn main() {
//!     arm_targets::process();
//! }
//! EOF
//! ```
//!
//! This will then let you write application code like:
//!
//! ```rust
//! #[cfg(arm_architecture = "armv7m")]
//! fn only_for_cortex_m3() { }
//!
//! #[cfg(arm_isa = "a32")]
//! fn can_use_arm_32bit_asm_here() { }
//! ```
//!
//! Without this crate, you are limited to `cfg(target_arch = "arm")`, which
//! isn't all that useful given how many 'Arm' targets there are.
//!
//! To see a full list of the features created by this crate, run the CLI tool:
//!
//! ```console
//! $ cargo install arm-targets
//! $ arm-targets
//! cargo:rustc-check-cfg=cfg(arm_isa, values("a64", "a32", "t32"))
//! cargo:rustc-check-cfg=cfg(arm_architecture, values("v4t", "v5te", "v6-m", "v7-m", "v7e-m", "v8-m.base", "v8-m.main", "v7-r", "v8-r", "v7-a", "v8-a"))
//! cargo:rustc-check-cfg=cfg(arm_profile, values("a", "r", "m", "legacy"))
//! cargo:rustc-check-cfg=cfg(arm_abi, values("eabi", "eabihf"))
//! ```

#[derive(Default)]
pub struct TargetInfo {
    isa: Option<Isa>,
    arch: Option<Arch>,
    profile: Option<Profile>,
    abi: Option<Abi>,
}

impl TargetInfo {
    /// Get the Arm Instruction Set Architecture of the target
    pub fn isa(&self) -> Option<Isa> {
        self.isa
    }

    /// Get the Arm Architecture version of the target
    pub fn arch(&self) -> Option<Arch> {
        self.arch
    }

    /// Get the Arm Architecture Profile of the target
    pub fn profile(&self) -> Option<Profile> {
        self.profile
    }

    /// Get the ABI of the target
    pub fn abi(&self) -> Option<Abi> {
        self.abi
    }
}

/// Process the ${TARGET} environment variable, and emit cargo configuration to
/// standard out.
pub fn process() -> TargetInfo {
    let target = std::env::var("TARGET").expect("build script TARGET variable");
    process_target(&target)
}

/// Process a given target string, and emit cargo configuration to standard out.
pub fn process_target(target: &str) -> TargetInfo {
    let mut target_info = TargetInfo::default();
    if let Some(isa) = Isa::get(target) {
        println!(r#"cargo:rustc-cfg=arm_isa="{}""#, isa);
        target_info.isa = Some(isa);
    }
    println!(
        r#"cargo:rustc-check-cfg=cfg(arm_isa, values({}))"#,
        Isa::values()
    );

    if let Some(arch) = Arch::get(target) {
        println!(r#"cargo:rustc-cfg=arm_architecture="{}""#, arch);
        target_info.arch = Some(arch);
    }
    println!(
        r#"cargo:rustc-check-cfg=cfg(arm_architecture, values({}))"#,
        Arch::values()
    );

    if let Some(profile) = Profile::get(target) {
        println!(r#"cargo:rustc-cfg=arm_profile="{}""#, profile);
        target_info.profile = Some(profile);
    }
    println!(
        r#"cargo:rustc-check-cfg=cfg(arm_profile, values({}))"#,
        Profile::values()
    );

    if let Some(abi) = Abi::get(target) {
        println!(r#"cargo:rustc-cfg=arm_abi="{}""#, abi);
        target_info.abi = Some(abi);
    }
    println!(
        r#"cargo:rustc-check-cfg=cfg(arm_abi, values({}))"#,
        Abi::values()
    );

    target_info
}

/// The Arm Instruction Set
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Isa {
    /// A64 instructions are executed by Arm processors in Aarch64 mode
    A64,
    /// A32 instructions are executed by Arm processors in Aarch32 Arm mode
    A32,
    /// T32 instructions are executed by Arm processors in Aarch32 Thumb mode
    T32,
}

impl Isa {
    /// Decode a target string
    pub fn get(target: &str) -> Option<Isa> {
        if target.starts_with("arm") {
            Some(Isa::A32)
        } else if target.starts_with("thumb") {
            Some(Isa::T32)
        } else if target.starts_with("aarch64") {
            Some(Isa::A64)
        } else {
            None
        }
    }

    /// Get a comma-separated list of values, suitable for cfg-check
    pub fn values() -> String {
        let string_versions: Vec<String> = [Isa::A64, Isa::A32, Isa::T32]
            .iter()
            .map(|i| format!(r#""{i}""#))
            .collect();
        string_versions.join(", ")
    }
}

impl core::fmt::Display for Isa {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Isa::A64 => "a64",
                Isa::A32 => "a32",
                Isa::T32 => "t32",
            }
        )
    }
}

/// The Arm Architecture
///
/// As defined by a particular revision of the Arm Architecture Reference Manual (ARM).
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Arch {
    /// Arm Architecture version 4, with Thumb support (e.g. ARM7TDMI)
    Armv4T,
    /// Arm Architecture version 5, with Thumb support and Enhanced DSP Instructions (e.g. ARM926EJ-S)
    Armv5TE,
    /// Arm Architecture version 6 (e.g. ARM1176JZF-S)
    Armv6,
    /// Armv6-M (e.g. Cortex-M0+)
    Armv6M,
    /// Armv7-M (e.g. Cortex-M3)
    Armv7M,
    /// Armv7E-M (e.g. Cortex-M4)
    Armv7EM,
    /// Armv8-M Baseline (e.g. Cortex-M23)
    Armv8MBase,
    /// Armv8-M with Mainline extensions (e.g. Cortex-M33)
    Armv8MMain,
    /// Armv7-R (e.g. Cortex-R5)
    Armv7R,
    /// Armv8-R (e.g. Cortex-R52)
    Armv8R,
    /// Armv7-A (e.g. Cortex-A8)
    Armv7A,
    /// Armv8-A (e.g. Cortex-A53)
    Armv8A,
}

impl Arch {
    /// Decode a target string
    pub fn get(target: &str) -> Option<Arch> {
        if target.starts_with("armv4t-") || target.starts_with("thumbv4t-") {
            Some(Arch::Armv4T)
        } else if target.starts_with("armv5te-") || target.starts_with("thumbv5te-") {
            Some(Arch::Armv5TE)
        } else if target.starts_with("thumbv6m-") {
            Some(Arch::Armv6M)
        } else if target.starts_with("thumbv7m-") {
            Some(Arch::Armv7M)
        } else if target.starts_with("thumbv7em-") {
            Some(Arch::Armv7EM)
        } else if target.starts_with("thumbv8m.base-") {
            Some(Arch::Armv8MBase)
        } else if target.starts_with("thumbv8m.main-") {
            Some(Arch::Armv8MMain)
        } else if target.starts_with("armv7r-")
            || target.starts_with("armebv7r-")
            || target.starts_with("thumbv7r-")
        {
            Some(Arch::Armv7R)
        } else if target.starts_with("armv8r-")
            || target.starts_with("aarch64r82-")
            || target.starts_with("aarch64v8r-")
            || target.starts_with("thumbv8r-")
        {
            Some(Arch::Armv8R)
        } else if target.starts_with("armv7a-") || target.starts_with("thumbv7a-") {
            Some(Arch::Armv7A)
        } else if target.starts_with("aarch64-") || target.starts_with("aarch64be-") {
            Some(Arch::Armv8A)
        } else if target.starts_with("arm-")
            || target.starts_with("armv6-")
            || target.starts_with("thumbv6-")
        {
            // If not specified, assume Armv6
            Some(Arch::Armv6)
        } else {
            None
        }
    }

    /// Get the Arm Architecture Profile
    pub fn profile(&self) -> Profile {
        match self {
            Arch::Armv6M | Arch::Armv7M | Arch::Armv7EM | Arch::Armv8MBase | Arch::Armv8MMain => {
                Profile::M
            }
            Arch::Armv4T | Arch::Armv5TE | Arch::Armv6 => Profile::Legacy,
            Arch::Armv7R | Arch::Armv8R => Profile::R,
            Arch::Armv7A | Arch::Armv8A => Profile::A,
        }
    }

    /// Get a comma-separated list of values, suitable for cfg-check
    pub fn values() -> String {
        let string_versions: Vec<String> = [
            Arch::Armv4T,
            Arch::Armv5TE,
            Arch::Armv6,
            Arch::Armv6M,
            Arch::Armv7M,
            Arch::Armv7EM,
            Arch::Armv8MBase,
            Arch::Armv8MMain,
            Arch::Armv7R,
            Arch::Armv8R,
            Arch::Armv7A,
            Arch::Armv8A,
        ]
        .iter()
        .map(|i| format!(r#""{i}""#))
        .collect();
        string_versions.join(", ")
    }
}

impl core::fmt::Display for Arch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Arch::Armv4T => "v4t",
                Arch::Armv5TE => "v5te",
                Arch::Armv6 => "v6",
                Arch::Armv6M => "v6-m",
                Arch::Armv7M => "v7-m",
                Arch::Armv7EM => "v7e-m",
                Arch::Armv7R => "v7-r",
                Arch::Armv8R => "v8-r",
                Arch::Armv8MBase => "v8-m.base",
                Arch::Armv8MMain => "v8-m.main",
                Arch::Armv7A => "v7-a",
                Arch::Armv8A => "v8-a",
            }
        )
    }
}

/// The Arm Architecture Profile.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Profile {
    /// Microcontrollers
    M,
    /// Real-Time
    R,
    /// Applications
    A,
    /// Legacy
    Legacy,
}

impl Profile {
    /// Decode a target string
    pub fn get(target: &str) -> Option<Profile> {
        let arch = Arch::get(target)?;
        Some(arch.profile())
    }

    /// Get a comma-separated list of values, suitable for cfg-check
    pub fn values() -> String {
        let string_versions: Vec<String> = [Profile::A, Profile::R, Profile::M, Profile::Legacy]
            .iter()
            .map(|i| format!(r#""{i}""#))
            .collect();
        string_versions.join(", ")
    }
}

impl core::fmt::Display for Profile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Profile::M => "m",
                Profile::R => "r",
                Profile::A => "a",
                Profile::Legacy => "legacy",
            }
        )
    }
}

/// The ABI
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Abi {
    /// Arm Embedded ABI
    Eabi,
    /// Arm Embedded ABI with Hard Float
    EabiHf,
}

impl Abi {
    /// Decode a target string
    pub fn get(target: &str) -> Option<Abi> {
        let _ = Arch::get(target)?;
        if target.ends_with("eabi") {
            Some(Abi::Eabi)
        } else if target.ends_with("eabihf") {
            Some(Abi::EabiHf)
        } else {
            None
        }
    }

    /// Get a comma-separated list of values, suitable for cfg-check
    pub fn values() -> String {
        let string_versions: Vec<String> = [Abi::Eabi, Abi::EabiHf]
            .iter()
            .map(|i| format!(r#""{i}""#))
            .collect();
        string_versions.join(", ")
    }
}

impl core::fmt::Display for Abi {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Abi::Eabi => "eabi",
                Abi::EabiHf => "eabihf",
            }
        )
    }
}

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

    #[test]
    fn armv4t_none_eabi() {
        let target = "armv4t-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv4T));
        assert_eq!(target_info.profile(), Some(Profile::Legacy));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn armv5te_none_eabi() {
        let target = "armv5te-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv5TE));
        assert_eq!(target_info.profile(), Some(Profile::Legacy));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn armv6_none_eabi() {
        let target = "armv6-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv6));
        assert_eq!(target_info.profile(), Some(Profile::Legacy));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn armv6_none_eabihf() {
        let target = "armv6-none-eabihf";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv6));
        assert_eq!(target_info.profile(), Some(Profile::Legacy));
        assert_eq!(target_info.abi(), Some(Abi::EabiHf));
    }

    #[test]
    fn arm_unknown_linux_gnueabi() {
        let target = "arm-unknown-linux-gnueabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv6));
        assert_eq!(target_info.profile(), Some(Profile::Legacy));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn thumbv6m_none_eabi() {
        let target = "thumbv6m-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv6M));
        assert_eq!(target_info.profile(), Some(Profile::M));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn thumbv7m_none_eabi() {
        let target = "thumbv7m-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv7M));
        assert_eq!(target_info.profile(), Some(Profile::M));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn thumbv7em_nuttx_eabihf() {
        let target = "thumbv7em-nuttx-eabihf";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv7EM));
        assert_eq!(target_info.profile(), Some(Profile::M));
        assert_eq!(target_info.abi(), Some(Abi::EabiHf));
    }

    #[test]
    fn thumbv8m_base_none_eabi() {
        let target = "thumbv8m.base-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv8MBase));
        assert_eq!(target_info.profile(), Some(Profile::M));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn thumbv8m_main_none_eabihf() {
        let target = "thumbv8m.main-none-eabihf";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv8MMain));
        assert_eq!(target_info.profile(), Some(Profile::M));
        assert_eq!(target_info.abi(), Some(Abi::EabiHf));
    }

    #[test]
    fn armv7r_none_eabi() {
        let target = "armv7r-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv7R));
        assert_eq!(target_info.profile(), Some(Profile::R));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn armv8r_none_eabihf() {
        let target = "armv8r-none-eabihf";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv8R));
        assert_eq!(target_info.profile(), Some(Profile::R));
        assert_eq!(target_info.abi(), Some(Abi::EabiHf));
    }

    #[test]
    fn thumbv8r_none_eabihf() {
        let target = "thumbv8r-none-eabihf";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::T32));
        assert_eq!(target_info.arch(), Some(Arch::Armv8R));
        assert_eq!(target_info.profile(), Some(Profile::R));
        assert_eq!(target_info.abi(), Some(Abi::EabiHf));
    }

    #[test]
    fn armv7a_none_eabi() {
        let target = "armv7a-none-eabi";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A32));
        assert_eq!(target_info.arch(), Some(Arch::Armv7A));
        assert_eq!(target_info.profile(), Some(Profile::A));
        assert_eq!(target_info.abi(), Some(Abi::Eabi));
    }

    #[test]
    fn aarch64_none_eabihf() {
        let target = "aarch64-unknown-none";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A64));
        assert_eq!(target_info.arch(), Some(Arch::Armv8A));
        assert_eq!(target_info.profile(), Some(Profile::A));
        assert_eq!(target_info.abi(), None);
    }

    #[test]
    fn aarch64v8r_none() {
        let target = "aarch64v8r-unknown-none";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A64));
        assert_eq!(target_info.arch(), Some(Arch::Armv8R));
        assert_eq!(target_info.profile(), Some(Profile::R));
        assert_eq!(target_info.abi(), None);
    }

    #[test]
    fn aarch64r82_none() {
        let target = "aarch64r82-unknown-none";
        let target_info = process_target(target);
        assert_eq!(target_info.isa(), Some(Isa::A64));
        assert_eq!(target_info.arch(), Some(Arch::Armv8R));
        assert_eq!(target_info.profile(), Some(Profile::R));
        assert_eq!(target_info.abi(), None);
    }
}