Skip to content

Instantly share code, notes, and snippets.

@dbiesecke
Forked from tebjan/mkbundle_cygwin.sh
Created August 27, 2014 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbiesecke/f2f9751701d46218b21a to your computer and use it in GitHub Desktop.
Save dbiesecke/f2f9751701d46218b21a to your computer and use it in GitHub Desktop.
Create a fullly bundled mono binary on *nix
#!/bin/bash
#
#Usage:
#1.) Install cygwin and make sure you select the following packages:
#- gcc-mingw
#- pkg-config
#- mingw-zlib1
#- mingw-zlib-devel
#
#2.) Install the following Mono release package: "Mono for Windows, Gtk#, and XSP"
#
#3.) Download this script place it besides your Application and modify it to fit your environment
# (read the comments in this file carefully).
#
#4.) Open cygwin and navigate to the output folder of your application where the executable and the
# scipt lies, e.g. YourProject/bin/Release
#
#5.) Execute the script with the command: ./mkbundle_cygwin.sh
#
#Troubleshooting:
#If your bundled executable does not work make sure the original .NET application works on mono.
# You can test that by calling mono.exe and pass your .NET app to it or by installing XamarinStudio
# and running your project from there (make you set mono as runtime).
#
#Big credits to the following sources on which this script is based on:
#http://www.joebest.org/2011/09/mono-and-mkbundle-on-windows.html
#http://blog.shilbert.com/2012/06/using-mono-to-avoid-depending-on-net-framework/
#http://stackoverflow.com/questions/4474613/can-not-compile-simple-c-sharp-application-with-mkbundle
#http://2oj.com/2012/03/compiling-mono-executable-into-aot-exe-file-on-windows/
#
# If this doesn't work, ensure you have UNIX line endings in this file
# (\n, not \r\n.) You can use Notepad++ to switch them.
# Cygwin package requirements: gcc-mingw, pkg-config
# If you want to pass -z to mkbundle: mingw-zlib1, mingw-zlib-devel
# crash immediately if anything bad happens
set -o errexit
set -o nounset
# progra~2 is Program Files (x86) on Windows x64, except without spaces in it,
# which mkbundle seems to like better.
mono_version="2.10.9"
export MONO=/cygdrive/c/progra~2/Mono-$mono_version
#mono's machine config file
machineconfig=$PROGRAMFILES\\Mono-$mono_version\\etc\\mono\\4.0\\machine.config
# Required to find mkbundle
export PATH=$PATH:$MONO/bin
# Required for the pkg-config call that mkbundle causes to work
export PKG_CONFIG_PATH=$MONO/lib/pkgconfig
#Add an icon
icon_name='"icon.ico"'
#Create icon.rc file
echo "1 ICON $icon_name" > icon.rc
#Compile icon to object file
windres icon.rc icon.o
# On Cygwin you used to be able to use gcc -mno-cygwin to make binaries
# that didn't depend on cygwin. That doesn't work with the current gcc 4.x,
# so I'm explicitly using the compiler installed by the gcc-mingw package,
# in case the user has normal, non-mingw gcc installed too.
#
# Another alternative would be to use an older version of GCC, e.g:
#
# export CC="gcc-3 -mno-cygwin -U _WIN32"
#
# The -U _WIN32 undefines the _WIN32 symbol. The source code mkbundle executes
# is totally broken on Win32 but actually works if you don't let it know
# that it is on Win32.
# Also add the icon.o file so that it gets compiled into the exe.
export CC="i686-pc-mingw32-gcc icon.o -U _WIN32"
output_name=Output.exe
# Call Mono's mkbundle with your assemblies.
# --deps tells it to gather dependencies.
# -o specifies the output file name.
# YourGame.exe and OpenTK.dll can be replaced with whatever assemblies you need to include.
# You don't need to specify system assemblies, mkbundle will pick them up because of --deps.
mkbundle YourGame.exe OpenTK.dll --deps --machine-config "$machineconfig" -o $output_name -z
#Cleanup icon.rc and icon.o
rm icon.rc
rm icon.o
# Copy mono-2.0.dll and zlib1.dll here since Output.exe depends on it.
cp $MONO/bin/mono-2.0.dll .
cp $MONO/bin/zlib1.dll .
# Test output file
./$output_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment