rust-lang / rust-lang/cargo

Request: cargo needs to help coordinate C compiler usage between `cc` and `rustc` on Windows-MSVC

Open
#7,148 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-build-scripts A-linkage C-feature-request S-needs-design
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

Describe the problem you are trying to solve

Currently on Window the Visual Studio linker is used. However, there's no absolute source of truth for what install of Visual Studio is being used. It's possible for cc to build a static C library with one toolchain, then rustc builds some Rust code and links with the static C library using another toolchain's linker. MSVC allows for static library ABI breakage between releases the same as Rust does, so if you mix two different releases it's potentially either a very weird linker error or silent runtime UB.

Plausible example situations that could trigger this problem:

  • The latest cc knows how to detect VS 2019 but is being built with an old rustc that doesn't know about VS 2019. The user has both versions of VS installed, and so cc picks 2019 while rustc picks 2017.
  • The user has several versions of Visual Studio installed but only one active in their path at once. They build their project once, then change their path to test the project with an alternate toolchain. They touch lib.rs which triggers a rebuild in the Rust crate, but the C dependency is left sitting there even though it's from the old toolchain.

Describe the solution you'd like

It seems like cargo should take over deciding what version of MSVC that both cc and rustc must both use during a particular build cycle by setting an environment variable or passing a command line flag or similar.

Also, I suppose the toolchain used to build a foo-sys dependency could be recorded as part of the crate's build data, and if the MSVC toolchain detected changes then that triggers the dependency to be rebuilt or throw an error or something like that.

Notes

@retep998 was the one who first brought this to my attention and is of course the full expert on this arcane Windows matter, I'm just reporting the soundness hole because I have a crate affected by this (an SDL2 wrapper).

It's possible this general issue can happen with static C libs on other systems I guess. You'd have to ask some C experts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining how Cargo coordinates cc and rustc for Windows-MSVC builds; the issue names no files or tests. Done would require a single authoritative MSVC toolchain choice for both compilers and a defined response when the toolchain changes for an existing C dependency.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.