rb-sys-build 0.9.129

Build system for rb-sys
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
use std::{
    collections::{hash_map::Keys, HashMap},
    env,
    path::PathBuf,
    process::Command,
};

use regex::Regex;
mod flags;
mod library;
mod search_path;

use library::*;
use search_path::*;
use std::ffi::OsString;

use crate::{
    debug_log, memoize,
    utils::{is_msvc, shellsplit},
};

use self::flags::Flags;

/// Extracts structured information from raw compiler/linker flags to make
/// compiling Ruby gems easier.
#[derive(Debug, PartialEq, Eq)]
pub struct RbConfig {
    pub search_paths: Vec<SearchPath>,
    pub libs: Vec<Library>,
    pub link_args: Vec<String>,
    pub cflags: Vec<String>,
    pub blocklist_lib: Vec<String>,
    pub blocklist_link_arg: Vec<String>,
    use_rpath: bool,
    value_map: HashMap<String, String>,
}

impl Default for RbConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl RbConfig {
    /// Creates a new, blank `RbConfig`. You likely want to use `RbConfig::current()` instead.
    pub(crate) fn new() -> RbConfig {
        RbConfig {
            blocklist_lib: vec![],
            blocklist_link_arg: vec![],
            search_paths: Vec::new(),
            libs: Vec::new(),
            link_args: Vec::new(),
            cflags: Vec::new(),
            value_map: HashMap::new(),
            use_rpath: false,
        }
    }

