svd2utra: implement svd2utra command

This now has proper command line handling.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-10-11 09:08:30 +08:00
parent c04b23ec67
commit 4847469f03
3 changed files with 92 additions and 7 deletions

View file

@ -8,3 +8,5 @@ edition = "2018"
[dependencies]
quick-xml = "0.19"
clap = "2"
anyhow = "1.0"

51
svd2utra/build.rs Normal file
View file

@ -0,0 +1,51 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
struct IgnoredError {}
impl<E> From<E> for IgnoredError
where
E: Error,
{
fn from(_: E) -> IgnoredError {
IgnoredError {}
}
}
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
}
fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim(), date.trim()),
_ => String::new(),
}
}
fn commit_hash() -> Result<String, IgnoredError> {
Ok(String::from_utf8(
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()?
.stdout,
)?)
}
fn commit_date() -> Result<String, IgnoredError> {
Ok(String::from_utf8(
Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()?
.stdout,
)?)
}

View file

@ -1,12 +1,44 @@
mod generate;
use generate::*;
use anyhow::Context;
use clap::{App, Arg};
use std::fs::File;
use std::io::{Read, Write};
fn main() {
println!("Generating using examples/soc.svd, output is example.rs (this stub is for testing only!)");
println!("Use `cargo xtask ci <path-to-svdfile>` to generate a final SVD file.");
let src = File::open("examples/soc.svd").unwrap();
let mut dest = File::create("example.rs").unwrap();
generate(src, &mut dest).unwrap();
fn main() -> anyhow::Result<()> {
let matches = App::new("svd2utra")
.about("Generate a UTRA 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")
.help("Output .rs file or crate directory")
.short("o")
.takes_value(true)
.value_name("FILE"),
)
.version(concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
))
.get_matches();
let src: Box<dyn Read> = match matches.value_of("input") {
Some(file) => Box::new(File::open(file).context("Cannot open the SVD file")?),
None => Box::new(std::io::stdin()),
};
let mut dest: Box<dyn Write> = match matches.value_of("output") {
None => Box::new(std::io::stdout()),
Some(path) => Box::new(File::open(path).context("Cannot open destination file")?),
};
generate::generate(src, &mut dest).context("Cannot generate output file")?;
Ok(())
}