From 47eca94d5d6b45e5f6da071f293336fbde31ada6 Mon Sep 17 00:00:00 2001 From: nivpgir Date: Sat, 26 Feb 2022 16:34:59 +0200 Subject: [PATCH 1/2] recompile snippet only if it's code has changed --- src/main.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 38c9102..3bfbcbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -330,7 +330,7 @@ fn main() { // proper Rust programs are accepted (this is a bit rough) let proper = code.find("fn main").is_some(); - let (rust_file, program) = if ! proper { + let (recompile, rust_file, program) = if ! proper { // otherwise we must create a proper program from the snippet // and write this as a file in the Runner bin directory... let mut extern_crates = args.get_strings("extern"); @@ -364,9 +364,16 @@ fn main() { } else { // we make up a name... bin.push("tmp.rs"); } + let recompile = if bin.is_file(){ + let old_code = fs::read_to_string(bin.clone()).unwrap(); + code.ne(&old_code) + } else { + true + }; + fs::write(&bin,&code).or_die("cannot write code"); let program = bin.with_extension(exe_suffix); - (bin, program) + (recompile, bin, program) } else { for line in code.lines() { if let Some(crate_name) = strutil::word_after(line,"extern crate ") { @@ -376,7 +383,7 @@ fn main() { // the 'proper' case - use the file name part bin.push(file.file_name().unwrap()); let program = bin.with_extension(exe_suffix); - (file, program) + (true, file, program) }; if b("run") { @@ -384,12 +391,14 @@ fn main() { args.quit(&format!("program {:?} does not exist",program)); } } else { - if ! compile_crate(&args,&state,"",&rust_file,Some(&program), externs, Vec::new()) { - process::exit(1); - } - if verbose { - println!("compiled {:?} successfully",rust_file); - } + if recompile{ + if ! compile_crate(&args, &state, "", &rust_file, Some(&program), externs, Vec::new()) { + process::exit(1); + } + if verbose { + println!("compiled {:?} successfully",rust_file); + } + } } if b("compile-only") { From 33b36855c8ce638bb1c3fef4a08d9774abd2f757 Mon Sep 17 00:00:00 2001 From: nivpgir Date: Sun, 27 Feb 2022 01:58:14 +0200 Subject: [PATCH 2/2] also recompile only if the compiled binary exists fixes a bug where a failed compilation doesn't recompile since if compilation failed the first time from an error that isn't a problem in the code (e.g. forgetting to run `runner --add`), running a second time won'y recompile, because the code hasn't changed. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3bfbcbe..c3784da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -366,7 +366,7 @@ fn main() { } let recompile = if bin.is_file(){ let old_code = fs::read_to_string(bin.clone()).unwrap(); - code.ne(&old_code) + code.ne(&old_code) && bin.with_extension(exe_suffix).is_file() } else { true };