#![allow(dead_code)]
use std::path::{Path, PathBuf};
pub fn ridl_rt_rlib(dir: &Path) -> PathBuf {
let source = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("ridl-rt")
.join("src")
.join("lib.rs");
let rlib = dir.join("libridl_rt.rlib");
let status = std::process::Command::new("rustc")
.args([
"--edition",
"2021",
"--crate-type",
"rlib",
"--crate-name",
"ridl_rt",
])
.arg("--cfg")
.arg(r#"feature="flatbuffers""#)
.arg("--cfg")
.arg(r#"feature="proto3""#)
.arg("--cfg")
.arg(r#"feature="repr-c""#)
.arg(&source)
.arg("-o")
.arg(&rlib)
.status()
.expect("rustc must be installed and runnable for this test to be meaningful");
assert!(
status.success(),
"ridl-rt must build as an rlib for the compile proofs to see it"
);
let head = std::fs::read(&rlib).expect("the rlib is readable");
assert!(
head.starts_with(b"!<arch>\n"),
"the helper must produce an rlib archive, not metadata under an rlib name"
);
rlib
}
pub fn run_program(name: &str, source: &str) {
let dir = tempfile::tempdir().expect("a temp dir is created");
let source_path = dir.path().join(format!("{name}.rs"));
let bin_path = dir.path().join(name);
std::fs::write(&source_path, source).expect("the generated source is written");
let ridl_rt = ridl_rt_rlib(dir.path());
let status = std::process::Command::new("rustc")
.args(["--edition", "2024", "--crate-type", "bin", "-D", "warnings"])
.arg("-o")
.arg(&bin_path)
.arg("--extern")
.arg(format!("ridl_rt={}", ridl_rt.display()))
.arg(&source_path)
.status()
.expect("rustc must be installed and runnable for this test to be meaningful");
assert!(
status.success(),
"the generated codec must compile as a program, source:\n{source}"
);
let run = std::process::Command::new(&bin_path)
.output()
.expect("the compiled program runs");
assert!(
run.status.success(),
"the generated codec must behave as declared, stderr:\n{}",
String::from_utf8_lossy(&run.stderr)
);
}
pub fn run_program_capturing_stdout(name: &str, source: &str) -> String {
let dir = tempfile::tempdir().expect("a temp dir is created");
let source_path = dir.path().join(format!("{name}.rs"));
let bin_path = dir.path().join(name);
std::fs::write(&source_path, source).expect("the generated source is written");
let ridl_rt = ridl_rt_rlib(dir.path());
let status = std::process::Command::new("rustc")
.args(["--edition", "2024", "--crate-type", "bin", "-D", "warnings"])
.arg("-o")
.arg(&bin_path)
.arg("--extern")
.arg(format!("ridl_rt={}", ridl_rt.display()))
.arg(&source_path)
.status()
.expect("rustc must be installed and runnable for this test to be meaningful");
assert!(
status.success(),
"the generated codec must compile as a program, source:\n{source}"
);
let run = std::process::Command::new(&bin_path)
.output()
.expect("the compiled program runs");
assert!(
run.status.success(),
"the generated codec must behave as declared, stderr:\n{}",
String::from_utf8_lossy(&run.stderr)
);
String::from_utf8(run.stdout).expect("the program writes UTF-8 on standard output")
}
pub fn check_for_wasm32(name: &str, source: &str) -> bool {
if !ensure_wasm32_target() {
return false;
}
let dir = tempfile::tempdir().expect("a temp dir is created");
let source_path = dir.path().join(format!("{name}.rs"));
std::fs::write(&source_path, source).expect("the generated source is written");
let ridl_rt_source = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("ridl-rt")
.join("src")
.join("lib.rs");
let rlib = dir.path().join("libridl_rt.rlib");
let status = std::process::Command::new("rustc")
.args([
"--edition",
"2021",
"--crate-type",
"rlib",
"--crate-name",
"ridl_rt",
"--target",
"wasm32-unknown-unknown",
])
.arg("--cfg")
.arg(r#"feature="flatbuffers""#)
.arg("--cfg")
.arg(r#"feature="proto3""#)
.arg("--cfg")
.arg(r#"feature="repr-c""#)
.arg(&ridl_rt_source)
.arg("-o")
.arg(&rlib)
.status()
.expect("rustc must be installed and runnable for this test to be meaningful");
assert!(
status.success(),
"ridl-rt must build for wasm32 before generated code can be checked against it"
);
let status = std::process::Command::new("rustc")
.args([
"--edition",
"2024",
"--crate-type",
"lib",
"--target",
"wasm32-unknown-unknown",
"--emit=metadata",
"-D",
"warnings",
])
.arg("-o")
.arg(dir.path().join(format!("lib{name}.rmeta")))
.arg("--extern")
.arg(format!("ridl_rt={}", rlib.display()))
.arg(&source_path)
.status()
.expect("rustc must be installed and runnable for this test to be meaningful");
assert!(
status.success(),
"the generated code must check for wasm32-unknown-unknown, source:\n{source}"
);
true
}
fn ensure_wasm32_target() -> bool {
let Ok(installed) = std::process::Command::new("rustup")
.args(["target", "list", "--installed"])
.output()
else {
return false;
};
if String::from_utf8_lossy(&installed.stdout)
.lines()
.any(|line| line.trim() == "wasm32-unknown-unknown")
{
return true;
}
std::process::Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.status()
.map(|status| status.success())
.unwrap_or(false)
}