From 9f46b676f7cf1609108b81b69f43981a7f730290 Mon Sep 17 00:00:00 2001 From: Noah Swerhun Date: Mon, 18 Sep 2023 11:21:41 -0500 Subject: [PATCH] build rule generation and cmdline arg parsing --- Cargo.toml | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a1db99..eea6847 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index f02172b..f4c12ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, } +#[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 = 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 = 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); } }