    /// All keys in the `RbConfig`'s value map.
    pub fn all_keys(&self) -> Keys<'_, String, String> {
        self.value_map.keys()
    }

    /// Instantiates a new `RbConfig` for the current Ruby.
    pub fn current() -> RbConfig {
        println!("cargo:rerun-if-env-changed=RUBY");

        let mut rbconfig = RbConfig::new();

        // Never use the current Ruby's RbConfig if we're cross compiling, or
        // else bad things happen
        let parsed = if rbconfig.is_cross_compiling() {
            HashMap::new()
        } else {
            let output = memoize!(String: {
                let ruby = env::var_os("RUBY").unwrap_or_else(|| OsString::from("ruby"));

                let config = Command::new(ruby)
                    .arg("--disable-gems")
                    .arg("-rrbconfig")
                    .arg("-e")
                    .arg("print RbConfig::CONFIG.map {|kv| kv.join(\"\x1F\")}.join(\"\x1E\")")
                    .env_remove("RUBYOPT")
                    .output()
                    .unwrap_or_else(|e| panic!("ruby not found: {}", e));
                if !config.status.success() {
                    panic!("non-zero exit status while dumping RbConfig: {:?}", config);
                }
                String::from_utf8(config.stdout).expect("RbConfig value not UTF-8!")
            });

            let mut parsed = HashMap::new();
            for line in output.split('\x1E') {
                let mut parts = line.splitn(2, '\x1F');
                if let (Some(key), Some(val)) = (parts.next(), parts.next()) {
                    parsed.insert(key.to_owned(), val.to_owned());
                }
            }
            parsed
        };

        parsed.get("cflags").map(|f| rbconfig.push_cflags(f));
        parsed.get("DLDFLAGS").map(|f| rbconfig.push_dldflags(f));

        rbconfig.value_map = parsed;

        rbconfig
    }

    /// Pushes the `LIBRUBYARG` flags so Ruby will be linked.
    pub fn link_ruby(&mut self, is_static: bool) -> &mut Self {
        let Some(libdir) = self.get("libdir") else {
            return self;
        };

        self.push_search_path(libdir.as_str());
        self.push_dldflags(&format!("-L{}", libdir));

        let librubyarg = if is_static {
            self.get("LIBRUBYARG_STATIC")
        } else {
            self.get("LIBRUBYARG_SHARED")
        };

        let librubyarg = match librubyarg {
            Some(lib) => lib,
            None => {
                debug_log!("WARN: LIBRUBYARG not found in RbConfig, skipping linking Ruby");
                return self;
            }
        };

        if is_msvc() {
            for lib in librubyarg.split_whitespace() {
                self.push_library(lib);
            }

            let mut to_link: Vec<String> = vec![];

            if let Some(libs) = self.get("LIBS") {
                to_link.extend(libs.split_whitespace().map(|s| s.to_string()));
            }

            if let Some(libs) = self.get("LOCAL_LIBS") {
                to_link.extend(libs.split_whitespace().map(|s| s.to_string()));
            }

            for lib in to_link {
                self.push_library(lib);
            }
        } else {
            self.push_dldflags(&librubyarg);

            if cfg!(unix) {
                self.use_rpath();
            }
        }

        self
    }

    /// Get the name for libruby-static (i.e. `ruby.3.1-static`).
    pub fn libruby_static_name(&self) -> String {
        let Some(lib) = self.get("LIBRUBY_A") else {
            return format!("{}-static", self.libruby_so_name());
        };

        lib.trim_start_matches("lib")
            .trim_end_matches(".a")
            .to_string()
    }

    /// Get the name for libruby (i.e. `ruby.3.1`)
    pub fn libruby_so_name(&self) -> String {
        self.get("RUBY_SO_NAME")
            .unwrap_or_else(|| "ruby".to_string())
    }

    /// Get the platform for the current ruby.
    pub fn platform(&self) -> String {
        self.get("platform")
            .unwrap_or_else(|| self.get("arch").expect("arch not found"))
    }

    /// Filter the libs, removing the ones that are not needed.
    pub fn blocklist_lib(&mut self, name: &str) -> &mut RbConfig {
        self.blocklist_lib.push(name.to_string());
        self
    }

    /// Blocklist a link argument.
    pub fn blocklist_link_arg(&mut self, name: &str) -> &mut RbConfig {
        self.blocklist_link_arg.push(name.to_string());
        self
    }

    /// Returns the current ruby program version.
    pub fn ruby_version_slug(&self) -> String {
        let ver = if let Some(progv) = self.get("RUBY_PROGRAM_VERSION") {
            progv
        } else if let Some(major_minor) = self.major_minor() {
            format!(
                "{}.{}.{}",
                major_minor.0,
                major_minor.1,
                self.get("TEENY").unwrap_or_else(|| "0".to_string())
            )
        } else if let Some(fallback) = self.get("ruby_version") {
            fallback
        } else {
            panic!("RUBY_PROGRAM_VERSION not found")
        };

        format!("{}-{}-{}", self.ruby_engine(), self.platform(), ver)
    }

    /// Get the CPPFLAGS from the RbConfig, making sure to subsitute variables.
    pub fn cppflags(&self) -> Vec<String> {
        if let Some(cppflags) = self.get("CPPFLAGS") {
            let flags = self.subst_shell_variables(&cppflags);
            shellsplit(flags)
        } else {
            vec![]
        }
    }

    /// Returns true if the current Ruby is cross compiling.
    pub fn is_cross_compiling(&self) -> bool {
        if let Some(cross) = self.get("CROSS_COMPILING") {
            cross == "yes" || cross == "1"
        } else {
            false
        }
    }

    /// Checks that Ruby's C headers use the same operating-system ABI as Cargo's target.
    ///
    /// Bindgen can locate headers from a cross-compilation sysroot, but headers from a Ruby
    /// built for a different target are not compatible. In particular, RubyInstaller uses
    /// MinGW headers which cannot be used with Rust's MSVC target.
    pub fn validate_cargo_target(&self) -> Result<(), String> {
        let Ok(cargo_target) = env::var("TARGET") else {
            return Ok(());
        };
        let Some(ruby_target_os) = self.get("target_os") else {
            return Ok(());
        };

        validate_target_compatibility(&ruby_target_os, &cargo_target)
    }

    /// Returns the value of the given key from the either the matching
    /// `RBCONFIG_{key}` environment variable or `RbConfig::CONFIG[{key}]` hash.
    pub fn get(&self, key: &str) -> Option<String> {
        self.try_rbconfig_env(key)
            .or_else(|| self.try_value_map(key))
    }

    /// Enables the use of rpath for linking.
    pub fn use_rpath(&mut self) -> &mut RbConfig {
        self.use_rpath = true;
        self
    }

    /// Push cflags string
    pub fn push_cflags(&mut self, cflags: &str) -> &mut Self {
        for flag in shellsplit(cflags) {
            if !self.cflags.contains(&flag) {
                self.cflags.push(flag.to_string());
            }
        }

        self
    }

    /// Get major/minor version tuple of Ruby
    pub fn major_minor(&self) -> Option<(u32, u32)> {
        let major = self.get("MAJOR").map(|v| v.parse::<u32>())?.ok()?;
        let minor = self.get("MINOR").map(|v| v.parse::<u32>())?.ok()?;
        Some((major, minor))
    }

    /// Get the rb_config output for cargo
    pub fn cargo_args(&self) -> Vec<String> {
        let mut result = vec![];

        let mut search_paths = vec![];

        for search_path in &self.search_paths {
            result.push(format!("cargo:rustc-link-search={}", search_path));
            search_paths.push(search_path.name.as_str());
        }

        for lib in &self.libs {
            if !self.blocklist_lib.iter().any(|b| lib.name.contains(b)) {
                result.push(format!("cargo:rustc-link-lib={}", lib));
            }

            if self.use_rpath && !lib.is_static() {
                result.push(format!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib));
            }
        }

        for link_arg in &self.link_args {
            if !self.blocklist_link_arg.iter().any(|b| link_arg == b) {
                result.push(format!("cargo:rustc-link-arg={}", link_arg));
            }
        }

        result
    }

    /// Print to rb_config output for cargo
    pub fn print_cargo_args(&self) {
        let cargo_args = self.cargo_args();

        for arg in &cargo_args {
            println!("{}", arg);
        }

        debug_log!("INFO: printing cargo args ({:?})", cargo_args);

        let encoded_cargo_args = cargo_args.join("\x1E");
        let encoded_cargo_args = encoded_cargo_args.replace('\n', "\x1F");

        println!("cargo:encoded_cargo_args={}", encoded_cargo_args);
    }

    /// Adds items to the rb_config based on a string from LDFLAGS/DLDFLAGS
    pub fn push_dldflags(&mut self, input: &str) -> &mut Self {
        let input = self.subst_shell_variables(input);
        let split_args = Flags::new(input.as_str());

        let search_path_regex = Regex::new(r"^-L\s*(?P<name>.*)$").unwrap();
        let lib_regex_short = Regex::new(r"^-l\s*(?P<name>\w+\S+)$").unwrap();
        let lib_regex_long = Regex::new(r"^--library=(?P<name>\w+\S+)$").unwrap();
        let dynamic_lib_regex = Regex::new(r"^-l\s*:lib(?P<name>\S+).(so|dylib|dll)$").unwrap();
        let framework_regex_short = Regex::new(r"^-F\s*(?P<name>.*)$").unwrap();
        let framework_regex_long = Regex::new(r"^-framework\s*(?P<name>.*)$").unwrap();

        for arg in split_args {
            let arg = arg.trim().to_owned();

            if let Some(name) = capture_name(&search_path_regex, &arg) {
                self.push_search_path(name.as_str());
            } else if let Some(name) = capture_name(&lib_regex_long, &arg) {
                self.push_library(name);
            } else if let Some(name) = capture_name(&lib_regex_short, &arg) {
                if name.contains("ruby") && name.contains("-static") {
                    self.push_library((LibraryKind::Static, name));
                } else {
                    self.push_library(name);
                }
            } else if let Some(name) = capture_name(&dynamic_lib_regex, &arg) {
                self.push_library((LibraryKind::Dylib, name));
            } else if let Some(name) = capture_name(&framework_regex_short, &arg) {
                self.push_search_path((SearchPathKind::Framework, name));
            } else if let Some(name) = capture_name(&framework_regex_long, &arg) {
                self.push_library((LibraryKind::Framework, name));
            } else {
                self.push_link_arg(arg);
            }
        }

        self
    }

    /// Sets a value for a key
    pub fn set_value_for_key(&mut self, key: &str, value: String) {
        self.value_map.insert(key.to_owned(), value);
    }

    // Check if has ABI version
    pub fn has_ruby_dln_check_abi(&self) -> bool {
        let Some((major, minor)) = self.major_minor() else {
            return false;
        };

        let patchlevel = self
            .get("PATCHLEVEL")
            .and_then(|v| v.parse::<i32>().ok())
            .unwrap_or(-1);

        // Ruby has ABI version on version 3.2 and later only on development
        // versions
        (major > 3 || (major == 3 && minor >= 2))
            && patchlevel == -1
            && !cfg!(target_family = "windows")
    }

    /// The RUBY_ENGINE we are building for
    pub fn ruby_engine(&self) -> RubyEngine {
        if let Some(engine) = self.get("ruby_install_name") {
            match engine.as_str() {
                "ruby" => RubyEngine::Mri,
                "jruby" => RubyEngine::JRuby,
                "truffleruby" => RubyEngine::TruffleRuby,
                _ => RubyEngine::Mri, // not sure how stable this is, so default to MRI to avoid breaking things
            }
        } else {
            RubyEngine::Mri
        }
    }

    // Examines the string from shell variables and expands them with values in the value_map
    fn subst_shell_variables(&self, input: &str) -> String {
        let mut result = String::new();
        let mut chars = input.chars().enumerate();

        while let Some((_, c)) = chars.next() {
            if c == '$' {
                if let Some((i, c)) = chars.next() {
                    if c == '(' {
                        let start = i + 1;
                        let mut end = start;

                        for (i, c) in chars.by_ref() {
                            if c == ')' {
                                end = i;
                                break;
                            }
                        }

                        let key = &input[start..end];

                        if let Some(val) = self.get(key) {
                            result.push_str(&val);
                        } else if let Some(val) = env::var_os(key) {
                            result.push_str(&val.to_string_lossy());
                        } else {
                            // Consume whitespace
                            chars.next();
                        }
                    } else {
                        result.push(c);
                    }
                }
            } else {
                result.push(c);
            }
        }

        result
    }

    pub fn have_ruby_header<T: AsRef<str>>(&self, header: T) -> bool {
        let Some(ruby_include_dir) = self.get("rubyhdrdir") else {
            return false;
        };
        PathBuf::from(ruby_include_dir)
            .join(header.as_ref())
            .exists()
    }

    fn push_search_path<T: Into<SearchPath>>(&mut self, path: T) -> &mut Self {
        let path = path.into();

        if !self.search_paths.contains(&path) {
            self.search_paths.push(path);
        }

        self
    }

    fn push_library<T: Into<Library>>(&mut self, lib: T) -> &mut Self {
        let lib = lib.into();

        if !self.libs.contains(&lib) {
            self.libs.push(lib);
        }

        self
    }

    fn push_link_arg<T: Into<String>>(&mut self, arg: T) -> &mut Self {
        let arg = arg.into();

        if !self.link_args.contains(&arg) {
            self.link_args.push(arg);
        }

        self
    }

    fn try_value_map(&self, key: &str) -> Option<String> {
        self.value_map
            .get(key)
            .map(|val| val.trim_matches('\n').to_owned())
    }

    fn try_rbconfig_env(&self, key: &str) -> Option<String> {
        let key = format!("RBCONFIG_{}", key);
        println!("cargo:rerun-if-env-changed={}", key);
        env::var(key).map(|v| v.trim_matches('\n').to_owned()).ok()
    }
}

