compiling_with_mono.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 using the following environment variables for the respective
  20. ``bits`` option: ``MONO32_PREFIX`` and ``MONO64_PREFIX``.
  21. The specified directory must contain the subdirectories ``bin``, ``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. Once this file is generated, you can build Godot for all the desired targets
  39. without the need to repeat this process.
  40. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono
  41. module enabled. Its exact name will differ based on your system and
  42. configuration, but should be of the form
  43. ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono``
  44. or ``bin/godot.windows.tools.64.exe``. Be especially aware of the **.mono**
  45. suffix! If you've previously compiled Godot without Mono support, you might have
  46. similarly named binaries without this suffix. These binaries can't be used to
  47. generate the Mono glue.
  48. Notes
  49. ^^^^^
  50. - **Do not build your final binaries with** ``mono_glue=no``.
  51. This disables C# scripting. This option must be used only for the temporary
  52. binary that will generate the glue. Godot will print a warning at startup if
  53. it was built without the glue sources.
  54. - The glue sources must be regenerated every time the ClassDB-registered API
  55. changes. That is, for example, when a new method is registered to the
  56. scripting API or one of the parameters of such a method changes.
  57. Godot will print an error at startup if there is an API mismatch
  58. between ClassDB and the glue sources.
  59. Rebuild with Mono glue
  60. ----------------------
  61. Once you have generated the Mono glue, you can build the final binary with
  62. ``mono_glue=yes``. This is the default value for ``mono_glue``, so you can also
  63. omit it. To build a Mono-enabled editor::
  64. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  65. And Mono-enabled export templates::
  66. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  67. If everything went well, apart from the normal output SCons should have created
  68. the following files in the ``bin`` directory:
  69. - If you're not linking the Mono runtime statically, the build script will place
  70. the Mono runtime shared library (``monosgen-2.0``) next to the Godot
  71. binary in the output directory. Make sure to include this library when
  72. distributing Godot.
  73. - Unlike "classical" Godot builds, when building with the mono module enabled
  74. (and depending of the target platform), a data directory may be created both
  75. for the editor and for export templates. This directory is important for
  76. proper functioning and must be distributed together with Godot.
  77. More details about this directory in
  78. :ref:`Data directory<compiling_with_mono_data_directory>`.
  79. Examples
  80. --------
  81. Example (Windows)
  82. ^^^^^^^^^^^^^^^^^
  83. ::
  84. # Build temporary binary
  85. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  86. # Generate glue sources
  87. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  88. ### Build binaries normally
  89. # Editor
  90. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  91. # Export templates
  92. scons p=windows target=release_debug tools=no module_mono_enabled=yes
  93. scons p=windows target=release tools=no module_mono_enabled=yes
  94. Example (X11)
  95. ^^^^^^^^^^^^^
  96. ::
  97. # Build temporary binary
  98. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  99. # Generate glue sources
  100. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  101. ### Build binaries normally
  102. # Editor
  103. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  104. # Export templates
  105. scons p=x11 target=release_debug tools=no module_mono_enabled=yes
  106. scons p=x11 target=release tools=no module_mono_enabled=yes
  107. .. _compiling_with_mono_data_directory:
  108. Data directory
  109. --------------
  110. The data directory is a dependency for Godot binaries built with the mono module
  111. enabled. It contains important files for the correct functioning of Godot. It
  112. must be distributed together with the Godot executable.
  113. Export templates
  114. ^^^^^^^^^^^^^^^^
  115. The name of the data directory for an export template differs based on the
  116. configuration it was built with. The format is
  117. ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.release_debug`` or
  118. ``data.mono.windows.64.release``.
  119. This directory must be placed with its original name next to the Godot export
  120. templates. When exporting a project, Godot will also copy this directory with
  121. the game executable but the name will be changed to ``data_<APPNAME>``, where
  122. ``<APPNAME>`` is the application name as specified in the project setting
  123. ``application/config/name``.
  124. In the case of macOS, where the export template is compressed as a ZIP archive,
  125. the contents of the data directory can be placed in the following locations
  126. inside the ZIP archive:
  127. +-------------------------------------------------------+---------------------------------------------------------------+
  128. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  129. +-------------------------------------------------------+---------------------------------------------------------------+
  130. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  131. +-------------------------------------------------------+---------------------------------------------------------------+
  132. Editor
  133. ^^^^^^
  134. The name of the data directory for the Godot editor will always be
  135. ``GodotSharp``. The contents of this directory are the following:
  136. - ``Api`` (optional)
  137. - ``Mono`` (optional)
  138. - ``Tools`` (required)
  139. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotSharpTools`` assembly.
  140. The ``Mono`` subdirectory is optional. It can be used to bundle the Mono framework assemblies and configuration files with the Godot editor, as well as some shared library dependencies like ``MonoPosixHelper``. This is important to avoid issues that might arise when the installed Mono version in the user's system may not be the same as the one the Godot editor was built with. You can make SCons copy these files to this subdirectory by passing the option ``copy_mono_root=yes`` when building the editor.
  141. The ``Api`` directory is also optional. Godot API assemblies are not bundled with the editor by default. Instead the Godot editor will generate and build them on the user's machine the first time they are required. This can be avoided by generating and building them manually and placing them in this subdirectory. If the editor can find them there, it will avoid the step of generating and building them again.
  142. The following is an example script for building and copying the Godot API assemblies:
  143. .. tabs::
  144. .. code-tab:: bash Bash
  145. DATA_API_DIR=./bin/GodotSharp/Api
  146. SOLUTION_DIR=/tmp/build_GodotSharp
  147. BUILD_CONFIG=Release
  148. # Generate the solution
  149. ./bin/<godot_binary> --generate-cs-api $SOLUTION_DIR
  150. # Build the solution
  151. msbuild $SOLUTION_DIR/GodotSharp.sln /p:Configuration=$BUILD_CONFIG
  152. # Copy the built files
  153. mkdir -p $DATA_API_DIR
  154. cp $SOLUTION_DIR/GodotSharp/bin/$BUILD_CONFIG/{GodotSharp.dll,GodotSharp.pdb,GodotSharp.xml} $DATA_API_DIR
  155. cp $SOLUTION_DIR/GodotSharpEditor/bin/$BUILD_CONFIG/{GodotSharpEditor.dll,GodotSharpEditor.pdb,GodotSharpEditor.xml} $DATA_API_DIR
  156. .. code-tab:: batch Batch
  157. set DATA_API_DIR=.\bin\GodotSharp\Api
  158. set SOLUTION_DIR=%Temp%\build_GodotSharp
  159. set BUILD_CONFIG=Release
  160. # Generate the solution
  161. .\bin\<godot_binary> --generate-cs-api %SOLUTION_DIR%
  162. # Build the solution
  163. msbuild %SOLUTION_DIR%\GodotSharp.sln /p:Configuration=%BUILD_CONFIG%
  164. # Copy the built files
  165. if not exist "%DATA_API_DIR%" mkdir %DATA_API_DIR%
  166. for %%I in (GodotSharp.dll GodotSharp.pdb GodotSharp.xml) do copy %SOLUTION_DIR%\GodotSharp\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  167. for %%I in (GodotSharpEditor.dll GodotSharpEditor.pdb GodotSharpEditor.xml) do copy %SOLUTION_DIR%\GodotSharpEditor\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  168. The script assumes it's being executed from the directory where SConstruct is located.
  169. ``<godot_binary>`` refers to the tools binary compiled with the Mono module enabled.
  170. 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:
  171. +-------------------------------------------------------+---------------------------------------------------------------+
  172. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  173. +-------------------------------------------------------+---------------------------------------------------------------+
  174. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  175. +-------------------------------------------------------+---------------------------------------------------------------+
  176. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  177. +-------------------------------------------------------+---------------------------------------------------------------+
  178. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  179. +-------------------------------------------------------+---------------------------------------------------------------+
  180. Command-line options
  181. --------------------
  182. The following is the list of command-line options available when building with
  183. the Mono module:
  184. - **module_mono_enabled**: Build Godot with the Mono module enabled
  185. (yes | **no**)
  186. - **mono_glue**: Whether to include the glue source files in the build
  187. and define ``MONO_GLUE_DISABLED`` as a preprocessor macro (**yes** | no)
  188. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available
  189. ( yes | **no** )
  190. - **mono_static**: Whether to link the Mono runtime statically
  191. (yes | **no**)
  192. - **copy_mono_root**: Whether to copy the Mono framework assemblies
  193. and configuration files required by the Godot editor (yes | **no**)