compiling_with_mono.rst 17 KB

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