forked from danielpclark/rutie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
409 lines (356 loc) · 11.9 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::{
collections::{HashMap, HashSet},
env,
ffi::OsString,
path::PathBuf,
process::Command,
};
#[cfg(target_os = "windows")]
use std::path::Path;
#[cfg(not(target_os = "macos"))]
use std::fs;
macro_rules! ci_stderr_log {
() => (eprint!("\n"));
($($arg:tt)*) => ({
if env::var_os("CI_STDERR_LOG").is_some() { eprintln!($($arg)*) }
})
}
fn rbconfig(key: &str) -> String {
let ruby = env::var_os("RUBY").unwrap_or(OsString::from("ruby"));
let config = Command::new(ruby)
.arg("-e")
.arg(format!("print RbConfig::CONFIG['{}']", key))
.output()
.unwrap_or_else(|e| panic!("ruby not found: {}", e));
String::from_utf8(config.stdout).expect("RbConfig value not UTF-8!")
}
#[cfg(not(target_os = "macos"))]
fn macos_static_ruby_dep() {}
#[cfg(target_os = "macos")]
fn macos_static_ruby_dep() {
println!("cargo:rustc-link-lib=framework=Foundation");
}
#[cfg(not(target_os = "windows"))]
fn windows_static_ruby_dep() {}
// Windows needs ligmp-10.dll as gmp.lib
#[cfg(target_os = "windows")]
fn windows_static_ruby_dep() {
Command::new("build/windows/vcbuild.cmd")
.arg("-arch=x64")
.arg("-host_arch=x64")
.arg("&&")
.arg("dumpbin")
.arg("/exports")
.arg("/out:exports.txt")
.arg(format!(
"{}/ruby_builtin_dlls/libgmp-10.dll",
rbconfig("bindir")
))
.output()
.unwrap();
Command::new("build/windows/exports.bat").output().unwrap();
let deps_dir = Path::new("target")
.join(env::var_os("PROFILE").unwrap())
.join("deps");
Command::new("build/windows/vcbuild.cmd")
.arg("-arch=x64")
.arg("-host_arch=x64")
.arg("&&")
.arg("lib")
.arg("/def:exports.def")
.arg("/name:gmp")
.arg(format!("/libpath:{}/ruby_builtin_dlls", rbconfig("bindir")))
.arg("/machine:x64")
.arg(format!("/out:{}/gmp.lib", deps_dir.to_string_lossy()))
.output()
.unwrap();
fs::remove_file("exports.def").expect("couldn't remove exports.def");
fs::remove_file("exports.txt").expect("couldn't remove exports.txt");
}
fn use_static() {
if let Some(location) = env::var_os("RUBY_STATIC_PATH").map(|s| s.to_string_lossy().to_string())
{
println!("cargo:rustc-link-search={}", location);
}
// If Windows
windows_static_ruby_dep();
// If Mac OS
macos_static_ruby_dep();
// **Flags must be last in order for linking!**
static_linker_args();
ci_stderr_log!("Using static linker flags");
}
fn use_dylib() {
println!("cargo:rustc-link-search={}", rbconfig("libdir"));
dynamic_linker_args();
ci_stderr_log!("Using dynamic linker flags");
}
#[cfg(target_os = "windows")]
fn delete<'a>(s: &'a str, from: &'a str) -> String {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in s.match_indices(from) {
result.push_str(unsafe { s.get_unchecked(last_end..start) });
last_end = start + part.len();
}
result.push_str(unsafe { s.get_unchecked(last_end..s.len()) });
result
}
#[cfg(target_os = "windows")]
fn purge_refptr_text() {
let buffer = fs::read_to_string("exports.def").expect("Failed to read 'exports.def'");
fs::write("exports.def", delete(&buffer, ".refptr."))
.expect("Failed to write update to 'exports.def'");
}
#[cfg(target_os = "windows")]
fn windows_support() {
println!("cargo:rustc-link-search={}", rbconfig("bindir"));
let mingw_libs: OsString = env::var_os("MINGW_LIBS").unwrap_or(OsString::from(format!(
"{}/ruby_builtin_dlls",
rbconfig("bindir")
)));
println!("cargo:rustc-link-search={}", mingw_libs.to_string_lossy());
let deps_dir = Path::new("target")
.join(env::var_os("PROFILE").unwrap())
.join("deps");
let libruby_so = rbconfig("LIBRUBY_SO");
let ruby_dll = Path::new(&libruby_so);
let name = ruby_dll.file_stem().unwrap();
let target = deps_dir.join(format!("{}.lib", name.to_string_lossy()));
Command::new("build/windows/vcbuild.cmd")
.arg("-arch=x64")
.arg("-host_arch=x64")
.arg("&&")
.arg("dumpbin")
.arg("/exports")
.arg("/out:exports.txt")
.arg(Path::new(&rbconfig("bindir")).join(&libruby_so))
.output()
.unwrap();
Command::new("build/windows/exports.bat").output().unwrap();
purge_refptr_text();
Command::new("build/windows/vcbuild.cmd")
.arg("-arch=x64")
.arg("-host_arch=x64")
.arg("&&")
.arg("lib")
.arg("/def:exports.def")
.arg(format!("/name:{}", name.to_string_lossy()))
.arg(format!("/libpath:{}", rbconfig("bindir")))
.arg("/machine:x64")
.arg(format!("/out:{}", target.to_string_lossy()))
.output()
.unwrap();
fs::remove_file("exports.def").expect("couldn't remove exports.def");
fs::remove_file("exports.txt").expect("couldn't remove exports.txt");
}
#[cfg(not(target_os = "windows"))]
fn windows_support() {}
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd"
))]
use std::os::unix::fs::symlink;
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd"
))]
fn ruby_lib_link_name() -> String {
// Rust with linker search paths doesn't seem to use those paths
// but rather resorts to the systems Ruby. So we symlink into
// our own deps directory for it to work.
let so_file = format!("libruby.so.{}.{}", rbconfig("MAJOR"), rbconfig("MINOR"));
let out_dir = env::var("OUT_DIR").unwrap();
let working_dir = out_dir.splitn(2, "/target").next().unwrap();
let destination = format!(
"{}/target/{}/deps",
working_dir,
env::var("PROFILE").unwrap()
);
let _ = fs::create_dir_all(&destination).expect("create_dir_all fail");
let source = format!("{}/{}", rbconfig("libdir"), so_file);
let target = format!("{}/{}", destination, so_file);
if fs::read_link(&target).is_err() {
let _ = symlink(source, target).expect("symlink fail");
}
rbconfig("RUBY_SO_NAME")
}
#[cfg(target_os = "macos")]
fn ruby_lib_link_name() -> String {
format!(
"{}.{}.{}",
rbconfig("RUBY_BASE_NAME"),
rbconfig("MAJOR"),
rbconfig("MINOR")
)
}
#[cfg(target_os = "windows")]
fn ruby_lib_link_name() -> String {
rbconfig("RUBY_SO_NAME")
}
fn dynamic_linker_args() {
let mut library = Library::new();
library.parse_libs_cflags(rbconfig("LIBRUBYARG_SHARED").as_bytes(), false);
println!("cargo:rustc-link-lib=dylib={}", ruby_lib_link_name());
library.parse_libs_cflags(rbconfig("LIBS").as_bytes(), false);
}
fn static_linker_args() {
let mut library = Library::new();
library.parse_libs_cflags(rbconfig("LIBRUBYARG_SHARED").as_bytes(), true);
library.parse_libs_cflags(
format!("-l{}-static", rbconfig("RUBY_SO_NAME")).as_bytes(),
true,
);
library.parse_libs_cflags(rbconfig("MAINLIBS").as_bytes(), false);
}
#[derive(Debug)]
pub struct Library {
pub libs: Vec<String>,
pub link_paths: Vec<PathBuf>,
pub frameworks: Vec<String>,
pub framework_paths: Vec<PathBuf>,
pub include_paths: Vec<PathBuf>,
pub defines: HashMap<String, Option<String>>,
pub version: String,
_priv: (),
}
impl Library {
fn new() -> Library {
Library {
libs: Vec::new(),
link_paths: Vec::new(),
include_paths: Vec::new(),
frameworks: Vec::new(),
framework_paths: Vec::new(),
defines: HashMap::new(),
version: String::new(),
_priv: (),
}
}
fn parse_libs_cflags(&mut self, output: &[u8], statik: bool) {
let mut is_msvc = false;
if let Ok(target) = env::var("TARGET") {
if target.contains("msvc") {
is_msvc = true;
}
}
let words = split_flags(output);
let parts = words
.iter()
.filter(|l| l.len() > 2)
.map(|arg| (&arg[0..2], &arg[2..]))
.collect::<HashSet<_>>();
let mut dirs = Vec::new();
for &(flag, val) in &parts {
match flag {
"-L" => {
let meta = format!("rustc-link-search=native={}", val);
println!("cargo:{}", &meta);
dirs.push(PathBuf::from(val));
self.link_paths.push(PathBuf::from(val));
}
"-F" => {
let meta = format!("rustc-link-search=framework={}", val);
println!("cargo:{}", &meta);
self.framework_paths.push(PathBuf::from(val));
}
"-I" => {
self.include_paths.push(PathBuf::from(val));
}
"-l" => {
// These are provided by the CRT with MSVC
if is_msvc && ["m", "c", "pthread"].contains(&val) {
continue;
}
if is_static() && statik {
let meta = format!("rustc-link-lib=static={}", val);
println!("cargo:{}", &meta);
} else {
let meta = format!("rustc-link-lib={}", val);
println!("cargo:{}", &meta);
}
self.libs.push(val.to_string());
}
"-D" => {
let mut iter = val.split("=");
self.defines.insert(
iter.next().unwrap().to_owned(),
iter.next().map(|s| s.to_owned()),
);
}
_ => {}
}
}
let mut iter = words.iter().flat_map(|arg| {
if arg.starts_with("-Wl,") {
arg[4..].split(',').collect()
} else {
vec![arg.as_ref()]
}
});
while let Some(part) = iter.next() {
if part != "-framework" {
continue;
}
if let Some(lib) = iter.next() {
let meta = format!("rustc-link-lib=framework={}", lib);
println!("cargo:{}", &meta);
self.frameworks.push(lib.to_string());
}
}
}
}
fn split_flags(output: &[u8]) -> Vec<String> {
let mut word = Vec::new();
let mut words = Vec::new();
for &b in output {
match b {
b' ' => {
if !word.is_empty() {
words.push(String::from_utf8(word).unwrap());
word = Vec::new();
}
}
_ => word.push(b),
}
}
if !word.is_empty() {
words.push(String::from_utf8(word).unwrap());
}
words
}
fn is_static() -> bool {
env::var_os("RUBY_STATIC").is_some()
}
fn should_link() -> bool {
std::env::var_os("NO_LINK_RUTIE").is_none()
&& std::env::var_os("CARGO_FEATURE_NO_LINK").is_none()
}
fn main() {
// Ruby programs calling Rust doesn't need cc linking
if should_link() {
// If windows OS do windows stuff
windows_support();
if is_static() {
ci_stderr_log!("RUBY_STATIC is set");
use_static()
} else {
match rbconfig("ENABLE_SHARED").as_str() {
"no" => use_static(),
"yes" => use_dylib(),
_ => {
let msg = "Error! Couldn't find a valid value for \
RbConfig::CONFIG['ENABLE_SHARED']. \
This may mean that your ruby's build config is corrupted. \
Possible solution: build a new Ruby with the `--enable-shared` configure opt.";
ci_stderr_log!("{}", &msg);
panic!("{}", msg)
}
}
}
}
}