-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will allow linking to the correct .so on either FreeBSD 13 or 14. Fixes #16
- Loading branch information
Showing
8 changed files
with
126 additions
and
893 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,77 @@ | ||
use std::{ | ||
env, | ||
fs, | ||
path::Path, | ||
os::unix | ||
}; | ||
extern crate regex; | ||
|
||
#[cfg(target_os = "freebsd")] | ||
fn main() { | ||
// FreeBSD 14 adds a libnv.so.1, which is the same as libnv.so.0 except that | ||
// its symbols are renamed so as not to collide with libnvpair.so's. Always | ||
// link to libnv.so.0. That way out binaries will be cross-compilable and | ||
// run on FreeBSD 11.0 or greater, and we can use the same symbol names for | ||
// all OS versions. | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let link = Path::join(Path::new(&out_dir), "libnv.so"); | ||
match fs::read_link(&link) { | ||
Ok(l) if l == link => (), | ||
Ok(_) => { | ||
fs::remove_file(&link).unwrap(); | ||
unix::fs::symlink("/lib/libnv.so.0", &link).unwrap(); | ||
}, | ||
Err(_) => { | ||
unix::fs::symlink("/lib/libnv.so.0", &link).unwrap(); | ||
} | ||
} | ||
println!("cargo:rerun-if-env-changed=OUT_DIR"); | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
println!("cargo:rustc-link-search=native={out_dir}"); | ||
|
||
// Link to libnv | ||
use regex::Regex; | ||
use std::{ | ||
env, | ||
fs::File, | ||
io::Write, | ||
path::PathBuf, | ||
}; | ||
|
||
println!("cargo:rerun-if-env-changed=LLVM_CONFIG_PATH"); | ||
println!("cargo:rustc-link-lib=nv"); | ||
let autobindings = bindgen::Builder::default() | ||
.header("/usr/include/sys/nv.h") | ||
.allowlist_function("nvlist_.*") | ||
.allowlist_function("FreeBSD_nvlist_.*") | ||
.allowlist_type("nvlist_t") | ||
.allowlist_type("FreeBSD_nvlist_t") | ||
.blocklist_type("size_t") | ||
.blocklist_type("__size_t") | ||
.blocklist_type("__uint64_t") | ||
.opaque_type("FILE") | ||
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
.generate() | ||
.expect("Unable to generate bindings") | ||
.to_string(); | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
|
||
// libnv.so.1 prepends a "FreeBSD_" to the names of all public symbols, but | ||
// achieves backwards-compatibility by #define'ing the old name to the new. | ||
// That allows libnv.so.1 to be linked to an application that also links to | ||
// libnvpair.so. That wasn't possible with libnv.so.0. However, bindgen | ||
// doesn't understand #define, so we have to post-process its output by | ||
// removing the "FreeBSD_" from public symbols. | ||
let mut fixed_bindings = String::new(); | ||
let mut prev_index = 0; | ||
let re = Regex::new("pub (fn|type) FreeBSD_([a-zA-Z0-9_]+)").unwrap(); | ||
for cap in re.captures_iter(&autobindings) { | ||
let index = cap.get(0).unwrap().start(); | ||
fixed_bindings.push_str(&autobindings[prev_index..index]); | ||
let new_fragment = match cap.get(1).unwrap().as_str() { | ||
"fn" => { | ||
let funcname = cap.get(2).unwrap().as_str(); | ||
fixed_bindings.push_str( | ||
&format!("#[link_name = \"FreeBSD_{}\"]\n", funcname)); | ||
format!("pub fn {}", funcname) | ||
}, | ||
"type" => { | ||
let typename = cap.get(2).unwrap().as_str(); | ||
fixed_bindings.push_str(&format!("pub type FreeBSD_{} = {};\n", | ||
typename, typename)); | ||
format!("pub type {}", typename) | ||
} | ||
_ => unreachable!() | ||
}; | ||
fixed_bindings.push_str(&new_fragment); | ||
prev_index = index + new_fragment.len() + "FreeBSD_".len(); | ||
} | ||
fixed_bindings.push_str(&autobindings[prev_index..]); | ||
|
||
File::create(out_path.join("bindings.rs")) | ||
.unwrap() | ||
.write_all(fixed_bindings.as_bytes()) | ||
.expect("Couldn't write bindings!"); | ||
} | ||
|
||
#[cfg(not(target_os = "freebsd"))] | ||
fn main() { | ||
// If we're building not on FreeBSD, there's no way the build can succeed. | ||
// This probably means we're building docs on docs.rs, so set this config | ||
// variable. We'll use it to stub out the crate well enough that | ||
// libnv's docs can build. | ||
println!("cargo:rustc-cfg=crossdocs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Fake definitions good enough to cross-build libnv's docs | ||
//! | ||
//! docs.rs does all of its builds on Linux, so the usual build script fails. | ||
//! As a workaround, we skip the usual build script when doing cross-builds, and | ||
//! define these stubs instead. | ||
pub type nvlist_t = *const i32; |
Oops, something went wrong.