#[derive(Debug, PartialEq, Eq)]
enum TargetFamily {
    WindowsGnu,
    WindowsMsvc,
    Os(&'static str),
}

fn ruby_target_family(target_os: &str) -> Option<TargetFamily> {
    let target_os = target_os.to_ascii_lowercase();

    if target_os.contains("mingw") {
        return Some(TargetFamily::WindowsGnu);
    }
    if target_os.contains("mswin") {
        return Some(TargetFamily::WindowsMsvc);
    }

    target_os_family(&target_os)
}

fn cargo_target_family(target: &str) -> Option<TargetFamily> {
    let target = target.to_ascii_lowercase();

    if target.contains("windows-gnu") || target.contains("windows-gnullvm") {
        return Some(TargetFamily::WindowsGnu);
    }
    if target.contains("windows-msvc") {
        return Some(TargetFamily::WindowsMsvc);
    }

    target_os_family(&target)
}

fn target_os_family(target: &str) -> Option<TargetFamily> {
    const OPERATING_SYSTEMS: &[&str] = &[
        "android",
        "darwin",
        "dragonfly",
        "emscripten",
        "freebsd",
        "haiku",
        "illumos",
        "ios",
        "linux",
        "netbsd",
        "openbsd",
        "solaris",
        "wasi",
    ];

    OPERATING_SYSTEMS
        .iter()
        .find(|operating_system| target.contains(*operating_system))
        .map(|operating_system| TargetFamily::Os(operating_system))
}

fn validate_target_compatibility(ruby_target_os: &str, cargo_target: &str) -> Result<(), String> {
    let Some(ruby_family) = ruby_target_family(ruby_target_os) else {
        return Ok(());
    };
    let Some(cargo_family) = cargo_target_family(cargo_target) else {
        return Ok(());
    };

    if ruby_family == cargo_family {
        return Ok(());
    }

    let mut message = format!(
        "Ruby was built for target_os={ruby_target_os:?}, but Cargo is compiling for \
         {cargo_target:?}. rb-sys cannot generate bindings from Ruby headers for a different \
         operating-system ABI."
    );

    if ruby_family == TargetFamily::WindowsGnu && cargo_family == TargetFamily::WindowsMsvc {
        message.push_str(
            " RubyInstaller uses MinGW; select the matching Rust GNU target (for 64-bit \
             RubyInstaller, set RUST_TARGET=x86_64-pc-windows-gnu or pass \
             --target x86_64-pc-windows-gnu to Cargo).",
        );
    } else {
        message.push_str(
            " When cross-compiling with rb-sys-dock, use `rb-sys-dock --build` or the \
             `rake native:$RUBY_TARGET` task so rake-compiler supplies the target Ruby's \
             RbConfig instead of the container's host Ruby config.",
        );
    }

    Err(message)
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum RubyEngine {
    Mri,
    TruffleRuby,
    JRuby,
}

impl std::fmt::Display for RubyEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RubyEngine::Mri => write!(f, "mri"),
            RubyEngine::TruffleRuby => write!(f, "truffleruby"),
            RubyEngine::JRuby => write!(f, "jruby"),
        }
    }
}

