place generated files in OUT_DIR

This commit is contained in:
Mattis Marjak
2016-05-11 16:52:15 +03:00
parent a5cb97d3cf
commit 85a4b97b70
2 changed files with 28 additions and 14 deletions

View File

@ -28,13 +28,14 @@ fn main() {
// If the MagickWand bindings are missing, generate them using // If the MagickWand bindings are missing, generate them using
// rust-bindgen. // rust-bindgen.
// //
let bindings_path = Path::new("src/bindings.rs"); let bindings_path_str = concat!(env!("OUT_DIR"), "/bindings.rs");
if !bindings_path.exists() { if !Path::new(bindings_path_str).exists() {
let bindgen_path = Path::new("rust-bindgen"); let bindgen_path = Path::new(concat!(env!("OUT_DIR"), "/rust-bindgen"));
if !bindgen_path.exists() { if !bindgen_path.exists() {
Command::new("git") Command::new("git")
.arg("clone") .arg("clone")
.arg("https://github.com/crabtw/rust-bindgen.git") .arg("https://github.com/crabtw/rust-bindgen.git")
.arg(bindgen_path)
.status().unwrap(); .status().unwrap();
// Checkout a version of rust-bindgen that is known to work; // Checkout a version of rust-bindgen that is known to work;
// more recent versions produce code that does not compile (the // more recent versions produce code that does not compile (the
@ -42,26 +43,35 @@ fn main() {
Command::new("git") Command::new("git")
.arg("checkout") .arg("checkout")
.arg("8a51860") .arg("8a51860")
.current_dir("rust-bindgen") .current_dir(bindgen_path)
.status().unwrap(); .status().unwrap();
}
let mut bindgen_bin = bindgen_path.to_path_buf();
bindgen_bin.push("target/debug/bindgen");
if !bindgen_bin.exists() {
Command::new("cargo") Command::new("cargo")
.arg("build") .arg("build")
.current_dir("rust-bindgen") .current_dir(bindgen_path)
.status().unwrap(); .status().unwrap();
} }
// Ensure MagickWand-config is in the PATH and report clearly if not. // Ensure MagickWand-config is in the PATH and report clearly if not.
if !Command::new("which").arg("MagickWand-config").status().unwrap().success() { if !Command::new("which").arg("MagickWand-config").status().unwrap().success() {
panic!("MagickWand-config not in the PATH, please install ImageMagick"); panic!("MagickWand-config not in the PATH, please install ImageMagick");
} }
let gen_h_path = concat!(env!("OUT_DIR"), "/gen.h");
// Create the header file that rust-bindgen needs as input. // Create the header file that rust-bindgen needs as input.
let mut gen_h = match File::create("gen.h") { let mut gen_h = match File::create(gen_h_path) {
Err(why) => panic!("could not create gen.h file: {}", Error::description(&why)), Err(why) => panic!("could not create {} file: {}", gen_h_path, Error::description(&why)),
Ok(file) => file Ok(file) => file
}; };
match gen_h.write_all(HEADER.as_bytes()) { match gen_h.write_all(HEADER.as_bytes()) {
Err(why) => panic!("could not write to gen.h: {}", Error::description(&why)), Err(why) => panic!("could not write to {}: {}", gen_h_path, Error::description(&why)),
Ok(_) => () Ok(_) => ()
}; };
// Get the compiler and linker flags for the MagickWand library. // Get the compiler and linker flags for the MagickWand library.
let mw_cflags_output = Command::new("MagickWand-config") let mw_cflags_output = Command::new("MagickWand-config")
.arg("--cflags") .arg("--cflags")
@ -73,8 +83,10 @@ fn main() {
.output().unwrap(); .output().unwrap();
let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim(); let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim();
let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect(); let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect();
// Combine all of that in the invocation of rust-bindgen. // Combine all of that in the invocation of rust-bindgen.
let mut cmd = &mut Command::new("./rust-bindgen/target/debug/bindgen"); let mut cmd = &mut Command::new(bindgen_bin);
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
// Mac requires that the xcode tools are installed so that // Mac requires that the xcode tools are installed so that
// rustc can find the clang.dylib file. See also issue // rustc can find the clang.dylib file. See also issue
@ -88,9 +100,9 @@ fn main() {
cmd.args(&mw_cflags_arr[..]) cmd.args(&mw_cflags_arr[..])
.arg("-builtins") .arg("-builtins")
.arg("-o") .arg("-o")
.arg("src/bindings.rs") .arg(bindings_path_str)
.args(&mw_ldflags_arr[..]) .args(&mw_ldflags_arr[..])
.arg("gen.h") .arg(gen_h_path)
.status().unwrap(); .status().unwrap();
// how to get the output of the command... // how to get the output of the command...
// let output = Commad::new(...).output().unwrap(); // let output = Commad::new(...).output().unwrap();
@ -98,8 +110,8 @@ fn main() {
// println!("cargo:output={}", out); // println!("cargo:output={}", out);
// let err = std::str::from_utf8(&output.stderr).unwrap(); // let err = std::str::from_utf8(&output.stderr).unwrap();
// println!("cargo:error={}", err); // println!("cargo:error={}", err);
match std::fs::remove_file("gen.h") { match std::fs::remove_file(gen_h_path) {
Err(why) => panic!("could not remove gen.h: {}", Error::description(&why)), Err(why) => panic!("could not remove {}: {}", gen_h_path, Error::description(&why)),
Ok(_) => () Ok(_) => ()
} }
} }

View File

@ -36,7 +36,9 @@ use std::ptr;
use libc::{c_uint, c_double, c_void}; use libc::{c_uint, c_double, c_void};
use filters::FilterType; use filters::FilterType;
mod bindings; mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
/// MagickWand is a Rustic wrapper to the Rust bindings to ImageMagick. /// MagickWand is a Rustic wrapper to the Rust bindings to ImageMagick.
/// ///