From 599c783d01617a8b3f1fae93ec4e2ef3e37e0c51 Mon Sep 17 00:00:00 2001 From: _cry64 Date: Tue, 31 Mar 2026 08:12:25 +1000 Subject: [PATCH] make build.rs prettier --- flake.nix | 9 ++++- nixide-sys/build.rs | 92 +++++++++++++++++---------------------------- 2 files changed, 43 insertions(+), 58 deletions(-) diff --git a/flake.nix b/flake.nix index 30f7a1d..49560e3 100644 --- a/flake.nix +++ b/flake.nix @@ -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 diff --git a/nixide-sys/build.rs b/nixide-sys/build.rs index 23c0e7c..1fee802 100644 --- a/nixide-sys/build.rs +++ b/nixide-sys/build.rs @@ -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 = libs + let lib_args: Vec = 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!");