diff --git a/src/project.rs b/src/project.rs index c62bfe9..4f09cb4 100644 --- a/src/project.rs +++ b/src/project.rs @@ -15,6 +15,10 @@ use std::{ const DEFAULT_FUZZ_DIR: &str = "fuzz"; +/// The name of the environment variable that exposes the cargo fuzz manifest +/// path during builds. +const BUILD_ENV_MANIFEST_DIR: &str = "CARGO_FUZZ_MANIFEST"; + pub struct FuzzProject { /// The project with fuzz targets fuzz_dir: PathBuf, @@ -320,6 +324,29 @@ impl FuzzProject { } } + // Helper function for `exec_build()` used to expose cargo-fuzz information + // via environment variables. Such environment variables can be used by fuzz + // target dependencies' build scripts to adjust its build settings based on + // these variables' values. + // + // This is called directly before the `cargo build ...` command is executed. + // The command is passed in as a mutable reference such that the subprocess' + // environment (not *this* process' environment) will have the environment + // variables set. + fn exec_build_expose_env( + &self, + cmd: &mut Command, + _mode: options::BuildMode, + _build: &options::BuildOptions, + _fuzz_target: Option<&str>, + ) -> Result<()> { + // expose the path to the cargo-fuzz manifest to the subprocess + let manifest_path = self.manifest_path(); + cmd.env(BUILD_ENV_MANIFEST_DIR, manifest_path.as_os_str()); + + Ok(()) + } + pub fn exec_build( &self, mode: options::BuildMode, @@ -342,6 +369,11 @@ impl FuzzProject { cmd.arg("--target-dir").arg(target_dir); } + // expose build information via environment variables, before executing + // the build command + self.exec_build_expose_env(&mut cmd, mode, build, fuzz_target) + .expect("Failed to set cargo-fuzz build environment variables."); + let status = cmd .status() .with_context(|| format!("failed to execute: {:?}", cmd))?;