Skip to content

Commit f3de077

Browse files
committed
transpile: Add disable_rustfmt command line option
1 parent 040d289 commit f3de077

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

c2rust-transpile/src/build_files/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ fn emit_build_rs(
226226
let output = reg.render("build.rs", &json).unwrap();
227227
let output_path = build_dir.join("build.rs");
228228
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
229-
rustfmt(&output_path, build_dir);
229+
230+
if !tcfg.disable_rustfmt {
231+
rustfmt(&output_path, build_dir);
232+
}
230233

231234
Some(path)
232235
}
@@ -256,7 +259,10 @@ fn emit_lib_rs(
256259
let output_path = build_dir.join(file_name);
257260
let output = reg.render("lib.rs", &json).unwrap();
258261
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
259-
rustfmt(&output_path, build_dir);
262+
263+
if !tcfg.disable_rustfmt {
264+
rustfmt(&output_path, build_dir);
265+
}
260266

261267
Some(path)
262268
}

c2rust-transpile/src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub struct TranspilerConfig {
9797
pub output_dir: Option<PathBuf>,
9898
pub translate_const_macros: TranslateMacros,
9999
pub translate_fn_macros: TranslateMacros,
100+
pub disable_rustfmt: bool,
100101
pub disable_refactoring: bool,
101102
pub preserve_unused_functions: bool,
102103
pub log_level: log::LevelFilter,
@@ -517,14 +518,18 @@ fn reorganize_definitions(
517518
}
518519

519520
invoke_refactor(build_dir)?;
520-
// fix the formatting of the output of `c2rust-refactor`
521-
let status = Command::new("cargo")
522-
.args(["fmt"])
523-
.current_dir(build_dir)
524-
.status()?;
525-
if !status.success() {
526-
warn!("cargo fmt failed, code may not be well-formatted");
521+
522+
if !tcfg.disable_rustfmt {
523+
// fix the formatting of the output of `c2rust-refactor`
524+
let status = Command::new("cargo")
525+
.args(["fmt"])
526+
.current_dir(build_dir)
527+
.status()?;
528+
if !status.success() {
529+
warn!("cargo fmt failed, code may not be well-formatted");
530+
}
527531
}
532+
528533
Ok(())
529534
}
530535

@@ -642,7 +647,9 @@ fn transpile_single(
642647
),
643648
};
644649

645-
rustfmt(&output_path, build_dir);
650+
if !tcfg.disable_rustfmt {
651+
rustfmt(&output_path, build_dir);
652+
}
646653

647654
Ok((output_path, pragmas, crates))
648655
}

c2rust-transpile/tests/snapshots.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn config() -> TranspilerConfig {
3939
output_dir: None,
4040
translate_const_macros: Default::default(),
4141
translate_fn_macros: Default::default(),
42+
disable_rustfmt: false,
4243
disable_refactoring: false,
4344
preserve_unused_functions: false,
4445
log_level: log::LevelFilter::Warn,

c2rust/src/bin/c2rust-transpile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ struct Args {
145145
#[clap(long)]
146146
emit_no_std: bool,
147147

148+
/// Disable running rustfmt after translation
149+
#[clap(long)]
150+
disable_rustfmt: bool,
151+
148152
/// Disable running refactoring tool after translation
149153
#[clap(long)]
150154
disable_refactoring: bool,
@@ -232,6 +236,7 @@ fn main() {
232236

233237
translate_const_macros: args.translate_const_macros.into(),
234238
translate_fn_macros: args.translate_fn_macros.into(),
239+
disable_rustfmt: args.disable_rustfmt,
235240
disable_refactoring: args.disable_refactoring,
236241
preserve_unused_functions: args.preserve_unused_functions,
237242

0 commit comments

Comments
 (0)