cc-rs icon indicating copy to clipboard operation
cc-rs copied to clipboard

Automatically pass /guard:cf to MSVC compiler when Control Flow Guard is enabled in rustc flags

Open roblabla opened this issue 3 years ago • 1 comments

This is a bit of a longshot, and I'm not sure if it makes sense. When compiling with -C control-flow-guard=yes, it'd be really cool if C code build with cc-rs was automatically built with the equivalent flag (e.g. /guard:cf). This would allow projects to get an almost complete protection just by setting a variable in their .cargo/config file, instead of having to manually set the appropriate environment variable (which requires some kind of build orchestration tool on top of cargo to automatically set the variable, as I don't think cargo has any way to set environment variables on its own for the whole build?).

I'm not sure if this can be achieved in a practical way though. For one, I'm not entirely sure if build scripts have a reliable way of getting the target RUSTFLAGS, or otherwise knowing whether control-flow-guard is enabled.

roblabla avatar Nov 02 '20 14:11 roblabla

Sounds reasonable to me! I'd be happy to take a PR for this.

Perhaps the easiest way for us to detect this is if rustc set a #[cfg] when CFG is enabled, that way we could look for CARGO_CFG_* and see if it's enabled or not.

alexcrichton avatar Nov 02 '20 14:11 alexcrichton