Add run-subcommand
This commit is contained in:
		
							parent
							
								
									7dd06567bb
								
							
						
					
					
						commit
						28934ea6fc
					
				| @ -104,3 +104,14 @@ fn find_objectfile(name: &str) -> String { | |||||||
|         .unwrap() |         .unwrap() | ||||||
|         .to_owned() |         .to_owned() | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | pub fn execute(path: &PathBuf) { | ||||||
|  |     let output = Command::new(path.clone()).output().expect("Unable to execute {path}"); | ||||||
|  | 
 | ||||||
|  |     if !output.status.success() { | ||||||
|  |         let code = output.status.code().unwrap_or(255); | ||||||
|  |         log::error!("{path:?} exited with code {code}"); | ||||||
|  |         println!("{}", unsafe { String::from_utf8_unchecked(output.stderr) }); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
| @ -2,7 +2,11 @@ use std::{fs, path::PathBuf}; | |||||||
| 
 | 
 | ||||||
| use argh::FromArgs; | use argh::FromArgs; | ||||||
| use log::*; | use log::*; | ||||||
| use reid::{compile_simple, ld::LDRunner, CustomIRs}; | use reid::{ | ||||||
|  |     compile_simple, | ||||||
|  |     ld::{execute, LDRunner}, | ||||||
|  |     CustomIRs, | ||||||
|  | }; | ||||||
| use reid_lib::compile::CompileOutput; | use reid_lib::compile::CompileOutput; | ||||||
| 
 | 
 | ||||||
| #[derive(FromArgs, PartialEq, Debug)] | #[derive(FromArgs, PartialEq, Debug)] | ||||||
| @ -23,6 +27,7 @@ struct Options { | |||||||
| #[argh(subcommand)] | #[argh(subcommand)] | ||||||
| enum Command { | enum Command { | ||||||
|     Build(BuildOpts), |     Build(BuildOpts), | ||||||
|  |     Run(RunOpts), | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(FromArgs, PartialEq, Debug)] | #[derive(FromArgs, PartialEq, Debug)] | ||||||
| @ -36,6 +41,17 @@ struct BuildOpts { | |||||||
|     path: PathBuf, |     path: PathBuf, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[derive(FromArgs, PartialEq, Debug)] | ||||||
|  | /// Build and then execute the executable
 | ||||||
|  | #[argh(subcommand, name = "run")] | ||||||
|  | struct RunOpts { | ||||||
|  |     #[argh(option, long = "lib", short = 'l')] | ||||||
|  |     /// additional libraries to link against (with ld)
 | ||||||
|  |     libraries: Vec<String>, | ||||||
|  |     #[argh(positional)] | ||||||
|  |     path: PathBuf, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| fn main() { | fn main() { | ||||||
|     let options: Options = argh::from_env(); |     let options: Options = argh::from_env(); | ||||||
|     let mut errlog = stderrlog::new(); |     let mut errlog = stderrlog::new(); | ||||||
| @ -45,12 +61,12 @@ fn main() { | |||||||
|     } |     } | ||||||
|     errlog.init().unwrap(); |     errlog.init().unwrap(); | ||||||
| 
 | 
 | ||||||
|     match options.command { |     match &options.command { | ||||||
|         Command::Build(build) => { |         Command::Build(BuildOpts { libraries, path }) | Command::Run(RunOpts { libraries, path }) => { | ||||||
|             let cpu = std::env::var("CPU").unwrap_or("generic".to_owned()); |             let cpu = std::env::var("CPU").unwrap_or("generic".to_owned()); | ||||||
|             let features = std::env::var("REIDFLAGS").unwrap_or("".to_owned()); |             let features = std::env::var("REIDFLAGS").unwrap_or("".to_owned()); | ||||||
| 
 | 
 | ||||||
|             let path = match build.path.canonicalize() { |             let path = match path.canonicalize() { | ||||||
|                 Ok(path) => path, |                 Ok(path) => path, | ||||||
|                 Err(e) => { |                 Err(e) => { | ||||||
|                     error!("{e}"); |                     error!("{e}"); | ||||||
| @ -64,6 +80,7 @@ fn main() { | |||||||
|             let llir_path = parent.with_extension("llir"); |             let llir_path = parent.with_extension("llir"); | ||||||
|             let mir_path = parent.with_extension("mir"); |             let mir_path = parent.with_extension("mir"); | ||||||
|             let asm_path = parent.with_extension("asm"); |             let asm_path = parent.with_extension("asm"); | ||||||
|  |             let out_path = object_path.with_extension("out"); | ||||||
| 
 | 
 | ||||||
|             let before = std::time::SystemTime::now(); |             let before = std::time::SystemTime::now(); | ||||||
| 
 | 
 | ||||||
| @ -108,13 +125,23 @@ fn main() { | |||||||
| 
 | 
 | ||||||
|                     let linker = std::env::var("LD").unwrap_or("ld".to_owned()); |                     let linker = std::env::var("LD").unwrap_or("ld".to_owned()); | ||||||
|                     let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m"); |                     let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m"); | ||||||
|                     for library in build.libraries { |                     for library in libraries { | ||||||
|                         linker = linker.with_library(&library); |                         linker = linker.with_library(&library); | ||||||
|                     } |                     } | ||||||
|                     linker.invoke(&object_path, &object_path.with_extension("out")); |                     linker.invoke(&object_path, &out_path); | ||||||
|  |                 } | ||||||
|  |                 Err(e) => { | ||||||
|  |                     log::error!("{e}"); | ||||||
|  |                     return; | ||||||
|                 } |                 } | ||||||
|                 Err(e) => panic!("{}", e), |  | ||||||
|             }; |             }; | ||||||
|  | 
 | ||||||
|  |             match &options.command { | ||||||
|  |                 Command::Build(_) => {} | ||||||
|  |                 Command::Run(_) => { | ||||||
|  |                     execute(&out_path); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user