-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
181 lines (162 loc) · 6.16 KB
/
Copy pathbuild.rs
File metadata and controls
181 lines (162 loc) · 6.16 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::env;
use std::path::PathBuf;
use std::fs::File;
use std::str::FromStr;
use std::io::prelude::*;
//use curl::easy::Easy;
use std::process::Command;
fn get_platform_name(majorver : i32,minorver : i32) -> (String,String) {
if cfg!(target_os = "windows") {
if cfg!(target_arch = "x86_64") {
("win64x86".to_string(), format!("mosek64_{}_{}",majorver,minorver))
}
else if cfg!(target_arch = "x86") {
("win32x86".to_string(), format!("mosek{}_{}",majorver,minorver))
}
else {
panic!("Unsupported architecture")
}
}
else if cfg!(target_os = "linux") {
if cfg!(target_arch = "x86_64") {
("linux64x86".to_string(),"mosek64".to_string())
}
else if cfg!(target_arch = "aarch64") {
("linuxaarch64".to_string(),"mosek64".to_string())
}
else {
panic!("Unsupported architecture")
}
}
else if cfg!(target_os = "macos") {
if cfg!(target_arch = "x86_64") {
("osx64x86".to_string(), "mosek64".to_string())
}
else if cfg!(target_arch = "aarch64") {
("osxaarch64".to_string(), "mosek64".to_string())
}
else {
panic!("Unsupported architecture")
}
}
else {
panic!("Unsupported operating system")
}
}
fn find_mosek_installation(pfname : &String, majorver : i32, minorver : i32) -> Option<String> {
let bindirvar = format!("MOSEK_BINDIR_{}{}",majorver,minorver);
let mut bindir_b = PathBuf::new();
match env::var_os(bindirvar.as_str()) {
Some(p) => bindir_b.push(p),
None => {
let inst_base =
match env::var_os("MOSEK_INST_BASE") {
Some(p) => p,
None =>
match env::var_os("HOME") {
Some(p) => p,
None =>
match env::var_os("HOMEPATH") {
Some(p) => {
let mut r = env::var_os("HOMEDRIVE").unwrap();
r.push(p);
r },
None => return None,
}
}
};
bindir_b.push(inst_base);
bindir_b.push("mosek");
bindir_b.push(format!("{}.{}",majorver,minorver));
bindir_b.push("tools");
bindir_b.push("platform");
bindir_b.push(pfname);
bindir_b.push("bin");
},
}
if ! bindir_b.as_path().is_dir() {
return None
//panic!("MOSEK bin directory {} does not exist or is not a directory",bindir_b.as_path().to_str().unwrap());
}
Some(bindir_b.as_path().to_str().unwrap().to_string())
}
fn getmosek(pfname : &String,majorver : i32, minorver : i32) -> String {
let mut outdir = PathBuf::new();
outdir.push(env::var_os("OUT_DIR").unwrap());
let targetdir = outdir.as_path();
let (archname,iszip) = match pfname.as_str() {
"linux64x86" => ("mosektoolslinux64x86.tar.bz2",false),
"linuxaarch64" => ("mosektoolslinuxaarch64.tar.bz2",false),
"osx64x86" => ("mosektoolsosx64x86.tar.bz2",false),
"osxaarch64" => ("mosektoolsosxaarch64.tar.bz2",false),
"win32x86" => ("mosektoolswin32x86.zip",true),
"win64x86" => ("mosektoolswin64x86.zip",true),
_ => panic!("Invalid platform")
};
let mut archfile = PathBuf::new();
archfile.push(targetdir);
archfile.push(archname);
if ! archfile.exists() {
let res = Command::new("curl")
.arg("--silent")
.arg(format!("https://download.mosek.com/stable/{}.{}/version",majorver,minorver).as_str())
.output()
.expect("Failed to get latest version");
let verstr = match String::from_utf8_lossy(res.stdout.as_ref()) {
std::borrow::Cow::Owned(s) => s,
std::borrow::Cow::Borrowed(s) => s.to_string()
};
let ver = verstr.trim();
Command::new("curl")
.arg("-o").arg(archfile.as_path())
.arg("--silent")
.arg(format!("https://download.mosek.com/stable/{}/{}",ver,archname).as_str())
.status()
.expect("Failed to get distro file");
// File written, now we have to unpack
if iszip {
panic!("Not implemented: Unzipping distro on Windows");
}
else {
Command::new("tar")
.arg("xjf").arg(archfile)
.arg("-C").arg(outdir.as_path())
.status()
.expect("Failed to unpack distro");
}
}
let mut res = PathBuf::new();
res.push(outdir.as_path());
res.push("mosek");
res.push(format!("{}.{}",majorver,minorver).as_str());
res.push("tools");
res.push("platform");
res.push(pfname.as_str());
res.push("bin");
res.as_path().to_str().unwrap().to_string()
}
fn readversion(filename : &str) -> (i32,i32) {
let mut mosekverstr = String::new();
match File::open(filename) {
Err(_) => panic!("Failed to open version file '{}'",filename),
Ok(mut f) => { let _ = f.read_to_string(& mut mosekverstr).unwrap(); }
}
match mosekverstr.find('.') {
None => panic!("Invalid version file '{}'",filename),
Some(p) => {
let vmajor : i32 = FromStr::from_str(&mosekverstr[0..p]).unwrap();
let vminor : i32 = FromStr::from_str(&mosekverstr[p+1..mosekverstr.len()-1]).unwrap();
(vmajor,vminor)
}
}
}
fn main() {
// 1. if MOSEK_BINDIR is set, use that
let (mskvermajor,mskverminor) = readversion("MOSEKVERSION");
let (pfname, libname) = get_platform_name(mskvermajor,mskverminor);
let libdir =
if let Some(p) = find_mosek_installation(&pfname,mskvermajor,mskverminor) { p }
else { getmosek(&pfname, mskvermajor, mskverminor) };
println!("cargo:rustc-link-search={}",libdir);
println!("cargo:rustc-flags=-L {} -l {}",libdir,libname);
}