diff --git a/src/runner.rs b/src/runner.rs index ff170246..7ce47d48 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -8,6 +8,7 @@ use crate::cli::{BuildArgs, Cli, Commands}; use crate::{ir::BuildGraph, manifest, ninja_gen}; use anyhow::{Context, Result}; use serde_json; +use std::borrow::Cow; use std::fs; use std::io::{self, BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; @@ -114,18 +115,28 @@ fn handle_build(cli: &Cli, args: &BuildArgs) -> Result<()> { let ninja = generate_ninja(cli)?; let targets = BuildTargets::new(&args.targets); - // Normalise the build file path and keep the temporary file alive for the - // duration of the Ninja invocation. - let (build_path, _tmp): (PathBuf, Option) = if let Some(path) = &args.emit { + // Normalize the build file path and keep the temporary file alive for the + // duration of the Ninja invocation. Borrow the emitted path when provided + // to avoid unnecessary allocation. + let build_path: Cow; + let mut tmp_file: Option = None; + if let Some(path) = &args.emit { write_ninja_file(path, &ninja)?; - (path.clone(), None) + build_path = Cow::Borrowed(path.as_path()); } else { let tmp = create_temp_ninja_file(&ninja)?; - (tmp.path().to_path_buf(), Some(tmp)) - }; + tmp_file = Some(tmp); + build_path = Cow::Borrowed( + tmp_file + .as_ref() + .expect("temporary Ninja file should exist") + .path(), + ); + } let program = resolve_ninja_program(); - run_ninja(program.as_path(), cli, &build_path, &targets)?; + run_ninja(program.as_path(), cli, build_path.as_ref(), &targets)?; + drop(tmp_file); Ok(()) }