mason/examples/build.rs
Jason Travis Smith d500c86d68 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.
2016-04-28 03:04:35 -04:00

189 lines
3.7 KiB
Rust

#[macro_use]
extern crate scribe;
extern crate mason;
use std::env;
use std::path::PathBuf;
use mason::Processor;
pub const RESOURCES_DIR: &'static str = "resources";
pub const MANIFEST_FILENAME: &'static str = "resources.msnr";
fn determine_resources_dir() -> PathBuf
{
let mut resources_dir: PathBuf;
// Get the cargo manifest directory and then append the
// resource directory name to it.
match ::std::env::var_os("CARGO_MANIFEST_DIR")
{
Some(dir) =>
{
resources_dir = PathBuf::from(dir);
}
None =>
{
resources_dir = PathBuf::new();
}
}
resources_dir.push(RESOURCES_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 =>
{
// Try to just get the current directory.
match env::current_dir()
{
Ok(dir) =>
{
build_file_dir = PathBuf::from(dir);
}
Err(error) =>
{
// Then just default to "".
warn!("{}", error);
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 ::std::env::var_os("NUM_JOBS")
{
Some(count) =>
{
match count.into_string()
{
Ok(count_string) =>
{
match count_string.parse::<u64>()
{
Ok(val) =>
{
task_count = val;
}
Err(error) =>
{
error!("{}", error);
}
}
}
Err(_) =>
{
error!("Unable to convert task count string from OsString.");
}
}
}
None =>
{
task_count = 1u64;
}
}
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());
processor.set_task_count(task_count);
processor.process_manifest_file();
}