compiling_with_mono.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. .. _doc_compiling_with_mono:
  2. Compiling with Mono
  3. ===================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. - Mono 5.12.0 or greater
  8. - MSBuild
  9. - NuGet
  10. - pkg-config
  11. You may need to import necessary certificates for NuGet to perform HTTPS requests. You can do this
  12. with the following command (on Windows, you can run it from the Mono command line prompt):
  13. ::
  14. mozroots --import --sync
  15. Environment variables
  16. ---------------------
  17. By default, SCons will try to find Mono in the Windows Registry on Windows or via ``pkg-config`` on other platforms.
  18. You can specify a different installation directory by passing the ``mono_prefix`` command-line option to SCons;
  19. e.g.: ``scons [...] mono_prefix=%ProgramFiles%/Mono``.
  20. This is the directory that contains the subdirectories ``include`` and ``lib``.
  21. Enable the Mono module
  22. ----------------------
  23. By default, the mono module is disabled for builds. To enable it you can pass the
  24. option ``module_mono_enabled=yes`` to your SCons command.
  25. Generate the glue
  26. -------------------
  27. The glue sources are the wrapper functions that will be called by managed methods. These source
  28. files must be generated before building your final binaries. In order to generate them, first,
  29. you must build a temporary Godot binary with the options ``tools=yes`` and ``mono_glue=no``:
  30. ::
  31. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=no
  32. After the build finishes, you need to run the compiled executable with the parameter
  33. ``--generate-mono-glue`` followed by the path to an output directory. This path
  34. must be ``modules/mono/glue`` in the Godot directory.
  35. ::
  36. <godot_binary> --generate-mono-glue modules/mono/glue
  37. This command will tell Godot to generate the file ``modules/mono/glue/mono_glue.gen.cpp``.
  38. Once this file is generated, you can build Godot for all the desired targets without the need to repeat this process.
  39. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono module enabled.
  40. Its exact name will differ based on your system and configuration, but should be of the form
  41. ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono`` or ``bin/godot.windows.tools.64.exe``.
  42. Be especially aware of the **.mono** suffix! If you compiled Godot without Mono support previously,
  43. you might have similarly named binaries without this suffix which can't be used to generate the Mono glue.
  44. Notes
  45. ^^^^^
  46. - **Do not** build your final binaries with ``mono_glue=no``. This disables C# scripting.
  47. This option must be used only for the temporary binary that will generate the glue.
  48. Godot will print a warning at startup if it was built without the glue sources.
  49. - The glue sources must be regenerated every time the ClassDB registered API changes. That is, for example,
  50. when a new method is registered to the scripting API or one of the parameter of such a method changes.
  51. Godot will print an error at startup if there is an API mismatch between ClassDB and the glue sources.
  52. Rebuild with Mono glue
  53. ----------------------
  54. Once you have generated the Mono glue, you can build the final binary with ``mono_glue=yes``.
  55. It's the default value for ``mono_glue`` so you can also omit it. You can build the Mono-enabled editor:
  56. ::
  57. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  58. And Mono-enabled export templates:
  59. ::
  60. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  61. If everything went well, apart from the normal output SCons should have created the following files in the ``bin`` directory:
  62. - If you're not static linking the Mono runtime, the build script will place the Mono runtime shared library (``monosgen-2.0``) next
  63. next to the Godot binary in the output directory. Make sure to include this library when distributing Godot. When targeting Android,
  64. no extra steps are required, as this library is automatically copied ``#platform/android/java/libs`` and gradle takes care of the rest.
  65. - Unlike "classical" Godot builds, when building with the mono module enabled and depending of the target platform a data directory
  66. may be created both for the editor and for export templates. This directory is important for proper functioning and must be
  67. distributed together with Godot. More details about this directory in :ref:`Data directory<compiling_with_mono_data_directory>`.
  68. Examples
  69. --------
  70. Example (Windows)
  71. ^^^^^^^^^^^^^^^^^
  72. ::
  73. # Build temporary binary
  74. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  75. # Generate glue sources
  76. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  77. ### Build binaries normally
  78. # Editor
  79. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  80. # Export templates
  81. scons p=windows target=debug tools=no module_mono_enabled=yes
  82. scons p=windows target=release tools=no module_mono_enabled=yes
  83. Example (X11)
  84. ^^^^^^^^^^^^^
  85. ::
  86. # Build temporary binary
  87. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  88. # Generate glue sources
  89. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  90. ### Build binaries normally
  91. # Editor
  92. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  93. # Export templates
  94. scons p=x11 target=debug tools=no module_mono_enabled=yes
  95. scons p=x11 target=release tools=no module_mono_enabled=yes
  96. .. _compiling_with_mono_data_directory:
  97. Data directory
  98. --------------
  99. The data directory is a dependency for Godot binaries built with the mono module enabled. It contains files
  100. that are important for the correct functioning of Godot. It must be distributed together with the Godot executable.
  101. There is no data directory when targeting ``Android`` so the following information does not apply to that platform.
  102. Export templates
  103. ^^^^^^^^^^^^^^^^
  104. The name of the data directory for a export template differs based on the configuration it was built with.
  105. The format is ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.debug`` or ``data.mono.windows.64.release``.
  106. In the case of export templates the data directory only contains Mono framework assemblies
  107. and configuration files, as well as some shared library dependencies like ``MonoPosixHelper``.
  108. This directory must be placed with its original name next to the Godot export templates.
  109. When exporting a project, Godot will also copy this directory with the game executable but
  110. the name will be changed to ``data_<APPNAME>``, where ``<APPNAME>`` is the application name
  111. as specified in the project setting ``application/config/name``.
  112. In the case of macOS, where the export template is compressed as a zip file, the
  113. contents of the data directory can be placed in the following locations inside the zip:
  114. +-------------------------------------------------------+---------------------------------------------------------------+
  115. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  116. +-------------------------------------------------------+---------------------------------------------------------------+
  117. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  118. +-------------------------------------------------------+---------------------------------------------------------------+
  119. Editor
  120. ^^^^^^^^
  121. The name of the data directory for the Godot editor will always be ``GodotSharp``.
  122. The main structure of this directory has the following subdirectories:
  123. - ``Api`` (optional)
  124. - ``Mono`` (optional)
  125. - ``Tools`` (required)
  126. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotSharpTools`` assembly.
  127. The ``Mono`` subdirectory is optional. It can be used to bundle the Mono framework assemblies and
  128. configuration files with the Godot editor, as well as some shared library dependencies like ``MonoPosixHelper``.
  129. This is important to avoid issues that might arise when the installed Mono version in the user's system may
  130. not be the same as the one the Godot editor was built with. You can make SCons copy these files to
  131. this subdirectory by passing the option ``copy_mono_root=yes`` when building the editor.
  132. The ``Api`` directory is also optional. Godot API assemblies are not bundled with the editor by default.
  133. Instead the Godot editor will generate and build them on the user's machine the first time they are required.
  134. This can be avoided by generating and building them manually and placing them in this subdirectory.
  135. If the editor can find them there, it will avoid the step of generating and building them again.
  136. The following is an example script for building and copying the Godot API assemblies:
  137. .. tabs::
  138. .. code-tab:: bash Bash
  139. DATA_API_DIR=./bin/GodotSharp/Api
  140. SOLUTION_DIR=/tmp/build_GodotSharp
  141. BUILD_CONFIG=Release
  142. # Generate the solution
  143. ./bin/<godot_binary> --generate-cs-api $SOLUTION_DIR
  144. # Build the solution
  145. msbuild $SOLUTION_DIR/GodotSharp.sln /p:Configuration=$BUILD_CONFIG
  146. # Copy the built files
  147. mkdir -p $DATA_API_DIR
  148. cp $SOLUTION_DIR/GodotSharp/bin/$BUILD_CONFIG/{GodotSharp.dll,GodotSharp.pdb,GodotSharp.xml} $DATA_API_DIR
  149. cp $SOLUTION_DIR/GodotSharpEditor/bin/$BUILD_CONFIG/{GodotSharpEditor.dll,GodotSharpEditor.pdb,GodotSharpEditor.xml} $DATA_API_DIR
  150. .. code-tab:: batch Batch
  151. set DATA_API_DIR=.\bin\GodotSharp\Api
  152. set SOLUTION_DIR=%Temp%\build_GodotSharp
  153. set BUILD_CONFIG=Release
  154. # Generate the solution
  155. .\bin\<godot_binary> --generate-cs-api %SOLUTION_DIR%
  156. # Build the solution
  157. msbuild %SOLUTION_DIR%\GodotSharp.sln /p:Configuration=%BUILD_CONFIG%
  158. # Copy the built files
  159. if not exist "%DATA_API_DIR%" mkdir %DATA_API_DIR%
  160. for %%I in (GodotSharp.dll GodotSharp.pdb GodotSharp.xml) do copy %SOLUTION_DIR%\GodotSharp\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  161. for %%I in (GodotSharpEditor.dll GodotSharpEditor.pdb GodotSharpEditor.xml) do copy %SOLUTION_DIR%\GodotSharpEditor\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  162. The script assumes it's being executed from the directory where SConstruct is located.
  163. ``<godot_binary>`` refers to the tools binary compiled with the Mono module enabled.
  164. In the case of macOS, if the Godot editor is distributed as a bundle, the contents of the data directory may be placed in the following locations:
  165. +-------------------------------------------------------+---------------------------------------------------------------+
  166. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  167. +-------------------------------------------------------+---------------------------------------------------------------+
  168. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  169. +-------------------------------------------------------+---------------------------------------------------------------+
  170. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  171. +-------------------------------------------------------+---------------------------------------------------------------+
  172. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  173. +-------------------------------------------------------+---------------------------------------------------------------+
  174. Targeting Android
  175. -----------------
  176. Compiling the Android export templates with Mono is a bit simpler than it is for the desktop platforms,
  177. as there are no additional steps required after building. There is no need to worry about any
  178. dependency like a data directory or the runtime shared library (when dynamically linking) as
  179. those are automatically added to the gradle project.
  180. **Important:** You need to manually specify the mono version with the ``MONO_VERSION`` environment variable.
  181. Before building Godot you do need to cross compile the Mono runtime for the target architectures. The easiest
  182. way to do this is to use the sdk Makefiles from the Mono repository. The following is an example bash script:
  183. *Note: We plan to distribute prebuilt packages of the Mono runtime in the future so you don't have to build it yourself.*
  184. .. code:: bash
  185. #!/bin/bash
  186. set -e;
  187. set -o pipefail;
  188. set -x;
  189. # You have to set the MONO_SOURCE_ROOT environment variable to point to the
  190. # Mono repository location in the file system before running this script.
  191. : ${MONO_SOURCE_ROOT:?Variable MONO_SOURCE_ROOT not set or empty}
  192. cd ${MONO_SOURCE_ROOT}
  193. # We're using the sdk makefiles distributed with Mono. In the future we may want to
  194. # write our own configuration to get rid of the stuff we don't need and reducing size.
  195. # We are not using the cross templates for now, so you can comment out the calls
  196. # to the AndroidCross* functions in '${MONO_SOURCE_ROOT}/sdks/builds/android.mk'.
  197. ANDROID_TOOLCHAIN_DIR=${ANDROID_TOOLCHAIN_DIR:-${HOME}/Android/Toolchain}
  198. ANDROID_TOOLCHAIN_CACHE_DIR=${ANDROID_TOOLCHAIN_CACHE_DIR:-${ANDROID_TOOLCHAIN_DIR}/android-archives}
  199. ANDROID_TOOLCHAIN_PREFIX=${ANDROID_TOOLCHAIN_PREFIX:-${ANDROID_TOOLCHAIN_DIR}/toolchains}
  200. # The Makefiles expect the Android SDK and NDK to be located at '${ANDROID_TOOLCHAIN_DIR}/sdk' and
  201. # '${ANDROID_TOOLCHAIN_DIR}/ndk' respectively. Godot uses '${ANDROID_TOOLCHAIN_DIR}/sdk/ndk-bundle'
  202. # for the NDK, so '${ANDROID_TOOLCHAIN_DIR}/ndk' can be setup as a symlink to that directory.
  203. if [ ! -d ${ANDROID_TOOLCHAIN_DIR}/sdk ]; then
  204. echo Directory not found ${ANDROID_TOOLCHAIN_DIR}/sdk
  205. exit 1
  206. fi
  207. if [ ! -d ${ANDROID_TOOLCHAIN_DIR}/ndk ]; then
  208. echo Directory not found ${ANDROID_TOOLCHAIN_DIR}/ndk
  209. exit 1
  210. fi
  211. export ANDROID_TOOLCHAIN_DIR ANDROID_TOOLCHAIN_CACHE_DIR ANDROID_TOOLCHAIN_PREFIX
  212. MAKE_NUM_JOBS=${MAKE_NUM_JOBS:-2}
  213. echo "
  214. DISABLE_IOS = 1
  215. DISABLE_MAC = 1
  216. DISABLE_WASM = 1
  217. DISABLE_WASM_CROSS = 1
  218. DISABLE_BCL = 1
  219. DISABLE_DESKTOP = 1
  220. DISABLE_LLVM = 1
  221. " > ${MONO_SOURCE_ROOT}/sdks/Make.config
  222. make -C sdks/builds provision-mxe
  223. make -C sdks/builds archive-android NINJA= IGNORE_PROVISION_ANDROID=1 IGNORE_PROVISION_MXE=1 -j ${MAKE_NUM_JOBS}
  224. # You can then pass to SCons: mono_prefix=${MONO_SOURCE_ROOT}/sdks/out/android-${TARGET_ARCH}-release
  225. set +x;
  226. Command-line options
  227. --------------------
  228. The following is the list of command-line options available when building with the mono module:
  229. - **module_mono_enabled**: Build Godot with the mono module enabled ( yes | **no** )
  230. - **mono_glue**: Whether to include the glue source files in the build and define `MONO_GLUE_DISABLED` as a preprocessor macro ( **yes** | no )
  231. - **mono_prefix**: Path to the Mono installation directory for the target platform and architecture
  232. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available ( yes | **no** )
  233. - **mono_static**: Whether to link the mono runtime statically ( yes | **no** )
  234. - **copy_mono_root**: Whether to copy the Mono framework assemblies and configuration files required by the Godot editor ( yes | **no** )