Skip to content

Add -m switch to create a module instead of a lib #510

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 1 commit into from
Apr 17, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- New `-m` switch generates a `mod.rs` file instead of `lib.rs`, which can
be used as a module inside a crate without further modification.

- Generated crates now contain the git commit hash and date of svd2rust
compilation.

Expand Down
63 changes: 34 additions & 29 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn render(
target: Target,
nightly: bool,
generic_mod: bool,
make_mod: bool,
device_x: &mut String,
) -> Result<TokenStream> {
let mut out = TokenStream::new();
Expand Down Expand Up @@ -51,29 +52,31 @@ pub fn render(
});
}

out.extend(quote! {
#![doc = #doc]
// Deny a subset of warnings
#![deny(const_err)]
#![deny(dead_code)]
#![deny(improper_ctypes)]
#![deny(missing_docs)]
#![deny(no_mangle_generic_items)]
#![deny(non_shorthand_field_patterns)]
#![deny(overflowing_literals)]
#![deny(path_statements)]
#![deny(patterns_in_fns_without_body)]
#![deny(private_in_public)]
#![deny(unconditional_recursion)]
#![deny(unused_allocation)]
#![deny(unused_comparisons)]
#![deny(unused_parens)]
#![deny(while_true)]
// Explicitly allow a few warnings that may be verbose
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![no_std]
});
out.extend(quote! { #![doc = #doc] });
if !make_mod {
out.extend(quote! {
// Deny a subset of warnings
#![deny(const_err)]
#![deny(dead_code)]
#![deny(improper_ctypes)]
#![deny(missing_docs)]
#![deny(no_mangle_generic_items)]
#![deny(non_shorthand_field_patterns)]
#![deny(overflowing_literals)]
#![deny(path_statements)]
#![deny(patterns_in_fns_without_body)]
#![deny(private_in_public)]
#![deny(unconditional_recursion)]
#![deny(unused_allocation)]
#![deny(unused_comparisons)]
#![deny(unused_parens)]
#![deny(while_true)]
// Explicitly allow a few warnings that may be verbose
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![no_std]
});
}

out.extend(quote! {
use core::ops::Deref;
Expand Down Expand Up @@ -145,12 +148,14 @@ pub fn render(
if generic_mod {
writeln!(File::create("generic.rs")?, "{}", generic_file)?;

out.extend(quote! {
#[allow(unused_imports)]
use generic::*;
#[doc="Common register and bit access and modify traits"]
pub mod generic;
});
if !make_mod {
out.extend(quote! {
#[allow(unused_imports)]
use generic::*;
#[doc="Common register and bit access and modify traits"]
pub mod generic;
});
}
} else {
let tokens = syn::parse_file(generic_file)?.into_token_stream();

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ pub fn generate(xml: &str, target: Target, nightly: bool) -> Result<Generation>

let device = svd::parse(xml)?;
let mut device_x = String::new();
let items = generate::device::render(&device, target, nightly, false, &mut device_x)
let items = generate::device::render(&device, target, nightly, false, false, &mut device_x)
.or(Err(SvdError::Render))?;

let mut lib_rs = String::new();
Expand Down
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ fn run() -> Result<()> {
.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("log_level")
.long("log")
Expand Down Expand Up @@ -91,10 +97,19 @@ fn run() -> Result<()> {
let nightly = matches.is_present("nightly_features");

let generic_mod = matches.is_present("generic_mod");
let make_mod = matches.is_present("make_mod");

let mut device_x = String::new();
let items = generate::device::render(&device, target, nightly, generic_mod, &mut device_x)?;
let mut file = File::create("lib.rs").expect("Couldn't create lib.rs file");
let items = generate::device::render(
&device,
target,
nightly,
generic_mod,
make_mod,
&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 data = items.to_string().replace("] ", "]\n");
file.write_all(data.as_ref())
Expand Down