xref: /drstd/build.rs (revision b38c79420b3f9b2af3cf74b30d9faaadb20eb8b0)
1507ea70eSGnoCiYeH extern crate cbindgen;
2b4db6069SGnoCiYeH extern crate cc;
3b4db6069SGnoCiYeH 
4*b38c7942SLoGin use std::fs::DirEntry;
5507ea70eSGnoCiYeH 
6507ea70eSGnoCiYeH // include src/header directories that don't start with '_'
7*b38c7942SLoGin #[allow(dead_code)]
include_dir(d: &DirEntry) -> bool8507ea70eSGnoCiYeH fn include_dir(d: &DirEntry) -> bool {
9507ea70eSGnoCiYeH     d.metadata().map(|m| m.is_dir()).unwrap_or(false)
10507ea70eSGnoCiYeH         && d.path()
11507ea70eSGnoCiYeH             .iter()
12507ea70eSGnoCiYeH             .nth(2)
13507ea70eSGnoCiYeH             .map_or(false, |c| c.to_str().map_or(false, |x| !x.starts_with("_")))
14507ea70eSGnoCiYeH }
15507ea70eSGnoCiYeH 
169670759bSGnoCiYeH // fn generate_bindings(cbindgen_config_path: &Path) {
179670759bSGnoCiYeH //     let relative_path = cbindgen_config_path
189670759bSGnoCiYeH //         .strip_prefix("src/header")
199670759bSGnoCiYeH //         .ok()
209670759bSGnoCiYeH //         .and_then(|p| p.parent())
219670759bSGnoCiYeH //         .and_then(|p| p.to_str())
229670759bSGnoCiYeH //         .unwrap()
239670759bSGnoCiYeH //         .replace("_", "/");
249670759bSGnoCiYeH //     let header_path = Path::new("target/include")
259670759bSGnoCiYeH //         .join(&relative_path)
269670759bSGnoCiYeH //         .with_extension("h");
279670759bSGnoCiYeH //     let mod_path = cbindgen_config_path.with_file_name("mod.rs");
289670759bSGnoCiYeH //     let config = cbindgen::Config::from_file(cbindgen_config_path).unwrap();
299670759bSGnoCiYeH //     cbindgen::Builder::new()
309670759bSGnoCiYeH //         .with_config(config)
319670759bSGnoCiYeH //         .with_src(mod_path)
329670759bSGnoCiYeH //         .generate()
339670759bSGnoCiYeH //         .expect("Unable to generate bindings")
349670759bSGnoCiYeH //         .write_to_file(header_path);
359670759bSGnoCiYeH // }
36507ea70eSGnoCiYeH 
main()37b4db6069SGnoCiYeH fn main() {
38*b38c7942SLoGin     // let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
39507ea70eSGnoCiYeH 
40507ea70eSGnoCiYeH     // Generate C includes
41507ea70eSGnoCiYeH     // - based on contents of src/header/**
42507ea70eSGnoCiYeH     // - headers written to target/include
439670759bSGnoCiYeH     // fs::read_dir(&Path::new("src/header"))
449670759bSGnoCiYeH     //     .unwrap()
459670759bSGnoCiYeH     //     .into_iter()
469670759bSGnoCiYeH     //     .filter_map(Result::ok)
479670759bSGnoCiYeH     //     .filter(|d| include_dir(d))
489670759bSGnoCiYeH     //     .map(|d| d.path().as_path().join("cbindgen.toml"))
499670759bSGnoCiYeH     //     .filter(|p| p.exists())
509670759bSGnoCiYeH     //     .for_each(|p| {
519670759bSGnoCiYeH     //         println!("cargo:rerun-if-changed={:?}", p.parent().unwrap());
529670759bSGnoCiYeH     //         println!("cargo:rerun-if-changed={:?}", p);
539670759bSGnoCiYeH     //         println!("cargo:rerun-if-changed={:?}", p.with_file_name("mod.rs"));
549670759bSGnoCiYeH     //         generate_bindings(&p);
559670759bSGnoCiYeH     //     });
56507ea70eSGnoCiYeH 
579670759bSGnoCiYeH     // // 指定链接搜索路径
589670759bSGnoCiYeH     // println!("cargo:rustc-link-search=native=./dlibc");
59507ea70eSGnoCiYeH 
609670759bSGnoCiYeH     // // 链接 dlibc 库
619670759bSGnoCiYeH     // println!("cargo:rustc-link-lib=dlibc");
62b4db6069SGnoCiYeH }
63