Skip to content

Commit 240fdd0

Browse files
committed
--output-dir
1 parent eb105a5 commit 240fdd0

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Option `-o`(`--output-path`) let you specify output directory path
13+
- Option `ignore_groups` for optional disabling #506
14+
1015
### Changed
1116

1217
- options can be set now with `svd2rust.toml` config
13-
- option `ignore_groups` for optional disabling #506
1418
- [breaking-change] move `const_generic` from features to options
1519
- use `Config` to pass options over all render levels
1620
- Use register iterator from `svd-parser`

src/generate/device.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use proc_macro2::{Ident, Span, TokenStream};
33
use quote::{quote, ToTokens};
44
use std::fs::File;
55
use std::io::Write;
6+
use std::path::Path;
67

78
use crate::util::{self, Config, ToSanitizedUpperCase};
89
use crate::Target;
@@ -11,7 +12,12 @@ use anyhow::Result;
1112
use crate::generate::{interrupt, peripheral};
1213

1314
/// Whole device generation
14-
pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<TokenStream> {
15+
pub fn render(
16+
d: &Device,
17+
config: &Config,
18+
device_x: &mut String,
19+
path: impl AsRef<Path>,
20+
) -> Result<TokenStream> {
1521
let mut out = TokenStream::new();
1622

1723
let commit_info = {
@@ -139,7 +145,11 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
139145

140146
let generic_file = std::str::from_utf8(include_bytes!("generic.rs"))?;
141147
if config.generic_mod {
142-
writeln!(File::create("generic.rs")?, "{}", generic_file)?;
148+
writeln!(
149+
File::create(&path.as_ref().join("generic.rs"))?,
150+
"{}",
151+
generic_file
152+
)?;
143153

144154
if !config.make_mod {
145155
out.extend(quote! {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ pub fn generate(xml: &str, config: &Config) -> Result<Generation> {
516516
let device = svd::parse(xml)?;
517517
let mut device_x = String::new();
518518
let items =
519-
generate::device::render(&device, &config, &mut device_x).or(Err(SvdError::Render))?;
519+
generate::device::render(&device, &config, &mut device_x, ".").or(Err(SvdError::Render))?;
520520

521521
let mut lib_rs = String::new();
522522
writeln!(

src/main.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![recursion_limit = "128"]
22

33
use log::error;
4+
use std::path::PathBuf;
45
use svd_parser as svd;
56

67
mod generate;
@@ -29,8 +30,17 @@ fn run() -> Result<()> {
2930
.takes_value(true)
3031
.value_name("FILE"),
3132
)
33+
.arg(
34+
Arg::with_name("output")
35+
.long("output-dir")
36+
.help("Directory to place generated files")
37+
.short("o")
38+
.takes_value(true)
39+
.value_name("PATH"),
40+
)
3241
.arg(
3342
Arg::with_name("config")
43+
.long("config")
3444
.help("Config TOML file")
3545
.short("c")
3646
.takes_value(true)
@@ -102,6 +112,8 @@ fn run() -> Result<()> {
102112
}
103113
}
104114

115+
let path = PathBuf::from(matches.value_of("output").unwrap_or("."));
116+
105117
let config_filename = matches.value_of("config").unwrap_or("");
106118

107119
let cfg = with_toml_env(&matches, &[config_filename, "svd2rust.toml"]);
@@ -139,17 +151,17 @@ fn run() -> Result<()> {
139151
};
140152

141153
let mut device_x = String::new();
142-
let items = generate::device::render(&device, &config, &mut device_x)?;
154+
let items = generate::device::render(&device, &config, &mut device_x, &path)?;
143155
let filename = if make_mod { "mod.rs" } else { "lib.rs" };
144-
let mut file = File::create(filename).expect("Couldn't create output file");
156+
let mut file = File::create(&path.join(filename)).expect("Couldn't create output file");
145157

146158
let data = items.to_string().replace("] ", "]\n");
147159
file.write_all(data.as_ref())
148160
.expect("Could not write code to lib.rs");
149161

150162
if target == Target::CortexM || target == Target::Msp430 || target == Target::XtensaLX {
151-
writeln!(File::create("device.x")?, "{}", device_x)?;
152-
writeln!(File::create("build.rs")?, "{}", build_rs())?;
163+
writeln!(File::create(&path.join("device.x"))?, "{}", device_x)?;
164+
writeln!(File::create(&path.join("build.rs"))?, "{}", build_rs())?;
153165
}
154166

155167
Ok(())

0 commit comments

Comments
 (0)