use std::io::{BufRead, BufReader, Write};
use std::env;
use std::fs::File;
use std::path::Path;
use regex::Regex;
pub fn export_bool_kconfig() {
let dotconfig = env::var("DOTCONFIG").expect("DOTCONFIG must be set by wrapper");
println!("cargo:rerun-if-env-changed=DOTCONFIG");
println!("cargo-rerun-if-changed={}", dotconfig);
let config_y = Regex::new(r"^(CONFIG_.*)=y$").unwrap();
let file = File::open(&dotconfig).expect("Unable to open dotconfig");
for line in BufReader::new(file).lines() {
let line = line.expect("reading line from dotconfig");
if let Some(caps) = config_y.captures(&line) {
println!("cargo:rustc-cfg={}", &caps[1]);
}
}
}
pub fn build_kconfig_mod() {
let dotconfig = env::var("DOTCONFIG").expect("DOTCONFIG must be set by wrapper");
let outdir = env::var("OUT_DIR").expect("OUT_DIR must be set");
let config_hex = Regex::new(r"^(CONFIG_.*)=(0x[0-9a-fA-F]+)$").unwrap();
let config_int = Regex::new(r"^(CONFIG_.*)=(-?[1-9][0-9]*)$").unwrap();
let config_str = Regex::new(r#"^(CONFIG_.*)=(".*")$"#).unwrap();
let gen_path = Path::new(&outdir).join("kconfig.rs");
let mut f = File::create(&gen_path).unwrap();
let file = File::open(&dotconfig).expect("Unable to open dotconfig");
for line in BufReader::new(file).lines() {
let line = line.expect("reading line from dotconfig");
if let Some(caps) = config_hex.captures(&line) {
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
writeln!(&mut f, "pub const {}: usize = {};",
&caps[1], &caps[2]).unwrap();
} else if let Some(caps) = config_int.captures(&line) {
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
writeln!(&mut f, "pub const {}: isize = {};",
&caps[1], &caps[2]).unwrap();
} else if let Some(caps) = config_str.captures(&line) {
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
writeln!(&mut f, "pub const {}: &'static str = {};",
&caps[1], &caps[2]).unwrap();
}
}
}