Skip to content

Commit

Permalink
Merge pull request #1172 from google/reduce-with-rustc
Browse files Browse the repository at this point in the history
Reduce includes a call to `rustc`
  • Loading branch information
adetaylor authored Oct 23, 2022
2 parents ee2bdf8 + 9d021f6 commit 6cca9b2
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions tools/reduce/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -277,33 +307,26 @@ 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
);
}

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,
&interestingness_test,
None,
&rs_path,
&extra_clang_args,
!matches.is_present("no-precompile"),
!matches.is_present("no-postcompile"),
)?;
let demo_dir_concat_path = demo_interestingness_test_dir.join("concat.h");
std::fs::copy(&concat_path, demo_dir_concat_path).unwrap();
Expand All @@ -312,13 +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,
&interestingness_test,
Some(matches.value_of("problem").unwrap()),
&rs_path,
&extra_clang_args,
!matches.is_present("no-precompile"),
!matches.is_present("no-postcompile"),
)?;
run_creduce(
matches.value_of("creduce").unwrap(),
Expand Down Expand Up @@ -451,15 +473,27 @@ fn format_gen_cmd<'a>(
}

fn create_interestingness_test(
matches: &ArgMatches,
gen_cmd: &str,
test_path: &Path,
problem: Option<&str>,
rs_file: &Path,
extra_clang_args: &[&str],
precompile: bool,
postcompile: 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<String> = matches
.get_many::<String>("rlibs")
.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.
Expand All @@ -469,6 +503,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
Expand All @@ -482,16 +522,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);
{
Expand Down

0 comments on commit 6cca9b2

Please sign in to comment.