fn capture_name(regex: &Regex, arg: &str) -> Option<String> {
    regex
        .captures(arg)
        .map(|cap| cap.name("name").unwrap().as_str().trim().to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{sync::Mutex, vec};

    lazy_static::lazy_static! {
        static ref ENV_LOCK: Mutex<()> = Mutex::new(());
    }

    fn with_locked_env<F, T>(f: F) -> T
    where
        F: FnOnce() -> T,
    {
        let _guard = ENV_LOCK.lock().unwrap();
        f()
    }

    #[test]
    fn test_extract_lib_search_paths() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-L/usr/local/lib -L/usr/lib");
        assert_eq!(
            rb_config.search_paths,
            vec!["/usr/local/lib".into(), "/usr/lib".into()]
        );
    }

    #[test]
    fn test_search_path_basic() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-L/usr/local/lib");

        assert_eq!(rb_config.search_paths, vec!["native=/usr/local/lib".into()]);
    }

    #[test]
    fn test_search_path_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-L /usr/local/lib");

        assert_eq!(rb_config.search_paths, vec!["/usr/local/lib".into()]);
    }

    #[test]
    fn test_search_path_space_in_path() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-L/usr/local/my lib");

        assert_eq!(
            rb_config.search_paths,
            vec!["native=/usr/local/my lib".into()]
        );
    }

    #[test]
    fn test_simple_lib() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-lfoo");

        assert_eq!(rb_config.libs, ["foo".into()]);
    }

    #[test]
    fn test_lib_with_nonascii() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-lws2_32");

        assert_eq!(rb_config.libs, ["ws2_32".into()]);
    }

    #[test]
    fn test_simple_lib_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-l foo");

        assert_eq!(rb_config.libs, ["foo".into()]);
    }

    #[test]
    fn test_verbose_lib_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("--library=foo");

        assert_eq!(rb_config.libs, ["foo".into()]);
    }

    #[test]
    fn test_dylib_with_colon_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-l :libssp.dylib");

        assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
    }

    #[test]
    fn test_so_with_colon_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-l :libssp.so");

        assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
    }

    #[test]
    fn test_dll_with_colon_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-l :libssp.dll");

        assert_eq!(rb_config.libs, ["dylib=ssp".into()]);
    }

    #[test]
    fn test_framework() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-F/some/path");

        assert_eq!(rb_config.search_paths, ["framework=/some/path".into()]);
    }

    #[test]
    fn test_framework_space() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-F /some/path");

        assert_eq!(
            rb_config.search_paths,
            [SearchPath {
                kind: SearchPathKind::Framework,
                name: "/some/path".into(),
            }]
        );
    }

    #[test]
    fn test_framework_arg_real() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-framework CoreFoundation");

        assert_eq!(
            rb_config.libs,
            [Library {
                kind: LibraryKind::Framework,
                name: "CoreFoundation".into(),
            }]
        );
    }

    #[test]
    fn test_libruby_static() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-lruby.3.1-static");

        assert_eq!(
            rb_config.cargo_args(),
            ["cargo:rustc-link-lib=static=ruby.3.1-static"]
        );
    }

    #[test]
    fn test_libruby_dynamic() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-lruby.3.1");

        assert_eq!(rb_config.cargo_args(), ["cargo:rustc-link-lib=ruby.3.1"]);
    }

    #[test]
    fn test_non_lib_dash_l() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("test_rubygems_20220413-976-lemgf9/prefix");

        assert_eq!(
            rb_config.link_args,
            vec!["test_rubygems_20220413-976-lemgf9/prefix"]
        );
    }

    #[test]
    fn test_real_dldflags() {
        let mut rb_config = RbConfig::new();
        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");

        assert_eq!(
            rb_config.link_args,
            vec![
                "-Wl,-undefined,dynamic_lookup",
                "-Wl,-multiply_defined,suppress"
            ]
        );
        assert_eq!(
            rb_config.search_paths,
            vec![
                SearchPath {
                    kind: SearchPathKind::Native,
                    name: "/Users/ianks/.asdf/installs/ruby/3.1.1/lib".to_string()
                },
                SearchPath {
                    kind: SearchPathKind::Native,
                    name: "/opt/homebrew/opt/openssl@1.1/lib".to_string()
                },
            ]
        );
    }

    #[test]
    fn test_crazy_cases() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-F   /something -l:libssp.a -static-libgcc ");

        assert_eq!(rb_config.link_args, vec!["-l:libssp.a", "-static-libgcc"]);
        assert_eq!(
            rb_config.search_paths,
            vec![SearchPath {
                kind: SearchPathKind::Framework,
                name: "/something".to_string()
            },]
        );
    }

    #[test]
    fn test_printing_cargo_args() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-L/Users/ianks/.asdf/installs/ruby/3.1.1/lib");
        rb_config.push_dldflags("-lfoo");
        rb_config.push_dldflags("-static-libgcc");
        let result = rb_config.cargo_args();

        assert_eq!(
            vec![
                "cargo:rustc-link-search=native=/Users/ianks/.asdf/installs/ruby/3.1.1/lib",
                "cargo:rustc-link-lib=foo",
                "cargo:rustc-link-arg=-static-libgcc"
            ],
            result
        );
    }

    #[test]
    fn test_use_rpath() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-lfoo");

        assert_eq!(vec!["cargo:rustc-link-lib=foo"], rb_config.cargo_args());

        rb_config.use_rpath();

        assert_eq!(
            vec![
                "cargo:rustc-link-lib=foo",
                "cargo:rustc-link-arg=-Wl,-rpath,foo"
            ],
            rb_config.cargo_args()
        );
    }

    #[test]
    fn test_link_mswin() {
        with_locked_env(|| {
            let old_var = env::var("TARGET").ok();
            env::set_var("TARGET", "x86_64-pc-windows-msvc");

            let mut rb_config = RbConfig::new();
            rb_config.set_value_for_key("LIBRUBYARG_SHARED", "x64-vcruntime140-ruby320.lib".into());
            rb_config.set_value_for_key("libdir", "D:/ruby-mswin/lib".into());
            rb_config.set_value_for_key("LIBS", "user32.lib".into());
            rb_config.link_ruby(false);

            assert_eq!(
                vec![
                    "cargo:rustc-link-search=native=D:/ruby-mswin/lib",
                    "cargo:rustc-link-lib=x64-vcruntime140-ruby320",
                    "cargo:rustc-link-lib=user32",
                ],
                rb_config.cargo_args()
            );

            if let Some(old_var) = old_var {
                env::set_var("TARGET", old_var);
            } else {
                env::remove_var("TARGET");
            }
        })
    }

    #[test]
    fn test_link_static() {
        with_locked_env(|| {
            let mut rb_config = RbConfig::new();
            rb_config.set_value_for_key("LIBRUBYARG_STATIC", "-lruby-static".into());
            rb_config.set_value_for_key("libdir", "/opt/ruby".into());

            rb_config.link_ruby(true);

            assert_eq!(
                vec![
                    "cargo:rustc-link-search=native=/opt/ruby",
                    "cargo:rustc-link-lib=static=ruby-static",
                ],
                rb_config.cargo_args()
            );
        });
    }

    #[test]
    fn test_prioritizes_rbconfig_env() {
        with_locked_env(|| {
            env::set_var("RBCONFIG_libdir", "/foo");
            let rb_config = RbConfig::new();

            assert_eq!(rb_config.get("libdir"), Some("/foo".into()));

            env::remove_var("RBCONFIG_libdir");
        });
    }

    #[test]
    fn test_never_loads_shell_rbconfig_if_cross_compiling() {
        with_locked_env(|| {
            env::set_var("RBCONFIG_CROSS_COMPILING", "yes");

            let rb_config = RbConfig::current();

            assert!(rb_config.value_map.is_empty());
        });
    }

    #[test]
    fn test_loads_shell_rbconfig_if_not_cross_compiling() {
        with_locked_env(|| {
            env::set_var("RBCONFIG_CROSS_COMPILING", "no");

            let rb_config = RbConfig::current();

            assert!(!rb_config.value_map.is_empty());
        });
    }

    #[test]
    fn test_accepts_matching_ruby_and_cargo_targets() {
        assert_eq!(
            validate_target_compatibility("mingw32", "x86_64-pc-windows-gnu"),
            Ok(())
        );
        assert_eq!(
            validate_target_compatibility("mingw-ucrt", "aarch64-pc-windows-gnullvm"),
            Ok(())
        );
        assert_eq!(
            validate_target_compatibility("linux-gnu", "aarch64-unknown-linux-gnu"),
            Ok(())
        );
        assert_eq!(
            validate_target_compatibility("darwin24", "aarch64-apple-darwin"),
            Ok(())
        );
    }

    #[test]
    fn test_rejects_mingw_ruby_with_msvc_cargo_target() {
        let error = validate_target_compatibility("mingw32", "x86_64-pc-windows-msvc").unwrap_err();

        assert!(error.contains("RubyInstaller uses MinGW"));
        assert!(error.contains("RUST_TARGET=x86_64-pc-windows-gnu"));
    }

    #[test]
    fn test_rejects_host_ruby_config_for_rb_sys_dock_target() {
        let error =
            validate_target_compatibility("linux-gnu", "x86_64-pc-windows-gnu").unwrap_err();

        assert!(error.contains("rb-sys-dock --build"));
        assert!(error.contains("rake native:$RUBY_TARGET"));
        assert!(error.contains("host Ruby config"));
    }

    #[test]
    fn test_allows_unknown_targets_for_backwards_compatibility() {
        assert_eq!(
            validate_target_compatibility("new-ruby-os", "x86_64-new-rust-os"),
            Ok(())
        );
    }

    #[test]
    fn test_libstatic() {
        let mut rb_config = RbConfig::new();
        rb_config.push_dldflags("-l:libssp.a");

        assert_eq!(rb_config.link_args, ["-l:libssp.a".to_string()]);
    }

    #[test]
    fn test_link_arg_blocklist() {
        let mut rb_config = RbConfig::new();
        rb_config.blocklist_link_arg("-Wl,--compress-debug-sections=zlib");
        rb_config.blocklist_link_arg("-s");
        rb_config.push_dldflags(
            "-lfoo -Wl,--compress-debug-sections=zlib -s -somethingthatshouldnotbeblocked",
        );

        assert_eq!(
            vec![
                "cargo:rustc-link-lib=foo",
                "cargo:rustc-link-arg=-somethingthatshouldnotbeblocked"
            ],
            rb_config.cargo_args()
        );
    }

    #[test]
    fn test_has_ruby_dln_check_abi() {
        // Helper to create RbConfig with specific version
        fn make_config(major: &str, minor: &str, patchlevel: &str) -> RbConfig {
            let mut rb_config = RbConfig::new();
            rb_config.set_value_for_key("MAJOR", major.into());
            rb_config.set_value_for_key("MINOR", minor.into());
            rb_config.set_value_for_key("PATCHLEVEL", patchlevel.into());
            rb_config
        }

        // Ruby 3.1.x (any patchlevel) - too old
        assert!(!make_config("3", "1", "-1").has_ruby_dln_check_abi());
        assert!(!make_config("3", "1", "0").has_ruby_dln_check_abi());

        // Ruby 3.2.0-dev (patchlevel -1) - should have ABI check
        #[cfg(not(target_family = "windows"))]
        assert!(make_config("3", "2", "-1").has_ruby_dln_check_abi());

        // Ruby 3.2.0 release (patchlevel 0) - no ABI check
        assert!(!make_config("3", "2", "0").has_ruby_dln_check_abi());

        // Ruby 3.3.0-dev - should have ABI check
        #[cfg(not(target_family = "windows"))]
        assert!(make_config("3", "3", "-1").has_ruby_dln_check_abi());

        // Ruby 4.0.0-dev - should have ABI check (this was the bug!)
        #[cfg(not(target_family = "windows"))]
        assert!(make_config("4", "0", "-1").has_ruby_dln_check_abi());

        // Ruby 4.0.0 release - no ABI check
        assert!(!make_config("4", "0", "0").has_ruby_dln_check_abi());

        // Ruby 4.1.0-dev - should have ABI check
        #[cfg(not(target_family = "windows"))]
        assert!(make_config("4", "1", "-1").has_ruby_dln_check_abi());

        // Ruby 2.7.x - too old
        assert!(!make_config("2", "7", "-1").has_ruby_dln_check_abi());
    }
}