File: features.rs

package info (click to toggle)
rust-rb-sys 0.9.102-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: ansic: 71; sh: 16; makefile: 2
file content (106 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download
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
use rb_sys_build::{utils::is_mswin_or_mingw, RbConfig};

use crate::version::Version;

pub(crate) fn is_global_allocator_enabled(rb_config: &RbConfig) -> bool {
    let (major, minor) = rb_config.major_minor();
    let current_version = Version::new(major, minor);
    let two_four = Version::new(2, 4);
    let is_enabled = is_env_variable_defined("CARGO_FEATURE_GLOBAL_ALLOCATOR");

    if current_version >= two_four {
        is_enabled
    } else {
        if is_enabled {
            eprintln!("WARN: The global allocator feature is only supported on Ruby 2.4+.");
        }
        false
    }
}

pub(crate) fn is_gem_enabled() -> bool {
    cfg!(rb_sys_gem)
}

pub(crate) fn is_no_link_ruby_enabled() -> bool {
    is_env_variable_defined("CARGO_FEATURE_NO_LINK_RUBY")
}

pub(crate) fn is_debug_build_enabled() -> bool {
    if is_linting() {
        return false;
    }

    println!("cargo:rerun-if-env-changed=RB_SYS_DEBUG_BUILD");

    is_env_variable_defined("RB_SYS_DEBUG_BUILD")
}

pub(crate) fn is_ruby_static_enabled(rbconfig: &RbConfig) -> bool {
    println!("cargo:rerun-if-env-changed=RUBY_STATIC");

    match std::env::var("RUBY_STATIC") {
        Ok(val) => val == "true" || val == "1",
        _ => {
            is_env_variable_defined("CARGO_FEATURE_RUBY_STATIC")
                || rbconfig.get("ENABLE_SHARED") == "no"
        }
    }
}

pub(crate) fn is_link_ruby_enabled() -> bool {
    if is_linting() {
        return false;
    }

    if is_no_link_ruby_enabled() {
        false
    } else if is_mswin_or_mingw() {
        true
    } else if is_gem_enabled() {
        if is_env_variable_defined("CARGO_FEATURE_LINK_RUBY") {
            let msg = "
                The `gem` and `link-ruby` features are mutually exclusive on this
                platform, since the libruby symbols will be available at runtime.

                If you for some reason want to dangerously link libruby for your gem
                (*not recommended*), you can remove the `gem` feature and add this
                to your `Cargo.toml`:

                [dependencies.rb-sys]
                features = [\"link-ruby\"] # Living dangerously!
            "
            .split('\n')
            .map(|line| line.trim())
            .collect::<Vec<_>>()
            .join("\n");

            eprintln!("ERROR: {}", msg);
            std::process::exit(1);
        } else {
            false
        }
    } else {
        is_env_variable_defined("CARGO_FEATURE_LINK_RUBY")
    }
}

pub(crate) fn is_env_variable_defined(name: &str) -> bool {
    std::env::var(name).is_ok()
}

fn is_linting() -> bool {
    println!("cargo:rerun-if-env-changed=RUSTC_WRAPPER");

    let clippy = match std::env::var_os("CARGO_CFG_FEATURE") {
        Some(val) => val.to_str().unwrap_or("").contains("clippy"),
        _ => false,
    };

    let rust_analyzer = match std::env::var_os("RUSTC_WRAPPER") {
        Some(val) => val.to_str().unwrap_or("").contains("rust-analyzer"),
        _ => false,
    };

    clippy || rust_analyzer
}