compiling_with_dotnet.rst 8.8 KB

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