-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.toml
149 lines (129 loc) · 4.6 KB
/
Makefile.toml
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
[env]
DEVICES = ['MCXN947', 'MCXN236', 'MCXA153', 'MCXA156']
USE_MIRROR_IN_CHINA = '1'
[tasks.pacgen]
script_runner = "@rust"
script = '''
//! ```cargo
//! [dependencies]
//! anyhow = "1"
//! glob = "0.3"
//! regex = "1"
//! quote = "1"
//! proc-macro2 = "1"
//! serde = "1"
//! serde_yaml = "=0.9.34-deprecated"
//! svd-parser = { git = "https://github.com/Dirbaio/svd.git", rev = "8426f3bb40dd2391e26cd087a0d6510fe21fdcbc", features = ["derive-from", "expand"] }
//! chiptool = { git = "https://github.com/embassy-rs/chiptool.git", rev = "63e36008ecbc1b68e49160703e256e2db7e40b89" }
//! env_logger = "0"
//! ```
use std::collections::HashMap;
use anyhow::{Context, Result};
use glob::glob;
use quote::quote;
const SVDS_DIR: &str = "./svds";
const TRANSFORMS_DIR: &str = "./transforms";
const CRATE_DIR: &str = ".";
#[derive(Debug, serde::Deserialize)]
struct Config {
includes: Option<Vec<String>>,
transforms: Option<Vec<chiptool::transform::Transform>>,
}
impl Config {
fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
serde_yaml::from_slice(&std::fs::read(path)?).context("unable to read config")
}
fn apply(&self, ir: &mut chiptool::ir::IR) -> Result<()> {
if let Some(includes) = &self.includes {
for include in includes {
let c = Self::from_file(&format!("{}/{}", TRANSFORMS_DIR, include))?;
c.apply(ir)?;
}
}
if let Some(transforms) = &self.transforms {
for transform in transforms {
transform.run(ir)?;
}
}
Ok(())
}
}
fn main() -> Result<()> {
env_logger::init();
let devices: Vec<_> = glob(&format!("{}/*.svd", SVDS_DIR))?
.map(|p| {
p.unwrap()
.file_stem()
.unwrap()
.to_string_lossy()
.to_string()
})
.collect();
let mut configs = HashMap::new();
for d in &devices {
configs.insert(
d.clone(),
Config::from_file(&format!("{}/{}.yaml", TRANSFORMS_DIR, d))?,
);
}
std::fs::create_dir_all(CRATE_DIR)?;
std::fs::write(
&format!("{}/src/common.rs", CRATE_DIR),
chiptool::generate::COMMON_MODULE,
)?;
let svd_parser_config = svd_parser::Config::default();
let render_opts = chiptool::generate::Options {
common_module: chiptool::generate::CommonModule::External(quote!(crate::common)),
};
for device in &devices {
let svd = svd_parser::parse_with_config(
std::fs::read_to_string(&format!("{}/{}.svd", SVDS_DIR, device))?.as_ref(),
&svd_parser_config,
)?;
let mut ir = chiptool::svd2ir::convert_svd(&svd)?;
configs[device].apply(&mut ir)?;
std::fs::create_dir_all(&format!("{}/src/{}", CRATE_DIR, device.to_lowercase()))?;
let items = chiptool::generate::render(&mut ir, &render_opts)?;
let path = format!("{}/src/{}/chip.rs", CRATE_DIR, device.to_lowercase());
std::fs::write(&path, items.to_string())?;
std::process::Command::new("rustfmt").arg(&path).output()?;
let items = chiptool::generate::render_device_x(&ir, &ir.devices.values().next().unwrap())?;
let path = format!("{}/src/{}/device.x", CRATE_DIR, device.to_lowercase());
std::fs::write(&path, items.to_string())?;
}
Ok(())
}
'''
[tasks.download_svds]
alias = "download_svds_unix"
windows_alias = "download_svds_windows"
[tasks.download_svds_unix]
script_runner = "bash"
script_extension = "sh"
script = '''
while IFS=';' read -ra DEVICES; do
for DEVICE in "${DEVICES[@]}"; do
if [ -f svds/$DEVICE.svd ]; then continue; fi
download_url=$(curl -s https://api.github.com/repos/nxp-mcuxpresso/mcux-soc-svd/contents/$DEVICE | jq '.[0].download_url')
save_path=svds/$DEVICE.svd
if [ $USE_MIRROR_IN_CHINA == '1' ]; then download_url=https://ghp.ci/$download_url; fi
curl --output $save_path --progress-bar $download_url
done
done <<< $DEVICES
'''
[tasks.download_svds_windows]
script_runner = "powershell"
script_extension = "ps1"
script = '''
$DEVICES = $env:DEVICES -Split ";"
foreach ($DEVICE in $DEVICES)
{
if (Test-Path ./svds/$DEVICE.svd) { continue }
$json = (curl.exe -s https://api.github.com/repos/nxp-mcuxpresso/mcux-soc-svd/contents/$DEVICE | ConvertFrom-Json)[0]
$download_url = $json.download_url
Write-Host "Saving $DEVICE.svd"
$save_path = "svds/$DEVICE.svd"
if ($env:USE_MIRROR_IN_CHINA -eq '1') { $download_url = "https://ghp.ci/$download_url" }
curl.exe --output $save_path --progress-bar $download_url
}
'''