extern crate curl;
extern crate flate2;
use std::env;
use std::fs::File;
use std::io::BufWriter;
use std::path::{Path, PathBuf};
use std::io::Write;
use flate2::read::GzDecoder;
use curl::easy::Easy;
const BINARIES_URL: &str = "http://sdkbinaries.tonlabs.io";
const BINARIES_VERSION: &str = "0.11.0";
fn main() {
let out = env::var("OUT_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", out);
println!("cargo:rustc-link-lib=dylib=ton_client");
install_binaries();
}
fn extract<P: AsRef<Path>, P2: AsRef<Path>>(archive_path: P, extract_to: P2) {
let file = File::open(archive_path).unwrap();
let mut unzipped = GzDecoder::new(file);
let mut target_file = File::create(extract_to).unwrap();
std::io::copy(&mut unzipped, &mut target_file).unwrap();
}
fn download_file(file_name: &str, download_dir: &PathBuf) {
let binary_url = format!("{}/{}", BINARIES_URL, file_name);
let file_name = download_dir.join(file_name);
if !file_name.exists() {
let f = File::create(&file_name).unwrap();
let mut writer = BufWriter::new(f);
let mut easy = Easy::new();
easy.url(&binary_url).unwrap();
easy.write_function(move |data| Ok(writer.write(data).unwrap()))
.unwrap();
easy.perform().unwrap();
let response_code = easy.response_code().unwrap();
if response_code != 200 {
panic!(
"Unexpected response code {} for {}",
response_code, binary_url
);
}
}
}
fn install_binaries() {
let version = BINARIES_VERSION.replace(".", "_");
let files = if cfg!(target_os="windows") {
vec![
(format!("tonclient_{}_win32_dll.gz", version), "ton_client.dll"),
(format!("tonclient_{}_win32_lib.gz", version), "ton_client.lib")
]
} else if cfg!(target_os="linux") {
vec![(format!("tonclient_{}_linux.gz", version), "libton_client.so")]
} else if cfg!(target_os="macos") {
vec![(format!("tonclient_{}_darwin.gz", version), "libton_client.dylib")]
} else {
panic!("Unknown target OS");
};
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
for (file, target) in &files {
download_file(&file, &out);
extract(out.join(file), out.join(target));
}
}