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

Fix : Properly detect C runtime through target_feature cfg #725

Closed
wants to merge 1 commit into from

Conversation

elast0ny
Copy link

@elast0ny elast0ny commented Oct 3, 2022

Currently, the cc crate tries to auto-detect the proper C runtime based on the contents of the CARGO_CFG_TARGET_FEATURE. Unfortunately, this env variable does not contain an accurate list of target features and may not contain the crt-static feature.

For example, if your project depends on cc and is configured to set the crt-static feature through a .cargo/config.toml file, cc will compile using the dynamic C runtime as it wont be able to see the crt-static feature through CARGO_CFG_TARGET_FEATURE.

See rust-lang/cargo#6858

@elast0ny elast0ny changed the title Detect C runtime through target feature cfg Fix : Properly detect C runtime through target_feature cfg Oct 3, 2022
@elast0ny
Copy link
Author

elast0ny commented Oct 3, 2022

This should be a bit more flexible than #723

.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.

@thomcc thomcc added the O-windows Windows targets and toolchains label Oct 26, 2022
@ChrisDenton
Copy link
Member

Thanks for the PR but I don't think this is the correct solution, as outlined by dot-asm, so I'm closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Windows targets and toolchains
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants