Add Windows/mingw32 support, native and cross#84
Merged
Merged
Conversation
This one turned out to be a lot easier than anticipated! As it happens, the existing code is already perfectly well capable of compiling things on windows and all that's needed is a bit of patching and plumbing on the linking side and voila, it just works. The setup, as tested by CI, uses mingw (plausibly it could be extended to msvc as well, but why?) to provide the compiler runtime and the Windows support libraries needed to link, but otherwise just works out of the box, and the binaries produced are fully stand-alone (apart from the usual Windows DLL dependencies). * Improve target triple handling - the "just works" thing might actually carry over to all platforms llvm supports, meaning that flags like `--cpu:arm64` will do the right thing - tests needed to verify this * Add support for cross-compiling to windows using `llvm-mingw` as the recommended windows environment * In theory, it should be easy to support an msvc build environment as well using something like [xwin](https://github.com/Jake-Shadle/xwin) to fetch the windows SDK - tests and documentation needed The way compilation is set up, `nlvm` will compile the Nim code as usual and then invoke the built-in `lld` linker to link an executable. To link, `lld` needs either `.dll` or `.lib` files for each shared library used by the application - the C and windows system libraries in particular (`msvcrt`, `kernel32` etc). Linking minics what `clang` would do. We also need a compiler runtime library - this is taken from the existing C compiler setup, meaning `libgcc` for `gcc` and `compiler-rt`/`libunwind` for `clang`. When cross-compiling, the same thing applies but we'll now also use `clang` to find the support libraries for the target environment, so clang needs to be compiled in such a way to find the installed mingw32-w64 environment - the easiest thing to do is to just download llvm-mingw and put it first in the PATH.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 one turned out to be a lot easier than anticipated!
As it happens, the existing code is already perfectly well capable of compiling things on windows and all that's needed is a bit of patching and plumbing on the linking side and voila, it just works.
The setup, as tested by CI, uses mingw (plausibly it could be extended to msvc as well, but why?) to provide the compiler runtime and the Windows support libraries needed to link, but otherwise just works out of the box, and the binaries produced are fully stand-alone (apart from the usual Windows DLL dependencies).
--cpu:arm64will do the right thing - tests needed to verify thisllvm-mingwas the recommended windows environmentThe way compilation is set up,
nlvmwill compile the Nim code as usual and then invoke the built-inlldlinker to link an executable.To link,
lldneeds either.dllor.libfiles for each shared library used by the application - the C and windows system libraries in particular (msvcrt,kernel32etc). Linking minics whatclangwould do.We also need a compiler runtime library - this is taken from the existing C compiler setup, meaning
libgccforgccandcompiler-rt/libunwindforclang.When cross-compiling, the same thing applies but we'll now also use
clangto find the support libraries for the target environment, so clang needs to be compiled in such a way to find the installed mingw32-w64 environment - the easiest thing to do is to just download llvm-mingw and put it first in the PATH.