Skip to content

Commit

Permalink
support c2rust <path/to/*.c>
Browse files Browse the repository at this point in the history
  • Loading branch information
aneksteind committed Nov 1, 2023
1 parent c603b25 commit ce017a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
45 changes: 42 additions & 3 deletions c2rust-transpile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod translator;
pub mod with_stmts;

use std::collections::HashSet;
use std::fs::{self, File};
use std::fs::{self, read_dir, File};
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -227,9 +227,44 @@ fn get_module_name(

/// Main entry point to transpiler. Called from CLI tools with the result of
/// clap::App::get_matches().
pub fn transpile(tcfg: TranspilerConfig, cc_db: &Path, extra_clang_args: &[&str]) {
pub fn transpile(tcfg: TranspilerConfig, source_or_cc_db: &Path, extra_clang_args: &[&str]) {
diagnostics::init(tcfg.enabled_warnings.clone(), tcfg.log_level);

let temp_json_path = "compile_commands.json";
let build_dir = get_build_dir(&tcfg, source_or_cc_db);

let cc_db = if source_or_cc_db.to_string_lossy().ends_with(".c") {
let mut temp_json: Vec<String> = Vec::new();
for entry in read_dir(build_dir.clone()).expect("Failed to read directory") {
if let Ok(entry) = entry {
let path = entry.path();
if path.extension() == Some(std::ffi::OsStr::new("c")) {
let command = format!(
r#"{{
"directory": "{}",
"command": "clang {}",
"file": "{}"
}}"#,
build_dir.to_str().unwrap(),
path.to_str().unwrap(),
path.to_str().unwrap()
);

temp_json.push(command);
}
}
}

let temp_json_content = format!("[{}]", temp_json.join(","));
fs::File::create(temp_json_path)
.and_then(|mut f| f.write_all(temp_json_content.as_bytes()))
.expect("Failed to create temp json");

Path::new(temp_json_path)
} else {
source_or_cc_db
};

let lcmds = get_compile_commands(cc_db, &tcfg.filter).unwrap_or_else(|_| {
panic!(
"Could not parse compile commands from {}",
Expand All @@ -246,7 +281,7 @@ pub fn transpile(tcfg: TranspilerConfig, cc_db: &Path, extra_clang_args: &[&str]
let mut workspace_members = vec![];
let mut num_transpiled_files = 0;
let mut transpiled_modules = Vec::new();
let build_dir = get_build_dir(&tcfg, cc_db);

for lcmd in &lcmds {
let cmds = &lcmd.cmd_inputs;
let lcmd_name = lcmd
Expand Down Expand Up @@ -367,6 +402,10 @@ pub fn transpile(tcfg: TranspilerConfig, cc_db: &Path, extra_clang_args: &[&str]
}

tcfg.check_if_all_binaries_used(&transpiled_modules);

if source_or_cc_db.to_string_lossy().ends_with(".c") {
let _ = fs::remove_file(temp_json_path);
}
}

/// Ensure that clang can locate the system headers on macOS 10.14+.
Expand Down
10 changes: 5 additions & 5 deletions c2rust/src/bin/c2rust-transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ fn main() {
tcfg.emit_modules = true
};

let cc_json_path = Path::new(&args.compile_commands);
let cc_json_path = cc_json_path.canonicalize().unwrap_or_else(|_| {
let source_or_cc_db = Path::new(&args.compile_commands);
let source_or_cc_db = source_or_cc_db.canonicalize().unwrap_or_else(|_| {
panic!(
"Could not find compile_commands.json file at path: {}",
cc_json_path.display()
"Could not find compile_commands.json or source files at path: {}",
source_or_cc_db.display()
)
});

Expand All @@ -240,5 +240,5 @@ fn main() {
.map(AsRef::as_ref)
.collect::<Vec<_>>();

c2rust_transpile::transpile(tcfg, &cc_json_path, &extra_args);
c2rust_transpile::transpile(tcfg, &source_or_cc_db, &extra_args);
}

0 comments on commit ce017a6

Please sign in to comment.