2017-03-04 14:04:57 -05:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub const BUILD_DIR: &'static str = "build";
|
|
|
|
|
|
|
|
pub const ERRNO_GENERATOR_FILENAME: &'static str = "errno.c";
|
|
|
|
pub const ERRNO_GENERATOR_OUTPUT_FILENAME: &'static str = "errno.gen";
|
|
|
|
|
|
|
|
pub const GENERATED_ERRNO_FILENAME: &'static str = "errno_values.rs";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn determine_base_dir() -> PathBuf
|
|
|
|
{
|
|
|
|
// Get the cargo manifest directory.
|
|
|
|
match ::std::env::var_os("CARGO_MANIFEST_DIR")
|
|
|
|
{
|
|
|
|
Some(dir) =>
|
|
|
|
{
|
|
|
|
PathBuf::from(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
None =>
|
|
|
|
{
|
|
|
|
PathBuf::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn determine_output_dir() -> PathBuf
|
|
|
|
{
|
|
|
|
let output_dir: PathBuf;
|
|
|
|
|
|
|
|
// Get the output directory for the build.
|
|
|
|
match ::std::env::var_os("OUT_DIR")
|
|
|
|
{
|
|
|
|
Some(dir) =>
|
|
|
|
{
|
|
|
|
output_dir = PathBuf::from(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
None =>
|
|
|
|
{
|
|
|
|
// Try to just get the current directory.
|
|
|
|
match env::current_dir()
|
|
|
|
{
|
|
|
|
Ok(dir) =>
|
|
|
|
{
|
|
|
|
output_dir = PathBuf::from(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(error) =>
|
|
|
|
{
|
|
|
|
// Then just default to "".
|
2025-03-03 02:07:54 -05:00
|
|
|
println!("WARN: {}", error);
|
2017-03-04 14:04:57 -05:00
|
|
|
output_dir = PathBuf::new();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
fn determine_build_dir() -> PathBuf
|
|
|
|
{
|
|
|
|
let mut build_dir: PathBuf;
|
|
|
|
|
|
|
|
build_dir = determine_base_dir();
|
|
|
|
build_dir.push(BUILD_DIR);
|
|
|
|
|
|
|
|
build_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
fn determine_c_compiler() -> OsString
|
|
|
|
{
|
|
|
|
match ::std::env::var_os("CC")
|
|
|
|
{
|
|
|
|
Some(compiler) =>
|
|
|
|
{
|
|
|
|
OsString::from(compiler)
|
|
|
|
}
|
|
|
|
|
|
|
|
None =>
|
|
|
|
{
|
|
|
|
OsString::from("gcc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_output_file(filename: &str) -> PathBuf
|
|
|
|
{
|
|
|
|
let mut output_dir: PathBuf;
|
|
|
|
|
|
|
|
output_dir = determine_output_dir();
|
|
|
|
output_dir.push(filename);
|
|
|
|
|
|
|
|
output_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_src_file(filename: &str) -> PathBuf
|
|
|
|
{
|
|
|
|
let mut build_dir: PathBuf;
|
|
|
|
|
|
|
|
build_dir = determine_build_dir();
|
|
|
|
build_dir.push(filename);
|
|
|
|
|
|
|
|
build_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile(src_files: &[PathBuf], output_name: &str) -> PathBuf
|
|
|
|
{
|
|
|
|
let c_compiler: OsString;
|
2020-02-12 22:48:29 -05:00
|
|
|
let output_file: PathBuf;
|
2017-03-04 14:04:57 -05:00
|
|
|
let mut initial_command: ::std::process::Command;
|
|
|
|
let command: &mut ::std::process::Command;
|
|
|
|
let last_command: &mut ::std::process::Command;
|
|
|
|
let final_command: &mut ::std::process::Command;
|
|
|
|
|
|
|
|
c_compiler = determine_c_compiler();
|
|
|
|
output_file = build_output_file(output_name);
|
|
|
|
|
|
|
|
initial_command = ::std::process::Command::new(c_compiler);
|
|
|
|
command = initial_command.args(src_files);
|
|
|
|
last_command = command.arg("-o");
|
|
|
|
final_command = last_command.arg(output_file.clone());
|
|
|
|
|
|
|
|
match final_command.output()
|
|
|
|
{
|
|
|
|
Ok(output) =>
|
|
|
|
{
|
|
|
|
if output.status.success() == false
|
|
|
|
{
|
2025-03-03 02:07:54 -05:00
|
|
|
println!("ERROR: {:?}", output);
|
|
|
|
panic!();
|
2017-03-04 14:04:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(error) =>
|
|
|
|
{
|
2025-03-03 02:07:54 -05:00
|
|
|
println!("ERROR: {}", error);
|
|
|
|
panic!();
|
2017-03-04 14:04:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output_file
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(executable: PathBuf, args: &[PathBuf])
|
|
|
|
{
|
|
|
|
let mut initial_command: ::std::process::Command;
|
|
|
|
let final_command: &mut ::std::process::Command;
|
|
|
|
|
|
|
|
initial_command = ::std::process::Command::new(executable);
|
|
|
|
final_command = initial_command.args(args);
|
|
|
|
|
|
|
|
match final_command.output()
|
|
|
|
{
|
|
|
|
Ok(output) =>
|
|
|
|
{
|
|
|
|
if output.status.success() == false
|
|
|
|
{
|
2025-03-03 02:07:54 -05:00
|
|
|
println!("ERROR: {:?}", output);
|
|
|
|
panic!();
|
2017-03-04 14:04:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(error) =>
|
|
|
|
{
|
2025-03-03 02:07:54 -05:00
|
|
|
println!("ERROR: {}", error);
|
|
|
|
panic!();
|
2017-03-04 14:04:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_and_run(output_name: &str, src_files: &[PathBuf], args: &[PathBuf])
|
|
|
|
{
|
|
|
|
let executable: PathBuf;
|
|
|
|
|
|
|
|
executable = compile(src_files, output_name);
|
|
|
|
run(executable, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main()
|
|
|
|
{
|
2019-01-19 23:54:34 -05:00
|
|
|
let _gen_file: PathBuf;
|
2020-02-12 22:48:29 -05:00
|
|
|
let errno_file: PathBuf;
|
|
|
|
let gen_errno_file: PathBuf;
|
2017-03-04 14:04:57 -05:00
|
|
|
|
|
|
|
errno_file = build_src_file(ERRNO_GENERATOR_FILENAME);
|
2019-01-19 23:54:34 -05:00
|
|
|
_gen_file = build_output_file(ERRNO_GENERATOR_OUTPUT_FILENAME);
|
2017-03-04 14:04:57 -05:00
|
|
|
gen_errno_file = build_output_file(GENERATED_ERRNO_FILENAME);
|
|
|
|
|
|
|
|
compile_and_run(ERRNO_GENERATOR_OUTPUT_FILENAME,
|
|
|
|
&[errno_file], &[gen_errno_file]);
|
|
|
|
}
|