1use anyhow::Result;2use clap::Parser;3use xshell::{cmd, Shell};45mod sourcegen;67#[derive(Parser)]8enum Opts {9 10 Sourcegen,11 12 Profile {13 #[arg(long, default_value = "true")]14 hyperfine: bool,15 #[arg(long)]16 callgrind: bool,17 #[arg(long)]18 cachegrind: bool,19 #[arg(long, default_value = "x86_64-unknown-linux-gnu")]20 target: String,21 args: Vec<String>,22 },23 24 Lint {25 26 #[arg(long)]27 fix: bool,28 },29 TestCBindings {30 #[arg(long, default_value = "x86_64-unknown-linux-gnu")]31 target: String,32 args: Vec<String>,33 },34}3536fn main() -> Result<()> {37 let sh = Shell::new()?;38 match Opts::parse() {39 Opts::Sourcegen => sourcegen::generate_ungrammar(),40 Opts::Profile {41 hyperfine,42 callgrind,43 cachegrind,44 args,45 target,46 } => {47 let out = sh.create_temp_dir()?;4849 50 cmd!(51 sh,52 "cargo build -Zbuild-std --target={target} --profile releasedebug"53 )54 .run()?;55 let built = format!("./target/{target}/releasedebug/jrsonnet");56 let bench_cmd = format!("{built} {}", args.join(" "));57 if hyperfine {58 cmd!(sh, "hyperfine {bench_cmd}").run()?;59 }60 if callgrind {61 let args = args.clone();62 let mut callgrind_out = out.path().to_owned();63 callgrind_out.push("callgrind.out.1");64 cmd!(sh, "valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file={callgrind_out} {built} {args...}").run()?;65 cmd!(sh, "kcachegrind {callgrind_out}").run()?;66 }67 if cachegrind {68 let mut cachegrind_out = out.path().to_owned();69 cachegrind_out.push("cachegrind.out.1");70 cmd!(sh, "valgrind --tool=cachegrind --cachegrind-out-file={cachegrind_out} {built} {args...}").run()?;71 cmd!(sh, "kcachegrind {cachegrind_out}").run()?;72 }7374 Ok(())75 }76 Opts::Lint { fix } => {77 let fmt_check = if fix { None } else { Some("--check") };78 cmd!(sh, "cargo fmt {fmt_check...}").run()?;79 Ok(())80 }81 Opts::TestCBindings { target, args } => {82 cmd!(83 sh,84 "cargo build -p libjsonnet --target={target} --release --no-default-features --features=interop-common,interop-threading"85 )86 .run()?;87 let built = format!("./target/{target}/release/libjsonnet.a");88 let c_bindings = "./bindings/c/";89 let test_file = "libjsonnet_test_file";90 cmd!(sh, "cp {built} {c_bindings}").run()?;91 sh.change_dir(c_bindings);9293 94 cmd!(sh, "gcc -c {test_file}.c").run()?;95 cmd!(sh, "gcc -o {test_file} -lc -lm {test_file}.o libjsonnet.a").run()?;96 let sh = Shell::new()?;9798 cmd!(sh, "{c_bindings}{test_file} {args...}").run()?;99100 Ok(())101 }102 }103}