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

Figure out how to get static linkage for MSVCRT #660

Closed
brson opened this issue Aug 17, 2016 · 12 comments
Closed

Figure out how to get static linkage for MSVCRT #660

brson opened this issue Aug 17, 2016 · 12 comments

Comments

@brson
Copy link
Contributor

brson commented Aug 17, 2016

In order to get the MSI installers working we need to build with the MSVC toolchain and link statically to the CRT.

@alexcrichton without an upstream solution how can we solve this the hard way? I'm thinking something like:

  • Modify gcc-rs to interpret some environment variable to mean pass /MT, in a fork if necessary
  • Build our own i686-pc-windows-msvc toolchain while setting that environment variable
  • Use that toolchain to build rustup, while setting the environment variable

cc #635 (comment)
cc rust-lang/rfcs#1684
cc @Boddlnagg @retep998

@retep998
Copy link
Member

retep998 commented Aug 18, 2016

The libc crate forces msvcrt.lib to be linked. Either it would have to be modified both for crates.io and the one used in std, or you'd have to set the linker to a wrapper program that strips any mentions of msvcrt.lib before forwarding it on to the actual linker.

Aside from libc, there's nothing else in rust itself that would have to be fixed to solve it the hard way.

In addition to gcc-rs, there is also cmake-rs and anything else that has a build script which compiles C/C++ code in some way.

@brson
Copy link
Contributor Author

brson commented Aug 18, 2016

Maybe the linker wrapper is the easiest solution to all these. We can strip msvcrt.lib and convert /MD to /MT.

So we don't need to build a custom toolchain at all, right? Just fix the linker invocation? Is there just one invocation of link.exe globally here, for the final exe/dll? This seems almost easy...

@retep998
Copy link
Member

/MD and /MT are flags passed to cl.exe for C/C++ code, not link.exe.

@brson
Copy link
Contributor Author

brson commented Aug 18, 2016

So that sounds like we do need a custom toolchain.

  • Create a new static-link.exe project that strips the msvcrt.lib
  • Modify gcc-rs to interpret some environment variable to mean pass /MT, in a fork if necessary
  • Build our own i686-pc-windows-msvc toolchain while setting that environment variable
  • Use that toolchain to build rustup, while setting the environment variable and the fake linker

@retep998
Copy link
Member

@brson If you do use a linker wrapper, then there is no need to rebuild the Rust toolchain. There is no C/C++ code in libstd and friends that could be affected. There is in rustc itself but that has no effect on code built by rustc.

@Boddlnagg
Copy link
Contributor

To pass /MT in gcc-rs I just used a local fork with a cargo override. A [replace] in Cargo.toml should be sufficient, right (in addition to the linker wrapper)?

@alexcrichton
Copy link
Member

@brson I think custom linker shenanigans would work here, but having never done it I'm not 100% sure it'd actually work.

@brson brson closed this as completed Aug 18, 2016
@brson brson reopened this Aug 18, 2016
@brson
Copy link
Contributor Author

brson commented Aug 18, 2016

To pass /MT in gcc-rs I just used a local fork with a cargo override. A [replace] in Cargo.toml should be sufficient, right (in addition to the linker wrapper)?

I think so.

@retep998
Copy link
Member

If this does turn out to work, then I can totally use this as an example to push along my RFC.

@retep998
Copy link
Member

This can now be implemented fairly easily considering Rust now has a built in way to link to the static CRT. rust-lang/rust#37406

@tesuji
Copy link
Contributor

tesuji commented Jan 25, 2020

@kinnison I think this issue is resolved.

@kinnison
Copy link
Contributor

Yep, thanks @lzutao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants