| 1 |
use std::path::PathBuf; |
| 2 |
use std::{ |
| 3 |
io::{self, Write}, |
| 4 |
process::{Command, Stdio}, |
| 5 |
}; |
| 6 |
|
| 7 |
fn main() { |
| 8 |
println!("cargo:rerun-if-changed=build.rs"); |
| 9 |
|
| 10 |
let is_static = cfg!(feature = "static"); |
| 11 |
let is_shared = cfg!(feature = "shared"); |
| 12 |
let target = std::env::var("TARGET").unwrap(); |
| 13 |
let out_dir = PathBuf::from(&std::env::var("OUT_DIR").unwrap()); |
| 14 |
|
| 15 |
// Copy isa-l source into out; not allow to modify things outside of out dir |
| 16 |
let src_dir = out_dir.join("isa-l"); |
| 17 |
if src_dir.exists() { |
| 18 |
std::fs::remove_dir_all(&src_dir).unwrap(); // maybe from a previous build |
| 19 |
} |
| 20 |
copy_dir::copy_dir("isa-l", &src_dir).unwrap(); |
| 21 |
|
| 22 |
let install_path = std::env::var("ISAL_INSTALL_PREFIX") |
| 23 |
.map(|p| PathBuf::from(&p).clone()) |
| 24 |
.unwrap_or(out_dir.clone()); |
| 25 |
|
| 26 |
let current_dir = std::env::current_dir().unwrap(); |
| 27 |
std::env::set_current_dir(&src_dir).unwrap(); |
| 28 |
|
| 29 |
// build from source |
| 30 |
#[cfg(not(feature = "use-system-isal"))] |
| 31 |
{ |
| 32 |
#[cfg(not(target_os = "windows"))] |
| 33 |
let cmd = { |
| 34 |
let status = Command::new("./autogen.sh") |
| 35 |
.stdout(Stdio::piped()) |
| 36 |
.stderr(Stdio::piped()) |
| 37 |
.output() |
| 38 |
.unwrap(); |
| 39 |
io::stdout().write_all(&status.stdout).unwrap(); |
| 40 |
io::stderr().write_all(&status.stderr).unwrap(); |
| 41 |
if !status.status.success() { |
| 42 |
panic!("autogen failed"); |
| 43 |
} |
| 44 |
|
| 45 |
let compiler = cc::Build::new().get_compiler(); |
| 46 |
let cflags = compiler.cflags_env().into_string().unwrap(); |
| 47 |
|
| 48 |
let mut configure_args = vec![ |
| 49 |
format!("--prefix={}", install_path.display()), |
| 50 |
format!("--host={}", target), |
| 51 |
format!("--enable-static={}", if is_static { "yes" } else { "no" }), |
| 52 |
format!("--enable-shared={}", if is_shared { "yes" } else { "no" }), |
| 53 |
format!("CFLAGS={}", cflags), |
| 54 |
format!("CC={}", compiler.path().display()), |
| 55 |
]; |
| 56 |
|
| 57 |
if !cfg!(target_os = "macos") { |
| 58 |
let ldflag = if is_static { "static" } else { "shared" }; |
| 59 |
configure_args.push(format!("LDFLAGS=-{}", ldflag)); |
| 60 |
configure_args.push("--with-pic=yes".to_string()); |
| 61 |
} |
| 62 |
|
| 63 |
let status = Command::new("./configure") |
| 64 |
.args(&configure_args) |
| 65 |
.stdout(Stdio::piped()) |
| 66 |
.stderr(Stdio::piped()) |
| 67 |
.output() |
| 68 |
.unwrap(); |
| 69 |
io::stdout().write_all(&status.stdout).unwrap(); |
| 70 |
io::stderr().write_all(&status.stderr).unwrap(); |
| 71 |
if !status.status.success() { |
| 72 |
panic!("configure failed"); |
| 73 |
} |
| 74 |
|
| 75 |
Command::new("make") |
| 76 |
.args(&["install-libLTLIBRARIES"]) |
| 77 |
.stdout(Stdio::piped()) |
| 78 |
.stderr(Stdio::piped()) |
| 79 |
.spawn() |
| 80 |
}; |
| 81 |
|
| 82 |
#[cfg(target_os = "windows")] |
| 83 |
let mut cmd = { |
| 84 |
Command::new("nmake") |
| 85 |
.args(["-f", "Makefile.nmake"]) |
| 86 |
.stdout(Stdio::piped()) |
| 87 |
.stderr(Stdio::piped()) |
| 88 |
.spawn() |
| 89 |
}; |
| 90 |
|
| 91 |
std::env::set_current_dir(¤t_dir).unwrap(); |
| 92 |
|
| 93 |
let output = cmd.unwrap().wait_with_output().unwrap(); |
| 94 |
io::stdout().write_all(&output.stdout).unwrap(); |
| 95 |
io::stderr().write_all(&output.stderr).unwrap(); |
| 96 |
if !output.status.success() { |
| 97 |
panic!("Building isa-l failed"); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
let libname = if cfg!(target_os = "windows") { |
| 102 |
println!("cargo:rustc-link-search=native={}", src_dir.display()); |
| 103 |
if cfg!(feature = "static") { |
| 104 |
"isa-l_static" |
| 105 |
} else { |
| 106 |
"isa-l" |
| 107 |
} |
| 108 |
} else { |
| 109 |
for subdir in ["bin", "lib", "lib64"] { |
| 110 |
let search_path = install_path.join(subdir); |
| 111 |
println!("cargo:rustc-link-search=native={}", search_path.display()); |
| 112 |
} |
| 113 |
"isal" |
| 114 |
}; |
| 115 |
|
| 116 |
#[cfg(feature = "static")] |
| 117 |
println!("cargo:rustc-link-lib=static={}", libname); |
| 118 |
|
| 119 |
#[cfg(feature = "shared")] |
| 120 |
println!("cargo:rustc-link-lib={}", libname); |
| 121 |
|
| 122 |
#[cfg(feature = "regenerate-bindings")] |
| 123 |
{ |
| 124 |
let out = PathBuf::from(&(format!("{}/igzip_lib.rs", std::env::var("OUT_DIR").unwrap()))); |
| 125 |
bindgen::Builder::default() |
| 126 |
// The input header we would like to generate |
| 127 |
// bindings for. |
| 128 |
.header("isa-l/include/igzip_lib.h") |
| 129 |
// Tell cargo to invalidate the built crate whenever any of the |
| 130 |
// included header files changed. |
| 131 |
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
| 132 |
.blocklist_type("__uint64_t_") |
| 133 |
.blocklist_type("__size_t") |
| 134 |
// Replaced by libc::FILE |
| 135 |
.blocklist_type("FILE") |
| 136 |
.blocklist_type("_IO_FILE") |
| 137 |
.blocklist_type("_IO_codecvt") |
| 138 |
.blocklist_type("_IO_wide_data") |
| 139 |
.blocklist_type("_IO_marker") |
| 140 |
.blocklist_type("_IO_lock_t") |
| 141 |
// Replaced by i64 |
| 142 |
.blocklist_type("LARGE_INTEGER") |
| 143 |
// Replaced by libc::timespec |
| 144 |
.blocklist_type("timespec") |
| 145 |
// etc |
| 146 |
.blocklist_type("__time_t") |
| 147 |
.blocklist_type("__syscall_slong_t") |
| 148 |
// .blocklist_type("__off64_t") |
| 149 |
.blocklist_type("__off_t") |
| 150 |
.size_t_is_usize(true) |
| 151 |
.generate() |
| 152 |
.expect("Unable to generate bindings") |
| 153 |
.write_to_file(out) |
| 154 |
.unwrap(); |
| 155 |
} |
| 156 |
} |