-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.rs
389 lines (340 loc) · 13.1 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::{
env,
ffi::OsString,
fs,
io,
io::{ Error, ErrorKind },
path::{ Path, PathBuf },
process::{ exit, Command },
};
#[cfg(target_family = "unix")]
use std::{
os::unix::ffi::OsStrExt,
ffi::OsStr,
};
#[cfg(target_family = "windows")]
use std::os::windows::ffi::OsStringExt;
#[allow(dead_code)]
struct InstallationPaths {
r_home: PathBuf,
include: PathBuf,
library: PathBuf,
}
#[allow(dead_code)]
#[derive(Debug)]
struct RVersionInfo {
major: String,
minor: String,
patch: String,
devel: String,
}
// frustratingly, something like the following does not exist in an
// OS-independent way in Rust
#[cfg(target_family = "unix")]
fn byte_array_to_os_string(bytes: &[u8]) -> OsString {
let os_str = OsStr::from_bytes(bytes);
os_str.to_os_string()
}
// convert bytes to wide-encoded characters on Windows
// from: https://stackoverflow.com/a/40456495/4975218
#[cfg(target_family = "windows")]
fn wide_from_console_string(bytes: &[u8]) -> Vec<u16> {
assert!(bytes.len() < std::i32::MAX as usize);
let mut wide;
let mut len;
unsafe {
let cp = kernel32::GetConsoleCP();
len = kernel32::MultiByteToWideChar(cp, 0, bytes.as_ptr() as *const i8, bytes.len() as i32, std::ptr::null_mut(), 0);
wide = Vec::with_capacity(len as usize);
len = kernel32::MultiByteToWideChar(cp, 0, bytes.as_ptr() as *const i8, bytes.len() as i32, wide.as_mut_ptr(), len);
wide.set_len(len as usize);
}
wide
}
#[cfg(target_family = "windows")]
fn byte_array_to_os_string(bytes: &[u8]) -> OsString {
// first, use Windows API to convert to wide encoded
let wide = wide_from_console_string(bytes);
// then, use `std::os::windows::ffi::OsStringExt::from_wide()`
OsString::from_wide(&wide)
}
fn probe_r_paths() -> io::Result<InstallationPaths> {
// First we locate the R home
let r_home = match env::var_os("R_HOME") {
// If the environment variable R_HOME is set we use it
Some(s) => PathBuf::from(s),
// Otherwise, we try to execute `R` to find `R_HOME`. Note that this is
// discouraged, see Section 1.6 of "Writing R Extensions"
// https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Writing-portable-packages
_ => {
let rout = Command::new("R")
.args(&[
"-s",
"-e",
r#"cat(normalizePath(R.home()))"#
])
.output()?;
let rout = byte_array_to_os_string(&rout.stdout);
if !rout.is_empty() {
PathBuf::from(rout)
} else {
return Err(Error::new(ErrorKind::Other, "Cannot find R home."));
}
}
};
// Now the library location. On Windows, it depends on the target architecture
let pkg_target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let library = if cfg!(target_os = "windows") {
if pkg_target_arch == "x86_64" {
Path::new(&r_home)
.join("bin")
.join("x64")
} else if pkg_target_arch == "x86" {
Path::new(&r_home)
.join("bin")
.join("i386")
} else {
panic!("Unknown architecture")
}
} else {
Path::new(&r_home).join("lib")
};
// Finally the include location. It may or may not be located under R home
let include = match env::var_os("R_INCLUDE_DIR") {
// If the environment variable R_INCLUDE_DIR is set we use it
Some(s) => PathBuf::from(s),
// Otherwise, we try to execute `R` to find the include dir. Here,
// we're using the R home we found earlier, to make sure we're consistent.
_ => {
let r_binary = if cfg!(target_os = "windows") {
Path::new(&library)
.join("R.exe")
} else {
Path::new(&r_home)
.join("bin")
.join("R")
};
let out = Command::new(&r_binary)
.args(&[
"-s",
"-e",
r#"cat(normalizePath(R.home('include')))"#
])
.output()?;
// if there are any errors we print them out, helps with debugging
if !out.stderr.is_empty() {
println!("> {}",
byte_array_to_os_string(&out.stderr)
.as_os_str()
.to_string_lossy()
);
}
let rout = byte_array_to_os_string(&out.stdout);
if !rout.is_empty() {
PathBuf::from(rout)
} else {
return Err(Error::new(ErrorKind::Other, "Cannot find R include."));
}
}
};
Ok(InstallationPaths {
r_home,
include,
library,
})
}
fn get_r_version_strings(r_paths: &InstallationPaths) -> io::Result<RVersionInfo> {
let r_binary = if cfg!(target_os = "windows") {
Path::new(&r_paths.library)
.join("R.exe")
} else {
Path::new(&r_paths.r_home)
.join("bin")
.join("R")
};
let out = Command::new(&r_binary)
.args(&[
"-s",
"-e",
r#"v <- strsplit(R.version$minor, '.', fixed = TRUE)[[1]];devel <- isTRUE(grepl('devel', R.version$status, fixed = TRUE));cat(R.version$major, v[1], paste0(v[2:length(v)], collapse = '.'), devel, sep = '\n')"#
])
.output()?;
let out = byte_array_to_os_string(&out.stdout)
.as_os_str()
.to_string_lossy()
.into_owned();
let mut lines = out.lines();
let major = match lines.next() {
Some(line) => line.to_string(),
_ => return Err(Error::new(ErrorKind::Other, "Cannot find R major version")),
};
let minor = match lines.next() {
Some(line) => line.to_string(),
_ => return Err(Error::new(ErrorKind::Other, "Cannot find R minor version")),
};
let patch = match lines.next() {
Some(line) => line.to_string(),
_ => return Err(Error::new(ErrorKind::Other, "Cannot find R patch level")),
};
let devel = match lines.next() {
Some(line) => if line == "TRUE" {
"-devel".to_string()
} else {
"".to_string()
},
_ => return Err(Error::new(ErrorKind::Other, "Cannot find R development status")),
};
Ok(RVersionInfo {
major,
minor,
patch,
devel,
})
}
#[cfg(feature = "use-bindgen")]
/// Generate bindings by calling bindgen.
fn generate_bindings(r_paths: &InstallationPaths) {
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let mut bindgen_builder = bindgen::Builder::default()
// These constants from libm break bindgen.
.blacklist_item("FP_NAN")
.blacklist_item("FP_INFINITE")
.blacklist_item("FP_ZERO")
.blacklist_item("FP_SUBNORMAL")
.blacklist_item("FP_NORMAL")
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks));
let target = env::var("TARGET").expect("Could not get the target triple");
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
println!("Generating bindings for target: {}, os: {}, architecture: {}", target, target_os, target_arch);
// Point to the correct headers
bindgen_builder = bindgen_builder.clang_args(&[
format!("-I{}", r_paths.include.display()),
format!("--target={}", target)
]);
// allow injection of an alternative include path to libclang
if let Some(alt_include) = env::var_os("LIBRSYS_LIBCLANG_INCLUDE_PATH") {
bindgen_builder = bindgen_builder.clang_arg(
format!("-I{}", PathBuf::from(alt_include).display()),
);
}
// Blacklist some types on i686
// https://github.com/rust-lang/rust-bindgen/issues/1823
// https://github.com/rust-lang/rust/issues/54341
// https://github.com/extendr/libR-sys/issues/39
if target_os == "windows" && target_arch == "x86" {
bindgen_builder =
bindgen_builder
.blacklist_item("max_align_t")
.blacklist_item("__mingw_ldbl_type_t");
}
// Finish the builder and generate the bindings.
let bindings = bindgen_builder
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Extract the version number from the R headers.
let version_matcher = regex::Regex::new(r"pub const R_VERSION ?: ?u32 = (\d+)").unwrap();
if let Some(version) = version_matcher.captures(bindings.to_string().as_str()) {
let version = version.get(1).unwrap().as_str().parse::<u32>().unwrap();
println!("cargo:r_version={}", version);
} else {
panic!("failed to find R_VERSION");
}
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings to default output path!");
// Also write the bindings to a folder specified by LIBRSYS_BINDINGS_OUTPUT_PATH, if defined
if let Some(alt_target) = env::var_os("LIBRSYS_BINDINGS_OUTPUT_PATH") {
let out_path = PathBuf::from(alt_target);
// if folder doesn't exist, try to create it
if !out_path.exists() {
fs::create_dir(&out_path)
.expect(&format!("Couldn't create output directory for bindings: {}", out_path.display()));
}
let version_info = get_r_version_strings(r_paths).expect("Could not obtain R version");
let out_file = out_path.join(
format!(
"bindings-{}-{}-R{}.{}{}.rs",
target_os, target_arch, version_info.major, version_info.minor, version_info.devel
)
);
bindings
.write_to_file(&out_file)
.expect(&format!("Couldn't write bindings: {}", out_file.display()));
}
}
#[allow(dead_code)]
/// Retrieve bindings from cache, if available. Errors out otherwise.
fn retrieve_prebuild_bindings(r_paths: &InstallationPaths) {
let version_info = get_r_version_strings(r_paths).expect("Could not obtain R version");
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let bindings_path = PathBuf::from(
env::var_os("LIBRSYS_BINDINGS_PATH")
.unwrap_or(OsString::from("bindings"))
);
// we try a few different file names, from more specific to less specific
let bindings_file_full = PathBuf::from(
format!(
"bindings-{}-{}-R{}.{}{}.rs",
target_os, target_arch, version_info.major, version_info.minor, version_info.devel
)
);
let bindings_file_novers = PathBuf::from(
format!("bindings-{}-{}.rs", target_os, target_arch)
);
let mut from = bindings_path.join(bindings_file_full);
if !from.exists() {
from = bindings_path.join(bindings_file_novers);
if !from.exists() {
panic!(
format!(
"Cannot find libR-sys bindings file for R {}.{}.{}{} on {} in {}. Consider compiling with default features enabled.",
version_info.major, version_info.minor, version_info.patch, version_info.devel, target_os, bindings_path.display()
)
)
} else {
println!(
"Warning: using generic {}-{} libR-sys bindings. These may not work for R {}.{}.{}{}.",
target_os, target_arch, version_info.major, version_info.minor, version_info.patch, version_info.devel
);
}
}
fs::copy(
&from,
PathBuf::from(env::var_os("OUT_DIR").unwrap())
.join("bindings.rs")
).expect("No precomputed bindings available!");
println!("cargo:rerun-if-changed={}", from.display());
}
fn main() {
let r_paths = probe_r_paths();
let r_paths = match r_paths {
Ok(result) => result,
Err(error) => {
println!("Problem locating local R install: {:?}", error);
exit(1);
}
};
println!("cargo:rustc-env=R_HOME={}", r_paths.r_home.display());
println!("cargo:r_home={}", r_paths.r_home.display()); // Becomes DEP_R_R_HOME for clients
// make sure cargo links properly against library
println!("cargo:rustc-link-search={}", r_paths.library.display());
println!("cargo:rustc-link-lib=dylib=R");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.h");
#[cfg(feature = "use-bindgen")]
generate_bindings(&r_paths);
#[cfg(not(feature = "use-bindgen"))]
retrieve_prebuild_bindings(&r_paths);
}