This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.rs
85 lines (77 loc) · 2.92 KB
/
build.rs
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
use std::env;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq)]
enum PolyfillLevel {
// Native, ie no polyfill. Just reexport from core::sync::atomic
Native,
// Full polyfill: polyfill both load/store and CAS with critical sections
Polyfill,
}
impl fmt::Display for PolyfillLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match *self {
Self::Native => "native",
Self::Polyfill => "polyfill",
};
write!(f, "{}", s)
}
}
fn main() {
let target = env::var("TARGET").unwrap();
use PolyfillLevel::*;
let patterns = [
("avr-*", (Polyfill, Polyfill)),
("msp430-none-elf", (Polyfill, Polyfill)),
("riscv32imac-*", (Native, Polyfill)),
("riscv32gc-*", (Native, Polyfill)),
("riscv32imc-*-espidf", (Native, Native)),
("riscv32*", (Polyfill, Polyfill)),
("thumbv4t-*", (Polyfill, Polyfill)),
("thumbv6m-*", (Polyfill, Polyfill)),
("thumbv7m-*", (Native, Polyfill)),
("thumbv7em-*", (Native, Polyfill)),
("thumbv8m.base-*", (Native, Polyfill)),
("thumbv8m.main-*", (Native, Polyfill)),
("xtensa-*-espidf", (Native, Native)),
("xtensa-esp32-*", (Native, Polyfill)),
("xtensa-esp32s2-*", (Polyfill, Polyfill)),
("xtensa-esp32s3-*", (Native, Polyfill)),
("xtensa-esp8266-*", (Polyfill, Polyfill)),
];
if let Some((_, (level, level64))) = patterns
.iter()
.find(|(pattern, _)| matches(pattern, &target))
{
if *level == PolyfillLevel::Polyfill {
println!("cargo:rustc-cfg=polyfill_u8");
println!("cargo:rustc-cfg=polyfill_u16");
println!("cargo:rustc-cfg=polyfill_u32");
println!("cargo:rustc-cfg=polyfill_usize");
println!("cargo:rustc-cfg=polyfill_i8");
println!("cargo:rustc-cfg=polyfill_i16");
println!("cargo:rustc-cfg=polyfill_i32");
println!("cargo:rustc-cfg=polyfill_isize");
println!("cargo:rustc-cfg=polyfill_ptr");
println!("cargo:rustc-cfg=polyfill_bool");
}
if *level64 == PolyfillLevel::Polyfill {
println!("cargo:rustc-cfg=polyfill_u64");
println!("cargo:rustc-cfg=polyfill_i64");
}
} else {
// If we don't know about the target, just reexport the entire `core::atomic::*`
// This doesn't polyfill anything, but it's guaranteed to never fail build.
println!("cargo:rustc-cfg=reexport_core");
}
}
// tiny glob replacement to avoid pulling in more crates.
// Supports 0 or 1 wildcards `*`
fn matches(pattern: &str, val: &str) -> bool {
if let Some(p) = pattern.find('*') {
let prefix = &pattern[..p];
let suffix = &pattern[p + 1..];
val.len() >= prefix.len() + suffix.len() && val.starts_with(prefix) && val.ends_with(suffix)
} else {
val == pattern
}
}