Skip to content

Commit

Permalink
implemented build flag for D lang
Browse files Browse the repository at this point in the history
  • Loading branch information
vakabus committed Jan 29, 2018
1 parent 41c92ae commit 2bc3017
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -78,14 +80,20 @@ 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");
println!();

// 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!("// ==============================");
Expand All @@ -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();
Expand Down

0 comments on commit 2bc3017

Please sign in to comment.