make build.rs prettier

This commit is contained in:
do butterflies cry? 2026-03-31 08:12:25 +10:00
parent f96f1cf95e
commit 599c783d01
Signed by: cry
GPG key ID: F68745A836CA0412
2 changed files with 43 additions and 58 deletions

View file

@ -24,6 +24,7 @@
allowUnfree = false;
allowBroken = false;
overlays = builtins.attrValues self.overlays or {};
# config.replaceStdenv = {pkgs}: with pkgs; llvmPackages_21.stdenv;
};
forAllSystems = f:
@ -60,13 +61,19 @@
packages = with pkgs; [
rustc
llvmPackages.lld
lldb
llvmPackages.lldb
# lldb
cargo
cargo-c
cargo-llvm-cov
cargo-nextest
clang # DEBUG
clang-tools # DEBUG
libcxx
rust-analyzer-unwrapped
(rustfmt.override {asNightly = true;})
clippy

View file

@ -1,5 +1,5 @@
use std::env;
use std::path::PathBuf;
use std::{env, fs};
use bindgen::callbacks::ParseCallbacks;
@ -18,31 +18,26 @@ impl ParseCallbacks for DoxygenCallbacks {
}
}
const LIBS: &[&'static str] = &[
#[cfg(feature = "nix-util-c")]
"nix-util-c",
#[cfg(feature = "nix-store-c")]
"nix-store-c",
#[cfg(feature = "nix-expr-c")]
"nix-expr-c",
#[cfg(feature = "nix-fetchers-c")]
"nix-fetchers-c",
#[cfg(feature = "nix-flake-c")]
"nix-flake-c",
#[cfg(feature = "nix-main-c")]
"nix-main-c",
];
fn main() {
// Invalidate the built crate whenever the wrapper changes
println!("cargo::rerun-if-changed=include/nix-util.h");
println!("cargo::rerun-if-changed=include/nix-store.h");
println!("cargo::rerun-if-changed=include/nix-expr.h");
println!("cargo::rerun-if-changed=include/nix-fetchers.h");
println!("cargo::rerun-if-changed=include/nix-flake.h");
println!("cargo::rerun-if-changed=include/nix-main.h");
// Invalidate the built crate if the binding headers change
// println!("cargo::rerun-if-changed=include");
let libs = [
#[cfg(feature = "nix-util-c")]
"nix-util-c",
#[cfg(feature = "nix-store-c")]
"nix-store-c",
#[cfg(feature = "nix-expr-c")]
"nix-expr-c",
#[cfg(feature = "nix-fetchers-c")]
"nix-fetchers-c",
#[cfg(feature = "nix-flake-c")]
"nix-flake-c",
#[cfg(feature = "nix-main-c")]
"nix-main-c",
];
let lib_args: Vec<String> = libs
let lib_args: Vec<String> = LIBS
.iter()
.map(|&name| {
let lib = pkg_config::probe_library(name)
@ -57,52 +52,35 @@ fn main() {
.map(|p| format!("-I{}", p.display()))
})
.flatten()
.chain(vec!["-Wall".to_owned(), "-xc++".to_owned()])
.collect();
let mut builder = bindgen::Builder::default()
// .clang_arg("") // libnix uses c++23
.clang_args(lib_args)
// Invalidate the built crate when an included header file changes
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Add `doxygen_bindgen` callbacks
.parse_callbacks(Box::new(DoxygenCallbacks))
// Format generated bindings with rustfmt
.formatter(bindgen::Formatter::Rustfmt)
.rustfmt_configuration_file(std::fs::canonicalize(".rustfmt.toml").ok());
.rustfmt_configuration_file(std::fs::canonicalize("rustfmt.toml").ok());
// The input headers we would like to generate bindings for
#[cfg(feature = "nix-util-c")]
{
builder = builder.header("include/nix-util.h")
}
#[cfg(feature = "nix-store-c")]
{
builder = builder.header("include/nix-store.h")
}
#[cfg(feature = "nix-expr-c")]
{
builder = builder.header("include/nix-expr.h")
}
#[cfg(feature = "nix-fetchers-c")]
{
builder = builder.header("include/nix-fetchers.h")
}
#[cfg(feature = "nix-flake-c")]
{
builder = builder.header("include/nix-flake.h")
}
#[cfg(feature = "nix-main-c")]
{
builder = builder.header("include/nix-main.h")
}
let bindings = builder
// Finish the builder and generate the bindings
.generate()
// Unwrap the Result and panic on failure
.expect("Unable to generate bindings");
// Register the input headers we would like to generate bindings for
builder = LIBS
.iter()
.map(|lib| {
let path = format!("include/{}.h", lib.strip_suffix("-c").unwrap());
assert!(fs::exists(&path).unwrap());
// Invalidate the built crate if the binding headers change
// println!("cargo::rerun-if-changed={path}");
path
})
.fold(builder, |builder, path| builder.header(path));
// Write the bindings to the $OUT_DIR/bindings.rs file
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings = builder.generate().expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");