Pick MSVC -arch: based on CARGO_CFG_TARGET_FEATURE for x86 / x86_64 targets #713
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is my first contribution to cc-rs, and I'm more than open to any critique or questions.
As it is now,
-arch:IA32
is passed for 32-bit builds if the target isi586
orclang-cl
is being used. This explicit override often breaks assumptions and always downgrades 32-bitclang-cl
builds from the default-arch:SSE2
to-arch:IA32
, potentially impacting performance.This PR attempts to only downgrade when there's reason to, and otherwise attempt to pick a target
-arch
that matches the features set inCARGO_CFG_TARGET_FEATURE
. Picking an-arch
that followsCARGO_CFG_TARGET_FEATURE
also means crates (both x86 and x86_64) that opt into support forAVX
orAVX2
throughtarget-feature
will now also have any dependencies built withcc
benefit from that.I'm using
clang-cl
to build a few 32-bit projects, so I'm most likely running into issues with this far more often than most people are. While usually it's at most a bit annoying thatcc
builds aren't respectingtarget-feature
, it actually breaks my builds.Today I ran into this same issue again when trying to integrate
mimalloc_rust
, which requiresSSE2
for atomics.Since this is the third crate I've attempted to use that has issues due to the same forced override, I decided to try fix the problem in
cc-rs
instead of forking yet another crate just to add.flag_if_supported("-arch:SSE2")
to be able to build it successfully.I opted to not add support for AVX512 for now since MSVC bundles all AVX512 extensions under the same
-arch:AVX512
, while Rust treats each extension as a separate feature. Adding support for it is possible, but I don't need it myself and I don't know how to do it cleanly, so I'll leave that to someone else. For now it should fallback toAVX2
, which is still an upgrade over the previousIA32
(clang-cl
) /SSE2
(cl.exe
).It's also possible to do the same thing for ARM builds, but since I have no experience with ARM I'll leave that task too for someone else.