Skip to content

output-dir #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Option `-o`(`--output-path`) let you specify output directory path

### Changed

- options can be set now with `svd2rust.toml` config
Expand Down
6 changes: 5 additions & 1 deletion src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke

let generic_file = std::str::from_utf8(include_bytes!("generic.rs"))?;
if config.generic_mod {
writeln!(File::create("generic.rs")?, "{}", generic_file)?;
writeln!(
File::create(config.output_dir.join("generic.rs"))?,
"{}",
generic_file
)?;

if !config.make_mod {
out.extend(quote! {
Expand Down
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![recursion_limit = "128"]

use log::error;
use std::path::PathBuf;
use svd_parser as svd;

mod generate;
Expand Down Expand Up @@ -29,8 +30,17 @@ fn run() -> Result<()> {
.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)
Expand Down Expand Up @@ -102,6 +112,8 @@ fn run() -> Result<()> {
}
}

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"]);
Expand Down Expand Up @@ -136,20 +148,21 @@ fn run() -> Result<()> {
make_mod,
const_generic,
ignore_groups,
output_dir: path.clone(),
};

let mut device_x = String::new();
let items = generate::device::render(&device, &config, &mut device_x)?;
let filename = if make_mod { "mod.rs" } else { "lib.rs" };
let mut file = File::create(filename).expect("Couldn't create output file");
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 {
writeln!(File::create("device.x")?, "{}", device_x)?;
writeln!(File::create("build.rs")?, "{}", build_rs())?;
writeln!(File::create(path.join("device.x"))?, "{}", device_x)?;
writeln!(File::create(path.join("build.rs"))?, "{}", build_rs())?;
}

Ok(())
Expand Down
18 changes: 17 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::svd::{Access, Cluster, Register, RegisterCluster, RegisterInfo};
use inflections::Inflect;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{quote, ToTokens};
use std::path::PathBuf;

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

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

#[derive(Clone, Copy, PartialEq, Default, Debug)]
#[derive(Clone, PartialEq, Debug)]
pub struct Config {
pub target: Target,
pub nightly: bool,
pub generic_mod: bool,
pub make_mod: bool,
pub const_generic: bool,
pub ignore_groups: bool,
pub output_dir: PathBuf,
}

impl Default for Config {
fn default() -> Self {
Self {
target: Target::default(),
nightly: false,
generic_mod: false,
make_mod: false,
const_generic: false,
ignore_groups: false,
output_dir: PathBuf::from("."),
}
}
}

#[allow(clippy::upper_case_acronyms)]
Expand Down