diff --git a/src/main.rs b/src/main.rs index f33a6a8..ef67107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate argparse; use std::io::*; use std::fs::*; +use std::process::Command; +use std::path::Path; mod c_code; use c_code::*; @@ -78,7 +80,7 @@ fn parse_args() -> CmdArgs { } fn main() { - let args = parse_args(); + let mut args = parse_args(); // write information header println!("// This source code was generated by anyexec2c."); println!("// Link: https://github.com/exyi/anyexec2C"); @@ -86,6 +88,12 @@ fn main() { // write the actual source code as C comments for f in args.comment_files { + let path = Path::new(&f); + if !path.exists() { + eprintln!("{} - file does not exist!", f); + ::std::process::exit(1); + } + println!("// ==============================", ); println!("// {}", f); println!("// =============================="); @@ -100,8 +108,51 @@ fn main() { } // build executable - //TODO - //TODO insert into args.exec_file + if let Some(build_file) = args.build_file { + let extension = build_file.clone(); + let extension = extension.split(".").last(); + if extension.is_none() { + eprintln!("Failed to split extension off build file name"); + ::std::process::exit(1); + } + let extension = extension.unwrap(); + match extension { + // D lang + "d" => { + let output = Command::new("dmd") + .arg("-O") + .arg(build_file) + .arg("-of=a.out") + .arg(format!("-od={}",::std::env::temp_dir().as_os_str().to_str().expect("TMP dir has non-unicode name!"))) + .output() + .expect("Failed to execute DMD to compile D source"); + // Check output and print it to stderr... + { + let stderr = ::std::io::stderr(); + let mut handle = stderr.lock(); + let _result = handle.write_all(&output.stdout); + let _result = handle.write_all(&output.stderr); + } + + if !output.status.success() { + eprintln!("\nCompilation error!"); + ::std::process::exit(1); + } + + // Check for compiled binary existence + let path = Path::new("a.out"); + if !path.exists() { + eprintln!("Couldn't find compiled binary... Failed."); + ::std::process::exit(1); + } + args.exec_file = Some(String::from("a.out")); + } + _ => { + eprintln!("File extension not recognized! Can't build!"); + ::std::process::exit(1); + } + } + } // read executable let exec_file = args.exec_file.unwrap();