use std::path::PathBuf;
fn main() {
embed_lockfile_deps();
if cfg!(feature = "vendored-rnp") {
return;
}
if std::env::var("TARGET").is_ok_and(|t| t.contains("ohos"))
&& let Ok(libdir) = std::env::var("RNP_LIB_DIR")
{
println!(
"cargo:rustc-link-search=native={}",
libdir.replace('\\', "/")
);
println!("cargo:rustc-link-lib=static=json-c");
println!("cargo:rustc-link-lib=static=sexpp");
println!("cargo:rustc-link-lib=static=bz2");
println!("cargo:rustc-link-lib=static=z");
return;
}
let static_link = cfg!(target_os = "windows") || std::env::var("ENPRO_STATIC_LINK").is_ok();
if static_link && let Ok(prefix) = std::env::var("PREFIX") {
let libdir = format!("{}/lib", prefix.replace('\\', "/"));
println!("cargo:rustc-link-search=native={}", libdir);
println!("cargo:rustc-link-lib=static=json-c");
println!("cargo:rustc-link-lib=static=sexpp");
println!("cargo:rustc-link-lib=static=bzip2");
println!("cargo:rustc-link-lib=static=zlib");
if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=static=stdc++");
}
}
}
fn find_workspace_lock() -> Option<PathBuf> {
let manifest = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").ok()?);
for dir in [manifest.clone(), manifest.parent()?.to_path_buf()] {
let candidate = dir.join("Cargo.lock");
if candidate.is_file() {
return Some(candidate);
}
}
None
}
fn parse_package_block(block: &str) -> Option<(String, String)> {
let mut name = None;
let mut version = None;
for line in block.lines() {
let line = line.trim();
if let Some(v) = line.strip_prefix("name = ") {
name = v.trim_matches('"').to_string().into();
} else if let Some(v) = line.strip_prefix("version = ") {
version = v.trim_matches('"').to_string().into();
}
}
Some((name?, version?))
}
fn embed_lockfile_deps() {
let Some(lock_path) = find_workspace_lock() else {
println!("cargo:rustc-env=ENPROT_DEP_LIST=");
return;
};
println!("cargo:rerun-if-changed={}", lock_path.display());
let text = std::fs::read_to_string(&lock_path)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", lock_path.display()));
let mut deps: Vec<String> = text
.split("[[package]]")
.skip(1)
.filter_map(parse_package_block)
.map(|(name, version)| format!("{name}@{version}"))
.collect();
deps.sort();
deps.dedup();
println!("cargo:rustc-env=ENPROT_DEP_LIST={}", deps.join(" "));
}