Compile & Run Native M1 MonoGame Applications

In case of people getting stuck with MonoGame while using a Mac running against Apple Silicon.
I have given up with Visual Studio extension working properly due to Intel specific components needed with MGCB.
As such, I’ve moved over to using Visual Studio Code, and the CLI approach to creating projects, as per the documentation, and the handy terminal built in to Code for extending my workflow.

With each project, I now have these 3 scripts in the root of every MonoGame project.

To edit the content with mgcb-editor: edit-content.sh

#!/bin/sh

# make sure that dotnet runs the Intel x64 version only
export PATH="/usr/local/share/dotnet/x64:$PATH"

# use the x64 version of dotnet to run mgcb-editor 
dotnet mgcb-editor Content/Content.mgcb

From adding and updating the content, I then need the project to run mgcb to build the content, which kicks off through a normal build automatically: build.sh

#!/bin/sh

# make sure that dotnet runs the Intel x64 version only
export PATH="/usr/local/share/dotnet/x64:$PATH"

# build and automtically make ready the mgcb pipeline toolkit
dotnet build

Using the combo of edit-content.sh and build.sh, that’s the majority of my development needs met.
The final part is to use publish.sh

#!/bin/sh

# make sure that dotnet runs the Intel x64 version only
export PATH="/usr/local/share/dotnet/x64:$PATH"

# build and automtically make ready the mgcb pipeline toolkit
dotnet publish -c Release -r osx-arm64 /p:PublishReadyToRun=false /p:TieredCompilation=false --self-contained true
dotnet publish -c Release -r osx-x64 /p:PublishReadyToRun=false /p:TieredCompilation=false --self-contained true

Although the self-contained flag is advised to be false, there is an issue on mine where if it is not self-contained, it just refuses to run as an executable. This seems to be part of the codesign with dotnet, and self-contained executables are signed.
Also, I have it generating both Intel and Arm executables, so it can be reorganised and set up as a universal app for deployment.

With these in place, I can then use VS Code, press F5 and it will then compile, debug and run my projects all in Apple Silicon.

2 Likes