Skip to content

Commit

Permalink
simpler logic to transpile one file ending in ".json" or multiple sou…
Browse files Browse the repository at this point in the history
…rce files
  • Loading branch information
aneksteind committed Nov 13, 2023
1 parent 20d3cde commit 0ea8aea
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions c2rust/src/bin/c2rust-transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,9 @@ struct Args {
#[clap(long = "ddebug-labels")]
debug_labels: bool,

/// Input compile_commands.json file or path to source files
#[clap(long, parse(from_os_str), required = false, conflicts_with = "sources")]
compile_commands: Option<PathBuf>,

/// Source files to process, only valid if compile_commands.json path is not provided.
#[clap(
long = "sources",
parse(from_os_str),
multiple_values = true,
required = false
)]
sources: Vec<PathBuf>,
/// Path to compile_commands.json
#[clap(parse(from_os_str), multiple_values = true)]
compile_commands: Vec<PathBuf>,

/// How to handle violated invariants or invalid code
#[clap(long, value_enum, default_value_t = InvalidCodes::CompileError)]
Expand Down Expand Up @@ -138,7 +129,7 @@ struct Args {
reorganize_definitions: bool,

/// Extra arguments to pass to clang frontend during parsing the input C file
#[clap(multiple = true)]
#[clap(multiple = true, last(true))]
extra_clang_args: Vec<String>,

/// Enable the specified warning (all enables all warnings)
Expand Down Expand Up @@ -235,10 +226,14 @@ fn main() {
tcfg.emit_modules = true
};

let compile_commands = if let Some(compile_commands_path) = args.compile_commands {
compile_commands_path
let compile_commands = if args.compile_commands.len() == 1
&& args.compile_commands[0].extension() == Some(std::ffi::OsStr::new("json"))
{
// Only one file provided and it's a JSON file
args.compile_commands[0].clone()
} else {
c2rust_transpile::create_temp_compile_commands(&args.sources)
// Otherwise, handle as a list of source files
c2rust_transpile::create_temp_compile_commands(&args.compile_commands)
};

let extra_args = args
Expand All @@ -250,7 +245,7 @@ fn main() {
c2rust_transpile::transpile(tcfg, &compile_commands, &extra_args);

// Remove the temporary compile_commands.json if it was created
if !args.sources.is_empty() {
if args.compile_commands.len() > 0 {
std::fs::remove_file(&compile_commands)
.expect("Failed to remove temporary compile_commands.json");
}
Expand Down

0 comments on commit 0ea8aea

Please sign in to comment.