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
use std::path::Path;
pub fn target_cpu() -> Result<Option<String>, String> {
if let Ok(force_override_cpu) = std::env::var("_DYLAN_RUST_TARGET_CPU_FORCE_OVERRIDE").map(|s| s.trim().to_owned()) {
if !force_override_cpu.is_empty() {
eprintln!("warning: force overriding CPU detection in crate '{}' to CPU '{}'",
env!("CARGO_PKG_NAME"), force_override_cpu);
return Ok(Some(force_override_cpu));
}
}
let target = if let Ok(target) = std::env::var("TARGET") {
target
} else {
return Err("cannot retrieve CPU name, please, pass --target flag to Cargo, e. g. \"--target atmega88pa.json\"".to_owned());
};
let target_json_relative_path = Path::new(&format!("{}.json", target)).to_owned();
let cpu_name = match parse_target_cpu_from_target_json(&target_json_relative_path) {
Some(target_cpu) => target_cpu,
None => {
eprintln!("[warning]: assuming a target CPU name of '{}' from the file name of the target spec JSON file", target);
if let Some(cpu_name) = target.strip_prefix("avr-unknown-gnu-"){
cpu_name.to_owned()
} else {
target
}
},
};
Ok(Some(cpu_name))
}
fn parse_target_cpu_from_target_json(possible_json_path: &Path)
-> Option<String> {
let json_content = match std::fs::read(possible_json_path) {
Ok(content) => String::from_utf8(content).unwrap(),
Err(..) => {
eprintln!("[warning]: cannot find target JSON file '{}' - due to limitations it needs to be in the crate root - is it?", possible_json_path.display());
return None;
},
};
let parsed = json::parse(&json_content).expect(&format!("failed to parse target JSON at '{}'", possible_json_path.display()));
match parsed {
json::JsonValue::Object(dict) => {
match dict.get("cpu") {
Some(target_cpu) => Some(target_cpu.as_str().expect("target CPU in JSON is not a string").to_owned()),
None => panic!("target specification file '{}' does not define a target CPU", possible_json_path.display()),
}
},
_ => None,
}
}