Skip to content

Commit

Permalink
Write version to zlib.pc
Browse files Browse the repository at this point in the history
  • Loading branch information
sagudev committed Sep 13, 2023
1 parent 004f3f9 commit 3f05a9d
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,31 @@ fn build_zlib(cfg: &mut cc::Build, target: &str) {
fs::copy("src/zlib/zconf.h", dst.join("include/zconf.h")).unwrap();

fs::create_dir_all(lib.join("pkgconfig")).unwrap();
fs::write(
lib.join("pkgconfig/zlib.pc"),
fs::read_to_string("src/zlib/zlib.pc.in")
.unwrap()
.replace("@prefix@", dst.to_str().unwrap()),
)
.unwrap();
let mut contents = fs::read_to_string("src/zlib/zlib.pc.in")
.unwrap()
.replace("@prefix@", dst.to_str().unwrap());
if let Some(version) = extract_version(dst.join("include/zlib.h")) {
contents = contents.replace("@VERSION@", &version);
}
fs::write(lib.join("pkgconfig/zlib.pc"), contents).unwrap();

println!("cargo:root={}", dst.to_str().unwrap());
println!("cargo:rustc-link-search=native={}", lib.to_str().unwrap());
println!("cargo:include={}/include", dst.to_str().unwrap());
}

fn extract_version(zlib_h: PathBuf) -> Option<String> {
if let Ok(zlib_h_content) = fs::read_to_string(zlib_h) {
zlib_h_content
.lines()
.find(|l| l.contains("ZLIB_VERSION"))
.map(|l| l.split("\"").nth(1).map(|s| s.to_owned()))
.flatten()
} else {
None
}
}

#[cfg(not(feature = "zlib-ng"))]
fn build_zlib_ng(_target: &str, _compat: bool) {}

Expand Down

0 comments on commit 3f05a9d

Please sign in to comment.