Skip to content

Support for cross-compiling on Linux#465

Draft
universeindex wants to merge 30 commits intosmartcmd:mainfrom
universeindex:main
Draft

Support for cross-compiling on Linux#465
universeindex wants to merge 30 commits intosmartcmd:mainfrom
universeindex:main

Conversation

@universeindex
Copy link

Description

I've implemented support for cross-compiling a Windows build on a Linux host using clang-cl. I added a toolchain file at cmake/LinuxCrosscompile.cmake that achieves this. I also modified many source files to not use backslashes in include statements (which error with clang-cl) and use case-sensitive paths (many Linux filesystems are case-sensitive, unlike NTFS, which causes errors). I avoided changing actual functionality as much as possible, so I hope this isn't too intrusive. Additionally I apologize for the messy commit messages, I hope this isn't a big issue, I have detailed all of the changes I made here.

Changes

Include statements use forward slashes instead of backslashes
Include statements reference files with correct capitalization
Added a CMake toolchain file for using clang-cl on Linux
Minecraft.Client/Xbox/MinecraftWindows.rc is not compiled on Linux, this causes errors otherwise, as it can't find the windows SDK and I am unsure how to specify the location here.
I also added documentation for building on Linux.

Previous Behavior

Running CMake on Linux would throw a hardcoded error stating that Linux isn't supported. When removed, and clang-cl is set to be used, many errors occur due to include paths using backslashes and inconsistent capitalization. The only way to build and run on Linux before is to use a Windows VM, and there is no way to get proper code completion on a Linux IDE.

Root Cause

Backslashes in include paths are supported on Windows, but not other systems, and NTFS is case-insensitive, so include paths do not have to match capitalization, also not on other systems.

New Behavior

The game now compiles and outputs a Windows binary on Linux using clang-cl.

Fix Implementation

See changes section

@universeindex
Copy link
Author

P.S. I tested building on Windows, which still works fine, so hopefully there shouldn't be any issues

@universeindex
Copy link
Author

Most commits made to upstream will probably conflict with those by overwriting the includes I changed back with backslashes, but I'll be fixing those until this is merged

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

Yes, I will be very happy to have clang-cl support and cross compilation support. Actually, clang-cl will be a test site for /fp:fast because it optimizes better than MSVC. Let me review this one.

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

Have you tested it compiling with MSVC? Both msbuild and cmake. I hope this doesn't break the MSVC compilation.

@universeindex
Copy link
Author

universeindex commented Mar 4, 2026

I tested CMake w/ visual studio generator on Windows (with MSVC) which worked fine, but I haven't tried the normal .sln yet. I can do that now, but it might be a minute, i'm working on a really slow machine currently

@universeindex
Copy link
Author

works fine, compiles and runs w/ visual studio build ^

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

Could you please tell, aside from #includes, what did you change? Reviewing that much files is pain.

@universeindex
Copy link
Author

I did rewrite Minecraft.Client/iob_shim.asm in C++ as IobShim.cpp because I couldn't get MASM to work under Linux, but that's it aside from the includes I believe.

@DwifteJB
Copy link

DwifteJB commented Mar 4, 2026

can confirm does compile on linux w/ cmake

well done :)

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

That asm file wasn't really an asm thing rather a MSVC linker hack and it appears we don't actually need that at all.

set(CMAKE_C_COMPILER_FRONTEND_VARIANT MSVC)
set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT MSVC)

add_compile_options(/winsysroot /opt/msvc --target=x86_64-pc-windows-msvc -fms-compatibility -fms-extensions -fdelayed-template-parsing)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cmake file assumes /opt/msvc whereas https://github.com/mstorsjo/msvc-wine recommends ~/my_msvc/opt/msvc

just a simple issue as you can syslink or run the msvc-wine installer as root & install to /opt/msvc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good catch, i think i might set it to CMAKE_BINARY_DIR/msvc so users can symlink one instead, or if theres a way to set options of a toolchain file that would be best
will change soon, not home atm

Copy link

@DwifteJB DwifteJB Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msvc-wine sets itself in the path directory (although the user has to set this up manually through something like .zshrc)

could do something like

whereis msbuild

to determine the directory?

if it isn't set as CMAKE_MSVC_DIR

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened pr on your fork that fixes this & a clang error universeindex#1

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

I think __iob_func should be implemented as __declspec(naked) + __asm.

@DwifteJB
Copy link

DwifteJB commented Mar 4, 2026

I think __iob_func should be implemented as __declspec(naked) + __asm.

_declspec(naked) seems to not be working since __asm seems to have been dropped for x64 (if you want to use it you have to use a .asm file, like how 4J seems to have done it)

__attribute__((naked)) & __asm__() works however

@DwifteJB
Copy link

DwifteJB commented Mar 4, 2026

will need someone to test compiling on windows however

@void2012
Copy link
Collaborator

void2012 commented Mar 4, 2026

