Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build.rs #110

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 12 additions & 38 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
#![allow(clippy::style)]

extern crate autocfg;

use std::env;
use std::path::PathBuf;


fn main() -> std::io::Result<()> {
fn main() {
let ac = autocfg::new();
ac.emit_rustc_version(1, 70);

let outdir = match std::env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
};
let outdir_path = PathBuf::from(outdir);

write_default_precision(&outdir_path, "default_precision.rs")?;
Ok(())
}

/// Create default_precision.rs, containg definition of constant DEFAULT_PRECISION
fn write_default_precision(outdir_path: &PathBuf, filename: &str) -> std::io::Result<()>
{

let default_prec = env::var("RUST_BIGDECIMAL_DEFAULT_PRECISION")
.map(|s| s.parse::<std::num::NonZeroU32>().expect("$RUST_BIGDECIMAL_DEFAULT_PRECISION must be an integer > 0"))
.map(|nz_num| nz_num.into())
.unwrap_or(100u32);

let default_precision_rs_path = outdir_path.join(filename);
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 default_precision = format!("const DEFAULT_PRECISION: u64 = {};", default_prec);
let outdir = std::env::var_os("OUT_DIR").unwrap();
let rust_file_path = PathBuf::from(outdir).join("default_precision.rs");

// Rewriting the file if it already exists with the same contents
// would force a rebuild.
match std::fs::read_to_string(&default_precision_rs_path) {
Ok(existing_contents) if existing_contents == default_precision => {},
_ => {
std::fs::write(&default_precision_rs_path, default_precision)
.expect("Could not write big decimal default-precision file");
}
};
let default_prec: u32 = env_var
.parse::<std::num::NonZeroU32>()
.expect("$RUST_BIGDECIMAL_DEFAULT_PRECISION must be an integer > 0")
.into();

println!("cargo:rerun-if-changed={}", default_precision_rs_path.display());
println!("cargo:rerun-if-env-changed={}", "RUST_BIGDECIMAL_DEFAULT_PRECISION");
let rust_file_contents = format!("const DEFAULT_PRECISION: u64 = {};", default_prec);

Ok(())
}
std::fs::write(rust_file_path, rust_file_contents).unwrap();
}