use std::path::{Path, PathBuf}; fn main() { println!("cargo:rerun-if-changed=liburing"); // configure liburing let liburing = out_dir().join("liburing"); if liburing.exists() { std::fs::remove_dir_all(&liburing).unwrap(); } let readonly_liburing = manifest().join("liburing"); std::process::Command::new("cp") .arg("--recursive") .arg(&readonly_liburing) .arg(&liburing) .status() .unwrap(); std::process::Command::new("sh") .arg("-c") .arg(format!( "cd {} && ./configure --cc=clang --cxx=clang++ --use-libc", path_to_string(&liburing) )) .status() .unwrap(); // build liburing-ffi.a let src = liburing.join("src"); if lto_enabled() { std::process::Command::new("make") .arg("-C") .arg(&src) .arg("liburing-ffi.a") .arg("CFLAGS=-g -O3 -Wall -Wextra -fno-stack-protector -flto=thin") .status() .unwrap(); } else { std::process::Command::new("make") .arg("-C") .arg(&src) .arg("liburing-ffi.a") .arg("CFLAGS=-g -O3 -Wall -Wextra -fno-stack-protector") .status() .unwrap(); } std::fs::copy(src.join("liburing-ffi.a"), src.join("libminiring.a")).unwrap(); println!("cargo:rustc-link-search={}", path_to_string(&src)); println!("cargo:rustc-link-lib=miniring"); // generate headers.rs let include = src.join("include"); std::fs::copy(src.join("ffi.c"), include.join("wrapper.h")).unwrap(); bindgen::builder() .header(path_to_string(include.join("wrapper.h"))) .allowlist_file(path_to_string(include.join("liburing.h"))) .allowlist_file(path_to_string(include.join("liburing/io_uring.h"))) .anon_fields_prefix("anonymous") .default_enum_style(bindgen::EnumVariation::ModuleConsts) .default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop) .derive_copy(true) .derive_debug(true) .derive_default(false) .explicit_padding(true) .layout_tests(true) .merge_extern_blocks(true) .sort_semantically(true) .use_core() .generate() .unwrap() .write_to_file(include.join("headers.rs")) .unwrap(); } fn manifest() -> PathBuf { std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) .canonicalize() .unwrap() } fn out_dir() -> PathBuf { std::path::Path::new(&std::env::var("OUT_DIR").unwrap()) .canonicalize() .unwrap() } fn path_to_string(path: impl AsRef) -> String { path.as_ref().to_str().unwrap().to_string() } fn lto_enabled() -> bool { std::env::var("CARGO_FEATURE_LTO").is_ok() }