1use std::{
2 collections::{hash_map::Keys, HashMap},
3 env,
4 path::PathBuf,
5 process::Command,
6};
7
8use regex::Regex;
9mod flags;
10mod library;
11mod search_path;
12
13use library::*;
14use search_path::*;
15use std::ffi::OsString;
16
17use crate::{
18 debug_log, memoize,
19 utils::{is_msvc, shellsplit},
20};
21
22use self::flags::Flags;
23
24#[derive(Debug, PartialEq, Eq)]
27pub struct RbConfig {
28 pub search_paths: Vec<SearchPath>,
29 pub libs: Vec<Library>,
30 pub link_args: Vec<String>,
31 pub cflags: Vec<String>,
32 pub blocklist_lib: Vec<String>,
33 pub blocklist_link_arg: Vec<String>,
34 use_rpath: bool,
35 value_map: HashMap<String, String>,
36}
37
38impl Default for RbConfig {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl RbConfig {
45 pub(crate) fn new() -> RbConfig {
47 RbConfig {
48 blocklist_lib: vec![],
49 blocklist_link_arg: vec![],
50 search_paths: Vec::new(),
51 libs: Vec::new(),
52 link_args: Vec::new(),
53 cflags: Vec::new(),
54 value_map: HashMap::new(),
55 use_rpath: false,
56 }
57 }
58
59 pub fn all_keys(&self) -> Keys<'_, String, String> {
61 self.value_map.keys()
62 }
63
64 pub fn current() -> RbConfig {
66 println!("cargo:rerun-if-env-changed=RUBY");
67
68 let mut rbconfig = RbConfig::new();
69
70 let parsed = if rbconfig.is_cross_compiling() {
73 HashMap::new()
74 } else {
75 let output = memoize!(String: {
76 let ruby = env::var_os("RUBY").unwrap_or_else(|| OsString::from("ruby"));
77
78 let config = Command::new(ruby)
79 .arg("--disable-gems")
80 .arg("-rrbconfig")
81 .arg("-e")
82 .arg("print RbConfig::CONFIG.map {|kv| kv.join(\"\x1F\")}.join(\"\x1E\")")
83 .output()
84 .unwrap_or_else(|e| panic!("ruby not found: {}", e));
85 if !config.status.success() {
86 panic!("non-zero exit status while dumping RbConfig: {:?}", config);
87 }
88 String::from_utf8(config.stdout).expect("RbConfig value not UTF-8!")
89 });
90
91 let mut parsed = HashMap::new();
92 for line in output.split('\x1E') {
93 let mut parts = line.splitn(2, '\x1F');
94 if let (Some(key), Some(val)) = (parts.next(), parts.next()) {
95 parsed.insert(key.to_owned(), val.to_owned());
96 }
97 }
98 parsed
99 };
100
101 parsed.get("cflags").map(|f| rbconfig.push_cflags(f));
102 parsed.get("DLDFLAGS").map(|f| rbconfig.push_dldflags(f));
103
104 rbconfig.value_map = parsed;
105
106 rbconfig
107 }
108
109 pub fn link_ruby(&mut self, is_static: bool) -> &mut Self {
111 let Some(libdir) = self.get("libdir") else {
112 return self;
113 };
114
115 self.push_search_path(libdir.as_str());
116 self.push_dldflags(&format!("-L{}", libdir));
117
118 let librubyarg = if is_static {
119 self.get("LIBRUBYARG_STATIC")
120 } else {
121 self.get("LIBRUBYARG_SHARED")
122 };
123
124 let librubyarg = match librubyarg {
125 Some(lib) => lib,
126 None => {
127 debug_log!("WARN: LIBRUBYARG not found in RbConfig, skipping linking Ruby");
128 return self;
129 }
130 };
131
132 if is_msvc() {
133 for lib in librubyarg.split_whitespace() {
134 self.push_library(lib);
135 }
136
137 let mut to_link: Vec<String> = vec![];
138
139 if let Some(libs) = self.get("LIBS") {
140 to_link.extend(libs.split_whitespace().map(|s| s.to_string()));
141 }
142
143 if let Some(libs) = self.get("LOCAL_LIBS") {
144 to_link.extend(libs.split_whitespace().map(|s| s.to_string()));
145 }
146
147 for lib in to_link {
148 self.push_library(lib);
149 }
150 } else {
151 self.push_dldflags(&librubyarg);
152
153 if cfg!(unix) {
154 self.use_rpath();
155 }
156 }
157
158 self
159 }
160
161 pub fn libruby_static_name(&self) -> String {
163 let Some(lib) = self.get("LIBRUBY_A") else {
164 return format!("{}-static", self.libruby_so_name());
165 };
166
167 lib.trim_start_matches("lib")
168 .trim_end_matches(".a")
169 .to_string()
170 }
171
172 pub fn libruby_so_name(&self) -> String {
174 self.get("RUBY_SO_NAME")
175 .unwrap_or_else(|| "ruby".to_string())
176 }
177
178 pub fn platform(&self) -> String {
180 self.get("platform")
181 .unwrap_or_else(|| self.get("arch").expect("arch not found"))
182 }
183
184 pub fn blocklist_lib(&mut self, name: &str) -> &mut RbConfig {
186 self.blocklist_lib.push(name.to_string());
187 self
188 }
189
190 pub fn blocklist_link_arg(&mut self, name: &str) -> &mut RbConfig {
192 self.blocklist_link_arg.push(name.to_string());
193 self
194 }
195
196 pub fn ruby_version_slug(&self) -> String {
198 let ver = if let Some(progv) = self.get("RUBY_PROGRAM_VERSION") {
199 progv
200 } else if let Some(major_minor) = self.major_minor() {
201 format!(
202 "{}.{}.{}",
203 major_minor.0,
204 major_minor.1,
205 self.get("TEENY").unwrap_or_else(|| "0".to_string())
206 )
207 } else if let Some(fallback) = self.get("ruby_version") {
208 fallback
209 } else {
210 panic!("RUBY_PROGRAM_VERSION not found")
211 };
212
213 format!("{}-{}-{}", self.ruby_engine(), self.platform(), ver)
214 }
215
216 pub fn cppflags(&self) -> Vec<String> {
218 if let Some(cppflags) = self.get("CPPFLAGS") {
219 let flags = self.subst_shell_variables(&cppflags);
220 shellsplit(flags)
221 } else {
222 vec![]
223 }
224 }
225
226 pub fn is_cross_compiling(&self) -> bool {
228 if let Some(cross) = self.get("CROSS_COMPILING") {
229 cross == "yes" || cross == "1"
230 } else {
231 false
232 }
233 }
234
235 pub fn get(&self, key: &str) -> Option<String> {
238 self.try_rbconfig_env(key)
239 .or_else(|| self.try_value_map(key))
240 }
241
242 pub fn use_rpath(&mut self) -> &mut RbConfig {
244 self.use_rpath = true;
245 self
246 }
247
248 pub fn push_cflags(&mut self, cflags: &str) -> &mut Self {
250 for flag in shellsplit(cflags) {
251 if !self.cflags.contains(&flag) {
252 self.cflags.push(flag.to_string());
253 }
254 }
255
256 self
257 }
258
259 pub fn major_minor(&self) -> Option<(u32, u32)> {
261 let major = self.get("MAJOR").map(|v| v.parse::<u32>())?.ok()?;
262 let minor = self.get("MINOR").map(|v| v.parse::<u32>())?.ok()?;
263 Some((major, minor))
264 }
265
266 pub fn cargo_args(&self) -> Vec<String> {
268 let mut result = vec![];
269
270 let mut search_paths = vec![];
271
272 for search_path in &self.search_paths {
273 result.push(format!("cargo:rustc-link-search={}", search_path));
274 search_paths.push(search_path.name.as_str());
275 }
276
277 for lib in &self.libs {
278 if !self.blocklist_lib.iter().any(|b| lib.name.contains(b)) {
279 result.push(format!("cargo:rustc-link-lib={}", lib));
280 }
281
282 if self.use_rpath && !lib.is_static() {
283 result.push(format!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib));
284 }
285 }
286
287 for link_arg in &self.link_args {
288 if !self.blocklist_link_arg.iter().any(|b| link_arg == b) {
289 result.push(format!("cargo:rustc-link-arg={}", link_arg));
290 }
291 }
292
293 result
294 }
295
296 pub fn print_cargo_args(&self) {
298 let cargo_args = self.cargo_args();
299
300 for arg in &cargo_args {
301 println!("{}", arg);
302 }
303
304 debug_log!("INFO: printing cargo args ({:?})", cargo_args);
305
306 let encoded_cargo_args = cargo_args.join("\x1E");
307 let encoded_cargo_args = encoded_cargo_args.replace('\n', "\x1F");
308
309 println!("cargo:encoded_cargo_args={}", encoded_cargo_args);
310 }
311
312 pub fn push_dldflags(&mut self, input: &str) -> &mut Self {
314 let input = self.subst_shell_variables(input);
315 let split_args = Flags::new(input.as_str());
316
317 let search_path_regex = Regex::new(r"^-L\s*(?P<name>.*)$").unwrap();
318 let lib_regex_short = Regex::new(r"^-l\s*(?P<name>\w+\S+)$").unwrap();
319 let lib_regex_long = Regex::new(r"^--library=(?P<name>\w+\S+)$").unwrap();
320 let dynamic_lib_regex = Regex::new(r"^-l\s*:lib(?P<name>\S+).(so|dylib|dll)$").unwrap();
321 let framework_regex_short = Regex::new(r"^-F\s*(?P<name>.*)$").unwrap();
322 let framework_regex_long = Regex::new(r"^-framework\s*(?P<name>.*)$").unwrap();
323
324 for arg in split_args {
325 let arg = arg.trim().to_owned();
326
327 if let Some(name) = capture_name(&search_path_regex, &arg) {
328 self.push_search_path(name.as_str());
329 } else if let Some(name) = capture_name(&lib_regex_long, &arg) {
330 self.push_library(name);
331 } else if let Some(name) = capture_name(&lib_regex_short, &arg) {
332 if name.contains("ruby") && name.contains("-static") {
333 self.push_library((LibraryKind::Static, name));
334 } else {
335 self.push_library(name);
336 }
337 } else if let Some(name) = capture_name(&dynamic_lib_regex, &arg) {
338 self.push_library((LibraryKind::Dylib, name));
339 } else if let Some(name) = capture_name(&framework_regex_short, &arg) {
340 self.push_search_path((SearchPathKind::Framework, name));
341 } else if let Some(name) = capture_name(&framework_regex_long, &arg) {
342 self.push_library((LibraryKind::Framework, name));
343 } else {
344 self.push_link_arg(arg);
345 }
346 }
347
348 self
349 }
350
351 pub fn set_value_for_key(&mut self, key: &str, value: String) {
353 self.value_map.insert(key.to_owned(), value);
354 }
355
356 pub fn has_ruby_dln_check_abi(&self) -> bool {
358 let Some((major, minor)) = self.major_minor() else {
359 return false;
360 };
361
362 let patchlevel = self
363 .get("PATCHLEVEL")
364 .and_then(|v| v.parse::<i32>().ok())
365 .unwrap_or(-1);
366
367 major >= 3 && minor >= 2 && patchlevel == -1 && !cfg!(target_family = "windows")
370 }
371
372 pub fn ruby_engine(&self) -> RubyEngine {
374 if let Some(engine) = self.get("ruby_install_name") {
375 match engine.as_str() {
376 "ruby" => RubyEngine::Mri,
377 "jruby" => RubyEngine::JRuby,
378 "truffleruby" => RubyEngine::TruffleRuby,
379 _ => RubyEngine::Mri, }
381 } else {
382 RubyEngine::Mri
383 }
384 }
385
386 fn subst_shell_variables(&self, input: &str) -> String {
388 let mut result = String::new();
389 let mut chars = input.chars().enumerate();
390
391 while let Some((_, c)) = chars.next() {
392 if c == '$' {
393 if let Some((i, c)) = chars.next() {
394 if c == '(' {
395 let start = i + 1;
396 let mut end = start;
397
398 for (i, c) in chars.by_ref() {
399 if c == ')' {
400 end = i;
401 break;
402 }
403 }
404
405 let key = &input[start..end];
406
407 if let Some(val) = self.get(key) {
408 result.push_str(&val);
409 } else if let Some(val) = env::var_os(key) {
410 result.push_str(&val.to_string_lossy());
411 } else {
412 chars.next();
414 }
415 } else {
416 result.push(c);
417 }
418 }
419 } else {
420 result.push(c);
421 }
422 }
423
424 result
425 }
426
427 pub fn have_ruby_header<T: AsRef<str>>(&self, header: T) -> bool {
428 let Some(ruby_include_dir) = self.get("rubyhdrdir") else {
429 return false;
430 };
431 PathBuf::from(ruby_include_dir)
432 .join(header.as_ref())
433 .exists()
434 }
435
436 fn push_search_path<T: Into<SearchPath>>(&mut self, path: T) -> &mut Self {
437 let path = path.into();
438
439 if !self.search_paths.contains(&path) {
440 self.search_paths.push(path);
441 }
442
443 self
444 }
445
446 fn push_library<T: Into<Library>>(&mut self, lib: T) -> &mut Self {
447 let lib = lib.into();
448
449 if !self.libs.contains(&lib) {
450 self.libs.push(lib);
451 }
452
453 self
454 }
455
456 fn push_link_arg<T: Into<String>>(&mut self, arg: T) -> &mut Self {
457 let arg = arg.into();
458
459 if !self.link_args.contains(&arg) {
460 self.link_args.push(arg);
461 }
462
463 self
464 }
465
466 fn try_value_map(&self, key: &str) -> Option<String> {
467 self.value_map
468 .get(key)
469 .map(|val| val.trim_matches('\n').to_owned())
470 }
471
472 fn try_rbconfig_env(&self, key: &str) -> Option<String> {
473 let key = format!("RBCONFIG_{}", key);
474 println!("cargo:rerun-if-env-changed={}", key);
475 env::var(key).map(|v| v.trim_matches('\n').to_owned()).ok()
476 }
477}
478
479#[derive(Debug, PartialEq, Eq, Clone, Copy)]
480pub enum RubyEngine {
481 Mri,
482 TruffleRuby,
483 JRuby,
484}
485
486impl std::fmt::Display for RubyEngine {
487 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
488 match self {
489 RubyEngine::Mri => write!(f, "mri"),
490 RubyEngine::TruffleRuby => write!(f, "truffleruby"),
491 RubyEngine::JRuby => write!(f, "jruby"),
492 }
493 }
494}
495
496fn capture_name(regex: &Regex, arg: &str) -> Option<String> {
497 regex
498 .captures(arg)
499 .map(|cap| cap.name("name").unwrap().as_str().trim().to_owned())
500}
501
502#[cfg(test)]
503mod tests {
504 use super::*;
505 use std::{sync::Mutex, vec};
506
507 lazy_static::lazy_static! {
508 static ref ENV_LOCK: Mutex<()> = Mutex::new(());
509 }
510
511 fn with_locked_env<F, T>(f: F) -> T
512 where
513 F: FnOnce() -> T,
514 {
515 let _guard = ENV_LOCK.lock().unwrap();
516 f()
517 }
518
519 #[test]
520 fn test_extract_lib_search_paths() {
521 let mut rb_config = RbConfig::new();
522 rb_config.push_dldflags("-L/usr/local/lib -L/usr/lib");
523 assert_eq!(
524 rb_config.search_paths,
525 vec!["/usr/local/lib".into(), "/usr/lib".into()]
526 );
527 }
528
529 #[test]
530 fn test_search_path_basic() {
531 let mut rb_config = RbConfig::new();
532 rb_config.push_dldflags("-L/usr/local/lib");
533
534 assert_eq!(rb_config.search_paths, vec!["native=/usr/local/lib".into()]);
535 }
536
537 #[test]
538 fn test_search_path_space() {
539 let mut rb_config = RbConfig::new();
540 rb_config.push_dldflags("-L /usr/local/lib");
541
542 assert_eq!(rb_config.search_paths, vec!["/usr/local/lib".into()]);
543 }
544
545 #[test]
546 fn test_search_path_space_in_path() {
547 let mut rb_config = RbConfig::new();
548 rb_config.push_dldflags("-L/usr/local/my lib");
549
550 assert_eq!(
551 rb_config.search_paths,
552 vec!["native=/usr/local/my lib".into()]
553 );
554 }
555
556 #[test]
557 fn test_simple_lib() {
558 let mut rb_config = RbConfig::new();
559 rb_config.push_dldflags("-lfoo");
560
561 assert_eq!(rb_config.libs, ["foo".into()]);
562 }
563
564 #[test]
565 fn test_lib_with_nonascii() {
566 let mut rb_config = RbConfig::new();
567 rb_config.push_dldflags("-lws2_32");
568
569 assert_eq!(rb_config.libs, ["ws2_32".into()]);
570 }
571
572 #[test]
573 fn test_simple_lib_space() {
574 let mut rb_config = RbConfig::new();
575 rb_config.push_dldflags("-l foo");
576
577 assert_eq!(rb_config.libs, ["foo".into()]);
578 }
579
580 #[test]
581 fn test_verbose_lib_space() {
582 let mut rb_config = RbConfig::new();
583 rb_config.push_dldflags("--library=foo");
584
585 assert_eq!(rb_config.libs, ["foo".into()]);
586 }
587
588 #[test]
589 fn test_dylib_with_colon_space() {
590 let mut rb_config = RbConfig::new();
591 rb_config.push_dldflags("-l :libssp.dylib");
592
593 assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
594 }
595
596 #[test]
597 fn test_so_with_colon_space() {
598 let mut rb_config = RbConfig::new();
599 rb_config.push_dldflags("-l :libssp.so");
600
601 assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
602 }
603
604 #[test]
605 fn test_dll_with_colon_space() {
606 let mut rb_config = RbConfig::new();
607 rb_config.push_dldflags("-l :libssp.dll");
608
609 assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
610 }
611
612 #[test]
613 fn test_framework() {
614 let mut rb_config = RbConfig::new();
615 rb_config.push_dldflags("-F/some/path");
616
617 assert_eq!(rb_config.search_paths, ["framework=/some/path".into()]);
618 }
619
620 #[test]
621 fn test_framework_space() {
622 let mut rb_config = RbConfig::new();
623 rb_config.push_dldflags("-F /some/path");
624
625 assert_eq!(
626 rb_config.search_paths,
627 [SearchPath {
628 kind: SearchPathKind::Framework,
629 name: "/some/path".into(),
630 }]
631 );
632 }
633
634 #[test]
635 fn test_framework_arg_real() {
636 let mut rb_config = RbConfig::new();
637 rb_config.push_dldflags("-framework CoreFoundation");
638
639 assert_eq!(
640 rb_config.libs,
641 [Library {
642 kind: LibraryKind::Framework,
643 name: "CoreFoundation".into(),
644 }]
645 );
646 }
647
648 #[test]
649 fn test_libruby_static() {
650 let mut rb_config = RbConfig::new();
651 rb_config.push_dldflags("-lruby.3.1-static");
652
653 assert_eq!(
654 rb_config.cargo_args(),
655 ["cargo:rustc-link-lib=static=ruby.3.1-static"]
656 );
657 }
658
659 #[test]
660 fn test_libruby_dynamic() {
661 let mut rb_config = RbConfig::new();
662 rb_config.push_dldflags("-lruby.3.1");
663
664 assert_eq!(rb_config.cargo_args(), ["cargo:rustc-link-lib=ruby.3.1"]);
665 }
666
667 #[test]
668 fn test_non_lib_dash_l() {
669 let mut rb_config = RbConfig::new();
670 rb_config.push_dldflags("test_rubygems_20220413-976-lemgf9/prefix");
671
672 assert_eq!(
673 rb_config.link_args,
674 vec!["test_rubygems_20220413-976-lemgf9/prefix"]
675 );
676 }
677
678 #[test]
679 fn test_real_dldflags() {
680 let mut rb_config = RbConfig::new();
681 rb_config.push_dldflags("-L/Users/ianks/.asdf/installs/ruby/3.1.1/lib -L/opt/homebrew/opt/openssl@1.1/lib -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress");
682
683 assert_eq!(
684 rb_config.link_args,
685 vec![
686 "-Wl,-undefined,dynamic_lookup",
687 "-Wl,-multiply_defined,suppress"
688 ]
689 );
690 assert_eq!(
691 rb_config.search_paths,
692 vec![
693 SearchPath {
694 kind: SearchPathKind::Native,
695 name: "/Users/ianks/.asdf/installs/ruby/3.1.1/lib".to_string()
696 },
697 SearchPath {
698 kind: SearchPathKind::Native,
699 name: "/opt/homebrew/opt/openssl@1.1/lib".to_string()
700 },
701 ]
702 );
703 }
704
705 #[test]
706 fn test_crazy_cases() {
707 let mut rb_config = RbConfig::new();
708 rb_config.push_dldflags("-F /something -l:libssp.a -static-libgcc ");
709
710 assert_eq!(rb_config.link_args, vec!["-l:libssp.a", "-static-libgcc"]);
711 assert_eq!(
712 rb_config.search_paths,
713 vec![SearchPath {
714 kind: SearchPathKind::Framework,
715 name: "/something".to_string()
716 },]
717 );
718 }
719
720 #[test]
721 fn test_printing_cargo_args() {
722 let mut rb_config = RbConfig::new();
723 rb_config.push_dldflags("-L/Users/ianks/.asdf/installs/ruby/3.1.1/lib");
724 rb_config.push_dldflags("-lfoo");
725 rb_config.push_dldflags("-static-libgcc");
726 let result = rb_config.cargo_args();
727
728 assert_eq!(
729 vec![
730 "cargo:rustc-link-search=native=/Users/ianks/.asdf/installs/ruby/3.1.1/lib",
731 "cargo:rustc-link-lib=foo",
732 "cargo:rustc-link-arg=-static-libgcc"
733 ],
734 result
735 );
736 }
737
738 #[test]
739 fn test_use_rpath() {
740 let mut rb_config = RbConfig::new();
741 rb_config.push_dldflags("-lfoo");
742
743 assert_eq!(vec!["cargo:rustc-link-lib=foo"], rb_config.cargo_args());
744
745 rb_config.use_rpath();
746
747 assert_eq!(
748 vec![
749 "cargo:rustc-link-lib=foo",
750 "cargo:rustc-link-arg=-Wl,-rpath,foo"
751 ],
752 rb_config.cargo_args()
753 );
754 }
755
756 #[test]
757 fn test_link_mswin() {
758 with_locked_env(|| {
759 let old_var = env::var("TARGET").ok();
760 env::set_var("TARGET", "x86_64-pc-windows-msvc");
761
762 let mut rb_config = RbConfig::new();
763 rb_config.set_value_for_key("LIBRUBYARG_SHARED", "x64-vcruntime140-ruby320.lib".into());
764 rb_config.set_value_for_key("libdir", "D:/ruby-mswin/lib".into());
765 rb_config.set_value_for_key("LIBS", "user32.lib".into());
766 rb_config.link_ruby(false);
767
768 assert_eq!(
769 vec![
770 "cargo:rustc-link-search=native=D:/ruby-mswin/lib",
771 "cargo:rustc-link-lib=x64-vcruntime140-ruby320",
772 "cargo:rustc-link-lib=user32",
773 ],
774 rb_config.cargo_args()
775 );
776
777 if let Some(old_var) = old_var {
778 env::set_var("TARGET", old_var);
779 } else {
780 env::remove_var("TARGET");
781 }
782 })
783 }
784
785 #[test]
786 fn test_link_static() {
787 with_locked_env(|| {
788 let mut rb_config = RbConfig::new();
789 rb_config.set_value_for_key("LIBRUBYARG_STATIC", "-lruby-static".into());
790 rb_config.set_value_for_key("libdir", "/opt/ruby".into());
791
792 rb_config.link_ruby(true);
793
794 assert_eq!(
795 vec![
796 "cargo:rustc-link-search=native=/opt/ruby",
797 "cargo:rustc-link-lib=static=ruby-static",
798 ],
799 rb_config.cargo_args()
800 );
801 });
802 }
803
804 #[test]
805 fn test_prioritizes_rbconfig_env() {
806 with_locked_env(|| {
807 env::set_var("RBCONFIG_libdir", "/foo");
808 let rb_config = RbConfig::new();
809
810 assert_eq!(rb_config.get("libdir"), Some("/foo".into()));
811
812 env::remove_var("RBCONFIG_libdir");
813 });
814 }
815
816 #[test]
817 fn test_never_loads_shell_rbconfig_if_cross_compiling() {
818 with_locked_env(|| {
819 env::set_var("RBCONFIG_CROSS_COMPILING", "yes");
820
821 let rb_config = RbConfig::current();
822
823 assert!(rb_config.value_map.is_empty());
824 });
825 }
826
827 #[test]
828 fn test_loads_shell_rbconfig_if_not_cross_compiling() {
829 with_locked_env(|| {
830 env::set_var("RBCONFIG_CROSS_COMPILING", "no");
831
832 let rb_config = RbConfig::current();
833
834 assert!(!rb_config.value_map.is_empty());
835 });
836 }
837
838 #[test]
839 fn test_libstatic() {
840 let mut rb_config = RbConfig::new();
841 rb_config.push_dldflags("-l:libssp.a");
842
843 assert_eq!(rb_config.link_args, ["-l:libssp.a".to_string()]);
844 }
845
846 #[test]
847 fn test_link_arg_blocklist() {
848 let mut rb_config = RbConfig::new();
849 rb_config.blocklist_link_arg("-Wl,--compress-debug-sections=zlib");
850 rb_config.blocklist_link_arg("-s");
851 rb_config.push_dldflags(
852 "-lfoo -Wl,--compress-debug-sections=zlib -s -somethingthatshouldnotbeblocked",
853 );
854
855 assert_eq!(
856 vec![
857 "cargo:rustc-link-lib=foo",
858 "cargo:rustc-link-arg=-somethingthatshouldnotbeblocked"
859 ],
860 rb_config.cargo_args()
861 );
862 }
863}