compiling_with_mono.rst 11 KB

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