From d500c86d68859e715d59df7f4a125b3d414f66cd Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Thu, 28 Apr 2016 03:04:35 -0400 Subject: [PATCH] A fancy new build script example. This uses the correct method of getting the environment variables and now correctly find the output directory. It needs more comments though. --- examples/build.rs | 117 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 102 insertions(+), 15 deletions(-) diff --git a/examples/build.rs b/examples/build.rs index 2b7b0ac..ec065f8 100644 --- a/examples/build.rs +++ b/examples/build.rs @@ -17,16 +17,13 @@ pub const MANIFEST_FILENAME: &'static str = "resources.msnr"; -pub fn main() +fn determine_resources_dir() -> PathBuf { - let task_count: u64; - let output_dir: PathBuf; let mut resources_dir: PathBuf; - let mut processor: Processor; // Get the cargo manifest directory and then append the // resource directory name to it. - match option_env!("CARGO_MANIFEST_DIR") + match ::std::env::var_os("CARGO_MANIFEST_DIR") { Some(dir) => { @@ -40,12 +37,67 @@ pub fn main() } resources_dir.push(RESOURCES_DIR); - // Get the output directory for the build. - match option_env!("OUT_DIR") + resources_dir +} + +fn determine_output_dir() -> PathBuf +{ + let mut next_parent: bool; + let mut output_dir: PathBuf; + + match ::std::env::var_os("OUT_DIR") { Some(dir) => { + // Take the given directory and walk up until + // we are at the release or debug directory. output_dir = PathBuf::from(dir); + + next_parent = true; + while next_parent == true + { + match output_dir.file_name() + { + Some(name) => + { + if name == "release" || name == "debug" + { + next_parent = false; + } + } + + None => + { + next_parent = false; + } + } + + if next_parent == true + { + output_dir.pop(); + } + } + } + + None => + { + error!("No output directory found."); + } + } + + output_dir +} + +fn determine_build_file_dir() -> PathBuf +{ + let build_file_dir: PathBuf; + + // Get the output directory for the build. + match ::std::env::var_os("OUT_DIR") + { + Some(dir) => + { + build_file_dir = PathBuf::from(dir); } None => @@ -55,34 +107,52 @@ pub fn main() { Ok(dir) => { - output_dir = PathBuf::from(dir); + build_file_dir = PathBuf::from(dir); } Err(error) => { // Then just default to "". warn!("{}", error); - output_dir = PathBuf::new(); + build_file_dir = PathBuf::new(); } } } } + build_file_dir +} + +fn determine_task_count() -> u64 +{ + let task_count: u64; + // Get how many compile tasks should be run at one time. - match option_env!("NUM_JOBS") + match ::std::env::var_os("NUM_JOBS") { Some(count) => { - match count.parse::() + match count.into_string() { - Ok(val) => + Ok(count_string) => { - task_count = val; + match count_string.parse::() + { + Ok(val) => + { + task_count = val; + } + + Err(error) => + { + error!("{}", error); + } + } } - Err(error) => + Err(_) => { - error!("{}", error); + error!("Unable to convert task count string from OsString."); } } } @@ -93,6 +163,23 @@ pub fn main() } } + task_count +} + + +pub fn main() +{ + let task_count: u64; + let _build_file_dir: PathBuf; + let output_dir: PathBuf; + let resources_dir: PathBuf; + let mut processor: Processor; + + _build_file_dir = determine_build_file_dir(); + output_dir = determine_output_dir(); + resources_dir = determine_resources_dir(); + task_count = determine_task_count(); + processor = Processor::new(); processor.set_resources_dir(resources_dir.as_path()); processor.set_output_dir(output_dir.as_path());