Skip to content

Hadrian: fix cross-compilation to Windows (#20657)

Sylvain Henry requested to merge hsyl20/ghc:hsyl20/windows into master

With this patch, Hadrian can now be used to build a cross-compiler for Windows (#20657 (closed)):

./configure --target=x86_64-w64-mingw32
build --bignum=native

Objects produced by this compiler still fail to link but this is tracked in #16780 (closed)


Hadrian: fix windows cross-build (#20657 (closed))

Many small things to fix:

  • Hadrian: platform triple is "x86_64-w64-mingw32" and this wasn't recognized by Hadrian (note "w64" instead of "unknown")

  • Hadrian was using the build platform ("isWindowsHost") to detect the use of the Windows toolchain, which was wrong. We now use the "targetOs" setting.

  • Hadrian was doing the same thing for Darwin so we fixed both at once, even if cross-compilation to Darwin is unlikely to happen afaik (cf "osxHost" vs "osxTarget" changes)

  • Hadrian: libffi name was computed in two different places and one of them wasn't taking the different naming on Windows into account.

  • Hadrian was passing "-Irts/include" when building the stage1 compiler leading to the same error as in #18143 (which is using make). stage1's RTS is stage0's one so mustn't do this.

  • Hadrian: Windows linker doesn't seem to support "-zorigin" so we don't pass it (similarly to Darwin)

  • Hadrian: hsc2hs in cross-compilation mode uses a trick (taken from autoconf): it defines "static int test_array[SOME_EXPR]" where SOME_EXPR is a constant expression. However GCC reports an error because SOME_EXPR is supposedly not constant. This is fixed by using another method enabled with the --via-asm flag of hsc2hs. It has been fixed in make build system (5f6fcf78) but not in Hadrian.

  • Hadrian: some packages are specifically built only on Windows but they shouldn't be when building a cross-compiler (touchy and ghci-wrapper). We now correctly detect this case and disable these packages.

  • Base: we use iNVALID_HANDLE_VALUE in a few places. It fixed some hsc2hs issues before we switched to --via-asm (see above). I've kept these changes are they make the code nicer.

  • Base: base's configure tries to detect if it is building for Windows but for some reason the $host_alias value is x86_64-windows in my case and it wasn't properly detected.

  • Base: libraries/base/include/winio_structs.h imported "Windows.h" with a leading uppercase. It doesn't work on case-sensitive systems when cross-compiling so we have to use "windows.h".

  • RTS: rts/win32/ThrIOManager.c was importin "rts\OSThreads.h" but this path isn't valid when cross-compiling. We replaced "" with "/".

  • DeriveConstants: this tool derives the constants from the target RTS header files. However these header files define StgAsyncIOResult only when mingw32_HOST_OS is set hence it seems we have to set it explicitly. Note that deriveConstants is called more than once (why? there is only one target for now so it shouldn't) and in the second case this value is correctly defined (probably coming indirectly from the import of "rts/PosixSource.h"). A better fix would probably be to disable the unneeded first run of deriveconstants.

Edited by Sylvain Henry

Merge request reports