-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
66 lines (57 loc) · 1.75 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![feature(let_chains)]
use std::{
collections::HashMap,
io::{BufWriter, Write},
};
fn main() {
generate_configs();
#[cfg(feature = "c-bindgen")]
generate_c_bindings();
}
fn generate_configs() {
#[derive(Debug, Clone, Copy)]
struct ConfigValue {
value: usize,
comment: &'static str,
}
const CONFIGS: &[(&str, ConfigValue)] = &[
("SHARD_SHIFT", ConfigValue {
value: 6 + 10,
comment: "The minimal allocation unit of slabs, in bits.",
}),
("SLAB_SHIFT", ConfigValue {
value: 2 + 10 + 10,
comment: "The minimal allocation unit of arenas, in bits.",
}),
];
let mut configs = CONFIGS.iter().copied().collect::<HashMap<_, _>>();
for (env, value) in std::env::vars() {
if let Some(name) = env.strip_prefix("FE_")
&& let Some(slot) = configs.get_mut(name)
&& let Ok(value) = value.parse::<usize>()
{
slot.value = value;
}
}
let output_dir = std::env::var("OUT_DIR").unwrap();
let file = std::fs::File::create(format!("{output_dir}/config.rs")).unwrap();
let mut file = BufWriter::new(file);
for (name, config) in configs {
writeln!(
&mut file,
"#[doc = \"{}\"] pub const {name}: usize = {};",
config.comment, config.value
)
.unwrap();
}
}
#[cfg(feature = "c-bindgen")]
fn generate_c_bindings() {
let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
cbindgen::Builder::new()
.with_crate(&crate_dir)
.with_config(cbindgen::Config::from_root_or_default(crate_dir))
.generate()
.expect("failed to generate C bindings")
.write_to_file("ferroc.h");
}