-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathmain.rs
More file actions
executable file
·222 lines (195 loc) · 7.05 KB
/
main.rs
File metadata and controls
executable file
·222 lines (195 loc) · 7.05 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
#![recursion_limit = "128"]
use std::path::PathBuf;
use svd_parser::svd;
use tracing::{error, info};
mod generate;
mod util;
use std::fs::File;
use std::io::Write;
use std::process;
use anyhow::{Context, Result};
use clap::{App, Arg};
use crate::util::{build_rs, Config, Target};
fn run() -> Result<()> {
use clap_conf::prelude::*;
use std::io::Read;
let matches =
App::new("svd2rust")
.about("Generate a Rust API from SVD files")
.arg(
Arg::with_name("input")
.help("Input SVD file")
.short("i")
.takes_value(true)
.value_name("FILE"),
)
.arg(
Arg::with_name("output")
.long("output-dir")
.help("Directory to place generated files")
.short("o")
.takes_value(true)
.value_name("PATH"),
)
.arg(
Arg::with_name("config")
.long("config")
.help("Config TOML file")
.short("c")
.takes_value(true)
.value_name("TOML_FILE"),
)
.arg(
Arg::with_name("target")
.long("target")
.help("Target architecture")
.takes_value(true)
.value_name("ARCH"),
)
.arg(
Arg::with_name("nightly_features")
.long("nightly")
.help("Enable features only available to nightly rustc"),
)
.arg(Arg::with_name("const_generic").long("const_generic").help(
"Use const generics to generate writers for same fields with different offsets",
))
.arg(
Arg::with_name("ignore_groups")
.long("ignore_groups")
.help("Don't add alternateGroup name as prefix to register name"),
)
.arg(
Arg::with_name("generic_mod")
.long("generic_mod")
.short("g")
.help("Push generic mod in separate file"),
)
.arg(
Arg::with_name("make_mod")
.long("make_mod")
.short("m")
.help("Create mod.rs instead of lib.rs, without inner attributes"),
)
.arg(
Arg::with_name("strict")
.long("strict")
.short("s")
.help("Make advanced checks due to parsing SVD"),
)
.arg(
Arg::with_name("log_level")
.long("log")
.short("l")
.help(&format!(
"Choose which messages to log (overrides {})",
tracing_subscriber::EnvFilter::DEFAULT_ENV
))
.takes_value(true),
)
.version(concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
))
.get_matches();
let xml = &mut String::new();
match matches.value_of("input") {
Some(file) => {
File::open(file)
.context("Cannot open the SVD file")?
.read_to_string(xml)
.context("Cannot read the SVD file")?;
}
None => {
let stdin = std::io::stdin();
stdin
.lock()
.read_to_string(xml)
.context("Cannot read from stdin")?;
}
}
let path = PathBuf::from(matches.value_of("output").unwrap_or("."));
let config_filename = matches.value_of("config").unwrap_or("");
let cfg = with_toml_env(&matches, &[config_filename, "svd2rust.toml"]);
setup_logging(&cfg);
let target = cfg
.grab()
.arg("target")
.conf("target")
.done()
.map(|s| Target::parse(&s))
.unwrap_or_else(|| Ok(Target::default()))?;
let nightly =
cfg.bool_flag("nightly_features", Filter::Arg) || cfg.bool_flag("nightly", Filter::Conf);
let generic_mod =
cfg.bool_flag("generic_mod", Filter::Arg) || cfg.bool_flag("generic_mod", Filter::Conf);
let make_mod =
cfg.bool_flag("make_mod", Filter::Arg) || cfg.bool_flag("make_mod", Filter::Conf);
let const_generic =
cfg.bool_flag("const_generic", Filter::Arg) || cfg.bool_flag("const_generic", Filter::Conf);
let ignore_groups =
cfg.bool_flag("ignore_groups", Filter::Arg) || cfg.bool_flag("ignore_groups", Filter::Conf);
let strict = cfg.bool_flag("strict", Filter::Arg) || cfg.bool_flag("strict", Filter::Conf);
let config = Config {
target,
nightly,
generic_mod,
make_mod,
const_generic,
ignore_groups,
strict,
output_dir: path.clone(),
};
let mut parser_config = svd_parser::Config::default();
parser_config.validate_level = if strict {
svd::ValidateLevel::Strict
} else {
svd::ValidateLevel::Weak
};
info!("Parsing device from SVD file");
let device = svd_parser::parse_with_config(xml, &parser_config)
.with_context(|| "Error parsing SVD file".to_string())?;
let mut device_x = String::new();
info!("Rendering device");
let items = generate::device::render(&device, &config, &mut device_x)
.with_context(|| "Error rendering device")?;
let filename = if make_mod { "mod.rs" } else { "lib.rs" };
let mut file = File::create(path.join(filename)).expect("Couldn't create output file");
let data = items.to_string().replace("] ", "]\n");
file.write_all(data.as_ref())
.expect("Could not write code to lib.rs");
if target == Target::CortexM
|| target == Target::Msp430
|| target == Target::XtensaLX
|| target == Target::RISCV
{
writeln!(File::create(path.join("device.x"))?, "{}", device_x)?;
writeln!(File::create(path.join("build.rs"))?, "{}", build_rs())?;
}
Ok(())
}
fn setup_logging<'a>(getter: &'a impl clap_conf::Getter<'a, String>) {
// * Log at `info` by default.
// * Allow users the option of setting complex logging filters using
// the `RUST_LOG` environment variable.
// * Override both of those if the logging level is set via
// the `log_level` config setting.
let filter = match getter.grab().arg("log_level").conf("log_level").done() {
Some(lvl) => tracing_subscriber::EnvFilter::from(lvl),
None => tracing_subscriber::EnvFilter::from_default_env()
.add_directive(tracing::Level::INFO.into()),
};
tracing_subscriber::fmt()
.without_time()
.with_target(false)
.with_env_filter(filter)
.compact()
.with_ansi(true)
.init();
}
fn main() {
if let Err(ref e) = run() {
error!("{:?}", e);
process::exit(1);
}
}