compiling_with_mono.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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
  12. requests.
  13. The recommended method is to use **curl**'s CA (Certificate Autorities) certificate bundle.
  14. Run the following commands to download and import it.
  15. On Windows, you can run it from the Mono command line prompt (or the regular prompt if you added Mono's ``bin`` directory to your ``PATH`` environment variable)::
  16. curl -LO https://curl.haxx.se/ca/cacert.pem
  17. cert-sync --user cacert.pem
  18. Alternatively, you can use the following command, though it's deprecated and may not work correctly::
  19. mozroots --import --sync
  20. Environment variables
  21. ---------------------
  22. By default, SCons will try to find Mono in the Windows Registry on Windows or
  23. via ``pkg-config`` on other platforms. You can specify a different installation
  24. directory by passing the ``mono_prefix`` command-line option to SCons; e.g.
  25. ``scons [...] mono_prefix=%ProgramFiles%/Mono``.
  26. This is the directory that contains the subdirectories ``include`` and ``lib``.
  27. Enable the Mono module
  28. ----------------------
  29. By default, the Mono module is disabled when building. To enable it, add the
  30. option ``module_mono_enabled=yes`` to the SCons command line.
  31. Generate the glue
  32. -------------------
  33. Glue sources are the wrapper functions that will be called by managed methods.
  34. These source files must be generated before building your final binaries. In
  35. order to generate them, first, you must build a temporary Godot binary with the
  36. options ``tools=yes`` and ``mono_glue=no``::
  37. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=no
  38. After the build finishes, you need to run the compiled executable with the
  39. parameter ``--generate-mono-glue`` followed by the path to an output directory.
  40. This path must be ``modules/mono/glue`` in the Godot directory::
  41. <godot_binary> --generate-mono-glue modules/mono/glue
  42. This command will tell Godot to generate the file ``modules/mono/glue/mono_glue.gen.cpp``
  43. and the C# solution for the Godot API at ``modules/mono/glue/Managed/Generated``.
  44. Once these files are generated, you can build Godot for all the desired targets
  45. without having to repeat this process.
  46. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono
  47. module enabled. Its exact name will differ based on your system and
  48. configuration, but should be of the form
  49. ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono``
  50. or ``bin/godot.windows.tools.64.exe``. Be especially aware of the **.mono**
  51. suffix! If you've previously compiled Godot without Mono support, you might have
  52. similarly named binaries without this suffix. These binaries can't be used to
  53. generate the Mono glue.
  54. Notes
  55. ^^^^^
  56. - **Do not build your final binaries with** ``mono_glue=no``.
  57. This disables C# scripting. This option must be used only for the temporary
  58. binary that will generate the glue. Godot will print a warning at startup if
  59. it was built without the glue sources.
  60. - The glue sources must be regenerated every time the ClassDB-registered API
  61. changes. That is, for example, when a new method is registered to the
  62. scripting API or one of the parameters of such a method changes.
  63. Godot will print an error at startup if there is an API mismatch
  64. between ClassDB and the glue sources.
  65. Rebuild with Mono glue
  66. ----------------------
  67. Once you have generated the Mono glue, you can build the final binary with
  68. ``mono_glue=yes``. This is the default value for ``mono_glue``, so you can also
  69. omit it. To build a Mono-enabled editor::
  70. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  71. And Mono-enabled export templates::
  72. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  73. If everything went well, apart from the normal output, SCons should have created
  74. the following files in the ``bin`` directory:
  75. - If you're not linking the Mono runtime statically, the build script will place
  76. the Mono runtime shared library (``monosgen-2.0``) next to the Godot
  77. binary in the output directory. Make sure to include this library when
  78. distributing Godot. When targeting Android, no extra steps are required as
  79. this library is automatically copied to ``#platform/android/java/libs`` and
  80. Gradle takes care of the rest.
  81. - Unlike "classical" Godot builds, when building with the Mono module enabled
  82. (and depending on the target platform), a data directory may be created both
  83. for the editor and for export templates. This directory is important for
  84. proper functioning and must be distributed together with Godot.
  85. More details about this directory in
  86. :ref:`Data directory<compiling_with_mono_data_directory>`.
  87. Examples
  88. --------
  89. Example (Windows)
  90. ^^^^^^^^^^^^^^^^^
  91. ::
  92. # Build temporary binary
  93. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  94. # Generate glue sources
  95. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  96. ### Build binaries normally
  97. # Editor
  98. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  99. # Export templates
  100. scons p=windows target=release_debug tools=no module_mono_enabled=yes
  101. scons p=windows target=release tools=no module_mono_enabled=yes
  102. Example (X11)
  103. ^^^^^^^^^^^^^
  104. ::
  105. # Build temporary binary
  106. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  107. # Generate glue sources
  108. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  109. ### Build binaries normally
  110. # Editor
  111. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  112. # Export templates
  113. scons p=x11 target=release_debug tools=no module_mono_enabled=yes
  114. scons p=x11 target=release tools=no module_mono_enabled=yes
  115. .. _compiling_with_mono_data_directory:
  116. Data directory
  117. --------------
  118. The data directory is a dependency for Godot binaries built with the Mono module
  119. enabled. It contains important files for the correct functioning of Godot. It
  120. must be distributed together with the Godot executable.
  121. .. note:: The information below doesn't apply to Android, as there is
  122. no data directory for that platform.
  123. Export templates
  124. ^^^^^^^^^^^^^^^^
  125. The name of the data directory for an export template differs based on the
  126. configuration it was built with. The format is
  127. ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.release_debug`` or
  128. ``data.mono.windows.64.release``.
  129. This directory must be placed with its original name next to the Godot export
  130. templates. When exporting a project, Godot will also copy this directory with
  131. the game executable but the name will be changed to ``data_<APPNAME>``, where
  132. ``<APPNAME>`` is the application name as specified in the project setting
  133. ``application/config/name``.
  134. In the case of macOS, where the export template is compressed as a ZIP archive,
  135. the contents of the data directory can be placed in the following locations
  136. inside the ZIP archive:
  137. +-------------------------------------------------------+---------------------------------------------------------------+
  138. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  139. +-------------------------------------------------------+---------------------------------------------------------------+
  140. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  141. +-------------------------------------------------------+---------------------------------------------------------------+
  142. Editor
  143. ^^^^^^^^
  144. The name of the data directory for the Godot editor will always be
  145. ``GodotSharp``. The contents of this directory are the following:
  146. - ``Api``
  147. - ``Mono`` (optional)
  148. - ``Tools``
  149. The ``Api`` subdirectory contains the Godot API assemblies. On macOS, if the
  150. Godot editor is distributed as a bundle, the contents of the data directory may
  151. be placed in the following locations:
  152. +-------------------------------------------------------+---------------------------------------------------------------+
  153. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  154. +-------------------------------------------------------+---------------------------------------------------------------+
  155. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  156. +-------------------------------------------------------+---------------------------------------------------------------+
  157. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  158. +-------------------------------------------------------+---------------------------------------------------------------+
  159. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  160. +-------------------------------------------------------+---------------------------------------------------------------+
  161. The ``Mono`` subdirectory is optional. It will be needed when distributing the
  162. editor, as issues can arise when the user-installed Mono version isn't identical
  163. to the one the Godot editor was built with. Pass ``copy_mono_root=yes`` to SCons
  164. when building the editor in order to create this folder and its contents.
  165. The ``Tools`` subdirectory contains tools required by the editor, like the
  166. ``GodotTools`` assemblies and its dependencies.
  167. Targeting Android
  168. -----------------
  169. Compiling the Android export templates with Mono is a bit simpler than it is for
  170. the desktop platforms, as there are no additional steps required after building.
  171. There is no need to worry about run-time dependencies like a data directory or
  172. the shared library (when dynamically linking) as those are automatically added
  173. to the Gradle project.
  174. Before building Godot, you need to cross compile the Mono runtime for the target
  175. architectures. We recommend using these
  176. `build scripts <https://github.com/godotengine/godot-mono-builds>`_.
  177. They simplify this process but also include some patches needed
  178. for proper functioning with Godot. See the README on the link above
  179. for instructions on how to use the scripts.
  180. Once you've built Mono, you can proceed to build Godot with the instructions
  181. described in this page and the
  182. :ref:`Compiling for Android<doc_compiling_for_android>` page. Make sure
  183. to let SCons know about the location of the Mono runtime you've just built:
  184. ``scons [...] mono_prefix="$HOME/mono-installs/android-armeabi-v7a-release"``
  185. (This path may be different on your system, depending on the options you used
  186. to build Mono).
  187. Command-line options
  188. --------------------
  189. The following is the list of command-line options available when building with
  190. the Mono module:
  191. - **module_mono_enabled**: Build Godot with the Mono module enabled
  192. (yes | **no**)
  193. - **mono_glue**: Whether to include the glue source files in the build
  194. and define ``MONO_GLUE_DISABLED`` as a preprocessor macro (**yes** | no)
  195. - **mono_prefix**: Path to the Mono installation directory
  196. for the target platform and architecture
  197. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available
  198. (yes | **no**)
  199. - **mono_static**: Whether to link the Mono runtime statically
  200. (yes | **no**)
  201. - **copy_mono_root**: Whether to copy the Mono framework assemblies
  202. and configuration files required by the Godot editor (yes | **no**)