build rule generation and cmdline arg parsing

This commit is contained in:
Noah Swerhun 2023-09-18 11:21:41 -05:00
parent c7e42eba71
commit 9f46b676f7
2 changed files with 36 additions and 2 deletions

View file

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.4.4", features = ["derive"] }
regex = "1.9.5"

View file

@ -2,6 +2,7 @@ use std::fs;
use std::io::Read;
use std::path::Path;
use regex::Regex;
use clap::Parser;
#[derive(Debug)]
struct CFile {
@ -10,6 +11,16 @@ struct CFile {
headers: Vec<String>,
}
#[derive(Parser, Debug)]
struct Args {
/// Replace default make output with nice build messages
#[arg(short, long)]
pretty: bool,
#[arg(default_value = "./build")]
build_dir: String,
}
fn parse_c_file(path: &Path, re: &Regex) -> CFile {
let strpath = path.to_str().unwrap();
let filename = path.file_stem().unwrap().to_str().unwrap();
@ -21,7 +32,10 @@ fn parse_c_file(path: &Path, re: &Regex) -> CFile {
let mut headers: Vec<String> = Vec::new();
let caps = re.captures_iter(&contents);
for (_, [include_file]) in caps.map(|c| c.extract()) {
headers.push(path.parent().unwrap().join(Path::new(include_file)).to_str().unwrap().to_string());
headers.push(path.parent().unwrap()
.join(Path::new(include_file))
.to_str().unwrap()
.to_string());
}
return CFile { path: strpath.to_string(), filename: filename.to_string(), headers }
@ -55,6 +69,12 @@ fn main() {
let mut project_files: Vec<CFile> = Vec::new();
let includes_regex = Regex::new(r#"#include\s+"(.*?)""#).unwrap();
let args = Args::parse();
let build_dir = Path::new(&args.build_dir);
let pretty = args.pretty;
let obj_dir = build_dir.join("obj");
recurse_into_dir(Path::new("./"), &mut |file| {
let path = file.path();
match path.extension() {
@ -72,7 +92,20 @@ fn main() {
});
for file in project_files.iter() {
println!("{:?}", file);
let mut build_rule: String = "#=mgen_rule=#\n".to_owned();
let objname = file.filename.to_owned() + ".o";
let objpath = obj_dir.join(&objname);
let objpath = objpath.to_str().unwrap();
build_rule.push_str(&format!("{}: {}", objpath, file.path));
for header in file.headers.iter() {
build_rule.push_str(&format!(" {}", header));
}
build_rule.push_str(&format!("\n\t$(CC) $(CFLAGS) -o {} -c {}\n", objpath, file.path ));
println!("'{}'", build_rule);
}
}