Skip to content

Commit f66f169

Browse files
bors[bot]burrbull
andauthored
Merge #518
518: output-dir r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents eb105a5 + 9d1363b commit f66f169

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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+
1014
### Changed
1115

1216
- options can be set now with `svd2rust.toml` config

src/generate/device.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
139139

140140
let generic_file = std::str::from_utf8(include_bytes!("generic.rs"))?;
141141
if config.generic_mod {
142-
writeln!(File::create("generic.rs")?, "{}", generic_file)?;
142+
writeln!(
143+
File::create(config.output_dir.join("generic.rs"))?,
144+
"{}",
145+
generic_file
146+
)?;
143147

144148
if !config.make_mod {
145149
out.extend(quote! {

src/main.rs

Lines changed: 16 additions & 3 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"]);
@@ -136,20 +148,21 @@ fn run() -> Result<()> {
136148
make_mod,
137149
const_generic,
138150
ignore_groups,
151+
output_dir: path.clone(),
139152
};
140153

141154
let mut device_x = String::new();
142155
let items = generate::device::render(&device, &config, &mut device_x)?;
143156
let filename = if make_mod { "mod.rs" } else { "lib.rs" };
144-
let mut file = File::create(filename).expect("Couldn't create output file");
157+
let mut file = File::create(path.join(filename)).expect("Couldn't create output file");
145158

146159
let data = items.to_string().replace("] ", "]\n");
147160
file.write_all(data.as_ref())
148161
.expect("Could not write code to lib.rs");
149162

150163
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())?;
164+
writeln!(File::create(path.join("device.x"))?, "{}", device_x)?;
165+
writeln!(File::create(path.join("build.rs"))?, "{}", build_rs())?;
153166
}
154167

155168
Ok(())

src/util.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::svd::{Access, Cluster, Register, RegisterCluster, RegisterInfo};
44
use inflections::Inflect;
55
use proc_macro2::{Ident, Literal, Span, TokenStream};
66
use quote::{quote, ToTokens};
7+
use std::path::PathBuf;
78

89
use anyhow::{anyhow, bail, Result};
910

@@ -13,14 +14,29 @@ pub const BITS_PER_BYTE: u32 = 8;
1314
/// that are not valid in Rust ident
1415
const BLACKLIST_CHARS: &[char] = &['(', ')', '[', ']', '/', ' ', '-'];
1516

16-
#[derive(Clone, Copy, PartialEq, Default, Debug)]
17+
#[derive(Clone, PartialEq, Debug)]
1718
pub struct Config {
1819
pub target: Target,
1920
pub nightly: bool,
2021
pub generic_mod: bool,
2122
pub make_mod: bool,
2223
pub const_generic: bool,
2324
pub ignore_groups: bool,
25+
pub output_dir: PathBuf,
26+
}
27+
28+
impl Default for Config {
29+
fn default() -> Self {
30+
Self {
31+
target: Target::default(),
32+
nightly: false,
33+
generic_mod: false,
34+
make_mod: false,
35+
const_generic: false,
36+
ignore_groups: false,
37+
output_dir: PathBuf::from("."),
38+
}
39+
}
2440
}
2541

2642
#[allow(clippy::upper_case_acronyms)]

0 commit comments

Comments
 (0)