-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
221 lines (186 loc) · 6.65 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
extern crate bindgen;
use glob::glob;
use std::env;
use std::error::Error;
use std::path::PathBuf;
#[cfg(feature = "bundled")]
use tempfile::tempdir;
#[cfg(feature = "bundled")]
use std::fs::File;
#[cfg(feature = "bundled")]
use std::io::Cursor;
#[cfg(feature = "bundled")]
use std::io::Write;
#[cfg(feature = "bundled")]
use std::path::Path;
#[cfg(feature = "bundled")]
pub fn is_bundled_feature_enabled() -> bool {
true
}
#[cfg(not(feature = "bundled"))]
pub fn is_bundled_feature_enabled() -> bool {
false
}
fn _build_from_scip_dir(path: &str) -> bindgen::Builder {
let lib_dir = PathBuf::from(&path).join("lib");
let lib_dir_path = lib_dir.to_str().unwrap();
if lib_dir.exists() {
println!("cargo:warning=Using SCIP from {}", lib_dir_path);
println!("cargo:rustc-link-search={}", lib_dir_path);
#[cfg(windows)]
let lib_dir_path = PathBuf::from(&path).join("bin");
#[cfg(windows)]
println!("cargo:rustc-link-search={}", lib_dir_path.to_str().unwrap());
} else {
panic!(
"{}",
format!(
"{}/lib does not exist, please check your SCIP installation",
path
)
);
}
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_dir_path);
let include_dir = PathBuf::from(&path).join("include");
let include_dir_path = include_dir.to_str().unwrap();
let scip_header_file = PathBuf::from(&path)
.join("include")
.join("scip")
.join("scip.h")
.to_str()
.unwrap()
.to_owned();
let scipdefplugins_header_file = PathBuf::from(&path)
.join("include")
.join("scip")
.join("scipdefplugins.h")
.to_str()
.unwrap()
.to_owned();
bindgen::Builder::default()
.header(scip_header_file)
.header(scipdefplugins_header_file)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg(format!("-I{}", include_dir_path))
}
fn lib_scip_in_dir(path: &str) -> bool {
glob(&format!("{}/lib/libscip*", path)).unwrap().count() > 0
}
fn look_in_scipoptdir_and_conda_env() -> Option<bindgen::Builder> {
let env_vars = vec!["SCIPOPTDIR", "CONDA_PREFIX"];
for env_var_name in env_vars {
println!("cargo:rerun-if-env-changed={}", env_var_name);
let env_var = env::var(env_var_name);
if let Ok(scip_dir) = env_var {
println!("cargo:warning=Looking for SCIP in {}", scip_dir);
if lib_scip_in_dir(&scip_dir) {
return Some(_build_from_scip_dir(&scip_dir));
} else {
println!("cargo:warning=SCIP was not found in {}", scip_dir);
}
} else {
println!("cargo:warning={} is not set", env_var_name);
}
}
return None
}
fn main() -> Result<(), Box<dyn Error>> {
let builder =
if is_bundled_feature_enabled() {
download_scip();
let path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scip_install");
_build_from_scip_dir(path.to_str().unwrap())
} else {
let builder = look_in_scipoptdir_and_conda_env();
if builder.is_some() {
builder.unwrap()
} else {
println!("cargo:warning=SCIP was not found in SCIPOPTDIR or in Conda environemnt");
println!("cargo:warning=Looking for SCIP in system libraries");
let headers_dir_path = "headers/";
let headers_dir = PathBuf::from(headers_dir_path);
let scip_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scip.h")
.to_str()
.unwrap()
.to_owned();
let scipdefplugins_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scipdefplugins.h")
.to_str()
.unwrap()
.to_owned();
bindgen::Builder::default()
.header(scip_header_file)
.header(scipdefplugins_header_file)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg(format!("-I{}", headers_dir_path))
}
};
#[cfg(windows)]
println!("cargo:rustc-link-lib=libscip");
#[cfg(not(windows))]
println!("cargo:rustc-link-lib=scip");
let builder = builder
.blocklist_item("FP_NAN")
.blocklist_item("FP_INFINITE")
.blocklist_item("FP_ZERO")
.blocklist_item("FP_SUBNORMAL")
.blocklist_item("FP_NORMAL")
.parse_callbacks(Box::new(bindgen::CargoCallbacks));
let bindings = builder.generate()?;
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("bindings.rs"))?;
Ok(())
}
#[cfg(feature = "bundled")]
fn download_scip() {
let extract_path = PathBuf::from(env::var("OUT_DIR").unwrap());
if extract_path.join("scip_install").exists() {
println!("cargo:warning=SCIP was previously downloaded, skipping download");
return;
}
let os = env::consts::OS;
let arch = std::env::consts::ARCH;
println!("cargo:warning=Detected OS: {}", os);
println!("cargo:warning=Detected arch: {}", arch);
let os_string = if os == "linux" && arch == "x86_64" {
"linux"
} else if os == "macos" && arch == "x86_64" {
"macos"
} else if os == "macos" && arch == "aarch64" {
"macos-arm"
} else if os == "windows" && arch == "x86_64" {
"windows"
} else {
panic!("Unsupported OS-arch combination: {}-{}", os, arch);
};
let url = format!(
"https://github.com/scipopt/scip-sys/releases/download/v0.1.9/libscip-{os_string}.zip"
);
download_and_extract_zip(&url, &extract_path).unwrap_or_else(
|e| panic!("Failed to download and extract SCIP: {}", e),
);
}
#[cfg(not(feature = "bundled"))]
fn download_scip() {}
#[cfg(feature = "bundled")]
fn download_and_extract_zip(url: &str, extract_path: &Path) -> Result<(), Box<dyn Error>> {
// Download the ZIP file
println!("cargo:warning=Downloading from {}", url);
let response = reqwest::blocking::Client::new().get(url).send()?;
let content = response.bytes()?;
// Create a temporary file to store the ZIP
let dir = tempdir()?;
let zip_path = dir.path().join("scip.zip");
let mut temp_file = File::create(&zip_path)?;
temp_file.write_all(&content)?;
let target_dir = PathBuf::from(extract_path);
println!("cargo:warning=Downloaded to {:?}", zip_path);
println!("cargo:warning=Extracting to {:?}", target_dir);
zip_extract::extract(Cursor::new(
std::fs::read(zip_path).unwrap(),
), &target_dir, false)?;
Ok(())
}