-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.rs
More file actions
executable file
·445 lines (379 loc) · 12 KB
/
main.rs
File metadata and controls
executable file
·445 lines (379 loc) · 12 KB
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
#![recursion_limit = "128"]
use anyhow::{bail, Context, Result};
use chiptool::{generate, svd2ir};
use clap::Parser;
use log::*;
use proc_macro2::TokenStream;
use quote::format_ident;
use regex::Regex;
use std::collections::HashSet;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
use std::{fs::File, io::stdout};
use svd_parser::ValidateLevel;
use chiptool::ir::IR;
#[derive(Parser)]
#[clap(version = "1.0", author = "Dirbaio <dirbaio@dirbaio.net>")]
struct Opts {
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Parser)]
enum Subcommand {
Generate(Generate),
ExtractAll(ExtractAll),
ExtractPeripheral(ExtractPeripheral),
Transform(Transform),
Fmt(Fmt),
Check(Check),
GenBlock(GenBlock),
}
/// Extract peripheral from SVD to YAML
#[derive(Parser)]
struct ExtractPeripheral {
/// SVD file path
#[clap(long)]
svd: String,
/// Peripheral from the SVD
#[clap(long)]
peripheral: String,
/// Transforms file path
#[clap(long)]
transform: Vec<String>,
}
/// Extract all peripherals from SVD to YAML
#[derive(Parser)]
struct ExtractAll {
/// SVD file path
#[clap(long)]
svd: String,
/// Output directory. Each peripheral will be created as a YAML file here.
#[clap(short, long)]
output: String,
}
/// Apply transform to YAML
#[derive(Parser)]
struct Transform {
/// Input YAML path
#[clap(short, long)]
input: String,
/// Output YAML path
#[clap(short, long)]
output: String,
/// Transforms file path
#[clap(short, long)]
transform: String,
}
/// Generate a PAC directly from a SVD
#[derive(Parser)]
struct Generate {
/// SVD file path
#[clap(long)]
svd: String,
/// Transforms file path
#[clap(long)]
transform: Vec<String>,
/// Path of the `common` module.
///
/// Defaults to `crate::common`.
#[clap(long)]
pub common_module: Option<String>,
}
/// Reformat a YAML
#[derive(Parser)]
struct Fmt {
/// Peripheral file path
files: Vec<String>,
/// Error if incorrectly formatted, instead of fixing.
#[clap(long)]
check: bool,
/// Remove unused enums
#[clap(long)]
remove_unused: bool,
}
/// Check a YAML for errors.
#[derive(Parser)]
struct Check {
/// Peripheral file path
files: Vec<String>,
#[clap(long)]
allow_register_overlap: bool,
#[clap(long)]
allow_field_overlap: bool,
#[clap(long)]
allow_enum_dup_value: bool,
#[clap(long)]
allow_unused_enums: bool,
#[clap(long)]
allow_unused_fieldsets: bool,
}
/// Generate Rust code from a YAML register block
#[derive(Parser)]
struct GenBlock {
/// Input YAML path
#[clap(short, long)]
input: String,
/// Output YAML path
#[clap(short, long)]
output: String,
/// Path of the `common` module.
///
/// Defaults to `crate::common`.
#[clap(long)]
pub common_module: Option<String>,
}
fn main() -> Result<()> {
env_logger::init();
let opts: Opts = Opts::parse();
match opts.subcommand {
Subcommand::ExtractPeripheral(x) => extract_peripheral(x),
Subcommand::ExtractAll(x) => extract_all(x),
Subcommand::Generate(x) => gen(x),
Subcommand::Transform(x) => transform(x),
Subcommand::Fmt(x) => fmt(x),
Subcommand::Check(x) => check(x),
Subcommand::GenBlock(x) => gen_block(x),
}
}
fn load_svd(path: &str) -> Result<svd_parser::svd::Device> {
let xml = &mut String::new();
File::open(path)
.context("Cannot open the SVD file")?
.read_to_string(xml)
.context("Cannot read the SVD file")?;
let config = svd_parser::Config::default()
.expand_properties(true)
.validate_level(ValidateLevel::Disabled);
let device = svd_parser::parse_with_config(xml, &config)?;
Ok(device)
}
fn load_config(path: &str) -> Result<Config> {
let config = fs::read(path).context("Cannot read the config file")?;
serde_yaml::from_slice(&config).context("cannot deserialize config")
}
fn extract_peripheral(args: ExtractPeripheral) -> Result<()> {
let config = if args.transform.is_empty() {
Config::default()
} else {
args.transform
.into_iter()
.map(|s| load_config(&s))
.collect::<Result<Config>>()?
};
let svd = load_svd(&args.svd)?;
let mut ir = IR::new();
let peri = args.peripheral;
let mut p = svd
.peripherals
.iter()
.find(|p| p.name == peri)
.expect("peripheral not found");
if let Some(f) = &p.derived_from {
p = svd
.peripherals
.iter()
.find(|p| p.name == *f)
.expect("derivedFrom peripheral not found");
}
chiptool::svd2ir::convert_peripheral(&mut ir, p)?;
// Descriptions in SVD's contain a lot of noise and weird formatting. Clean them up.
let description_cleanups = [
// Fix weird newline spam in descriptions.
(Regex::new("[ \n]+").unwrap(), " "),
// Fix weird tab and cr spam in descriptions.
(Regex::new("[\r\t]+").unwrap(), " "),
// Replace double-space (end of sentence) with period.
(
Regex::new(r"(?<first_sentence>.*?)[\s]{2}(?<next_sentence>.*)").unwrap(),
"$first_sentence. $next_sentence",
),
// Make sure every description ends with a period.
(
Regex::new(r"(?<full_description>.*)(?<last_character>[\s'[^\.\s']])$").unwrap(),
"$full_description$last_character.",
),
// Eliminate space characters between end of description and the closing period.
(
Regex::new(r"(?<full_description>.*)\s\.$").unwrap(),
"$full_description.",
),
];
for (re, rep) in description_cleanups.iter() {
chiptool::transform::map_descriptions(&mut ir, |d| re.replace_all(d, *rep).into_owned())?;
}
for t in &config.transforms {
info!("running: {:?}", t);
t.run(&mut ir)?;
}
// Ensure consistent sort order in the YAML.
chiptool::transform::sort::Sort {}.run(&mut ir).unwrap();
serde_yaml::to_writer(stdout(), &ir).unwrap();
Ok(())
}
fn extract_all(args: ExtractAll) -> Result<()> {
std::fs::create_dir_all(&args.output)?;
let svd = load_svd(&args.svd)?;
for p in &svd.peripherals {
if p.derived_from.is_some() {
continue;
}
let mut ir = IR::new();
chiptool::svd2ir::convert_peripheral(&mut ir, p)?;
// Fix weird newline spam in descriptions.
let re = Regex::new("[ \n]+").unwrap();
chiptool::transform::map_descriptions(&mut ir, |d| re.replace_all(d, " ").into_owned())?;
// Ensure consistent sort order in the YAML.
chiptool::transform::sort::Sort {}.run(&mut ir).unwrap();
let f = File::create(PathBuf::from(&args.output).join(format!("{}.yaml", p.name)))?;
serde_yaml::to_writer(f, &ir).unwrap();
}
Ok(())
}
fn gen(args: Generate) -> Result<()> {
let config = if args.transform.is_empty() {
Config::default()
} else {
args.transform
.into_iter()
.map(|s| load_config(&s))
.collect::<Result<Config>>()?
};
let svd = load_svd(&args.svd)?;
let mut ir = svd2ir::convert_svd(&svd)?;
// Fix weird newline spam in descriptions.
let re = Regex::new("[ \n]+").unwrap();
chiptool::transform::map_descriptions(&mut ir, |d| re.replace_all(d, " ").into_owned())?;
for t in &config.transforms {
info!("running: {:?}", t);
t.run(&mut ir)?;
}
let generate_opts = generate::Options {
common_module: args
.common_module
.map_or(generate::CommonModule::Builtin, |path| {
let path = parse_path(path);
generate::CommonModule::External(quote::quote!(#path))
}),
};
let items = generate::render(&ir, &generate_opts).unwrap();
fs::write("lib.rs", items.to_string())?;
Ok(())
}
fn transform(args: Transform) -> Result<()> {
let data = fs::read(&args.input)?;
let mut ir: IR = serde_yaml::from_slice(&data)?;
let config = load_config(&args.transform)?;
for t in &config.transforms {
info!("running: {:?}", t);
t.run(&mut ir)?;
}
let data = serde_yaml::to_string(&ir)?;
fs::write(&args.output, data.as_bytes())?;
Ok(())
}
fn fmt(args: Fmt) -> Result<()> {
for file in args.files {
let got_data = fs::read(&file)?;
let mut ir: IR = serde_yaml::from_slice(&got_data)?;
if args.remove_unused {
let mut used_enums = HashSet::new();
for fs in ir.fieldsets.values_mut() {
for f in fs.fields.iter_mut().filter(|f| f.enumm.is_some()) {
used_enums.insert(f.enumm.as_ref().unwrap().clone());
}
}
ir.enums.retain(|name, _| used_enums.contains(name));
}
// Ensure consistent sort order in the YAML.
chiptool::transform::sort::Sort {}.run(&mut ir).unwrap();
// Trim all descriptions
let cleanup = |s: &mut Option<String>| {
if let Some(s) = s.as_mut() {
*s = s.trim().to_string()
}
};
for b in ir.blocks.values_mut() {
cleanup(&mut b.description);
for i in b.items.iter_mut() {
cleanup(&mut i.description);
}
}
for b in ir.fieldsets.values_mut() {
cleanup(&mut b.description);
for i in b.fields.iter_mut() {
cleanup(&mut i.description);
}
}
for b in ir.enums.values_mut() {
cleanup(&mut b.description);
for i in b.variants.iter_mut() {
cleanup(&mut i.description);
}
}
let want_data = serde_yaml::to_string(&ir)?;
if got_data != want_data.as_bytes() {
if args.check {
bail!("File {} is not correctly formatted", &file);
} else {
fs::write(&file, want_data)?;
}
}
}
Ok(())
}
fn check(args: Check) -> Result<()> {
let opts = chiptool::validate::Options {
allow_enum_dup_value: args.allow_enum_dup_value,
allow_field_overlap: args.allow_field_overlap,
allow_register_overlap: args.allow_register_overlap,
allow_unused_enums: args.allow_unused_enums,
allow_unused_fieldsets: args.allow_unused_fieldsets,
};
let mut fails = 0;
for file in args.files {
let got_data = fs::read(&file)?;
let ir: IR = serde_yaml::from_slice(&got_data)?;
let errs = chiptool::validate::validate(&ir, opts.clone());
fails += errs.len();
for e in errs {
println!("{}: {}", &file, e);
}
}
if fails != 0 {
bail!("{} failures", fails)
}
Ok(())
}
fn gen_block(args: GenBlock) -> Result<()> {
let data = fs::read(&args.input)?;
let mut ir: IR = serde_yaml::from_slice(&data)?;
chiptool::transform::Sanitize {}.run(&mut ir).unwrap();
// Ensure consistent sort order in the YAML.
chiptool::transform::sort::Sort {}.run(&mut ir).unwrap();
let generate_opts = generate::Options {
common_module: args
.common_module
.map_or(generate::CommonModule::Builtin, |path| {
let path = parse_path(path);
generate::CommonModule::External(quote::quote!(#path))
}),
};
let items = generate::render(&ir, &generate_opts).unwrap();
fs::write(&args.output, items.to_string())?;
Ok(())
}
#[derive(Default, serde::Serialize, serde::Deserialize)]
struct Config {
transforms: Vec<chiptool::transform::Transform>,
}
impl FromIterator<Config> for Config {
fn from_iter<I: IntoIterator<Item = Config>>(iter: I) -> Self {
let transforms: Vec<_> = iter.into_iter().flat_map(|c| c.transforms).collect();
Self { transforms }
}
}
fn parse_path(path: String) -> TokenStream {
let segments = path.split("::").map(|s| format_ident!("{}", s));
quote::quote!(#(#segments)::*)
}