From bc5f4ff0574f1c82516884a412d6091a25e686e5 Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Fri, 21 Oct 2022 14:35:29 +0100 Subject: [PATCH 1/3] Compile generated rust code during reduction. This was necessary to help to diagnose #1170. --- tools/reduce/src/main.rs | 70 +++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/tools/reduce/src/main.rs b/tools/reduce/src/main.rs index 03b4fd602..ceb5a4ed2 100644 --- a/tools/reduce/src/main.rs +++ b/tools/reduce/src/main.rs @@ -36,6 +36,13 @@ autocxx-reduce file -I my-inc-dir -h my-header -d 'generate!(\"MyClass\")' -k -- "}; fn main() { + // Assemble some defaults for command line arguments + let current_exe = std::env::current_exe().unwrap(); + let our_dir = current_exe.parent().unwrap(); + let default_gen_cmd = our_dir.join("autocxx-gen").to_str().unwrap().to_string(); + let rust_libs_path1 = our_dir.to_str().unwrap().to_string(); + let rust_libs_path2 = our_dir.join("deps").to_str().unwrap().to_string(); + let default_rlibs = &[rust_libs_path1.as_str(), rust_libs_path2.as_str()]; let matches = Command::new("autocxx-reduce") .version(crate_version!()) .author(crate_authors!()) @@ -138,6 +145,24 @@ fn main() { .long("gen-cmd") .value_name("GEN-CMD") .help("where to find autocxx-gen") + .default_value(&default_gen_cmd) + .takes_value(true), + ) + .arg( + Arg::new("rustc") + .long("rustc") + .value_name("RUSTC") + .help("where to find rustc") + .default_value("rustc") + .takes_value(true), + ) + .arg( + Arg::new("rlibs") + .long("rlibs") + .value_name("LIBDIR") + .help("where to find rlibs/rmetas for cxx and autocxx") + .default_values(default_rlibs) + .multiple_values(true) .takes_value(true), ) .arg( @@ -171,6 +196,11 @@ fn main() { .long("no-postcompile") .help("Do not post-compile the C++ generated by autocxxgen"), ) + .arg( + Arg::new("no-rustc") + .long("no-rustc") + .help("Do not compile the rust generated by autocxxgen"), + ) .arg( Arg::new("suppress-cxx-inclusions") .long("suppress-cxx-inclusions") @@ -277,20 +307,23 @@ fn do_run(matches: ArgMatches, tmp_dir: &TempDir) -> Result<(), std::io::Error> .collect(); let extra_clang_args: Vec<&str> = extra_clang_args.iter().map(|s| s.as_ref()).collect_vec(); - let default_gen_cmd = std::env::current_exe()? - .parent() - .unwrap() - .join("autocxx-gen") - .to_str() - .unwrap() - .to_string(); - let gen_cmd = matches.value_of("gen-cmd").unwrap_or(&default_gen_cmd); + let gen_cmd = matches.value_of("gen-cmd").unwrap(); if !Path::new(gen_cmd).exists() { panic!( "autocxx-gen not found in {}. hint: autocxx-reduce --gen-cmd /path/to/autocxx-gen", gen_cmd ); } + + let rustc = matches.value_of("rustc").unwrap(); + + let rust_libs_paths: Vec = matches + .get_many::("rlib") + .expect("No rlib path specified") + .into_iter() + .cloned() + .collect(); + run_sample_gen_cmd(gen_cmd, &rs_path, tmp_dir.path(), &extra_clang_args)?; // Create and run an interestingness test which does not filter its output through grep. let demo_interestingness_test_dir = tmp_dir.path().join("demo-interestingness-test"); @@ -298,12 +331,15 @@ fn do_run(matches: ArgMatches, tmp_dir: &TempDir) -> Result<(), std::io::Error> let interestingness_test = demo_interestingness_test_dir.join("test-demo.sh"); create_interestingness_test( gen_cmd, + &rust_libs_paths, &interestingness_test, None, &rs_path, &extra_clang_args, + rustc, !matches.is_present("no-precompile"), !matches.is_present("no-postcompile"), + !matches.is_present("no-rustc"), )?; let demo_dir_concat_path = demo_interestingness_test_dir.join("concat.h"); std::fs::copy(&concat_path, demo_dir_concat_path).unwrap(); @@ -313,12 +349,15 @@ fn do_run(matches: ArgMatches, tmp_dir: &TempDir) -> Result<(), std::io::Error> let interestingness_test = tmp_dir.path().join("test.sh"); create_interestingness_test( gen_cmd, + &rust_libs_paths, &interestingness_test, Some(matches.value_of("problem").unwrap()), &rs_path, &extra_clang_args, + rustc, !matches.is_present("no-precompile"), !matches.is_present("no-postcompile"), + !matches.is_present("no-rustc"), )?; run_creduce( matches.value_of("creduce").unwrap(), @@ -452,12 +491,15 @@ fn format_gen_cmd<'a>( fn create_interestingness_test( gen_cmd: &str, + rust_libs_path: &[String], test_path: &Path, problem: Option<&str>, rs_file: &Path, extra_clang_args: &[&str], + rustc_path: &str, precompile: bool, postcompile: bool, + rustc: bool, ) -> Result<(), std::io::Error> { announce_progress("Creating interestingness test"); // Ensure we refer to the input header by relative path @@ -469,6 +511,12 @@ fn create_interestingness_test( // For the compile afterwards, we have to avoid including any system headers. // We rely on equivalent content being hermetically inside concat.h. let postcompile_step = make_compile_step(postcompile, "gen0.cc", extra_clang_args); + let rustc_step = if rustc { + let rust_libs_path = rust_libs_path.iter().map(|p| format!(" -L{}", p)).join(" "); + format!("{} --extern cxx --extern autocxx {} --crate-type rlib --emit=metadata --edition=2021 autocxx-ffi-default-gen.rs 2>&1", rustc_path, rust_libs_path) + } else { + "echo Skipping rustc".to_string() + }; // -q below to exit immediately as soon as a match is found, to avoid // extra compile/codegen steps let problem_grep = problem @@ -482,16 +530,16 @@ fn create_interestingness_test( {} echo Move mv concat.h concat-body.h - echo Codegen (echo \"#ifndef __CONCAT_H__\"; echo \"#define __CONCAT_H__\"; echo '#include \"concat-body.h\"'; echo \"#endif\") > concat.h - (trap \"if [[ \\$? -eq 139 ]]; then echo Segfault; fi\" CHLD; {} {} 2>&1 && cat autocxx-ffi-default-gen.rs && cat autocxxgen*.h && {} 2>&1 ) {} + echo Codegen + (trap \"if [[ \\$? -eq 139 ]]; then echo Segfault; fi\" CHLD; {} {} 2>&1 && cat autocxx-ffi-default-gen.rs && cat autocxxgen*.h && {} && {} 2>&1 ) {} echo Remove rm concat.h echo Swap back mv concat-body.h concat.h echo Done "}, - precompile_step, gen_cmd, args, postcompile_step, problem_grep + precompile_step, gen_cmd, args, rustc_step, postcompile_step, problem_grep ); println!("Interestingness test:\n{}", content); { From 1cdbcc9648a24524793e41bbbb4383a49e7e762e Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Fri, 21 Oct 2022 14:39:29 +0100 Subject: [PATCH 2/3] Clippy complaint. --- tools/reduce/src/main.rs | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/tools/reduce/src/main.rs b/tools/reduce/src/main.rs index ceb5a4ed2..0340b0942 100644 --- a/tools/reduce/src/main.rs +++ b/tools/reduce/src/main.rs @@ -315,31 +315,18 @@ fn do_run(matches: ArgMatches, tmp_dir: &TempDir) -> Result<(), std::io::Error> ); } - let rustc = matches.value_of("rustc").unwrap(); - - let rust_libs_paths: Vec = matches - .get_many::("rlib") - .expect("No rlib path specified") - .into_iter() - .cloned() - .collect(); - run_sample_gen_cmd(gen_cmd, &rs_path, tmp_dir.path(), &extra_clang_args)?; // Create and run an interestingness test which does not filter its output through grep. let demo_interestingness_test_dir = tmp_dir.path().join("demo-interestingness-test"); std::fs::create_dir(&demo_interestingness_test_dir).unwrap(); let interestingness_test = demo_interestingness_test_dir.join("test-demo.sh"); create_interestingness_test( + &matches, gen_cmd, - &rust_libs_paths, &interestingness_test, None, &rs_path, &extra_clang_args, - rustc, - !matches.is_present("no-precompile"), - !matches.is_present("no-postcompile"), - !matches.is_present("no-rustc"), )?; let demo_dir_concat_path = demo_interestingness_test_dir.join("concat.h"); std::fs::copy(&concat_path, demo_dir_concat_path).unwrap(); @@ -348,16 +335,12 @@ fn do_run(matches: ArgMatches, tmp_dir: &TempDir) -> Result<(), std::io::Error> // Now the main interestingness test let interestingness_test = tmp_dir.path().join("test.sh"); create_interestingness_test( + &matches, gen_cmd, - &rust_libs_paths, &interestingness_test, Some(matches.value_of("problem").unwrap()), &rs_path, &extra_clang_args, - rustc, - !matches.is_present("no-precompile"), - !matches.is_present("no-postcompile"), - !matches.is_present("no-rustc"), )?; run_creduce( matches.value_of("creduce").unwrap(), @@ -490,18 +473,27 @@ fn format_gen_cmd<'a>( } fn create_interestingness_test( + matches: &ArgMatches, gen_cmd: &str, - rust_libs_path: &[String], test_path: &Path, problem: Option<&str>, rs_file: &Path, extra_clang_args: &[&str], - rustc_path: &str, - precompile: bool, - postcompile: bool, - rustc: bool, ) -> Result<(), std::io::Error> { announce_progress("Creating interestingness test"); + let precompile = !matches.is_present("no-precompile"); + let postcompile = !matches.is_present("no-postcompile"); + let rustc = !matches.is_present("no-rustc"); + + let rustc_path = matches.value_of("rustc").unwrap(); + + let rust_libs_path: Vec = matches + .get_many::("rlib") + .expect("No rlib path specified") + .into_iter() + .cloned() + .collect(); + // Ensure we refer to the input header by relative path // because creduce will invoke us in some other directory with // a copy thereof. From 9d021f66ff7928323191f41e6703f796338129c8 Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Fri, 21 Oct 2022 18:49:59 +0100 Subject: [PATCH 3/3] Typo. --- tools/reduce/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/reduce/src/main.rs b/tools/reduce/src/main.rs index 0340b0942..bf0846447 100644 --- a/tools/reduce/src/main.rs +++ b/tools/reduce/src/main.rs @@ -488,7 +488,7 @@ fn create_interestingness_test( let rustc_path = matches.value_of("rustc").unwrap(); let rust_libs_path: Vec = matches - .get_many::("rlib") + .get_many::("rlibs") .expect("No rlib path specified") .into_iter() .cloned()