Skip to content

Commit cc8a44b

Browse files
committed
transpile: Run rustfmt on generated .rs files
1 parent 4e79794 commit cc8a44b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

c2rust-transpile/src/build_files/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ use std::collections::BTreeMap;
22
use std::fs::{self, File};
33
use std::io::Write;
44
use std::path::{Path, PathBuf};
5+
use std::process::Command;
56
use std::str::FromStr;
67

78
use handlebars::Handlebars;
9+
use log::warn;
810
use pathdiff::diff_paths;
911
use serde_derive::Serialize;
1012
use serde_json::json;
1113

1214
use super::compile_cmds::LinkCmd;
1315
use super::TranspilerConfig;
14-
use crate::get_module_name;
1516
use crate::CrateSet;
1617
use crate::ExternCrateDetails;
1718
use crate::PragmaSet;
19+
use crate::{get_module_name, rustfmt};
1820

1921
#[derive(Debug, Copy, Clone)]
2022
pub enum BuildDirectoryContents {
@@ -225,7 +227,10 @@ fn emit_build_rs(
225227
});
226228
let output = reg.render("build.rs", &json).unwrap();
227229
let output_path = build_dir.join("build.rs");
228-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
230+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
231+
rustfmt(&output_path, build_dir);
232+
233+
Some(path)
229234
}
230235

231236
/// Emit lib.rs (main.rs) for a library (binary). Returns `Some(path)`
@@ -252,8 +257,10 @@ fn emit_lib_rs(
252257

253258
let output_path = build_dir.join(file_name);
254259
let output = reg.render("lib.rs", &json).unwrap();
260+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
261+
rustfmt(&output_path, build_dir);
255262

256-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
263+
Some(path)
257264
}
258265

259266
/// If we translate variadic functions, the output will only compile

c2rust-transpile/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ fn transpile_single(
620620
),
621621
};
622622

623+
rustfmt(&output_path, build_dir);
624+
623625
Ok((output_path, pragmas, crates))
624626
}
625627

@@ -667,3 +669,15 @@ fn get_output_path(
667669
input_path
668670
}
669671
}
672+
673+
fn rustfmt(output_path: &Path, build_dir: &Path) {
674+
let status = Command::new("rustfmt")
675+
.args(["--edition", "2021"])
676+
.arg(output_path)
677+
.current_dir(build_dir)
678+
.status();
679+
680+
if !status.map_or(false, |status| status.success()) {
681+
warn!("rustfmt failed, code may not be well-formatted");
682+
}
683+
}

0 commit comments

Comments
 (0)