nixide/nixide-sys/build.rs

200 lines
6.6 KiB
Rust
Raw Normal View History

2026-04-02 09:52:29 +10:00
use std::env;
use std::fs;
2026-04-10 11:51:27 +10:00
use std::iter;
2026-03-13 17:16:40 +10:00
use std::path::PathBuf;
2026-04-02 09:52:29 +10:00
use bindgen::RustEdition;
use bindgen::callbacks::{ItemKind, ParseCallbacks};
use heck::ToSnekCase;
use heck::ToUpperCamelCase;
2026-03-31 11:19:58 +10:00
use itertools::Itertools;
2026-03-13 17:16:40 +10:00
#[derive(Debug)]
struct DoxygenCallbacks;
impl ParseCallbacks for DoxygenCallbacks {
fn process_comment(&self, comment: &str) -> Option<String> {
match doxygen_bindgen::transform(comment) {
Ok(res) => Some(res),
Err(err) => {
2026-03-29 00:00:30 +10:00
println!("cargo::warning=Problem processing doxygen comment: {comment}\n{err}");
2026-03-13 17:16:40 +10:00
None
2026-03-29 00:00:30 +10:00
},
2026-03-13 17:16:40 +10:00
}
}
}
2026-04-02 09:52:29 +10:00
#[inline]
fn strip_variant_prefix(
prefix: &'static str,
enum_name: &str,
variant: &str,
) -> Result<String, String> {
variant
.strip_prefix(prefix)
.map(str::to_owned)
.ok_or(format!(
"[bindfmt] enum {enum_name} expected prefix \"{prefix}\" but got {}",
&variant
))
}
2026-04-02 23:38:34 +10:00
/// Bindfmt is just the name im giving to the callbacks
/// that handle renaming C/C++ tokens.
#[derive(Debug)]
struct BindfmtCallbacks;
2026-04-02 09:52:29 +10:00
impl ParseCallbacks for BindfmtCallbacks {
fn enum_variant_name(
&self,
_enum_name: Option<&str>,
_original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue,
) -> Option<String> {
let variant = _original_variant_name.to_upper_camel_case();
_enum_name.map(|enum_name| match enum_name.to_upper_camel_case().as_ref() {
"NixVerbosity" => strip_variant_prefix("NixLvl", enum_name, &variant).unwrap(),
"NixErr" => strip_variant_prefix("NixErr", enum_name, &variant)
.or_else(|_| strip_variant_prefix("Nix", enum_name, &variant))
.unwrap(),
2026-04-02 23:38:34 +10:00
"ValueType" => strip_variant_prefix("NixType", enum_name, &variant)
.or_else(|_| strip_variant_prefix("N", enum_name, &variant))
.unwrap(),
2026-04-02 09:52:29 +10:00
_ => variant,
})
}
fn item_name(&self, _item_info: bindgen::callbacks::ItemInfo) -> Option<String> {
Some(match _item_info.kind {
ItemKind::Type => _item_info.name.to_upper_camel_case(),
_ => _item_info.name.to_snek_case(),
})
}
fn include_file(&self, _filename: &str) {
eprintln!("[debug] including file: {}", _filename);
}
}
2026-04-10 11:51:27 +10:00
const FEATURES: &[&'static str] = &[
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-util-c")]
2026-04-10 11:51:27 +10:00
"util",
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-store-c")]
2026-04-10 11:51:27 +10:00
"store",
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-expr-c")]
2026-04-10 11:51:27 +10:00
"expr",
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-fetchers-c")]
2026-04-10 11:51:27 +10:00
"fetchers",
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-flake-c")]
2026-04-10 11:51:27 +10:00
"flake",
2026-03-31 08:12:25 +10:00
#[cfg(feature = "nix-main-c")]
2026-04-10 11:51:27 +10:00
"main",
2026-03-31 08:12:25 +10:00
];
2026-03-13 17:16:40 +10:00
2026-03-31 08:12:25 +10:00
fn main() {
2026-04-10 11:51:27 +10:00
let libs: Vec<pkg_config::Library> = FEATURES
2026-04-02 09:52:29 +10:00
.iter()
2026-04-10 11:51:27 +10:00
.map(|&feature| {
let name = &format!("nix-{feature}-c");
2026-04-10 10:12:53 +10:00
pkg_config::probe_library(name).expect(&format!("Unable to find .pc file for {}", name))
2026-04-02 09:52:29 +10:00
})
2026-04-10 10:12:53 +10:00
.collect();
2026-04-10 11:51:27 +10:00
#[allow(unused)]
2026-04-10 10:12:53 +10:00
let include_paths: Vec<PathBuf> = libs
.clone()
.into_iter()
.map(|lib| lib.include_paths)
.flatten()
2026-04-10 11:51:27 +10:00
.chain(iter::once(fs::canonicalize("./libnixide-c").unwrap()))
2026-04-10 10:12:53 +10:00
.unique()
.collect();
2026-04-10 11:51:27 +10:00
#[allow(unused)]
2026-04-10 10:12:53 +10:00
let link_paths: Vec<PathBuf> = libs
.clone()
.into_iter()
.map(|lib| lib.link_files)
2026-04-02 09:52:29 +10:00
.flatten()
.unique()
.collect();
2026-04-10 11:51:27 +10:00
// build the libnixide-c extension
2026-04-02 23:38:34 +10:00
cc::Build::new()
// .cargo_output(true)
// .cargo_warnings(true)
// .cargo_metadata(true)
// .cargo_debug(cfg!(debug_assertions))
.cpp(true)
.std("c++23") // libnix compiles against `-std=c++23`
2026-04-10 11:51:27 +10:00
.cpp_link_stdlib("c++") // libstdc++ for GNU, c++ for Clang
.opt_level((!cfg!(debug_assertions)) as u32 * 3)
2026-04-10 11:51:27 +10:00
.files(FEATURES.iter().map(|&feature| format!("libnixide-c/nixide_api_{feature}.cc")))
2026-04-02 23:38:34 +10:00
.includes(&include_paths)
2026-04-10 11:51:27 +10:00
.compile("nixide-c"); // libnixide-c
2026-04-02 23:38:34 +10:00
2026-04-10 11:51:27 +10:00
let clang_args: Vec<String> = vec!["-x", "c++", "-std=c++23"]
.into_iter()
.map(|s: &str| s.to_owned())
.chain(include_paths.iter().map(|p| format!("-I{}", p.display())))
.collect();
dbg!(&clang_args);
let mut builder = bindgen::Builder::default()
2026-04-02 09:52:29 +10:00
.rust_edition(RustEdition::Edition2024)
.clang_args(clang_args)
2026-03-13 17:16:40 +10:00
// Add `doxygen_bindgen` callbacks
.parse_callbacks(Box::new(DoxygenCallbacks))
2026-04-02 09:52:29 +10:00
.parse_callbacks(Box::new(BindfmtCallbacks))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
2026-03-13 17:16:40 +10:00
// Format generated bindings with rustfmt
.formatter(bindgen::Formatter::Rustfmt)
2026-04-02 09:52:29 +10:00
.rustfmt_configuration_file(std::fs::canonicalize("rustfmt.toml").ok())
2026-04-02 23:38:34 +10:00
// Control allow/block listing
2026-04-10 10:12:53 +10:00
.allowlist_recursively(true)
.allowlist_file(r".*nix_api_[a-z]+(/[a-z_]+)?\.h")
2026-04-10 11:51:27 +10:00
.allowlist_file(r".*nixide_api_[a-z]+(/[a-z_]+)?\.h")
2026-04-10 10:12:53 +10:00
// .layout_tests(false) // DEBUG
2026-04-02 09:52:29 +10:00
.use_core() // use ::core instead of ::std
.ctypes_prefix("::core::ffi") // use ::core::ffi instead of ::std::os::raw
.time_phases(true)
2026-04-02 23:38:34 +10:00
// Wrap all unsafe operations in unsafe blocks
2026-04-02 09:52:29 +10:00
.wrap_unsafe_ops(true)
.trust_clang_mangling(true)
.respect_cxx_access_specs(true)
.default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: false })
2026-04-02 23:38:34 +10:00
// .translate_enum_integer_types(false)
2026-04-02 09:52:29 +10:00
.size_t_is_usize(true)
2026-04-10 10:12:53 +10:00
// .use_distinct_char16_t(false) // DEBUG
2026-04-02 09:52:29 +10:00
.generate_comments(false)
.generate_cstr(true) // use &CStr instead of &[u8]
2026-04-10 10:12:53 +10:00
.fit_macro_constants(false)
2026-04-02 09:52:29 +10:00
.explicit_padding(true)
2026-04-10 10:12:53 +10:00
// .enable_cxx_namespaces()
// .represent_cxx_operators(false) // DEBUG
2026-04-02 09:52:29 +10:00
.enable_function_attribute_detection()
.raw_line("/** These bindings were auto-generated for the Nixide project (https://github.com/cry128/nixide) */");
2026-03-31 08:12:25 +10:00
// Register the input headers we would like to generate bindings for
2026-04-10 11:51:27 +10:00
builder = FEATURES
2026-03-31 08:12:25 +10:00
.iter()
2026-04-10 11:51:27 +10:00
.map(|&feature| {
let path = format!("include/nix-{feature}.h");
2026-03-31 08:12:25 +10:00
assert!(fs::exists(&path).unwrap());
// Invalidate the built crate if the binding headers change
2026-04-02 09:52:29 +10:00
println!("cargo::rerun-if-changed={path}");
2026-03-31 08:12:25 +10:00
path
})
.fold(builder, |builder, path| builder.header(path));
2026-03-13 17:16:40 +10:00
// Write the bindings to the $OUT_DIR/bindings.rs file
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
2026-03-31 08:12:25 +10:00
let bindings = builder.generate().expect("Unable to generate bindings");
2026-03-13 17:16:40 +10:00
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}