use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Stdio};
fn fail_on_empty_directory(name: &str) {
if fs::read_dir(name).unwrap().count() == 0 {
println!(
"The `{}` directory is empty, did you forget to pull the submodules?",
name
);
println!("Try `git submodule update --init --recursive`");
panic!();
}
}
fn main() {
fail_on_empty_directory("mupdf");
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=wrapper.c");
let out_dir = env::var("OUT_DIR").unwrap();
let current_dir = env::current_dir().unwrap();
let mupdf_dir = current_dir.join("mupdf");
let output = Command::new("make")
.arg("libs")
.arg(format!("OUT={}", out_dir))
.arg("USE_SYSTEM_LIBS=no")
.arg("HAVE_X11=no")
.arg("HAVE_GLUT=no")
.arg("HAVE_CURL=no")
.arg("XCFLAGS=-DNOTO_SMALL -DNO_CJK")
.current_dir(mupdf_dir)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.expect("make failed");
if output.status.code() != Some(0) {
panic!("{:?}", String::from_utf8(output.stdout).unwrap());
}
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=mupdf");
println!("cargo:rustc-link-lib=static=mupdf-third");
let mut build = cc::Build::new();
build.file("wrapper.c");
build.include("./mupdf/include");
build.compile("libmupdf-wrapper.a");
println!("cargo:rustc-link-lib=static=mupdf-wrapper");
let bindings = bindgen::Builder::default()
.clang_arg("-I./mupdf/include")
.header("wrapper.h")
.header("wrapper.c")
.whitelist_function("fz_.*")
.whitelist_function("pdf_.*")
.whitelist_function("ucdn_.*")
.whitelist_function("mupdf_.*")
.whitelist_type("fz_.*")
.whitelist_type("pdf_.*")
.whitelist_var("fz_.*")
.whitelist_var("FZ_.*")
.whitelist_var("pdf_.*")
.whitelist_var("PDF_.*")
.whitelist_var("UCDN_.*")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}