use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
verify_runtime_version();
println!("cargo:rerun-if-changed=Cargo.toml");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let target_dir = out_dir
.parent() .and_then(|p| p.parent()) .and_then(|p| p.parent()) .expect("Could not find target directory");
let direct_lib = target_dir.join("libseq_runtime.a");
let runtime_lib = if direct_lib.exists() {
direct_lib
} else {
let deps_dir = target_dir.join("deps");
find_runtime_in_deps(&deps_dir).unwrap_or_else(|| {
panic!(
"Runtime library not found.\n\
Looked in: {}\n\
And deps: {}\n\
OUT_DIR was: {}",
direct_lib.display(),
deps_dir.display(),
out_dir.display()
)
})
};
println!(
"cargo:rustc-env=SEQ_RUNTIME_LIB_PATH={}",
runtime_lib.display()
);
println!("cargo:rerun-if-changed={}", runtime_lib.display());
}
fn find_runtime_in_deps(deps_dir: &PathBuf) -> Option<PathBuf> {
if !deps_dir.exists() {
return None;
}
fs::read_dir(deps_dir).ok()?.find_map(|entry| {
let entry = entry.ok()?;
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with("libseq_runtime") && name_str.ends_with(".a") {
Some(entry.path())
} else {
None
}
})
}
fn verify_runtime_version() {
let compiler_version = env!("CARGO_PKG_VERSION");
let cargo_toml_content =
fs::read_to_string("Cargo.toml").expect("Failed to read compiler/Cargo.toml");
let cargo_toml: toml::Value = cargo_toml_content
.parse()
.expect("Failed to parse Cargo.toml");
let runtime_version = cargo_toml
.get("build-dependencies")
.and_then(|deps| deps.get("seq-runtime"))
.and_then(|dep| {
match dep {
toml::Value::Table(t) => t.get("version").and_then(|v| v.as_str()),
toml::Value::String(s) => Some(s.as_str()),
_ => None,
}
})
.expect("Could not find seq-runtime version in Cargo.toml");
let runtime_version = runtime_version.trim_start_matches('=');
if compiler_version != runtime_version {
panic!(
"\n\n\
╔══════════════════════════════════════════════════════════════╗\n\
║ VERSION MISMATCH ERROR ║\n\
╠══════════════════════════════════════════════════════════════╣\n\
║ seq-compiler version: {:<39}║\n\
║ seq-runtime version: {:<39}║\n\
║ ║\n\
║ The embedded runtime MUST match the compiler version. ║\n\
║ This ensures published crates.io packages are trustworthy. ║\n\
║ ║\n\
║ Update compiler/Cargo.toml to pin seq-runtime to: ║\n\
║ version = \"={:<46}║\n\
╚══════════════════════════════════════════════════════════════╝\n",
compiler_version, runtime_version, compiler_version
);
}
println!(
"cargo:warning=✓ Version verified: seq-compiler {} with seq-runtime {}",
compiler_version, runtime_version
);
}