accept config and output file on command line

This commit is contained in:
Noah Swerhun 2024-03-04 14:38:11 -06:00
parent dcb2c2d35e
commit 8199e20885
3 changed files with 211 additions and 14 deletions

179
Cargo.lock generated
View file

@ -2,6 +2,100 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "anstream"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -14,6 +108,12 @@ version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.2.5" version = "2.2.5"
@ -34,6 +134,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
name = "ngen" name = "ngen"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"toml", "toml",
] ]
@ -84,6 +185,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "strsim"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.52" version = "2.0.52"
@ -135,6 +242,78 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.6.5" version = "0.6.5"

View file

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

View file

@ -1,6 +1,16 @@
use clap::Parser;
use std::fs::{read, write}; use std::fs::{read, write};
use toml::{Table, Value}; use toml::{Table, Value};
#[derive(Parser)]
struct Args {
#[arg(short, long, default_value = "ngen.toml")]
config_file: String,
#[arg(short, long, default_value = "build.ninja")]
output_file: String,
}
fn print_tabs(num: usize) { fn print_tabs(num: usize) {
for _ in 0..num { for _ in 0..num {
print!(" "); print!(" ");
@ -71,7 +81,8 @@ fn target_generator(name: &str, config_table: &Table) -> String {
}; };
ret.push_str(&format!( ret.push_str(&format!(
r#"# BEGIN TARGET {name} "\
# BEGIN TARGET {name}
rule cc_{name} rule cc_{name}
deps = gcc deps = gcc
depfile = $dep depfile = $dep
@ -84,7 +95,7 @@ rule link_{name}
build $builddir/{name}/obj: mkdir build $builddir/{name}/obj: mkdir
build $builddir/{name}/dep: mkdir build $builddir/{name}/dep: mkdir
"# "
)); ));
let sources = config_table let sources = config_table
.get("sources") .get("sources")
@ -101,7 +112,7 @@ build $builddir/{name}/dep: mkdir
let obj_name = format!("$builddir/{name}/obj/{new_file}.o"); let obj_name = format!("$builddir/{name}/obj/{new_file}.o");
let dep_name = format!("$builddir/{name}/dep/{new_file}.o.d"); let dep_name = format!("$builddir/{name}/dep/{new_file}.o.d");
ret.push_str(&format!( ret.push_str(&format!(
"build {obj_name}: cc_{name} {s} | build.ninja || $builddir/{name}/obj $builddir/{name}/dep\n dep = {dep_name}\n" "build {obj_name}: cc_{name} {s}\n dep = {dep_name}\n"
)); ));
object_list.push(obj_name); object_list.push(obj_name);
} }
@ -117,7 +128,7 @@ build $builddir/{name}/dep: mkdir
ret.push_str(&format!("\nbuild $builddir/{name}/{outfile}: link_{name} ")); ret.push_str(&format!("\nbuild $builddir/{name}/{outfile}: link_{name} "));
ret.push_str(&object_list.join(" ")); ret.push_str(&object_list.join(" "));
ret.push_str(&format!( ret.push_str(&format!(
" \nbuild {name}: phony $builddir/{name}/{outfile}\n" " || $builddir/{name}/obj $builddir/{name}/dep\nbuild {name}: phony $builddir/{name}/{outfile}\n"
)); ));
ret.push_str(&format!("# END TARGET {name}\n")); ret.push_str(&format!("# END TARGET {name}\n"));
@ -125,15 +136,19 @@ build $builddir/{name}/dep: mkdir
} }
fn main() { fn main() {
let conf_file_name = "ngen.toml"; let args = Args::parse();
let output_file = &args.output_file;
let config_file_name = &args.config_file;
let conf_file_bytes = let conf_file_bytes =
read(conf_file_name).unwrap_or_else(|e| panic!("fatal: could not read file: {e}")); read(config_file_name).unwrap_or_else(|e| panic!("fatal: could not read file: {e}"));
let conf_file = String::from_utf8_lossy(&conf_file_bytes); let conf_file = String::from_utf8_lossy(&conf_file_bytes);
let conf = toml::from_str::<Table>(&conf_file) let conf = toml::from_str::<Table>(&conf_file)
.unwrap_or_else(|e| panic!("fatal: could not parse config file: {e}")); .unwrap_or_else(|e| panic!("fatal: could not parse config file: {e}"));
let mut ninjafile = String::from(&format!( let mut ninjafile = String::from(
r#"# Generated by ngen. Do not modify by hand. "\
# Generated by ngen. Do not modify by hand.
builddir = build builddir = build
@ -142,13 +157,15 @@ rule mkdir
description = Creating directory $out description = Creating directory $out
rule regen rule regen
command = ngen -f $in command = ngen -c $in -o $out
generator = 1 generator = 1
description = Regenerating build.ninja description = Regenerating $out
build build.ninja: regen ngen.toml ",
pool = console );
"#,
ninjafile.push_str(&format!(
"build {output_file}: regen {config_file_name}\n pool = console\n"
)); ));
for k in conf.keys() { for k in conf.keys() {
@ -167,6 +184,6 @@ build build.ninja: regen ngen.toml
} }
} }
write("build.ninja", ninjafile) write(output_file, ninjafile)
.unwrap_or_else(|e| panic!("fatal: could not write ninja file: {e}")); .unwrap_or_else(|e| panic!("fatal: could not write ninja file: {e}"));
} }