diff --git a/build.rs b/build.rs index 30fcb5d..dd1c808 100644 --- a/build.rs +++ b/build.rs @@ -28,9 +28,11 @@ fn main() { // If the MagickWand bindings are missing, generate them using // rust-bindgen. // - let bindings_path_str = concat!(env!("OUT_DIR"), "/bindings.rs"); - if !Path::new(bindings_path_str).exists() { - let bindgen_path = Path::new(concat!(env!("OUT_DIR"), "/rust-bindgen")); + let out_dir = ::std::env::var("OUT_DIR").unwrap(); + let bindings_path_str = out_dir.clone() + "/bindings.rs"; + if !Path::new(&bindings_path_str).exists() { + let bindgen_path_str = out_dir.clone() + "/rust-bindgen"; + let bindgen_path = Path::new(&bindgen_path_str); if !bindgen_path.exists() { Command::new("git") .arg("clone") @@ -50,20 +52,30 @@ fn main() { let mut bindgen_bin = bindgen_path.to_path_buf(); bindgen_bin.push("target/debug/bindgen"); if !bindgen_bin.exists() { - Command::new("cargo") - .arg("build") - .current_dir(bindgen_path) - .status().unwrap(); + let mut cmd = Command::new("cargo"); + cmd.arg("build").current_dir(bindgen_path); + println!("BINDGEN_BUILD={:?}", cmd); + cmd.status().unwrap(); } + // Get the compiler and linker flags for the MagickWand library. + let mw_cflags_output = Command::new("pkg-config") + .arg("--cflags") + .arg("MagickWand") + .output().unwrap(); + let mw_cflags = std::str::from_utf8(&mw_cflags_output.stdout).unwrap().trim(); + let mw_cflags_arr: Vec<&str> = mw_cflags.split_whitespace().collect(); + println!("CFLAGS={:?}", mw_cflags_arr); + let mw_ldflags_output = Command::new("pkg-config") + .arg("--ldflags") + .arg("MagickWand") + .output().unwrap(); + let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim(); + let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect(); + println!("LDFLAGS={:?}", mw_ldflags_arr); - // Ensure MagickWand-config is in the PATH and report clearly if not. - if !Command::new("which").arg("MagickWand-config").status().unwrap().success() { - panic!("MagickWand-config not in the PATH, please install ImageMagick"); - } - - let gen_h_path = concat!(env!("OUT_DIR"), "/gen.h"); + let gen_h_path = out_dir.clone() + "/gen.h"; // Create the header file that rust-bindgen needs as input. - let mut gen_h = match File::create(gen_h_path) { + let mut gen_h = match File::create(&gen_h_path) { Err(why) => panic!("could not create {} file: {}", gen_h_path, Error::description(&why)), Ok(file) => file }; @@ -72,19 +84,6 @@ fn main() { Ok(_) => () }; - // Get the compiler and linker flags for the MagickWand library. - let mw_cflags_output = Command::new("MagickWand-config") - .arg("--cflags") - .output().unwrap(); - let mw_cflags = std::str::from_utf8(&mw_cflags_output.stdout).unwrap().trim(); - let mw_cflags_arr: Vec<&str> = mw_cflags.split_whitespace().collect(); - let mw_ldflags_output = Command::new("MagickWand-config") - .arg("--ldflags") - .output().unwrap(); - let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim(); - let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect(); - - // Combine all of that in the invocation of rust-bindgen. let mut cmd = &mut Command::new(bindgen_bin); if cfg!(target_os = "macos") { @@ -96,27 +95,29 @@ fn main() { panic!("missing {}, run xcode-select --install", LIBPATH); } cmd.env("DYLD_LIBRARY_PATH", LIBPATH); + + // For the sake of easily building and testing on Mac, include the path + // to MagickWand. Chances are MagickWand is in /usr/local/lib, or + // somewhere else that rustc can find it. + println!("cargo:rustc-link-search=native=/usr/local/lib"); } cmd.args(&mw_cflags_arr[..]) .arg("-builtins") .arg("-o") .arg(bindings_path_str) .args(&mw_ldflags_arr[..]) - .arg(gen_h_path) - .status().unwrap(); + .arg(&gen_h_path); + println!("BINDING_GENERATION={:?}", cmd); + cmd.status().unwrap(); // how to get the output of the command... // let output = Commad::new(...).output().unwrap(); // let out = std::str::from_utf8(&output.stdout).unwrap(); // println!("cargo:output={}", out); // let err = std::str::from_utf8(&output.stderr).unwrap(); // println!("cargo:error={}", err); - match std::fs::remove_file(gen_h_path) { + match std::fs::remove_file(&gen_h_path) { Err(why) => panic!("could not remove {}: {}", gen_h_path, Error::description(&why)), Ok(_) => () } } - // For the sake of easily building and testing on Mac, include the path - // to MagickWand. Chances are MagickWand is in /usr/local/lib, or - // somewhere else that rustc can find it. - println!("cargo:rustc-link-search=native=/usr/local/lib"); }