git.delta.rocks / jrsonnet / refs/commits / d14f5fd9b388

difftreelog

source

xtask/src/main.rs2.8 KiBsourcehistory
1use anyhow::Result;2use clap::Parser;3use xshell::{cmd, Shell};45mod sourcegen;67#[derive(Parser)]8enum Opts {9	/// Generate files for rowan parser10	Sourcegen,11	/// Profile file execution12	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 = env!("TARGET_PLATFORM"))]20		target: String,21		args: Vec<String>,22	},23	/// Run all lints enforced by this repo24	Lint {25		/// Also fix found issues when possible.26		#[arg(long)]27		fix: bool,28	},29	/// Build and run test file from `bindings/c`30	TestCBindings {31		#[arg(long, default_value = env!("TARGET_PLATFORM"))]32		target: String,33		/// Which bindings file to build and run34		#[arg(long, default_value = "libjsonnet_test_file")]35		test_file: String,36		args: Vec<String>,37	},38}3940fn main() -> Result<()> {41	let sh = Shell::new()?;42	match Opts::parse() {43		Opts::Sourcegen => sourcegen::generate_ungrammar(),44		Opts::Profile {45			hyperfine,46			callgrind,47			cachegrind,48			args,49			target,50		} => {51			let out = sh.create_temp_dir()?;5253			// build-std54			cmd!(55				sh,56				"cargo build -Zbuild-std --target={target} --profile releasedebug"57			)58			.run()?;59			let built = format!("./target/{target}/releasedebug/jrsonnet");60			let bench_cmd = format!("{built} {}", args.join(" "));61			if hyperfine {62				cmd!(sh, "hyperfine {bench_cmd}").run()?;63			}64			if callgrind {65				let args = args.clone();66				let mut callgrind_out = out.path().to_owned();67				callgrind_out.push("callgrind.out.1");68				cmd!(sh, "valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file={callgrind_out} {built} {args...}").run()?;69				cmd!(sh, "kcachegrind {callgrind_out}").run()?;70			}71			if cachegrind {72				let mut cachegrind_out = out.path().to_owned();73				cachegrind_out.push("cachegrind.out.1");74				cmd!(sh, "valgrind --tool=cachegrind --cachegrind-out-file={cachegrind_out} {built} {args...}").run()?;75				cmd!(sh, "kcachegrind {cachegrind_out}").run()?;76			}7778			Ok(())79		}80		Opts::Lint { fix } => {81			let fmt_check = if fix { None } else { Some("--check") };82			cmd!(sh, "cargo fmt {fmt_check...}").run()?;83			Ok(())84		}85		Opts::TestCBindings {86			target,87			test_file,88			args,89		} => {90			cmd!(91				sh,92				"cargo build -p libjsonnet --target={target} --release --no-default-features --features=interop-common,interop-threading"93			)94			.run()?;95			let built = format!("./target/{target}/release/libjsonnet.a");96			let c_bindings = "./bindings/c/";97			cmd!(sh, "cp {built} {c_bindings}").run()?;98			sh.change_dir(c_bindings);99100			// TODO: Pass target to gcc?101			cmd!(sh, "gcc -c {test_file}.c").run()?;102			cmd!(sh, "gcc -o {test_file} -lc -lm {test_file}.o libjsonnet.a").run()?;103			let sh = Shell::new()?;104105			cmd!(sh, "{c_bindings}{test_file} {args...}").run()?;106107			Ok(())108		}109	}110}