compiling_with_mono.rst 17 KB

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