forked from akubera/bigdecimal-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
48 lines (33 loc) · 1.76 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
use std::env;
use std::path::{Path, PathBuf};
fn main() {
let ac = autocfg::new();
ac.emit_rustc_version(1, 70);
// Option::zip
ac.emit_rustc_version(1, 46);
// Remove this comment if enabled proptests
// ::PROPERTY-TESTS:: autocfg::emit("property_tests");
let outdir: PathBuf = std::env::var_os("OUT_DIR").unwrap().into();
write_default_precision_file(&outdir);
write_default_rounding_mode(&outdir);
}
/// Create default_precision.rs, containing definition of constant DEFAULT_PRECISION loaded in src/lib.rs
fn write_default_precision_file(outdir: &Path) {
let env_var = env::var("RUST_BIGDECIMAL_DEFAULT_PRECISION").unwrap_or_else(|_| "100".to_owned());
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_PRECISION");
let rust_file_path = outdir.join("default_precision.rs");
let default_prec: u32 = env_var
.parse::<std::num::NonZeroU32>()
.expect("$RUST_BIGDECIMAL_DEFAULT_PRECISION must be an integer > 0")
.into();
let rust_file_contents = format!("const DEFAULT_PRECISION: u64 = {};", default_prec);
std::fs::write(rust_file_path, rust_file_contents).unwrap();
}
/// Create default_rounding_mode.rs, using value of RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE environment variable
fn write_default_rounding_mode(outdir: &Path) {
let rounding_mode_name = env::var("RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE").unwrap_or_else(|_| "HalfEven".to_owned());
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE");
let rust_file_path = outdir.join("default_rounding_mode.rs");
let rust_file_contents = format!("const DEFAULT_ROUNDING_MODE: RoundingMode = RoundingMode::{};", rounding_mode_name);
std::fs::write(rust_file_path, rust_file_contents).unwrap();
}