-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
65 lines (56 loc) · 2.28 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// build.rs
use std::process::Command;
use std::env::{var};
use std::path::PathBuf;
macro_rules! feature(
($name:expr) => (var(concat!("CARGO_FEATURE_", $name)).is_ok());
);
macro_rules! variable(
($name:expr) => (var($name).unwrap());
);
fn main() {
let kind = if feature!("STATIC") { "static" } else { "dylib" };
let source = PathBuf::from("fortran");
let output = PathBuf::from(variable!("OUT_DIR").replace(r"\", "/"));
let os = if cfg!(target_os = "macos"){"Macos"}
else if cfg!(target_os = "windows"){"Windows"}
else {"Linux"};
let make_cmd = if os == "Windows" { "mingw32-make" } else { "make" };
run(Command::new(make_cmd)
.arg(kind)
.arg(format!("OUTPUT={}", output.display()))
.arg(format!("OSNAME={}", os))
.current_dir(&source));
println!("cargo:rustc-link-search={}", output.display());
println!("cargo:rustc-link-lib={}=lbfgs", kind);
println!("cargo:rustc-link-lib=dylib=gcc");
let target = variable!("TARGET");
let mut fc_lib_type = "dylib";
if target == "x86_64-apple-darwin" || target == "x86_64-pc-windows-gnu" {
fc_lib_type = "static";
// Poke $FC$ for static lib folder
let fc_out = Command::new(variable!("FC"))
.arg("-print-file-name=libgfortran.a")
.output()
.expect("Failed to find libgfortran.a");
let fc_stdout = String::from_utf8(fc_out.stdout).expect("Invalid path to libgfortran.a");
let fc_lib_cwd = PathBuf::from(fc_stdout.to_string());
let fc_lib_pwd = fc_lib_cwd.parent().expect("Path to libgfortran.a not found");
println!("cargo:rustc-link-search={}", fc_lib_pwd.to_str().unwrap());
}
println!("cargo:rustc-link-lib={}=gfortran", fc_lib_type);
if target == "x86_64-apple-darwin" {
println!("cargo:rustc-link-lib={}=quadmath", fc_lib_type);
}
}
fn run(command: &mut Command) {
println!("Running: {:?}", command);
match command.status() {
Ok(status) => if !status.success() {
panic!("`{:?}` failed: {}", command, status);
},
Err(error) => {
panic!("failed to execute `{:?}`: {}", command, error);
},
}
}