compiling_for_windows.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. .. _doc_compiling_for_windows:
  2. Compiling for Windows
  3. =====================
  4. .. highlight:: shell
  5. .. seealso::
  6. This page describes how to compile Windows editor and export template binaries from source.
  7. If you're looking to export your project to Windows instead, read :ref:`doc_exporting_for_windows`.
  8. Requirements
  9. ------------
  10. For compiling under Windows, the following is required:
  11. - `Visual Studio Community <https://www.visualstudio.com/vs/community/>`_,
  12. version 2017 or later. VS 2019 is recommended.
  13. **Make sure to read "Installing Visual Studio caveats" below or you
  14. will have to run/download the installer again.**
  15. - `MinGW-w64 <https://mingw-w64.org/>`_ with GCC can be used as an alternative to
  16. Visual Studio. Be sure to install/configure it to use the ``posix`` thread model.
  17. **Important:** When using MinGW to compile the ``master`` branch, you need GCC 9 or later.
  18. - `Python 3.6+ <https://www.python.org/downloads/windows/>`_.
  19. **Make sure to enable the option to add Python to the ``PATH`` in the installer.**
  20. - `SCons 3.0+ <https://scons.org/pages/download.html>`_ build system. Using the
  21. latest release is recommended, especially for proper support of recent Visual
  22. Studio releases.
  23. .. note:: If you have `Scoop <https://scoop.sh/>`_ installed, you can easily
  24. install MinGW and other dependencies using the following command::
  25. scoop install gcc python scons make mingw
  26. .. note:: If you have `MSYS2 <https://www.msys2.org/>`_ installed, you can easily
  27. install MinGW and other dependencies using the following command::
  28. pacman -S mingw-w64-x86_64-python3-pip mingw-w64-x86_64-gcc \
  29. mingw-w64-i686-python3-pip mingw-w64-i686-gcc make
  30. For each MSYS2 MinGW subsystem, you should then run
  31. `pip3 install scons` in its shell.
  32. .. seealso:: To get the Godot source code for compiling, see
  33. :ref:`doc_getting_source`.
  34. For a general overview of SCons usage for Godot, see
  35. :ref:`doc_introduction_to_the_buildsystem`.
  36. Setting up SCons
  37. ----------------
  38. To install SCons, open the command prompt and run the following command::
  39. python -m pip install scons
  40. If you are prompted with the message
  41. ``Defaulting to user installation because normal site-packages is not
  42. writeable``, you may have to run that command again using elevated
  43. permissions. Open a new command prompt as an Administrator then run the command
  44. again to ensure that SCons is available from the ``PATH``.
  45. To check whether you have installed Python and SCons correctly, you can
  46. type ``python --version`` and ``scons --version`` into a command prompt
  47. (``cmd.exe``).
  48. If the commands above don't work, make sure to add Python to your ``PATH``
  49. environment variable after installing it, then check again.
  50. You can do so by running the Python installer again and enabling the option
  51. to add Python to the ``PATH``.
  52. If SCons cannot detect your Visual Studio installation, it might be that your
  53. SCons version is too old. Update it to the latest version with
  54. ``python -m pip install --upgrade scons``.
  55. .. _doc_compiling_for_windows_install_vs:
  56. Installing Visual Studio caveats
  57. --------------------------------
  58. If installing Visual Studio 2017 or 2019, make sure to enable **C++** in
  59. the list of workflows to install.
  60. If installing Visual Studio 2015, make sure to run a **Custom**
  61. installation instead of **Typical** and select **C++** as a language there.
  62. If you've already made the mistake of installing Visual Studio without
  63. C++ support, run the installer again; it should present you a **Modify** button.
  64. Running the installer from *Add/Remove Programs* will only give you
  65. a **Repair** option, which won't let you install C++ tools.
  66. Downloading Godot's source
  67. --------------------------
  68. Refer to :ref:`doc_getting_source` for detailed instructions.
  69. The tutorial will assume from now on that you placed the source code in
  70. ``C:\godot``.
  71. .. warning::
  72. To prevent slowdowns caused by continuous virus scanning during compilation,
  73. add the Godot source folder to the list of exceptions in your antivirus
  74. software.
  75. For Windows Defender, hit the :kbd:`Windows` key, type "Windows Security"
  76. then hit :kbd:`Enter`. Click on **Virus & threat protection** on the left
  77. panel. Under **Virus & threat protection settings** click on **Mange Settings**
  78. and scroll down to **Exclusions**. Click **Add or remove exclusions** then
  79. add the Godot source folder.
  80. Compiling
  81. ---------
  82. Selecting a compiler
  83. ~~~~~~~~~~~~~~~~~~~~
  84. SCons will automatically find and use an existing Visual Studio installation.
  85. If you do not have Visual Studio installed, it will attempt to use
  86. MinGW instead. If you already have Visual Studio installed and want to
  87. use MinGW, pass ``use_mingw=yes`` to the SCons command line. Note that MSVC
  88. builds cannot be performed from the MSYS2 or MinGW shells. Use either
  89. ``cmd.exe`` or PowerShell instead.
  90. During development, using the Visual Studio compiler is usually a better idea,
  91. as it links the Godot binary much faster than MinGW. However, MinGW can
  92. produce more optimized binaries using link-time optimization (see below),
  93. making it a better choice for production use.
  94. Running SCons
  95. ~~~~~~~~~~~~~
  96. After opening a command prompt, change to the root directory of
  97. the engine source code (using ``cd``) and type::
  98. C:\godot> scons platform=windows
  99. .. note:: When compiling with multiple CPU threads, SCons may warn about
  100. pywin32 being missing. You can safely ignore this warning.
  101. If all goes well, the resulting binary executable will be placed in
  102. ``C:\godot\bin\`` with the name ``godot.windows.editor.x86_32.exe`` or
  103. ``godot.windows.editor.x86_64.exe``. By default, SCons will build a binary matching
  104. your CPU architecture, but this can be overridden using ``arch=x86_64`` or
  105. ``arch=x86_32``.
  106. This executable file contains the whole engine and runs without any
  107. dependencies. Running it will bring up the Project Manager.
  108. .. note:: If you are compiling Godot for production use, then you can
  109. make the final executable smaller and faster by adding the
  110. SCons option ``target=template_release``.
  111. If you are compiling Godot with MinGW, you can make the binary
  112. even smaller and faster by adding the SCons option ``lto=full``.
  113. As link-time optimization is a memory-intensive process,
  114. this will require about 7 GB of available RAM while compiling.
  115. .. note:: If you want to use separate editor settings for your own Godot builds
  116. and official releases, you can enable
  117. :ref:`doc_data_paths_self_contained_mode` by creating a file called
  118. ``._sc_`` or ``_sc_`` in the ``bin/`` folder.
  119. Development in Visual Studio
  120. ----------------------------
  121. Using an IDE is not required to compile Godot, as SCons takes care of everything.
  122. But if you intend to do engine development or debugging of the engine's C++ code,
  123. you may be interested in configuring a code editor or an IDE.
  124. Folder-based editors don't require any particular setup to start working with Godot's
  125. codebase. To edit projects with Visual Studio they need to be set up as a solution.
  126. You can create a Visual Studio solution via SCons by running SCons with
  127. the ``vsproj=yes`` parameter, like this::
  128. scons p=windows vsproj=yes
  129. You will be able to open Godot's source in a Visual Studio solution now,
  130. and able to build Godot using Visual Studio's **Build** button.
  131. .. seealso:: See :ref:`doc_configuring_an_ide_vs` for further details.
  132. Cross-compiling for Windows from other operating systems
  133. --------------------------------------------------------
  134. If you are a Linux or macOS user, you need to install
  135. `MinGW-w64 <https://mingw-w64.org/doku.php>`__, which typically comes in 32-bit
  136. and 64-bit variants. The package names may differ based on your distribution,
  137. here are some known ones:
  138. +----------------+--------------------------------------------------------------+
  139. | **Arch Linux** | :: |
  140. | | |
  141. | | pacman -Sy mingw-w64 |
  142. +----------------+--------------------------------------------------------------+
  143. | **Debian** / | :: |
  144. | **Ubuntu** | |
  145. | | apt install mingw-w64 |
  146. +----------------+--------------------------------------------------------------+
  147. | **Fedora** | :: |
  148. | | |
  149. | | dnf install mingw64-gcc-c++ mingw64-winpthreads-static \ |
  150. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  151. +----------------+--------------------------------------------------------------+
  152. | **macOS** | :: |
  153. | | |
  154. | | brew install mingw-w64 |
  155. +----------------+--------------------------------------------------------------+
  156. | **Mageia** | :: |
  157. | | |
  158. | | urpmi mingw64-gcc-c++ mingw64-winpthreads-static \ |
  159. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  160. +----------------+--------------------------------------------------------------+
  161. Before attempting the compilation, SCons will check for
  162. the following binaries in your ``PATH`` environment variable::
  163. i686-w64-mingw32-gcc
  164. x86_64-w64-mingw32-gcc
  165. If the binaries are not located in the ``PATH`` (e.g. ``/usr/bin``),
  166. you can define the following environment variable to give a hint to
  167. the build system::
  168. export MINGW_PREFIX="/path/to/mingw"
  169. Where ``/path/to/mingw`` is the path containing the ``bin`` directory where
  170. ``i686-w64-mingw32-gcc`` and ``x86_64-w64-mingw32-gcc`` are located (e.g.
  171. ``/opt/mingw-w64`` if the binaries are located in ``/opt/mingw-w64/bin``).
  172. To make sure you are doing things correctly, executing the following in
  173. the shell should result in a working compiler (the version output may
  174. differ based on your system)::
  175. ${MINGW_PREFIX}/bin/x86_64-w64-mingw32-gcc --version
  176. # x86_64-w64-mingw32-gcc (GCC) 13.2.0
  177. .. note:: When cross-compiling for Windows using MinGW-w64, keep in mind only
  178. ``x86_64`` and ``x86_32`` architectures are supported. Be sure to
  179. specify the right ``arch=`` option when invoking SCons if building
  180. from a different architecture.
  181. Troubleshooting
  182. ~~~~~~~~~~~~~~~
  183. Cross-compiling from some Ubuntu versions may lead to
  184. `this bug <https://github.com/godotengine/godot/issues/9258>`_,
  185. due to a default configuration lacking support for POSIX threading.
  186. You can change that configuration following those instructions,
  187. for 64-bit::
  188. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  189. <choose x86_64-w64-mingw32-gcc-posix from the list>
  190. sudo update-alternatives --config x86_64-w64-mingw32-g++
  191. <choose x86_64-w64-mingw32-g++-posix from the list>
  192. And for 32-bit::
  193. sudo update-alternatives --config i686-w64-mingw32-gcc
  194. <choose i686-w64-mingw32-gcc-posix from the list>
  195. sudo update-alternatives --config i686-w64-mingw32-g++
  196. <choose i686-w64-mingw32-g++-posix from the list>
  197. Creating Windows export templates
  198. ---------------------------------
  199. Windows export templates are created by compiling Godot without the editor,
  200. with the following flags::
  201. C:\godot> scons platform=windows target=template_debug arch=x86_32
  202. C:\godot> scons platform=windows target=template_release arch=x86_32
  203. C:\godot> scons platform=windows target=template_debug arch=x86_64
  204. C:\godot> scons platform=windows target=template_release arch=x86_64
  205. If you plan on replacing the standard export templates, copy these to the
  206. following location, replacing ``<version>`` with the version identifier
  207. (such as ``3.1.1.stable`` or ``3.2.dev``)::
  208. %USERPROFILE%\AppData\Roaming\Godot\templates\<version>\
  209. With the following names::
  210. windows_32_debug.exe
  211. windows_32_release.exe
  212. windows_64_debug.exe
  213. windows_64_release.exe
  214. However, if you are using custom modules or custom engine code, you
  215. may instead want to configure your binaries as custom export templates
  216. here:
  217. .. image:: img/wintemplates.png
  218. You don't need to copy them in this case, just reference the resulting
  219. files in the ``bin\`` directory of your Godot source folder, so the next
  220. time you build, you will automatically have the custom templates referenced.