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

Use msvc on windows, not mingw #1768

graydon opened this issue Feb 6, 2012 · 10 comments

Use msvc on windows, not mingw #1768

graydon opened this issue Feb 6, 2012 · 10 comments
A-driver Area: rustc_driver that ties everything together into the `rustc` compiler A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows


Copy link

graydon commented Feb 6, 2012

We're using a pretty rare toolchain on windows. Msvc is the "standard" one, and it'd be good if we could ship a rustc that is comfortable with the native windows toolchain rather than mingw.

Copy link

This issue is very relevant if rust eventually allows linking with C code (i.e. issue #1850). Although in theory the MinGW C toolchain is ABI compatible with MSVC, in practice this isn't the case and will cause things to fail in subtle and mysterious ways, especially as code size gets large. Sorry for the long list of examples that follows, but I wanted to illustrate the issues I've had with MinGW/MSVC interop.

  • MinGW and MSVC actually use different symbol decorations for similar usages of stdcall/dllimport, requiring modification of headers (or passing extra flags to the compiler) to even begin to link.
  • Linking a static .lib built with MSVC to object files generated by MinGW with ld.exe will rarely work, even according to their own documentation. My personal experience is that usually ld.exe will segfault when attempting anything more than a trivial example. Having rustc use link.exe (MSVC) on Windows would be a huge step towards interop with existing VC++ code.
  • The situation with DLLs is better, but still a mess. Import libraries rarely "just work", and you'll end up having to extract/write .def files or link to the .dll directly; after spending an hour messing with things like reimp.exe and the --out-implib flag, you'll start to wonder why you are using MinGW in the first place, and if you could avoid it somehow.
  • Even if you manage to get the DLL to link, MinGW dlls link against msvcrt.dll and VC++10 links against msvcr100.dll, causing subtle bugs when data is passed cross DLL boundary and used with different runtimes. This limitation plagues a lot of Linux software compiled on Windows via MinGW. Examples include GTK+ which requires one to use the outdated VC++6 compiler or MinGW to compile code linking against it, and Qt has separate distributions for MinGW and the various MSVC versions.

Certainly this last issue applies to any code compiled with incompatible version of a compiler (i.e. VS2005/VS2010), however the issue is that mingw links with such an outdated version of MSVC that it is not useful for new code being compiled today. I've seen activity in the MinGW mailing list about working with msvcr100.dll, so this may disappear soon. However, it still stands that using static/shared libs built with the MS toolchain with MinGWs toolchain is somewhere between a lot of hassle and impossible. This hassle would be saved if compiling .c code (e.g. #1850) was done with MSVC, and then linked with the rustc objects generated by llvm.

Note that I have very little experience with llvm, so i'm not sure how compatible LLVM's ABI (used for rust objects) is with MSVC. I know that it is possible to be compatible though, as Intel's compiler (icc) does just fine importing .lib/.def/.dll's built with MSVC. If llvm is anywhere near as compatible as icc with with cl.exe/link.exe then rustc would inherit those advantages.

Copy link

I would like to comment on the random assumption that mingw is "a pretty rare toolchain on windows".

Facts : maintains a mingw toolchain for scilab
mingw-w64 makes adjustments and improvements
google go can be compiled with mingw
ReactOS uses a mingw environment
numerous FOSS projects embrace mingw

prove that the comment is invalid. For my work mingw is vital while rust is not. However mingw gives me an opportunity to explore rust and consider it for future products. The fact that Mozilla embraces Visual Studio does not prove that VS is necessarily the only option. Chromium is a counterexample. The lack of interoperability of mingw with VS is also something that I do not personally consider a big issue.

Mingw is interoperable with many scripting languages, many full fledged languages like FreePascal/Mono/D and Go. In this respect I would like to ask you to maintain mingw along with VS or keep mingw and add clang+mingw libs.

Copy link

Blei commented Jun 12, 2013

Problem: jemalloc requires mingw.

Copy link

jdm commented Jun 12, 2013

Firefox uses jemalloc on Windows with MSVC:

Copy link

jemalloc definitely supports MSVC, but I don't think it supports acting as a drop-in malloc/free replacement for the entire process. I think you have to compile it with the je_ prefix and switch the calls to that, so we would lose LLVM's hardcoded optimizations for malloc/free but not much else.

Copy link

Aatch commented Jun 12, 2013

@thestinger it's actually not as hard coded as it seems. There's just a list of library calls and some information about them as a function pass. We should be able to extend it to provide the appropriate data to let LLVM do the optimizations for je_ prefixed versions same as the regular calls.

Copy link

@Aatch: there's more than just that - there are specific optimizations related to free and malloc

Copy link

visiting for bug triage, email from 2013-08-05

Copy link

klutzy commented Sep 3, 2013

We use some c99 functions like fdim but it is not in msvcrt runtime. #8663 indicates that mingw/mingw-w64 implement them manually, so for msvc we have to import some portion from them.

Copy link

#9367 is a better solution, since we're strongly tied to LLVM and we can distribute the toolchain with our snapshots + installer. They are striving to be fully compatible with the existing MSVC++ toolchain.

@graydon graydon removed their assignment Jun 16, 2014
bors added a commit that referenced this issue May 20, 2015
Special thanks to @retep998 for the [excellent writeup](rust-lang/rfcs#1061) of tasks to be done and @ricky26 for initially blazing the trail here!

# MSVC Support

This goal of this series of commits is to add MSVC support to the Rust compiler
and build system, allowing it more easily interoperate with Visual Studio
installations and native libraries compiled outside of MinGW.

The tl;dr; of this change is that there is a new target of the compiler,
`x86_64-pc-windows-msvc`, which will not interact with the MinGW toolchain at
all and will instead use `link.exe` to assemble output artifacts.

## Why try to use MSVC?

With today's Rust distribution, when you install a compiler on Windows you also
install `gcc.exe` and a number of supporting libraries by default (this can be
opted out of). This allows installations to remain independent of MinGW
installations, but it still generally requires native code to be linked with
MinGW instead of MSVC. Some more background can also be found in #1768 about the
incompatibilities between MinGW and MSVC.

Overall the current installation strategy is quite nice so long as you don't
interact with native code, but once you do the usage of a MinGW-based `gcc.exe`
starts to get quite painful.

Relying on a nonstandard Windows toolchain has also been a long-standing "code
smell" of Rust and has been slated for remedy for quite some time now. Using a
standard toolchain is a great motivational factor for improving the
interoperability of Rust code with the native system.

## What does it mean to use MSVC?

"Using MSVC" can be a bit of a nebulous concept, but this PR defines it as:

* The build system for Rust will build as much code as possible with the MSVC
  compiler, `cl.exe`.
* The build system will use native MSVC tools for managing archives.
* The compiler will link all output with `link.exe` instead of `gcc.exe`.

None of these are currently implemented today, but all are required for the
compiler to fluently interoperate with MSVC.

## How does this all work?

At the highest level, this PR adds a new target triple to the Rust compiler:


All logic for using MSVC or not is scoped within this triple and code can
conditionally build for MSVC or MinGW via:

    #[cfg(target_env = "msvc")]

It is expected that auto builders will be set up for MSVC-based compiles in
addition to the existing MinGW-based compiles, and we will likely soon start
shipping MSVC nightlies where `x86_64-pc-windows-msvc` is the host target triple
of the compiler.

# Summary of changes

Here I'll explain at a high level what many of the changes made were targeted
at, but many more details can be found in the commits themselves. Many thanks to
@retep998 for the excellent writeup in rust-lang/rfcs#1061 and @RicK26 for a lot
of the initial proof-of-concept work!

## Build system changes

As is probably expected, a large chunk of this PR is changes to Rust's build
system to build with MSVC. At a high level **it is an explicit non goal** to
enable building outside of a MinGW shell, instead all Makefile infrastructure we
have today is retrofitted with support to use MSVC instead of the standard MSVC
toolchain. Some of the high-level changes are:

* The configure script now detects when MSVC is being targeted and adds a number
  of additional requirements about the build environment:
  * The `--msvc-root` option must be specified or `cl.exe` must be in PATH to
    discover where MSVC is installed. The compiler in use is also required to
    target x86_64.
  * Once the MSVC root is known, the INCLUDE/LIB environment variables are
    scraped so they can be reexported by the build system.
  * CMake is required to build LLVM with MSVC (and LLVM is also configured with
    CMake instead of the normal configure script).
  * jemalloc is currently unconditionally disabled for MSVC targets as jemalloc
    isn't a hard requirement and I don't know how to build it with MSVC.
* Invocations of a C and/or C++ compiler are now abstracted behind macros to
  appropriately call the underlying compiler with the correct format of
  arguments, for example there is now a macro for "assemble an archive from
  objects" instead of hard-coded invocations of `$(AR) crus liboutput.a ...`
* The output filenames for standard libraries such as morestack/compiler-rt are
  now "more correct" on windows as they are shipped as `foo.lib` instead of
* Rust targets can now depend on native tools provided by LLVM, and as you'll
  see in the commits the entire MSVC target depends on `llvm-ar.exe`.
* Support for custom arbitrary makefile dependencies of Rust targets has been
  added. The MSVC target for `rustc_llvm` currently requires a custom `.DEF`
  file to be passed to the linker to get further linkages to complete.

## Compiler changes

The modifications made to the compiler have so far largely been minor tweaks
here and there, mostly just adding a layer of abstraction over whether MSVC or a
GNU-like linker is being used. At a high-level these changes are:

* The section name for metadata storage in dynamic libraries is called `.rustc`
  for MSVC-based platorms as section names cannot contain more than 8
* The implementation of `rustc_back::Archive` was refactored, but the
  functionality has remained the same.
* Targets can now specify the default `ar` utility to use, and for MSVC this
  defaults to `llvm-ar.exe`
* The building of the linker command in `rustc_trans:🔙:link` has been
  abstracted behind a trait for the same code path to be used between GNU and
  MSVC linkers.

## Standard library changes

Only a few small changes were required to the stadnard library itself, and only
for minor differences between the C runtime of msvcrt.dll and MinGW's libc.a

* Some function names for floating point functions have leading underscores, and
  some are not present at all.
* Linkage to the `advapi32` library for crypto-related functions is now
* Some small bits of C code here and there were fixed for compatibility with
  MSVC's cl.exe compiler.

# Future Work

This commit is not yet a 100% complete port to using MSVC as there are still
some key components missing as well as some unimplemented optimizations. This PR
is already getting large enough that I wanted to draw the line here, but here's
a list of what is not implemented in this PR, on purpose:

## Unwinding

The revision of our LLVM submodule [does not seem to implement][llvm] does not
support lowering SEH exception handling on the Windows MSVC targets, so
unwinding support is not currently implemented for the standard library (it's
lowered to an abort).


It looks like, however, that upstream LLVM has quite a bit more support for SEH
unwinding and landing pads than the current revision we have, so adding support
will likely just involve updating LLVM and then adding some shims of our own
here and there.

## dllimport and dllexport

An interesting part of Windows which MSVC forces our hand on (and apparently
MinGW didn't) is the usage of `dllimport` and `dllexport` attributes in LLVM IR
as well as native dependencies (in C these correspond to

Whenever a dynamic library is built by MSVC it must have its public interface
specified by functions tagged with `dllexport` or otherwise they're not
available to be linked against. This poses a few problems for the compiler, some
of which are somewhat fundamental, but this commit alters the compiler to attach
the `dllexport` attribute to all LLVM functions that are reachable (e.g. they're
already tagged with external linkage). This is suboptimal for a few reasons:

* If an object file will never be included in a dynamic library, there's no need
  to attach the dllexport attribute. Most object files in Rust are not destined
  to become part of a dll as binaries are statically linked by default.
* If the compiler is emitting both an rlib and a dylib, the same source object
  file is currently used but with MSVC this may be less feasible. The compiler
  may be able to get around this, but it may involve some invasive changes to
  deal with this.

The flipside of this situation is that whenever you link to a dll and you import
a function from it, the import should be tagged with `dllimport`. At this time,
however, the compiler does not emit `dllimport` for any declarations other than
constants (where it is required), which is again suboptimal for even more

* Calling a function imported from another dll without using `dllimport` causes
  the linker/compiler to have extra overhead (one `jmp` instruction on x86) when
  calling the function.
* The same object file may be used in different circumstances, so a function may
  be imported from a dll if the object is linked into a dll, but it may be
  just linked against if linked into an rlib.
* The compiler has no knowledge about whether native functions should be tagged
  dllimport or not.

For now the compiler takes the perf hit (I do not have any numbers to this
effect) by marking very little as `dllimport` and praying the linker will take
care of everything. Fixing this problem will likely require adding a few
attributes to Rust itself (feature gated at the start) and then strongly
recommending static linkage on Windows! This may also involve shipping a
statically linked compiler on Windows instead of a dynamically linked compiler,
but these sorts of changes are pretty invasive and aren't part of this PR.

## CI integration

Thankfully we don't need to set up a new snapshot bot for the changes made here as our snapshots are freestanding already, we should be able to use the same snapshot to bootstrap both MinGW and MSVC compilers (once a new snapshot is made from these changes).

I plan on setting up a new suite of auto bots which are testing MSVC configurations for now as well, for now they'll just be bootstrapping and not running tests, but once unwinding is implemented they'll start running all tests as well and we'll eventually start gating on them as well.


I'd love as many eyes on this as we've got as this was one of my first interactions with MSVC and Visual Studio, so there may be glaring holes that I'm missing here and there!

cc @retep998, @ricky26, @vadimcn, @klutzy 

r? @brson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
A-driver Area: rustc_driver that ties everything together into the `rustc` compiler A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows
None yet

No branches or pull requests

9 participants