compiling_with_mono.rst 17 KB

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