compiling_with_mono.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. .. _doc_compiling_with_mono:
  2. Compiling with Mono
  3. ===================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. - Mono 6.12.0 or greater
  8. - MSBuild
  9. - NuGet
  10. - **On Linux/macOS only:** pkg-config
  11. You may need to import necessary certificates for NuGet to perform HTTPS
  12. requests.
  13. The recommended method is to use **curl**'s CA (Certificate Authorities) certificate bundle.
  14. Run the following commands to download and import it. On Windows, you can run it
  15. from the Mono command line prompt (or the regular prompt if you added Mono's
  16. ``bin`` directory to your ``PATH`` environment variable)::
  17. # If using PowerShell, replace `curl` with `curl.exe` below.
  18. curl -LO https://curl.haxx.se/ca/cacert.pem
  19. cert-sync --user cacert.pem
  20. Alternatively, you can use the following command, though it's deprecated and may not work correctly::
  21. mozroots --import --sync
  22. Environment variables
  23. ---------------------
  24. By default, SCons will try to find Mono in the Windows Registry on Windows or
  25. via ``pkg-config`` on other platforms. You can specify a different installation
  26. directory by passing the ``mono_prefix`` command-line option to SCons; e.g.
  27. ``scons [...] mono_prefix=%ProgramFiles%/Mono``.
  28. This is the directory that contains the subdirectories ``include`` and ``lib``.
  29. Enable the Mono module
  30. ----------------------
  31. By default, the Mono module is disabled when building. To enable it, add the
  32. option ``module_mono_enabled=yes`` to the SCons command line.
  33. Generate the glue
  34. -----------------
  35. Glue sources are the wrapper functions that will be called by managed methods.
  36. These source files must be generated before building your final binaries. In
  37. order to generate them, first, you must build a temporary Godot binary with the
  38. options ``tools=yes`` and ``mono_glue=no``::
  39. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=no
  40. After the build finishes, you need to run the compiled executable with the
  41. parameter ``--generate-mono-glue`` followed by the path to an output directory.
  42. This path must be ``modules/mono/glue`` in the Godot directory::
  43. <godot_binary> --generate-mono-glue modules/mono/glue
  44. This command will tell Godot to generate the file ``modules/mono/glue/mono_glue.gen.cpp``
  45. and the C# solution for the Godot API at ``modules/mono/glue/Managed/Generated``.
  46. Once these files are generated, you can build Godot for all the desired targets
  47. without having to repeat this process.
  48. ``<godot_binary>`` refers to the tools binary you compiled above with the Mono
  49. module enabled. Its exact name will differ based on your system and
  50. configuration, but should be of the form
  51. ``bin/godot.<platform>.tools.<bits>.mono``, e.g. ``bin/godot.x11.tools.64.mono``
  52. or ``bin/godot.windows.tools.64.mono.exe``. Be especially aware of the **.mono**
  53. suffix! If you've previously compiled Godot without Mono support, you might have
  54. similarly named binaries without this suffix. These binaries can't be used to
  55. generate the Mono glue.
  56. Notes
  57. ^^^^^
  58. - **Do not build your final binaries with** ``mono_glue=no``.
  59. This disables C# scripting. This option must be used only for the temporary
  60. binary that will generate the glue. Godot will print a warning at startup if
  61. it was built without the glue sources.
  62. - The glue sources must be regenerated every time the ClassDB-registered API
  63. changes. That is, for example, when a new method is registered to the
  64. scripting API or one of the parameters of such a method changes.
  65. Godot will print an error at startup if there is an API mismatch
  66. between ClassDB and the glue sources.
  67. Rebuild with Mono glue
  68. ----------------------
  69. Once you have generated the Mono glue, you can build the final binary with
  70. ``mono_glue=yes``. This is the default value for ``mono_glue``, so you can also
  71. omit it. To build a Mono-enabled editor::
  72. scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes
  73. And Mono-enabled export templates::
  74. scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes
  75. If everything went well, apart from the normal output, SCons should have created
  76. the following files in the ``bin`` directory:
  77. - If you're not linking the Mono runtime statically, the build script will place
  78. the Mono runtime shared library (``monosgen-2.0``) next to the Godot
  79. binary in the output directory. Make sure to include this library when
  80. distributing Godot. When targeting Android, no extra steps are required as
  81. this library is automatically copied to ``#platform/android/java/libs`` and
  82. Gradle takes care of the rest.
  83. - Unlike "classical" Godot builds, when building with the Mono module enabled
  84. (and depending on the target platform), a data directory may be created both
  85. for the editor and for export templates. This directory is important for
  86. proper functioning and must be distributed together with Godot.
  87. More details about this directory in
  88. :ref:`Data directory<compiling_with_mono_data_directory>`.
  89. Examples
  90. --------
  91. Example (Windows)
  92. ^^^^^^^^^^^^^^^^^
  93. ::
  94. # Build temporary binary
  95. scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
  96. # Generate glue sources
  97. bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue
  98. ### Build binaries normally
  99. # Editor
  100. scons p=windows target=release_debug tools=yes module_mono_enabled=yes
  101. # Export templates
  102. scons p=windows target=release_debug tools=no module_mono_enabled=yes
  103. scons p=windows target=release tools=no module_mono_enabled=yes
  104. Example (X11)
  105. ^^^^^^^^^^^^^
  106. ::
  107. # Build temporary binary
  108. scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
  109. # Generate glue sources
  110. bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
  111. ### Build binaries normally
  112. # Editor
  113. scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
  114. # Export templates
  115. scons p=x11 target=release_debug tools=no module_mono_enabled=yes
  116. scons p=x11 target=release tools=no module_mono_enabled=yes
  117. .. _compiling_with_mono_data_directory:
  118. Data directory
  119. --------------
  120. The data directory is a dependency for Godot binaries built with the Mono module
  121. enabled. It contains important files for the correct functioning of Godot. It
  122. must be distributed together with the Godot executable.
  123. .. note:: The information below doesn't apply for Android, iOS and WASM,
  124. as there is no data directory for these platforms.
  125. Export templates
  126. ^^^^^^^^^^^^^^^^
  127. The name of the data directory for an export template differs based on the
  128. configuration it was built with. The format is
  129. ``data.mono.<platform>.<bits>.<target>``, e.g. ``data.mono.x11.32.release_debug`` or
  130. ``data.mono.windows.64.release``.
  131. This directory must be placed with its original name next to the Godot export
  132. templates. When exporting a project, Godot will also copy this directory with
  133. the game executable but the name will be changed to ``data_<APPNAME>``, where
  134. ``<APPNAME>`` is the application name as specified in the project setting
  135. ``application/config/name``.
  136. In the case of macOS, where the export template is compressed as a ZIP archive,
  137. the contents of the data directory can be placed in the following locations
  138. inside the ZIP archive:
  139. +-------------------------------------------------------+---------------------------------------------------------------+
  140. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``/osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  141. +-------------------------------------------------------+---------------------------------------------------------------+
  142. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``/osx_template.app/Contents/Resources/GodotSharp/Mono/etc`` |
  143. +-------------------------------------------------------+---------------------------------------------------------------+
  144. Editor
  145. ^^^^^^
  146. The name of the data directory for the Godot editor will always be
  147. ``GodotSharp``. The contents of this directory are the following:
  148. - ``Api``
  149. - ``Mono`` (optional)
  150. - ``Tools``
  151. The ``Api`` subdirectory contains the Godot API assemblies. On macOS, if the
  152. Godot editor is distributed as a bundle, the contents of the data directory may
  153. be placed in the following locations:
  154. +-------------------------------------------------------+---------------------------------------------------------------+
  155. | ``bin/data.mono.<platform>.<bits>.<target>/Api`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Api`` |
  156. +-------------------------------------------------------+---------------------------------------------------------------+
  157. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/lib`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib`` |
  158. +-------------------------------------------------------+---------------------------------------------------------------+
  159. | ``bin/data.mono.<platform>.<bits>.<target>/Mono/etc`` | ``<bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc`` |
  160. +-------------------------------------------------------+---------------------------------------------------------------+
  161. | ``bin/data.mono.<platform>.<bits>.<target>/Tools`` | ``<bundle_name>.app/Contents/Frameworks/GodotSharp/Tools`` |
  162. +-------------------------------------------------------+---------------------------------------------------------------+
  163. The ``Mono`` subdirectory is optional. It will be needed when distributing the
  164. editor, as issues can arise when the user-installed Mono version isn't identical
  165. to the one the Godot editor was built with. Pass ``copy_mono_root=yes`` to SCons
  166. 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
  168. ``GodotTools`` assemblies and its dependencies.
  169. Building the Mono runtime
  170. -------------------------
  171. When building Godot for the desktop, you will likely use the pre-built Mono runtime
  172. that is installed on your system. This likely won't be the case when targeting other
  173. platforms like Android, iOS and WebAssembly. You will have to build the Mono runtime
  174. yourself for those platforms.
  175. We recommend using these `build scripts <https://github.com/godotengine/godot-mono-builds>`_.
  176. They simplify this process but also include some patches needed
  177. for proper functioning with Godot. See the README on the link above
  178. for instructions on how to use the scripts.
  179. Targeting Android
  180. -----------------
  181. Compiling the Android export templates with Mono is a bit simpler than it is for
  182. the desktop platforms, as there are no additional steps required after building.
  183. There is no need to worry about run-time dependencies like a data directory or
  184. the shared library (when dynamically linking) as those are automatically added
  185. to the Gradle project.
  186. Once you've built Mono, you can proceed to build Godot with the instructions
  187. described in this page and the
  188. :ref:`Compiling for Android<doc_compiling_for_android>` page.
  189. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.:
  190. ``scons [...] mono_prefix="$HOME/mono-installs/android-armeabi-v7a-release"``
  191. (This path may be different on your system).
  192. Targeting iOS
  193. -------------
  194. Once you've built Mono, you can proceed to build Godot with the instructions
  195. described in this page and the
  196. :ref:`Compiling for iOS<doc_compiling_for_ios>` page.
  197. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.:
  198. ``scons [...] mono_prefix="$HOME/mono-installs/ios-arm64-release"``
  199. (This path may be different on your system).
  200. After building Godot for each architecture, you will notice SCons has
  201. copied the Mono libraries for each of them to the output directory:
  202. ::
  203. #bin/libmono-native.iphone.<arch>.a
  204. #bin/libmonosgen-2.0.iphone.<arch>.a
  205. #bin/libmonoprofiler-log.iphone.<arch>.a
  206. #bin/libmono-ilgen.iphone.<arch>.a
  207. #bin/libmono-ee-interp.iphone.<arch>.a
  208. #bin/libmono-icall-table.iphone.<arch>.a
  209. The last three are only for iOS devices and are not available for the iOS simulator.
  210. These libraries must be put in universal (multi-architecture) "fat"
  211. files to be distributed with the export templates.
  212. The following bash script will create the "fat" libraries in the directory ``#bin/ios/iphone-mono-libs``:
  213. ::
  214. mkdir -p bin/ios
  215. mkdir -p bin/ios/iphone-mono-libs
  216. lipo -create bin/libmonosgen-2.0.iphone.arm64.a bin/libmonosgen-2.0.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmonosgen-2.0.iphone.fat.a
  217. lipo -create bin/libmono-native.iphone.arm64.a bin/libmono-native.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmono-native.iphone.fat.a
  218. lipo -create bin/libmono-profiler-log.iphone.arm64.a bin/libmono-profiler-log.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmono-profiler-log.iphone.fat.a
  219. # The Mono libraries for the interpreter are not available for simulator builds
  220. lipo -create bin/libmono-ee-interp.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-ee-interp.iphone.fat.a
  221. lipo -create bin/libmono-icall-table.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-icall-table.iphone.fat.a
  222. lipo -create bin/libmono-ilgen.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-ilgen.iphone.fat.a
  223. The ``iphone-mono-libs`` folder must be distributed with the export templates.
  224. The Godot editor will look for the libraries in ``<templates>/iphone-mono-libs/lib<name>.iphone.fat.a``.
  225. Targeting WebAssembly
  226. ---------------------
  227. Building for WebAssembly currently involves the same process regardless of whether the Mono module is enabled.
  228. Once you've built Mono, you can proceed to build Godot with the instructions
  229. described in this page and the
  230. :ref:`Compiling for the Web<doc_compiling_for_web>` page.
  231. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.:
  232. ``scons [...] mono_prefix="$HOME/mono-installs/wasm-runtime-release"``
  233. (This path may be different on your system).
  234. Base Class Library
  235. ------------------
  236. The export templates must also include the BCL (Base Class Library) for each target platform.
  237. Godot looks for the BCL folder at ``<templates>/bcl/<target_platform>``,
  238. where ``<target_platform>`` is the same name passed to the SCons ``platform`` option,
  239. e.g.: ``<templates>/bcl/windows``, ``<templates>/bcl/javascript``.
  240. Alternatively, Godot will look for them in the following locations:
  241. +-------------------+---------------------------------+
  242. | Android | ``<templates>/bcl/monodroid`` |
  243. +-------------------+---------------------------------+
  244. | iOS | ``<templates>/bcl/monotouch`` |
  245. +-------------------+---------------------------------+
  246. | WebAssembly | ``<templates>/bcl/wasm`` |
  247. +-------------------+---------------------------------+
  248. | Linux and macOS | ``<templates>/bcl/net_4_x`` |
  249. +-------------------+---------------------------------+
  250. | Windows | ``<templates>/bcl/net_4_x_win`` |
  251. +-------------------+---------------------------------+
  252. As of now, we're assuming the same BCL profile can be used for both Linux and macOS,
  253. but this may change in the future as they're not guaranteed to be the same
  254. (as is the case with the Windows BCL).
  255. If the target platform is the same as the platform of the Godot editor,
  256. then the editor will use the BCL it's running on (``<data_folder>/Mono/lib/mono/4.5``)
  257. if it cannot find the BCL in the export templates.
  258. AOT cross-compilers
  259. -------------------
  260. To perform ahead-of-time (AOT) compilation for other platforms, Godot needs to have
  261. access to the Mono cross-compilers for that platform and architecture.
  262. Godot will look for the cross-compiler executable in the AOT compilers folder.
  263. The location of this folder is ``<data_folder>/Tools/aot-compilers/``.
  264. In order to build the cross-compilers we recommend using these
  265. `build scripts <https://github.com/godotengine/godot-mono-builds>`_.
  266. After building them, copy the executable to the Godot AOT compilers directory. The
  267. executable name is ``<triple>-mono-sgen``, e.g.: ``aarch64-apple-darwin-mono-sgen``.
  268. Command-line options
  269. --------------------
  270. The following is the list of command-line options available when building with
  271. the Mono module:
  272. - **module_mono_enabled**\ =yes | **no**
  273. - Build Godot with the Mono module enabled.
  274. - **mono_glue**\ =\ **yes** | no
  275. - Whether to include the glue source files in the build
  276. and define ``MONO_GLUE_DISABLED`` as a preprocessor macro.
  277. - **mono_prefix**\ =path
  278. - Path to the Mono installation directory for the target platform and architecture.
  279. - **mono_static**\ =yes | no
  280. - Whether to link the Mono runtime statically.
  281. - The default is **yes** for iOS and WASM, and **no** for other platforms.
  282. - **copy_mono_root**\ =yes | **no**
  283. - Whether to copy the Mono framework assemblies
  284. and configuration files required by the Godot editor.