This is a reproducible Dockerfile for cross compiling with MSVC on Linux, usable as base image for CI style setups.
This downloads and unpacks the necessary Visual Studio components using the same installer manifests as Visual Studio 2017/2019's installer uses. Downloading and installing it requires accepting the license, available at https://go.microsoft.com/fwlink/?LinkId=2086102 for the currently latest version.
As Visual Studio isn't redistributable, the resulting docker image isn't either.
Build the docker image like this:
docker build .
After building the docker image, there are 4 directories with tools,
in /opt/msvc/bin/<arch>
, for all architectures out of x86
,
x64
, arm
and arm64
, that can be added to the PATH before building
with it.
The installer scripts also work fine without docker; just run the following two commands:
./vsdownload.py --dest <source dir>
./install.py <source dir> <optional destination dir> -l <optional llvm/clang version suffix>
The unpacking requires recent versions of msitools (0.98) and libgcab (1.2); sufficiently new versions are available in e.g. Ubuntu 19.04.
With this toolchain, cross-compiling rust code has never been easier. Here's what you need to do to get setup:
- Install this toolchain, e.g.
./vsdownload.py --dest /opt/msvc && ./install.py /opt/msvc
. You can install it anywhere. - Install
clang
,llvm-lib
,lld
andllvm-rc
:clang
will be used to compile any C dependencies you may have with thecc
crate. Windows officially supports buildingmsvc
binaries in clang since 2019, so it should handle sdk headers and libraries just fine.lld-link
will be used to link rust executablesllvm-lib
will be used to statically link code when using thecc
crate. It generates.lib
files compatible with the windows linker.llvm-rc
will be used to compile resource files (used to add manifests and icons to an executable) with theembed-resource
crate. This will also generate.lib
files.
- In your shell, source the following script:
source /opt/msvc/bin/rustenv.sh
Congratulations, you're now all set!