I think __iob_func should be implemented as __declspec(naked) + __asm.

_declspec(naked) seems to not be working since __asm seems to have been dropped for x64 (if you want to use it you have to use a .asm file, like how 4J seems to have done it)

__attribute__((naked)) & __asm__() works however

Forgot about it that, as I am used to x86 development. Uncool, Microsoft. Anyway, maybe conditionally use the .asm file for MSVC assembler and .cpp with [[__gnu__::__naked__]] for Clang-cl?

@DRAGONTOS
Copy link

i dont seem to be able to compile this
image

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

hello, i've fixed the merge conflicts and also added the linux build pipeline, is there anything else i should do before this is ready to merge? @void2012

I don't see a successful run of the ci on your repo
image

you should do that just to be sure

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

hello, i've fixed the merge conflicts and also added the linux build pipeline, is there anything else i should do before this is ready to merge? @void2012

I don't see a successful run of the ci on your repo image

you should do that just to be sure

It seems to fail
https://github.com/Twig6943/MinecraftConsoles/actions/runs/22791109398/job/66117716430

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

You might wanna look at this which does build (goes further at least)

@universeindex
Copy link
Author

oh, thanks, do you mind if i just use this?

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

oh, thanks, do you mind if i just use this?

Sure xD (wait for it to get a successfull build tho)

@universeindex
Copy link
Author

oh nevermind i just set the wrong toolchain file, lol
should work now, or build at least, maybe it will break somewhere after that now that i've just said that it should work

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

Seems to get stuck at the end on me (or might finish all of a sudden who knows)

Screenshot_20260307-070849_Helium

Also the run on your repo seems to have errors that doesn't happen on mine

Building thrm seperately (minecraft.world & minecraft.client) might change things (for the better or worse, not sure which)

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

Yea also gets stuck on yours after that error
It doesn:t give an error on mine but seems to get stuck at the very end.

I'll try to look into it tomorrow but no promises

@universeindex
Copy link
Author

it seems to be compiling without errors, but has been frozen for almost an hour now... not sure if this means it broke or not

@carterisonline
Copy link

There are some mentions of the /MP flag causing wine-msvc to hang: mstorsjo/msvc-wine#153 (comment)

I tried removing /MP from build.ninja and running ninja -j 1 instead, but it just ended up hanging even earlier in the build.

Here's the build script I'm using: https://gist.github.com/carterisonline/42a647b01beb93738fa8f8f278459d45
On nixpkgs with python3, msitools, cmake, ninja, libclang, lld

@universeindex
Copy link
Author

that's probably referring to the actual msvc running under wine, in this case msvc-wine is only providing the windows sdk and clang is the actual compiler, so i doubt it

@carterisonline
Copy link

Yeah MSVC is weird and I'm thinking that /MP applied to more than the compiler. On my end I'm seeing 5 identical processes trying to link (?) with Lib.exe

@void2012
Copy link
Collaborator

void2012 commented Mar 7, 2026

Note: if /MP is broken for you, it doesn't mean we need to disable it for the whole project. It significantly boosts the compilation speed.

@carterisonline
Copy link

Yeah waiting for this to compile with only one thread made me seriously consider switching to Windows. I can't wish that on anybody

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

Anybody got a successful run? or know what to change?

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

I did everything in the .yml in a local machine and it built fine

image

It seems to only get stuck on github actions

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

What are the odds theres something wrong with Minecraft.Client/stubs.cpp as it gets stuck on that, not on the last file?

@xgui4
Copy link

xgui4 commented Mar 7, 2026

based, if that mean i can cross-compile on my linux system without a vm that could awesome, as i could make changes easily :).

@bulkmoerls
Copy link
Contributor

let it all sink in right now please

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

What are the odds theres something wrong with Minecraft.Client/stubs.cpp as it gets stuck on that, not on the last file?

I removed everything in it besides pragma once but yea it just got stuck earlier I'm out of ideas

@universeindex
Copy link
Author

I feel like this PR is having some scope creep, maybe we should just move the workflow stuff to its own PR?
The actual linux crosscompilation stuff I made this PR for initially is basically ready for merging anyways (once i fix merge conflicts again)

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

I feel like this PR is having some scope creep, maybe we should just move the workflow stuff to its own PR? The actual linux crosscompilation stuff I made this PR for initially is basically ready for merging anyways (once i fix merge conflicts again)

Makes sense to me as nobody has an idea as to how to fix the workflow as of writing this, and this is sort of a blocker for this pr as the .yml is included.

@universeindex
Copy link
Author

I'll just delete it for now then

@universeindex
Copy link
Author

there are build errors right now, fixing them

@universeindex
Copy link
Author

I don't know wtf happened, but there are huge amounts of build errors happening now, give me a bit to fix those

@universeindex universeindex marked this pull request as draft March 7, 2026 21:48
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

Successfully merging this pull request may close these issues.