compiling_with_mono.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 using the following environment variables for the respective
  19. ``bits`` option: ``MONO32_PREFIX`` and ``MONO64_PREFIX``.
  20. The specified directory must contain the subdirectories ``bin``, ``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. This is 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.
  64. - Unlike "classical" Godot builds, when building with the mono module enabled and depending of the target platform a data directory
  65. may be created both for the editor and for export templates. This directory is important for proper functioning and must be
  66. distributed together with Godot. More details about this directory in :ref:`Data directory<compiling_with_mono_data_directory>`.
  67. Examples
  68. --------
  69. Example (Windows)
  70. ^^^^^^^^^^^^^^^^^
  71. ::
  72. # Build temporary binary
  73. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  74. # Generate glue sources
  75. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  76. ### Build binaries normally
  77. # Editor
  78. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  79. # Export templates
  80. scons p=windows target=debug tools=no module_mono_enabled=yes
  81. scons p=windows target=release tools=no module_mono_enabled=yes
  82. Example (X11)
  83. ^^^^^^^^^^^^^
  84. ::
  85. # Build temporary binary
  86. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  87. # Generate glue sources
  88. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  89. ### Build binaries normally
  90. # Editor
  91. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  92. # Export templates
  93. scons p=x11 target=debug tools=no module_mono_enabled=yes
  94. scons p=x11 target=release tools=no module_mono_enabled=yes
  95. .. _compiling_with_mono_data_directory:
  96. Data directory
  97. --------------
  98. The data directory is a dependency for Godot binaries built with the mono module enabled. It contains files
  99. that are important for the correct functioning of Godot. It must be distributed together with the Godot executable.
  100. Export templates
  101. ^^^^^^^^^^^^^^^^
  102. The name of the data directory for a export template differs based on the configuration it was built with.
  103. The format is ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.debug`` or ``data.mono.windows.64.release``.
  104. This directory must be placed with its original name next to the Godot export templates.
  105. When exporting a project, Godot will also copy this directory with the game executable but
  106. the name will be changed to ``data_<APPNAME>``, where ``<APPNAME>`` is the application name
  107. as specified in the project setting ``application/config/name``.
  108. In the case of macOS, where the export template is compressed as a zip file, the
  109. contents of the data directory can be placed in the following locations inside the zip:
  110. +-------------------------------------------------------+---------------------------------------------------------------+
  111. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  112. +-------------------------------------------------------+---------------------------------------------------------------+
  113. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  114. +-------------------------------------------------------+---------------------------------------------------------------+
  115. Editor
  116. ^^^^^^
  117. The name of the data directory for the Godot editor will always be ``GodotSharp``.
  118. The contents of this directory are the following:
  119. - ``Api`` (optional)
  120. - ``Mono`` (optional)
  121. - ``Tools`` (required)
  122. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotSharpTools`` assembly.
  123. 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.
  124. 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.
  125. The following is an example script for building and copying the Godot API assemblies:
  126. .. tabs::
  127. .. code-tab:: bash Bash
  128. DATA_API_DIR=./bin/GodotSharp/Api
  129. SOLUTION_DIR=/tmp/build_GodotSharp
  130. BUILD_CONFIG=Release
  131. # Generate the solution
  132. ./bin/<godot_binary> --generate-cs-api $SOLUTION_DIR
  133. # Build the solution
  134. msbuild $SOLUTION_DIR/GodotSharp.sln /p:Configuration=$BUILD_CONFIG
  135. # Copy the built files
  136. mkdir -p $DATA_API_DIR
  137. cp $SOLUTION_DIR/GodotSharp/bin/$BUILD_CONFIG/{GodotSharp.dll,GodotSharp.pdb,GodotSharp.xml} $DATA_API_DIR
  138. cp $SOLUTION_DIR/GodotSharpEditor/bin/$BUILD_CONFIG/{GodotSharpEditor.dll,GodotSharpEditor.pdb,GodotSharpEditor.xml} $DATA_API_DIR
  139. .. code-tab:: batch Batch
  140. set DATA_API_DIR=.\bin\GodotSharp\Api
  141. set SOLUTION_DIR=%Temp%\build_GodotSharp
  142. set BUILD_CONFIG=Release
  143. # Generate the solution
  144. .\bin\<godot_binary> --generate-cs-api %SOLUTION_DIR%
  145. # Build the solution
  146. msbuild %SOLUTION_DIR%\GodotSharp.sln /p:Configuration=%BUILD_CONFIG%
  147. # Copy the built files
  148. if not exist "%DATA_API_DIR%" mkdir %DATA_API_DIR%
  149. for %%I in (GodotSharp.dll GodotSharp.pdb GodotSharp.xml) do copy %SOLUTION_DIR%\GodotSharp\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  150. for %%I in (GodotSharpEditor.dll GodotSharpEditor.pdb GodotSharpEditor.xml) do copy %SOLUTION_DIR%\GodotSharpEditor\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  151. The script assumes it's being executed from the directory where SConstruct is located.
  152. ``<godot_binary>`` refers to the tools binary compiled with the Mono module enabled.
  153. The ``Api`` subdirectory contains the Godot API assemblies.
  154. 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:
  155. +-------------------------------------------------------+---------------------------------------------------------------+
  156. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  157. +-------------------------------------------------------+---------------------------------------------------------------+
  158. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  159. +-------------------------------------------------------+---------------------------------------------------------------+
  160. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  161. +-------------------------------------------------------+---------------------------------------------------------------+
  162. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  163. +-------------------------------------------------------+---------------------------------------------------------------+
  164. The ``Mono`` subdirectory is optional but will be needed when distributing the editor, as some issues might arise
  165. when the installed Mono version in the user's system is not be the same as the one the Godot editor was built with.
  166. Pass ``copy_mono_root=yes`` to SCons when building the editor in order to create this folder and its contents.
  167. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotTools`` assemblies and its dependencies.
  168. Command-line options
  169. --------------------
  170. The following is the list of command-line options available when building with the mono module:
  171. - **module_mono_enabled**: Build Godot with the mono module enabled ( yes | **no** )
  172. - **mono_glue**: Whether to include the glue source files in the build and define `MONO_GLUE_DISABLED` as a preprocessor macro ( **yes** | no )
  173. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available ( yes | **no** )
  174. - **mono_static**: Whether to link the mono runtime statically ( yes | **no** )
  175. - **copy_mono_root**: Whether to copy the Mono framework assemblies and configuration files required by the Godot editor ( yes | **no** )