git.delta.rocks / jrsonnet / refs/commits / 6bd914605815

difftreelog

source

xtask/src/main.rs3.4 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	/// Update C++/Golang golden testsuites from git39	UpdateTestsuites,40}4142fn main() -> Result<()> {43	let sh = Shell::new()?;44	match Opts::parse() {45		Opts::Sourcegen => sourcegen::generate_ungrammar(),46		Opts::Profile {47			hyperfine,48			callgrind,49			cachegrind,50			args,51			target,52		} => {53			let out = sh.create_temp_dir()?;5455			cmd!(56				sh,57				"cargo build --target={target} --profile releasedebug"58			)59			.run()?;60			let built = format!("./target/{target}/releasedebug/jrsonnet");61			let bench_cmd = format!("{built} {}", args.join(" "));62			if hyperfine {63				cmd!(sh, "hyperfine {bench_cmd}").run()?;64			}65			if callgrind {66				let args = args.clone();67				let mut callgrind_out = out.path().to_owned();68				callgrind_out.push("callgrind.out.1");69				cmd!(sh, "valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file={callgrind_out} {built} {args...}").run()?;70				cmd!(sh, "kcachegrind {callgrind_out}").run()?;71			}72			if cachegrind {73				let mut cachegrind_out = out.path().to_owned();74				cachegrind_out.push("cachegrind.out.1");75				cmd!(sh, "valgrind --tool=cachegrind --cachegrind-out-file={cachegrind_out} {built} {args...}").run()?;76				cmd!(sh, "kcachegrind {cachegrind_out}").run()?;77			}7879			Ok(())80		}81		Opts::Lint { fix } => {82			let fmt_check = if fix { None } else { Some("--check") };83			cmd!(sh, "cargo fmt {fmt_check...}").run()?;84			Ok(())85		}86		Opts::TestCBindings {87			target,88			test_file,89			args,90		} => {91			cmd!(92				sh,93				"cargo build -p libjsonnet --target={target} --release --no-default-features --features=interop-common,interop-threading"94			)95			.run()?;96			let built = format!("./target/{target}/release/libjsonnet.a");97			let c_bindings = "./bindings/c/";98			cmd!(sh, "cp {built} {c_bindings}").run()?;99			sh.change_dir(c_bindings);100101			// TODO: Pass target to gcc?102			cmd!(sh, "gcc -c {test_file}.c").run()?;103			cmd!(sh, "gcc -o {test_file} -lc -lm {test_file}.o libjsonnet.a").run()?;104			let sh = Shell::new()?;105106			cmd!(sh, "{c_bindings}{test_file} {args...}").run()?;107108			Ok(())109		}110		Opts::UpdateTestsuites => {111			let _pushd = sh.push_dir("tests");112			let git_dir = sh.create_temp_dir()?;113			let git_dir_path = git_dir.path();114			cmd!(115				sh,116				"git clone https://github.com/google/jsonnet.git --depth=1 {git_dir_path}/jsonnet"117			)118			.run()?;119			cmd!(120				sh,121				"git clone https://github.com/google/go-jsonnet.git --depth=1 {git_dir_path}/go-jsonnet"122			)123			.run()?;124			sh.remove_path("cpp_test_suite")?;125			sh.remove_path("go_testdata")?;126			cmd!(sh, "mv {git_dir_path}/jsonnet/test_suite cpp_test_suite").run()?;127			cmd!(sh, "mv {git_dir_path}/go-jsonnet/testdata go_testdata").run()?;128129			Ok(())130		}131	}132}