rb_sys_env/
lib.rs

1#![allow(rustdoc::bare_urls)]
2#![doc = include_str!("../readme.md")]
3
4#[macro_use]
5mod utils;
6mod defines;
7mod rb_env;
8mod ruby_version;
9
10use std::error::Error;
11
12pub use defines::Defines;
13pub use rb_env::RbEnv;
14pub use ruby_version::RubyVersion;
15
16/// Configures Cargo linking based on the `DEP_RB_*` environment variables. This
17/// is needed to ensure that Cargo properly links to libruby on Windows..
18///
19/// ```should_panic
20/// // In your crate's build.rs
21///
22/// pub fn main() -> Result<(), Box<dyn std::error::Error>> {
23///     let rb_env = rb_sys_env::activate()?;
24///
25///     if rb_env.ruby_major_minor() < (2, 7) {
26///         panic!("Your Ruby version is EOL!");
27///     }
28///
29///     Ok(())
30/// }
31///
32/// // In your crate's lib.rs
33/// pub fn is_ruby_flonum_activated() -> bool {
34///     cfg!(ruby_use_flonum)
35/// }
36/// ```
37pub fn activate() -> Result<RbEnv, Box<dyn Error>> {
38    let env = RbEnv::default();
39
40    env.print_cargo_rustc_cfg();
41    env.print_encoded_cargo_args();
42
43    if std::env::var_os("RB_SYS_ENV_DEBUG").is_some() {
44        eprintln!("=======================");
45        eprintln!("The \"RB_SYS_ENV_DEBUG\" env var was detecting, aborted build.");
46        std::process::exit(1);
47    }
48
49    Ok(env)
50}
51
52/// Loads the `DEP_RB_*` environment variables, without setting Cargo configuration.
53///
54/// *Note*: This will not activate the `rb-sys` crate's features to ensure Ruby is properly linked.
55///
56/// ```
57/// // In your crate's build.rs
58///
59/// pub fn main() -> Result<(), Box<dyn std::error::Error>> {
60///     let rb_env = rb_sys_env::load()?;
61///
62///     rb_env.print_cargo_rerun_if_changed();
63///     rb_env.print_cargo_rustc_cfg();
64///     rb_env.print_encoded_cargo_args();
65///
66///     if rb_env.ruby_major_minor() < (2, 7) {
67///         panic!("Your Ruby version is EOL!");
68///     }
69///
70///     Ok(())
71/// }
72/// ```
73pub fn load() -> Result<RbEnv, Box<dyn Error>> {
74    let env = RbEnv::default();
75
76    Ok(env)
77}