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

Let cc-rs choose the correct optimization flags for MSVC (/O2 instead of /Ox) #1596

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,9 @@ fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Pa
let _ = c.define("NDEBUG", None);
}

if compiler.is_like_msvc() {
if std::env::var("OPT_LEVEL").unwrap() == "0" {
let _ = c.flag("/Od"); // Disable optimization for debug builds.
// run-time checking: (s)tack frame, (u)ninitialized variables
let _ = c.flag("/RTCsu");
} else {
let _ = c.flag("/Ox"); // Enable full optimization.
Copy link
Owner

Choose a reason for hiding this comment

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

Do you agree that cc-rs does the right thing automatically, so that we can just remove both this line and the analogous "/Od" line above? I would prefer to do that, if cc-rs is automatically doing the right thing. (In general, I want to remove as much logic from build.rs that duplicates that cc-rs does as possible; see recent PRs that change build.rs to this effect.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't previously used cc-rs so I assumed manually specifying optimization flags was somehow necessary 😅
Well, cc-rs can determine the correct flag for each compiler: https://github.com/rust-lang/cc-rs/blob/2447a2ba5f455c00b1563193e125b60eebbd8ebe/src/lib.rs#L1748-L1749

I quickly checked the docs and there's this function that lets you manually specify the optimization level, but it also says that the OPT_LEVEL environment variable is used to determine the correct value otherwise (which is what is done manually here).

About "/Od", it is the default so no need to specify it: https://learn.microsoft.com/en-us/cpp/build/reference/od-disable-debug?view=msvc-170

It's not clear to me whether "/RTCsu" is on by default:
https://learn.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=msvc-170

The Visual Studio IDE does enable it for debug builds but that might not be true when using the compiler directly. We might need to read a little more, or just keep it to be sure.

tl;dr
Yes, I totally agree, no optimization flags should be manually set unless there's a good reason for doing it. The "/RTCsu" situation is not 100% clear to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took another look, but I couldn't conclusively find anything indicating that /RTCsu is enabled by default. Therefore I left that part but removed the rest. If you think you don't need /RTCsu, then the whole if statement can be removed, otherwise I would say the change is ready to be merged

Copy link
Owner

Choose a reason for hiding this comment

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

@andoalon I would be happy to have the /RTCsu removed.

}
if compiler.is_like_msvc() && std::env::var("OPT_LEVEL").unwrap() == "0" {
// run-time checking: (s)tack frame, (u)ninitialized variables
let _ = c.flag("/RTCsu");
}

// Allow cross-compiling without a target sysroot for these targets.
Expand Down
Loading