compiling_with_mono.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. - NuGet
  12. You may need to import necessary certificates for NuGet to perform HTTPS requests. You can do this 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. You can specify a different installation directory by using the following environment variables for the respective ``bits`` option: ``MONO32_PREFIX`` and ``MONO64_PREFIX``.
  18. The specified directory must contain the subdirectories ``bin``, ``include``, and ``lib``.
  19. Enable the Mono module
  20. ----------------------
  21. By default, the mono module is disabled for builds. To enable it you can pass the option ``module_mono_enabled=yes`` to your SCons command.
  22. Generate the glue
  23. -------------------
  24. The glue sources are the wrapper functions that will be called by managed methods. These source files must be generated before building your final binaries. In order to generate them, first, you must build a temporary Godot binary with the options ``tools=yes`` and ``mono_glue=no``:
  25. ::
  26. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=no
  27. After the build finishes, you need to run the compiled executable with the parameter ``--generate-mono-glue`` followed by the path to an output directory. This path must be ``modules/mono/glue`` in the Godot directory.
  28. ::
  29. <godot_binary> --generate-mono-glue modules/mono/glue
  30. This command will tell Godot to generate the file ``modules/mono/glue/mono_glue.gen.cpp``.
  31. Once this file is generated, you can build Godot for all the desired targets without the need to repeat this process.
  32. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono module enabled.
  33. Its exact name will differ based on your system and configuration, but should be of the form ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono`` or ``bin/godot.windows.tools.64.exe``.
  34. Be especially aware of the **.mono** suffix! If you compiled Godot without Mono support previously, you might have similarly named binaries without this suffix which can't be used to generate the Mono glue.
  35. Notes
  36. ^^^^^
  37. - **Do not** build your final binaries with ``mono_glue=no``. This disables C# scripting. This option must be used only for the temporary binary that will generate the glue. Godot will print a warning at startup if it was built without the glue sources.
  38. - The glue sources must be regenerated every time the ClassDB bindings changes. That is, for example, when a new method is added to ClassDB or one of the parameter of such a method changes. Godot will print an error at startup if there is an API mismatch between ClassDB and the glue sources.
  39. Rebuild with Mono glue
  40. ----------------------
  41. Once you have generated the Mono glue, you can build the final binary with ``mono_glue=yes``.
  42. It's the default value for ``mono_glue`` so you can also omit it. You can build the Mono-enabled editor:
  43. ::
  44. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  45. And Mono-enabled export templates:
  46. ::
  47. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  48. If everything went well, apart from the normal output SCons should have created the following files in the ``bin`` directory:
  49. - If you're not static linking the Mono runtime, the build script will place the Mono runtime shared library next to the Godot binary.
  50. - Unlike "classical" Godot builds, when building with the mono module enabled a data directory will be created both for the editor and for export templates. This directory is important for proper functioning and must be distributed together with Godot. More details about this directory in :ref:`Data directory<compiling_with_mono_data_directory>`.
  51. Examples
  52. --------
  53. Example (Windows)
  54. ^^^^^^^^^^^^^^^^^
  55. ::
  56. # Build temporary binary
  57. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  58. # Generate glue sources
  59. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  60. ### Build binaries normally
  61. # Editor
  62. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  63. # Export templates
  64. scons p=windows target=debug tools=no module_mono_enabled=yes
  65. scons p=windows target=release tools=no module_mono_enabled=yes
  66. Example (X11)
  67. ^^^^^^^^^^^^^
  68. ::
  69. # Build temporary binary
  70. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  71. # Generate glue sources
  72. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  73. ### Build binaries normally
  74. # Editor
  75. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  76. # Export templates
  77. scons p=x11 target=debug tools=no module_mono_enabled=yes
  78. scons p=x11 target=release tools=no module_mono_enabled=yes
  79. .. _compiling_with_mono_data_directory:
  80. Data directory
  81. --------------
  82. The data directory is a dependency for Godot binaries built with the mono module enabled. It contains files that are important for the correct functioning of Godot. It must be distributed next to the Godot executable.
  83. Export templates
  84. ^^^^^^^^^^^^^^^^
  85. The name of the data directory for a export template differs based on the configuration it was built with. The format is ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.debug`` or ``data.mono.windows.64.release``.
  86. In the case of export templates the data directory only contains Mono framework assemblies and configuration files, as well as some shared library dependencies like ``MonoPosixHelper``.
  87. This directory must be placed with its original name next to the Godot export templates. When exporting a project, Godot will also copy this directory with the game executable but the name will be changed to ``data_<APPNAME>``, where ``<APPNAME>`` is the application name as specified in the project setting ``application/config/name``.
  88. In the case of macOS, where the export template is compressed as a zip file, the contents of the data directory can be placed in the following locations inside the zip:
  89. +-------------------------------------------------------+---------------------------------------------------------------+
  90. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  91. +-------------------------------------------------------+---------------------------------------------------------------+
  92. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  93. +-------------------------------------------------------+---------------------------------------------------------------+
  94. Editor
  95. ^^^^^^^^
  96. The name of the data directory for the Godot editor will always be ``GodotSharp``. The main structure of this directory has the following subdirectories:
  97. - ``Api`` (optional)
  98. - ``Mono`` (optional)
  99. - ``Tools`` (required)
  100. The ``Tools`` subdirectory contains tools required by the editor, like the ``GodotSharpTools`` assembly.
  101. 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.
  102. 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.
  103. The following is an example script for building and copying the Godot API assemblies:
  104. .. tabs::
  105. .. code-tab:: bash Bash
  106. DATA_API_DIR=./bin/GodotSharp/Api
  107. SOLUTION_DIR=/tmp/build_GodotSharp
  108. BUILD_CONFIG=Release
  109. # Generate the solution
  110. ./bin/<godot_binary> --generate-cs-api $SOLUTION_DIR
  111. # Build the solution
  112. msbuild $SOLUTION_DIR/GodotSharp.sln /p:Configuration=$BUILD_CONFIG
  113. # Copy the built files
  114. mkdir -p $DATA_API_DIR
  115. cp $SOLUTION_DIR/GodotSharp/bin/$BUILD_CONFIG/{GodotSharp.dll,GodotSharp.pdb,GodotSharp.xml} $DATA_API_DIR
  116. cp $SOLUTION_DIR/GodotSharpEditor/bin/$BUILD_CONFIG/{GodotSharpEditor.dll,GodotSharpEditor.pdb,GodotSharpEditor.xml} $DATA_API_DIR
  117. .. code-tab:: batch Batch
  118. set DATA_API_DIR=.\bin\GodotSharp\Api
  119. set SOLUTION_DIR=%Temp%\build_GodotSharp
  120. set BUILD_CONFIG=Release
  121. # Generate the solution
  122. .\bin\<godot_binary> --generate-cs-api %SOLUTION_DIR%
  123. # Build the solution
  124. msbuild %SOLUTION_DIR%\GodotSharp.sln /p:Configuration=%BUILD_CONFIG%
  125. # Copy the built files
  126. if not exist "%DATA_API_DIR%" mkdir %DATA_API_DIR%
  127. for %%I in (GodotSharp.dll GodotSharp.pdb GodotSharp.xml) do copy %SOLUTION_DIR%\GodotSharp\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  128. for %%I in (GodotSharpEditor.dll GodotSharpEditor.pdb GodotSharpEditor.xml) do copy %SOLUTION_DIR%\GodotSharpEditor\bin\%BUILD_CONFIG%\%%I %DATA_API_DIR%
  129. The script assumes it's being executed from the directory where SConstruct is located.
  130. ``<godot_binary>`` refers to the tools binary compiled with the Mono module enabled.
  131. 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:
  132. +-------------------------------------------------------+---------------------------------------------------------------+
  133. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  134. +-------------------------------------------------------+---------------------------------------------------------------+
  135. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  136. +-------------------------------------------------------+---------------------------------------------------------------+
  137. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  138. +-------------------------------------------------------+---------------------------------------------------------------+
  139. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  140. +-------------------------------------------------------+---------------------------------------------------------------+
  141. Command-line options
  142. --------------------
  143. The following is the list of command-line options available when building with the mono module:
  144. - **module_mono_enabled**: Build Godot with the mono module enabled ( yes | **no** )
  145. - **mono_glue**: Whether to include the glue source files in the build and define `MONO_GLUE_DISABLED` as a preprocessor macro ( **yes** | no )
  146. - **xbuild_fallback**: Whether to fallback to xbuild if MSBuild is not available ( yes | **no** )
  147. - **mono_static**: Whether to link the mono runtime statically ( yes | **no** )
  148. - **copy_mono_root**: Whether to copy the Mono framework assemblies and configuration files required by the Godot editor ( yes | **no** )