Commit 5e591e74 authored by Sven Liefgen's avatar Sven Liefgen
Browse files

Rerurn build.rs on change of environment variable

parent cce95d95
......@@ -8,56 +8,58 @@ fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("xlap_generated.rs");
let xlap = env::var("XLAP").unwrap();
let yaml = fs::read_to_string(&xlap).unwrap();
if let Ok(xlap) = env::var("XLAP") {
let yaml = fs::read_to_string(&xlap).unwrap();
let yaml = YamlLoader::load_from_str(yaml.as_str()).unwrap().pop().unwrap();
let yaml = YamlLoader::load_from_str(yaml.as_str()).unwrap().pop().unwrap();
// Create XLAP array containing a table for each kind
// Create XLAP array containing a table for each kind
let packet_types = &yaml["packet_types"];
let packet_types_count = packet_types.as_hash().unwrap().len();
let packet_types = &yaml["packet_types"];
let packet_types_count = packet_types.as_hash().unwrap().len();
let mut static_xlap = format!("static XLAP: [AtomicPtr<TimestampTable>; {}] = [\n", packet_types_count);
for _ in 0..packet_types_count {
static_xlap.push_str(" AtomicPtr::new(std::ptr::null_mut()),\n");
}
static_xlap.push_str("];\n");
// Create constants for kinds
let mut kind_consts = String::new();
for (i, kind) in packet_types.as_hash().unwrap().keys().enumerate() {
kind_consts.push_str("pub const TS_");
kind_consts.push_str(kind.as_str().unwrap().to_uppercase().as_str());
kind_consts.push_str(": usize = ");
kind_consts.push_str(i.to_string().as_str());
kind_consts.push_str(";\n");
}
let mut static_xlap = format!("static XLAP: [AtomicPtr<TimestampTable>; {}] = [\n", packet_types_count);
for _ in 0..packet_types_count {
static_xlap.push_str(" AtomicPtr::new(std::ptr::null_mut()),\n");
}
static_xlap.push_str("];\n");
// Create constants for kinds
let mut kind_consts = String::new();
for (i, kind) in packet_types.as_hash().unwrap().keys().enumerate() {
kind_consts.push_str("pub const TS_");
kind_consts.push_str(kind.as_str().unwrap().to_uppercase().as_str());
kind_consts.push_str(": usize = ");
kind_consts.push_str(i.to_string().as_str());
kind_consts.push_str(";\n");
}
// Create timestamps
// Create timestamps
let timestamps = &yaml["stamps"];
let timestamps = &yaml["stamps"];
let mut timestamp_id_enum = String::from("#[derive(EnumCount, EnumIter, AsRefStr)]\n");
timestamp_id_enum.push_str("pub enum TimestampId {\n");
for timestamp_id in timestamps.as_hash().unwrap().keys() {
timestamp_id_enum.push_str(" ");
timestamp_id_enum.push_str(timestamp_id.as_str().unwrap());
timestamp_id_enum.push_str(",\n");
}
timestamp_id_enum.push_str("}\n");
let mut timestamp_id_enum = String::from("#[derive(EnumCount, EnumIter, AsRefStr)]\n");
timestamp_id_enum.push_str("pub enum TimestampId {\n");
for timestamp_id in timestamps.as_hash().unwrap().keys() {
timestamp_id_enum.push_str(" ");
timestamp_id_enum.push_str(timestamp_id.as_str().unwrap());
timestamp_id_enum.push_str(",\n");
fs::write(
&dest_path,
format!("{}\n{}\n{}\n",
static_xlap,
kind_consts,
timestamp_id_enum,
)
)
.unwrap();
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=src/lib.rs");
println!("cargo::rerun-if-changed={}", xlap);
println!("cargo::rerun-if-env-changed=XLAP");
}
timestamp_id_enum.push_str("}\n");
fs::write(
&dest_path,
format!("{}\n{}\n{}\n",
static_xlap,
kind_consts,
timestamp_id_enum,
)
)
.unwrap();
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=src/lib.rs");
println!("cargo::rerun-if-changed={}", xlap);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment