Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Environment Variables during Build #387

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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))?;
Expand Down