Skip to content

Fix : Properly detect C runtime through target_feature cfg #725

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,10 +1491,7 @@ impl Build {
Some(true) => "-MT",
Some(false) => "-MD",
None => {
let features = self
.getenv("CARGO_CFG_TARGET_FEATURE")
.unwrap_or(String::new());
if features.contains("crt-static") {
if cfg!(target_feature = "crt-static") {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a reason for why it's inappropriate to use cfg! here. Keyword is that it applies to the build script itself, not the compilation target.

Copy link
Author

@elast0ny elast0ny Oct 3, 2022

Choose a reason for hiding this comment

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

My tests with these changes were able to properly infer the C runtime based on the settings in my config.toml from the parent crate.

I thought the cc build script gets compiled during the build invocation of the parent crate which means the cgf! directive has access to the parent crate target_feature no ?

Copy link
Contributor

Choose a reason for hiding this comment

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

The build script is compiled for the platform that executes the Rust compiler, but it doesn't necessarily mean that the compiler was asked to generate code for the same platform. Well, one can argue that *-windows-msvc binary code is generated only by *-windows-msvc toolchain, but formally speaking it's not safe assumption to make. And cc-rs doesn't make it.

"-MT"
} else {
"-MD"
Expand Down