compiling_with_mono.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 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 without the need to repeat this process.
  40. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono module enabled.
  41. Its exact name will differ based on your system and configuration, but should be of the form
  42. ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono`` or ``bin/godot.windows.tools.64.exe``.
  43. Be especially aware of the **.mono** suffix! If you compiled Godot without Mono support previously,
  44. you might have similarly named binaries without this suffix which can't be used to generate the Mono glue.
  45. Notes
  46. ^^^^^
  47. - **Do not** build your final binaries with ``mono_glue=no``. This disables C# scripting.
  48. This option must be used only for the temporary binary that will generate the glue.
  49. Godot will print a warning at startup if it was built without the glue sources.
  50. - The glue sources must be regenerated every time the ClassDB registered API changes. That is, for example,
  51. when a new method is registered to the scripting API or one of the parameter of such a method changes.
  52. Godot will print an error at startup if there is an API mismatch between ClassDB and the glue sources.
  53. Rebuild with Mono glue
  54. ----------------------
  55. Once you have generated the Mono glue, you can build the final binary with ``mono_glue=yes``.
  56. This is the default value for ``mono_glue`` so you can also omit it. You can build the Mono-enabled editor:
  57. ::
  58. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  59. And Mono-enabled export templates:
  60. ::
  61. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  62. If everything went well, apart from the normal output SCons should have created the following files in the ``bin`` directory:
  63. - If you're not static linking the Mono runtime, the build script will place the Mono runtime shared library (``monosgen-2.0``) next
  64. next to the Godot binary in the output directory. Make sure to include this library when distributing Godot. When targeting Android,
  65. no extra steps are required as this library is automatically copied to ``#platform/android/java/libs`` and gradle takes care of the rest.
  66. - Unlike "classical" Godot builds, when building with the mono module enabled and depending of the target platform a data directory
  67. may be created both for the editor and for export templates. This directory is important for proper functioning and must be
  68. distributed together with Godot. More details about this directory in :ref:`Data directory<compiling_with_mono_data_directory>`.
  69. Examples
  70. --------
  71. Example (Windows)
  72. ^^^^^^^^^^^^^^^^^
  73. ::
  74. # Build temporary binary
  75. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  76. # Generate glue sources
  77. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  78. ### Build binaries normally
  79. # Editor
  80. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  81. # Export templates
  82. scons p=windows target=debug tools=no module_mono_enabled=yes
  83. scons p=windows target=release tools=no module_mono_enabled=yes
  84. Example (X11)
  85. ^^^^^^^^^^^^^
  86. ::
  87. # Build temporary binary
  88. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  89. # Generate glue sources
  90. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  91. ### Build binaries normally
  92. # Editor
  93. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  94. # Export templates
  95. scons p=x11 target=debug tools=no module_mono_enabled=yes
  96. scons p=x11 target=release tools=no module_mono_enabled=yes
  97. .. _compiling_with_mono_data_directory:
  98. Data directory
  99. --------------
  100. The data directory is a dependency for Godot binaries built with the mono module enabled. It contains files
  101. that are important for the correct functioning of Godot. It must be distributed together with the Godot executable.
  102. This information does not apply for ``Android`` as there is no data directory for that platform.
  103. Export templates
  104. ^^^^^^^^^^^^^^^^
  105. The name of the data directory for a export template differs based on the configuration it was built with.
  106. The format is ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.debug`` or ``data.mono.windows.64.release``.
  107. This directory must be placed with its original name next to the Godot export templates.
  108. When exporting a project, Godot will also copy this directory with the game executable but
  109. the name will be changed to ``data_<APPNAME>``, where ``<APPNAME>`` is the application name
  110. as specified in the project setting ``application/config/name``.
  111. In the case of macOS, where the export template is compressed as a zip file, the
  112. contents of the data directory can be placed in the following locations inside the zip:
  113. +-------------------------------------------------------+---------------------------------------------------------------+
  114. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  115. +-------------------------------------------------------+---------------------------------------------------------------+
  116. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  117. +-------------------------------------------------------+---------------------------------------------------------------+
  118. Editor
  119. ^^^^^^^^
  120. The name of the data directory for the Godot editor will always be ``GodotSharp``.
  121. The contents of this directory are the following:
  122. - ``Api``
  123. - ``Mono`` (optional)
  124. - ``Tools``
  125. The ``Api`` subdirectory contains the Godot API assemblies.
  126. 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:
  127. +-------------------------------------------------------+---------------------------------------------------------------+
  128. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  129. +-------------------------------------------------------+---------------------------------------------------------------+
  130. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  131. +-------------------------------------------------------+---------------------------------------------------------------+
  132. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  133. +-------------------------------------------------------+---------------------------------------------------------------+
  134. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  135. +-------------------------------------------------------+---------------------------------------------------------------+
  136. The ``Mono`` subdirectory is optional but will be needed when distributing the editor, as some issues might arise
  137. when the installed Mono version in the user's system is not be the same as the one the Godot editor was built with.
  138. Pass ``copy_mono_root=yes`` to SCons when building the editor in order to create this folder and its contents.
  139. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotTools`` assemblies and its dependencies.
  140. Targeting Android
  141. -----------------
  142. Compiling the Android export templates with Mono is a bit simpler than it is for the desktop platforms,
  143. as there are no additional steps required after building. There is no need to worry about any
  144. dependency like a data directory or the runtime shared library (when dynamically linking) as
  145. those are automatically added to the gradle project.
  146. Before building Godot you do need to cross compile the Mono runtime for the target architectures.
  147. We recommend using these `build scripts <https://github.com/godotengine/godot-mono-builds>`_.
  148. They simplify this process but also include some patches needed for proper functioning with Godot.
  149. Read the README for instructions on how to use the scripts.
  150. Once you've built Mono, you can proceed to build Godot with the instructions described
  151. in this page and the :ref:`Compiling for Android<doc_compiling_for_android>` page.
  152. Make sure to let SCons know about the location of the Mono runtime you just built:
  153. ``scons [...] mono_prefix=$HOME/mono-installs/android-armeabi-v7a-release``
  154. (This path may be different on your system depending on the options you used to build Mono).
  155. Command-line options
  156. --------------------
  157. The following is the list of command-line options available when building with the mono module:
  158. - **module_mono_enabled**: Build Godot with the mono module enabled ( yes | **no** )
  159. - **mono_glue**: Whether to include the glue source files in the build and define `MONO_GLUE_DISABLED` as a preprocessor macro ( **yes** | no )
  160. - **mono_prefix**: Path to the Mono installation directory for the target platform and architecture
  161. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available ( yes | **no** )
  162. - **mono_static**: Whether to link the mono runtime statically ( yes | **no** )
  163. - **copy_mono_root**: Whether to copy the Mono framework assemblies and configuration files required by the Godot editor ( yes | **no** )