Skip to content

Support for cross-compiling on Linux#465

Open
universeindex wants to merge 27 commits intosmartcmd:mainfrom
universeindex:main
Open

Support for cross-compiling on Linux#465
universeindex wants to merge 27 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

@niansa
Copy link

niansa commented Mar 6, 2026

Alright I'll put together a Dockerfile real quick then.

@niansa
Copy link

niansa commented Mar 6, 2026

There are Dockerfiles already in the repo lol

Either way in case anyone can use it, here's a script I wrote for testing purposes:

# Fresh Debian Trixie install

dpkg --add-architecture i386
apt update
apt install wine wine32 wine64 msitools git python3 cmake ninja-build lld-19 llvm-19 clang-19

DISPLAY= wineboot

# MSVC install
(
    git clone https://github.com/mstorsjo/msvc-wine.git
    cd msvc-wine
    git checkout 32b504c63b869681cda6824a20e30b74cb718432

    mkdir /opt/msvc
    ./vsdownload.py --dest /opt/msvc --accept-license
    ./install.sh /opt/msvc

    ln -s /usr/bin/clang-cl{-19,}
    ln -s /usr/bin/lld-link{-19,}
)

# MSVC setup
export PATH="/opt/msvc/bin/x64:$PATH"

# Project build
(
    git clone https://github.com/universeindex/MinecraftConsoles.git --depth 1 --recursive
    cd MinecraftConsoles
    mkdir build
    cd build
    cmake .. -GNinja -DCMAKE_TOOLCHAIN_FILE=../cmake/LinuxCrosscompile.cmake
    ninja
)

@niansa
Copy link

niansa commented Mar 6, 2026

I might try to get this to build using clang-cl with mingw 🤔
That would avoid a LOT of complications but probably introduces other complications on the way

@universeindex
Copy link
Author

tried this, but i started to get many more strange errors, i think lce depends on msvc specific stdlib stuff
i think this system works fine, we just need to add clang-cl into the visual studio build and then this pr is done (hopefully)
later i could add scripts to automatically download the msvc toolchain, but due to the eula it would have to download the official packages and extract those, which requires the msitools package

@niansa
Copy link

niansa commented Mar 6, 2026

Yeah, fuck this codebase lol... using backslashes in include paths is already insane enough
Depending on MSVC sucks, but the quality... who the HECK thought it'd be a good idea to using namespace std; in every single header file??

@universeindex
Copy link
Author

God only knows

@BlueishSapphire
Copy link

Would it be reasonable to split this into multiple PRs? Converting backslashes to forward slashes and fixing the casing issues in #includes would be easy to split off into a new PR and merge it right now. It would also make developing on HEAD on linux a lot easier in the meantime while the rest of this PR is still being developed.

@universeindex
Copy link
Author

good idea actually, didnt think of that
not sure how i would go about that now that all my changes are mangled together though.. probably I'll make a new branch on my fork and start from scratch, i used scripts to do the conversion anyways so it shouldn't be that hard
I'm not home now but i can gladly do that over the weekend, thanks for the idea

@void2012
Copy link
Collaborator

void2012 commented Mar 6, 2026

Okay, screw the Visual Studio solution file, I can't add new configuration either. Let's add clang-cl as cmake only for now. Resolve the branch conflicts and imma merge this one.

@universeindex
Copy link
Author

We love microslop <3
I'll see if i can fix merge conflicts soon but I am still a little busy at school right now, so sorry if it takes a few hours

@BlueishSapphire
Copy link

I can make a new branch and PR with the #include changes, I'll just have to run a couple sed scripts

@universeindex
Copy link
Author

looks like void is ready to merge this existing one once i fix up merge conflicts though, so maybe not, sorry

@DRAGONTOS
Copy link

    ninja

this script made me possible to also compile this :3

@AE70
Copy link

AE70 commented Mar 6, 2026

Since there's no updates, I fixed the conflicts and got this to work in one of the latest commit of upstream and can merge automatically as of speaking, PR the PR? https://github.com/AE70/MinecraftConsoles/tree/pr465 .

@DRAGONTOS
Copy link

Since there's no updates, I fixed the conflicts and got this to work in one of the latest commit of upstream and can merge automatically as of speaking, PR the PR? https://github.com/AE70/MinecraftConsoles/tree/pr465 .

it doesnt seem to have audio but that could be a upstream issue

@AE70
Copy link

AE70 commented Mar 6, 2026

it doesnt seem to have audio but that could be a upstream issue

not upstream issue, need to fix that.

@universeindex
Copy link
Author

was it one of my commits that broke audio? i never touched that, last i checked it was working, weird

@AE70
Copy link

AE70 commented Mar 7, 2026

was it one of my commits that broke audio? i never touched that, last i checked it was working, weird

Nope, its the miniaudio implementation and how it is built in linux which i'm still trying to figure out why it doesn't work.

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

@universeindex nice work on this. Could you add a workflow like this ? It might result in lower build time

@universeindex
Copy link
Author

will look into it, I know nothing about ci pipelines though

@Twig6943
Copy link
Contributor

Twig6943 commented Mar 7, 2026

will look into it, I know nothing about ci pipelines though

It's easy. You can copy paste the one I linked edit 1,2 lines and it'll be ready I can do so if you want

(Also unrelated but I used your cmake file for a pvz project that's all good right?)

@universeindex
Copy link
Author

alr, will do
yea thats fine, technically none of this repo's code can be licensed as we're basing this off leaked code LMAO so yea idc

@universeindex
Copy link
Author

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

@universeindex universeindex marked this pull request as ready for review March 7, 2026 02:57
@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)

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.

8 participants