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

Support building from a MinGW-w64-based compiler #15077

Merged

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Oct 11, 2024

This is a continuation of #15070 that allows a compiler built with MinGW-w64 to itself build programs correctly. Resolves part of #6170.

  • Because linker flags for GCC may now be executed on a Windows environment, we use the correct form of argument quoting. We also drop -rdynamic since that only makes sense for ELF executables.
  • Targetting x86_64-windows-gnu, including normal compilations from such a Crystal compiler, will not copy dependent DLLs to the output directory. Crystal itself and programs built under MSYS2 will just work as long as the proper environment is used. You are on your own here, although ldd exists on MSYS2 so that you don't need the MSVC build tools for this.
  • The correct GCC compiler flag to select wmain over main as the C entry point is -municode. (The system entry point is presumably _start now.)
  • legacy_stdio_definitions.obj doesn't exist on MinGW-w64, so we disable it outside MSVC.
  • For build command lines that are too long on Windows, we use GCC's response file support.

To build a MinGW-w64 compiler:

@REM on the MSVC developer prompt
make -fMakefile.win crystal
bin\crystal build --cross-compile --target=x86_64-windows-gnu src\compiler\crystal.cr -Dwithout_interpreter
# on MSYS2's UCRT64 environment
pacman -Sy \
  mingw-w64-ucrt-x86_64-gc mingw-w64-ucrt-x86_64-pcre2 mingw-w64-ucrt-x86_64-libiconv \
  mingw-w64-ucrt-x86_64-zlib mingw-w64-ucrt-x86_64-openssl mingw-w64-ucrt-x86_64-llvm
cc crystal.obj -o crystal \
  $(pkg-config bdw-gc libpcre2-8 iconv zlib openssl --libs) \
  $(llvm-config --libs --system-libs --ldflags) \
  -lDbgHelp -lole32 -lWS2_32
export CRYSTAL_PATH='lib;$ORIGIN\src'
export CRYSTAL_LIBRARY_PATH=''

Now you can run or build a considerable number of files from here, such as ./crystal.exe samples/2048.cr and ./crystal.exe spec spec/std/regex_spec.cr. Notable omissions are OpenSSL and LLVM, as fixing their version detection macros is a bit complicated.

The interpreter is not supported. Most likely, Crystal::Loader would have a GCC-style .parse, but the rest of the functionality would be identical to the MSVC LoadLibraryExW-based loader.

Also, some invocations like ./crystal.exe spec spec/std/json will fail since the whole command line string is too long. Similar to MSVC, GCC also handles response files starting with @, so this can be implemented later; a workaround is to use --single-module.

For reference, here are all the useful MSYS2 packages and their corresponding pkg-config names:

MSYS2 package name pkg-config name
mingw-w64-ucrt-x86_64-gc bdw-gc
mingw-w64-ucrt-x86_64-pcre2 libpcre2-8
mingw-w64-ucrt-x86_64-libiconv iconv
mingw-w64-ucrt-x86_64-gmp gmp
mingw-w64-ucrt-x86_64-zlib zlib
mingw-w64-ucrt-x86_64-libxml2 libxml-2.0
mingw-w64-ucrt-x86_64-libyaml yaml-0.1
mingw-w64-ucrt-x86_64-openssl openssl
mingw-w64-ucrt-x86_64-libffi libffi
mingw-w64-ucrt-x86_64-llvm (use llvm-config instead)

@HertzDevil HertzDevil added kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:compiler:cli labels Oct 11, 2024
@straight-shoota straight-shoota added this to the 1.15.0 milestone Oct 18, 2024
@straight-shoota straight-shoota merged commit 3a78e8a into crystal-lang:master Oct 19, 2024
66 checks passed
@HertzDevil HertzDevil deleted the feature/mingw-w64-compiler branch October 20, 2024 06:50
@HertzDevil HertzDevil added platform:windows-gnu Windows support based on the MinGW-w64 toolchain + MSYS2 and removed platform:windows Windows support based on the MSVC toolchain / Win32 API labels Oct 20, 2024
straight-shoota pushed a commit that referenced this pull request Oct 23, 2024
Cross-compiles a MinGW-w64-based Crystal compiler from Ubuntu, then links it on MSYS2's UCRT64 environment. Resolves part of #6170.

The artifact includes the compiler, all dependent DLLs, and the source code only. It is not a complete installation since it is missing e.g. the documentation and the licenses, but it is sufficient for bootstrapping further native compiler builds within MSYS2.

The resulting binary is portable within MSYS2 and can be executed from anywhere inside an MSYS2 shell, although compilation requires `mingw-w64-ucrt-x86_64-cc`, probably `mingw-w64-ucrt-x86_64-pkgconf`, plus the respective development libraries listed in #15077. The DLLs bundled under `bin/` are needed to even start Crystal since they are dynamically linked at load time; they are not strictly needed if Crystal is always run only within MSYS2, but that is the job of an actual `PKGBUILD` file.
CTC97 pushed a commit to CTC97/crystal that referenced this pull request Nov 9, 2024
)

Cross-compiles a MinGW-w64-based Crystal compiler from Ubuntu, then links it on MSYS2's UCRT64 environment. Resolves part of crystal-lang#6170.

The artifact includes the compiler, all dependent DLLs, and the source code only. It is not a complete installation since it is missing e.g. the documentation and the licenses, but it is sufficient for bootstrapping further native compiler builds within MSYS2.

The resulting binary is portable within MSYS2 and can be executed from anywhere inside an MSYS2 shell, although compilation requires `mingw-w64-ucrt-x86_64-cc`, probably `mingw-w64-ucrt-x86_64-pkgconf`, plus the respective development libraries listed in crystal-lang#15077. The DLLs bundled under `bin/` are needed to even start Crystal since they are dynamically linked at load time; they are not strictly needed if Crystal is always run only within MSYS2, but that is the job of an actual `PKGBUILD` file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature platform:windows-gnu Windows support based on the MinGW-w64 toolchain + MSYS2 topic:compiler:cli
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants