malcontent/nss/build.rs

59 lines
2.1 KiB
Rust
Raw Permalink Normal View History

2024-01-06 10:29:09 +01:00
// SPDX-FileCopyrightText: Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
// SPDX-License-Identifier: GPL-3.0-or-later
use {std::env, std::io::Error, std::io::ErrorKind, std::path::PathBuf};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.hpp");
// Required by NSS 2
println!("cargo:rustc-cdylib-link-arg=-Wl,-soname,libnss_malcontent.so.2");
// Pass down the path to the output folder to the integration test binary
let cdylib_dir = get_cargo_target_dir().unwrap();
println!("cargo:rustc-env=CDYLIB_OUT_DIR={}", cdylib_dir.display());
// Set the DT_RPATH of the test binary to search in its own folder first
println!("cargo:rustc-link-arg-tests=-Wl,--disable-new-dtags");
println!("cargo:rustc-link-arg-tests=-Wl,--rpath=$ORIGIN");
let bindings = bindgen::Builder::default()
.header("wrapper.hpp")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.newtype_enum("nss_status")
.allowlist_type("nss_status")
.newtype_enum("HErrno")
.allowlist_type("HErrno")
.newtype_enum("EaiRetcode")
.allowlist_type("EaiRetcode")
.allowlist_type("gaih_addrtuple")
.allowlist_function("__nss_configure_lookup")
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn get_cargo_target_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let profile = std::env::var("PROFILE")?;
let mut path = out_dir.as_path();
loop {
if let Some(parent) = path.parent() {
path = parent;
if parent.ends_with(&profile) {
return Ok(path.to_path_buf());
}
} else {
return Err(Box::new(Error::new(
ErrorKind::NotFound,
"Unable to determine cargo build target directory",
)));
}
}
}