git.delta.rocks / jrsonnet / refs/commits / fca1ee17e1b0

difftreelog

source

crates/nix-eval/build.rs2.3 KiBsourcehistory
1use bindgen::{2	RustEdition,3	callbacks::{ItemInfo, ParseCallbacks},4};5use std::path::PathBuf;67#[derive(Debug)]8struct StripPrefix;9impl ParseCallbacks for StripPrefix {10	fn item_name(&self, name: ItemInfo<'_>) -> Option<String> {11		name.name.strip_prefix("nix_").map(ToOwned::to_owned)12	}13}1415fn main() {16	// Link nix C++ libraries for cxx17	for lib in &[18		"nix-util",19		"nix-util-c",20		"nix-store",21		"nix-expr",22		"nix-flake",23		"nix-fetchers",24		"bdw-gc",25	] {26		if let Ok(library) = pkg_config::probe_library(lib) {27			for lib_path in library.libs {28				println!("cargo:rustc-link-lib={lib_path}");29			}30			for search_path in library.link_paths {31				println!("cargo:rustc-link-search=native={}", search_path.display());32			}33		}34	}3536	cxx_build::bridge("src/logging.rs")37		.file("src/logging.cc")38		.std("c++23")39		.shared_flag(true)40		.compile("nix-eval-logging");41	cxx_build::bridge("src/lib.rs")42		.file("src/lib.cc")43		.std("c++23")44		.shared_flag(true)45		.compile("nix-eval");4647	println!("cargo:rerun-if-changed=src/lib.cc");48	println!("cargo:rerun-if-changed=src/lib.hh");49	println!("cargo:rerun-if-changed=src/logging.cc");50	println!("cargo:rerun-if-changed=src/logging.hh");5152	//53	let mut libnix = bindgen::builder()54		.rust_edition(RustEdition::Edition2024)55		.header_contents(56			"nix.h",57			"58				#define GC_THREADS59				#include <gc/gc.h>60				#include <nix_api_expr.h>61				#include <nix_api_store.h>62				#include <nix_api_flake.h>63				#include <nix_api_fetchers.h>64				#include <nix_api_util.h>65				#include <nix_api_value.h>66			",67		)68		.parse_callbacks(Box::new(StripPrefix));6970	for header in pkg_config::probe_library("nix-expr-c")71		.expect("nix-expr-c")72		.include_paths73		.into_iter()74		.chain(75			pkg_config::probe_library("nix-flake-c")76				.expect("nix-flake-c")77				.include_paths78				.into_iter(),79		)80		.chain(81			pkg_config::probe_library("nix-fetchers-c")82				.expect("nix-fetchers-c")83				.include_paths84				.into_iter(),85		)86		.chain(87			pkg_config::probe_library("bdw-gc")88				.expect("bdw-gc")89				.include_paths90				.into_iter(),91		) {92		libnix = libnix.clang_arg(format!("-I{}", header.to_str().expect("path is utf-8")));93	}9495	let mut out = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR is set by cargo"));96	out.push("bindings.rs");97	libnix98		.generate()99		.expect("generate bindings")100		.write_to_file(out)101		.expect("write bindings");102}