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

Conversation

cwshugg
Copy link
Contributor

@cwshugg cwshugg commented Oct 7, 2024

Hi - this PR implements a way to expose environment variables during the build command that's executed during exec_build(). This is done with the aim to give dependencies the ability to detect a cargo-fuzz build versus a non-cargo-fuzz build.

I implemented this feature while doing work to get cargo-fuzz working on Windows to build and fuzz a DLL. Certain dependencies may need to use different built settings when being built for production versus for fuzzing. Exposing these environment variables at build-time allow build.rs scripts to detect cargo-fuzz builds.

Currently just two environment variables are exposed, but more could of course be added:

  • CARGO_FUZZ - A flag that's set to 1 when cargo-fuzz is performing a build. Useful for detecting cargo-fuzz without wanting any extra information.
  • CARGO_FUZZ_MANIFEST - Exposes the path to the cargo-fuzz Cargo.toml manifest that was used when cargo fuzz [build/run/etc.] was invoked. Useful in the event a user needs to determine which fuzzing project ran the command.

Use-Case Example

As I mentioned above, a user may need to modify the build flags in a fuzzing dependency, but only when the dependency is being built for fuzzing. A brief example of how this would work:

// build.rs

use std::env;

fn main()
{
    // ...
    if let Ok(_) = env::var("CARGO_FUZZ")
    {
        // we know this dependency is being built for fuzzing!
        println!("cargo:rustc-link-arg=CUSTOM_FUZZING_LINKER_ARG");
        println!("cargo:rustc-flags=CUSTOM_FUZZING_RUSTFLAGS");
        println!("cargo:rustc-cfg=my_experimental_feature=ENABLE");
    }
    // ...
}

@cwshugg
Copy link
Contributor Author

cwshugg commented Oct 14, 2024

Hi @fitzgen - just bumping this to you. Would love to get your feedback on the change and potentially get it merged.

…llows for Windows DLLs to be built *without* the '/include:main' linker argument. This, plus a few other tricks, allows for Windows DLLs to be built for fuzzing.
@cwshugg cwshugg changed the title Expose Environment Variables during Fuzzing Build Expose env vars during build & add new build option to support Windows DLLs Oct 15, 2024
@fitzgen
Copy link
Member

fitzgen commented Oct 16, 2024

Thanks! Do you mind splitting these features out into different PRs so we can discuss them independently?

@cwshugg cwshugg changed the title Expose env vars during build & add new build option to support Windows DLLs Expose env vars during Build Oct 16, 2024
@cwshugg cwshugg changed the title Expose env vars during Build Expose Environment Variables during Build Oct 16, 2024
@cwshugg
Copy link
Contributor Author

cwshugg commented Oct 16, 2024

@fitzgen - Sure thing, thanks for taking a look. Here is the second PR.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A couple things to address before we merge this.

src/project.rs Outdated Show resolved Hide resolved
src/project.rs Outdated Show resolved Hide resolved
…t up subprocess environment. Also, changed function name to better indicate it's role as a helper function
@cwshugg
Copy link
Contributor Author

cwshugg commented Oct 23, 2024

@fitzgen - Thanks for reviewing these PRs! I've addressed your comments; let me know what you thin.

@fitzgen
Copy link
Member

fitzgen commented Oct 23, 2024

Thanks! Just fyi I’m on vacation and won’t be able to look at this again until the middle of next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants