Skip to content

Commit

Permalink
[fud2] add flag to print commands instead of running them (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
jku20 authored Oct 7, 2024
1 parent 7260196 commit eb398ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/running-calyx/fud2/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Here are some options you might need:
* `emit`: Just print the Ninja build file to stdout. The `gen` mode is therefore approximately `fud2 -m emit > .fud2/build.ninja`.
* `plan`: Print a brief description of the plan, i.e., the sequence of operations that the build would run.
* `dot`: Print a [GraphViz][] depiction of the plan. Try `fud2 -m dot | dot -Tpdf > graph.pdf` and take a look.
* `cmds`: Print the commands Ninja would run when executing the plan, but do not execute them.

There are also some subcommands for doing things other than building stuff:

Expand Down
6 changes: 5 additions & 1 deletion fud2/fud-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Mode {
ShowDot,
Generate,
Run,
Cmds,
}

impl FromStr for Mode {
Expand All @@ -25,6 +26,7 @@ impl FromStr for Mode {
"gen" => Ok(Mode::Generate),
"run" => Ok(Mode::Run),
"dot" => Ok(Mode::ShowDot),
"cmds" => Ok(Mode::Cmds),
_ => Err("unknown mode".to_string()),
}
}
Expand All @@ -38,6 +40,7 @@ impl Display for Mode {
Mode::Generate => write!(f, "gen"),
Mode::Run => write!(f, "run"),
Mode::ShowDot => write!(f, "dot"),
Mode::Cmds => write!(f, "cmds"),
}
}
}
Expand Down Expand Up @@ -380,7 +383,8 @@ pub fn cli(driver: &Driver, config: &figment::Figment) -> anyhow::Result<()> {
Mode::ShowDot => run.show_dot(),
Mode::EmitNinja => run.emit_to_stdout()?,
Mode::Generate => run.emit_to_dir(&workdir)?.keep(),
Mode::Run => run.emit_and_run(&workdir)?,
Mode::Run => run.emit_and_run(&workdir, false)?,
Mode::Cmds => run.emit_and_run(&workdir, true)?,
}

Ok(())
Expand Down
10 changes: 8 additions & 2 deletions fud2/fud-core/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ impl<'a> Run<'a> {
Ok(dir)
}

/// Emit `build.ninja` to a temporary directory and then actually execute ninja.
pub fn emit_and_run(&self, dir: &Utf8Path) -> EmitResult {
/// Emit `build.ninja` to a temporary directory and then actually execute Ninja.
///
/// If `print_cmds` is true, Ninja will print commands it is to run instead of executing them.
pub fn emit_and_run(&self, dir: &Utf8Path, print_cmds: bool) -> EmitResult {
// Emit the Ninja file.
let dir = self.emit_to_dir(dir)?;

Expand Down Expand Up @@ -369,6 +371,10 @@ impl<'a> Run<'a> {
cmd.arg("--verbose");
}

if print_cmds {
cmd.arg("-tcommands");
}

cmd.stdout(std::io::stderr()); // Send Ninja's stdout to our stderr.
let status = cmd.status().map_err(ninja_cmd_io_error)?;

Expand Down

0 comments on commit eb398ea

Please sign in to comment.