diff --git a/build.rs b/build.rs index 7430808cecd4fe1d31ba60c1d13726de1d8f5d9f..2f88dcd0355cb36731d33d570d7d955c1ee05186 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,7 @@ use regex::Regex; use std::{ env, + ffi::OsStr, fs::File, io::{BufRead, BufReader, Write}, path::PathBuf, @@ -10,26 +11,35 @@ fn main() { let target_os = env::var("CARGO_CFG_LITEOS").is_ok(); // Decode liteos configuration information and automatically generate configuration files. if target_os { - let pwd = env::var("PWD").unwrap(); - let pwd = PathBuf::from_str(&pwd).unwrap(); + let out_dir = env::var("OUT_DIR").unwrap(); + let out_path = PathBuf::from_str(&out_dir).unwrap(); + let mut top_path = PathBuf::new(); + for i in out_path.iter() { + if i == OsStr::new("target") { + break; + } + top_path.push(i); + } + let manifest_path = env::var("CARGO_MANIFEST_DIR").unwrap(); let manifest_path = PathBuf::from_str(&manifest_path).unwrap(); let los_path = env::var("CARGO_CFG_LOS_PATH").unwrap(); - let los_path = pwd.join(&los_path); + let los_path = top_path.join(&los_path).canonicalize().unwrap(); let fs = File::options() .read(true) .open(los_path.join(".config")) .unwrap(); + let fs = BufReader::new(fs); - println!("{}",manifest_path.join("src/liteos/config.rs").to_str().unwrap()); let mut fcfg = File::options() .write(true) .create(true) .truncate(true) .open(manifest_path.join("src/liteos/config.rs")) .unwrap(); + let is_empty_line = Regex::new(r"^ *?#|^ *?$").unwrap(); let kv = Regex::new(r#"^ *?(\w+) *?= *?(.*)$"#).unwrap(); @@ -58,6 +68,10 @@ fn main() { "cargo:rerun-if-changed={}", los_path.join(".config").to_str().unwrap() ); + println!( + "cargo:rerun-if-changed={}", + top_path.join(".cargo/config.toml").to_str().unwrap() + ) } println!("cargo:rerun-if-changed=build.rs"); }