Skip to content

Commit

Permalink
Run bindgen at build time
Browse files Browse the repository at this point in the history
This will allow linking to the correct .so on either FreeBSD 13 or 14.

Fixes #16
  • Loading branch information
asomers committed Jan 12, 2023
1 parent ffaa33f commit 0f4f3d5
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 893 deletions.
1 change: 1 addition & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ task:
install_script:
- fetch https://sh.rustup.rs -o rustup.sh
- sh rustup.sh -y --default-toolchain ${RUST_VERSION}
- pkg install -y llvm
cargo_cache:
folder: $HOME/.cargo/registry
build_script:
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ license = "BSD-2-Clause"
description = "Safe and rustic wrapper around libnv-sys."
repository = "https://github.com/Inner-Heaven/libnv-rs"

[workspace]
members = [ ".", "libnv-sys" ]

[package.metadata.release]
dev-version-ext = "pre"

Expand Down
9 changes: 9 additions & 0 deletions libnv-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ description = "FFI bindings to libnv."
[lib]
name = "libnv_sys"

[package.metadata.docs.rs]
targets = [
"x86_64-unknown-freebsd",
]

[dependencies]
libc = "0.2"

[build-dependencies]
bindgen = { version = "0.60.0", features=[] }
regex = "1.6.0"
14 changes: 0 additions & 14 deletions libnv-sys/bindgen.sh

This file was deleted.

99 changes: 72 additions & 27 deletions libnv-sys/build.rs
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");
}
6 changes: 6 additions & 0 deletions libnv-sys/src/fakes.rs
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;
Loading

0 comments on commit 0f4f3d5

Please sign in to comment.