compiling_with_dotnet.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. .. _doc_compiling_with_dotnet:
  2. Compiling with .NET
  3. ===================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. - `.NET SDK 8.0+ <https://dotnet.microsoft.com/download>`_
  8. You can use ``dotnet --info`` to check which .NET SDK versions are installed.
  9. Enable the .NET module
  10. ----------------------
  11. .. note:: C# support for Godot has historically used the
  12. `Mono <https://www.mono-project.com/>`_ runtime instead of the
  13. `.NET Runtime <https://github.com/dotnet/runtime>`_ and internally
  14. many things are still named ``mono`` instead of ``dotnet`` or
  15. otherwise referred to as ``mono``.
  16. By default, the .NET module is disabled when building. To enable it, add the
  17. option ``module_mono_enabled=yes`` to the SCons command line, while otherwise
  18. following the instructions for building the desired Godot binaries.
  19. Generate the glue
  20. -----------------
  21. Parts of the sources of the managed libraries are generated from the ClassDB.
  22. These source files must be generated before building the managed libraries.
  23. They can be generated by any .NET-enabled Godot editor binary by running it with
  24. the parameters ``--headless --generate-mono-glue`` followed by the path to an
  25. output directory.
  26. This path must be ``modules/mono/glue`` in the Godot directory::
  27. <godot_binary> --headless --generate-mono-glue modules/mono/glue
  28. This command will tell Godot to generate the C# bindings for the Godot API at
  29. ``modules/mono/glue/GodotSharp/GodotSharp/Generated``, and the C# bindings for
  30. the editor tools at ``modules/mono/glue/GodotSharp/GodotSharpEditor/Generated``.
  31. Once these files are generated, you can build Godot's managed libraries for all
  32. the desired targets without having to repeat this process.
  33. ``<godot_binary>`` refers to the editor binary you compiled with the .NET module
  34. enabled. Its exact name will differ based on your system and configuration, but
  35. should be of the form ``bin/godot.<platform>.editor.<arch>.mono``, e.g.
  36. ``bin/godot.linuxbsd.editor.x86_64.mono`` or
  37. ``bin/godot.windows.editor.x86_32.mono.exe``. Be especially aware of the
  38. **.mono** suffix! If you've previously compiled Godot without .NET support, you
  39. might have similarly named binaries without this suffix. These binaries can't be
  40. used to generate the .NET glue.
  41. .. note:: The glue sources must be regenerated every time the ClassDB-registered
  42. API changes. That is, for example, when a new method is registered to
  43. the scripting API or one of the parameters of such a method changes.
  44. Godot will print an error at startup if there is an API mismatch
  45. between ClassDB and the glue sources.
  46. Building the managed libraries
  47. ------------------------------
  48. Once you have generated the .NET glue, you can build the managed libraries with
  49. the ``build_assemblies.py`` script::
  50. ./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin
  51. If everything went well, the ``GodotSharp`` directory, containing the managed
  52. libraries, should have been created in the ``bin`` directory.
  53. .. note:: By default, all development builds share a version number, which can
  54. cause some issues with caching of the NuGet packages. To solve this
  55. issue either use ``GODOT_VERSION_STATUS`` to give every build a unique
  56. version or delete ``GodotNuGetFallbackFolder`` after every build to
  57. clear the package cache.
  58. Unlike "classical" Godot builds, when building with the .NET module enabled
  59. (and depending on the target platform), a data directory may be created both
  60. for the editor and for exported projects. This directory is important for
  61. proper functioning and must be distributed together with Godot.
  62. More details about this directory in
  63. :ref:`Data directory<compiling_with_dotnet_data_directory>`.
  64. Build Platform
  65. ^^^^^^^^^^^^^^
  66. Provide the ``--godot-platform=<platform>`` argument to control for which
  67. platform specific the libraries are built. Omit this argument to build for the
  68. current system.
  69. This currently only controls the inclusion of the support for Visual Studio as
  70. an external editor, the libraries are otherwise identical.
  71. NuGet packages
  72. ^^^^^^^^^^^^^^
  73. The API assemblies, source generators, and custom MSBuild project SDK are
  74. distributed as NuGet packages. This is all transparent to the user, but it can
  75. make things complicated during development.
  76. In order to use Godot with a development version of those packages, a local
  77. NuGet source must be created where MSBuild can find them.
  78. First, pick a location for the local NuGet source. If you don't have a
  79. preference, create an empty directory at one of these recommended locations:
  80. - On Windows, ``C:\Users\<username>\MyLocalNugetSource``
  81. - On Linux, \*BSD, etc., ``~/MyLocalNugetSource``
  82. This path is referred to later as ``<my_local_source>``.
  83. After picking a directory, run this .NET CLI command to configure NuGet to use
  84. your local source:
  85. ::
  86. dotnet nuget add source <my_local_source> --name MyLocalNugetSource
  87. When you run the ``build_assemblies.py`` script, pass ``<my_local_source>`` to
  88. the ``--push-nupkgs-local`` option:
  89. ::
  90. ./modules/mono/build_scripts/build_assemblies.py --godot-output-dir ./bin --push-nupkgs-local <my_local_source>
  91. This option ensures the packages will be added to the specified local NuGet
  92. source and that conflicting versions of the package are removed from the NuGet
  93. cache. It's recommended to always use this option when building the C# solutions
  94. during development to avoid mistakes.
  95. Double Precision Support (REAL_T_IS_DOUBLE)
  96. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  97. When building Godot with double precision support, i.e. the ``precision=double``
  98. argument for scons, the managed libraries must be adjusted to match by passing
  99. the ``--precision=double`` argument:
  100. ::
  101. ./modules/mono/build_scripts/build_assemblies.py --godot-output-dir ./bin --push-nupkgs-local <my_local_source> --precision=double
  102. Examples
  103. --------
  104. Example (Windows)
  105. ^^^^^^^^^^^^^^^^^
  106. ::
  107. # Build editor binary
  108. scons platform=windows target=editor module_mono_enabled=yes
  109. # Build export templates
  110. scons platform=windows target=template_debug module_mono_enabled=yes
  111. scons platform=windows target=template_release module_mono_enabled=yes
  112. # Generate glue sources
  113. bin/godot.windows.editor.x86_64.mono --headless --generate-mono-glue modules/mono/glue
  114. # Build .NET assemblies
  115. ./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin --godot-platform=windows
  116. Example (Linux, \*BSD)
  117. ^^^^^^^^^^^^^^^^^^^^^^
  118. ::
  119. # Build editor binary
  120. scons platform=linuxbsd target=editor module_mono_enabled=yes
  121. # Build export templates
  122. scons platform=linuxbsd target=template_debug module_mono_enabled=yes
  123. scons platform=linuxbsd target=template_release module_mono_enabled=yes
  124. # Generate glue sources
  125. bin/godot.linuxbsd.editor.x86_64.mono --headless --generate-mono-glue modules/mono/glue
  126. # Generate binaries
  127. ./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin --godot-platform=linuxbsd
  128. .. _compiling_with_dotnet_data_directory:
  129. Data directory
  130. --------------
  131. The data directory is a dependency for Godot binaries built with the .NET module
  132. enabled. It contains important files for the correct functioning of Godot. It
  133. must be distributed together with the Godot executable.
  134. Editor
  135. ^^^^^^
  136. The name of the data directory for the Godot editor will always be
  137. ``GodotSharp``. This directory contains an ``Api`` subdirectory with the Godot
  138. API assemblies and a ``Tools`` subdirectory with the tools required by the
  139. editor, like the ``GodotTools`` assemblies and its dependencies.
  140. On macOS, if the Godot editor is distributed as a bundle, the ``GodotSharp``
  141. directory may be placed in the ``<bundle_name>.app/Contents/Resources/``
  142. directory inside the bundle.
  143. Export templates
  144. ^^^^^^^^^^^^^^^^
  145. The data directory for exported projects is generated by the editor during the
  146. export. It is named ``data_<APPNAME>_<ARCH>``, where ``<APPNAME>`` is the
  147. application name as specified in the project setting ``application/config/name``
  148. and ``<ARCH>`` is the current architecture of the export.
  149. In the case of multi-architecture exports multiple such data directories will be
  150. generated.
  151. Command-line options
  152. --------------------
  153. The following is the list of command-line options available when building with
  154. the .NET module:
  155. - **module_mono_enabled**\ =yes | **no**
  156. - Build Godot with the .NET module enabled.