compiling_for_windows.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. .. _doc_compiling_for_windows:
  2. Compiling for Windows
  3. =====================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. For compiling under Windows, the following is required:
  8. - `Visual Studio Community <https://www.visualstudio.com/vs/community/>`_,
  9. version 2017 or later. VS 2019 is recommended.
  10. **Make sure to read "Installing Visual Studio caveats" below or you
  11. will have to run/download the installer again.**
  12. - `MinGW-w64 <http://mingw-w64.org/>`_ with GCC can be used as an alternative to
  13. Visual Studio. Be sure to install/configure it to use the ``posix`` thread model.
  14. **Important:** When using MinGW to compile the ``master`` branch, you need GCC 9 or later.
  15. - `Python 3.5+ <https://www.python.org/downloads/windows/>`_.
  16. **Make sure to enable the option to add Python to the ``PATH`` in the installer.**
  17. - `SCons <https://www.scons.org/>`_ build system. Using the latest release is
  18. recommended, especially for proper support of recent Visual Studio releases.
  19. .. note:: If you have `Scoop <https://scoop.sh/>`_ installed, you can easily
  20. install MinGW and other dependencies using the following command::
  21. scoop install gcc python scons make
  22. .. note:: If you have `MSYS2 <https://www.msys2.org/>`_ installed, you can easily
  23. install MinGW and other dependencies using the following command::
  24. pacman -S mingw-w64-x86_64-python3-pip mingw-w64-x86_64-gcc \
  25. mingw-w64-i686-python3-pip mingw-w64-i686-gcc make
  26. For each MSYS2 MinGW subsystem, you should then run
  27. `pip3 install scons` in its shell.
  28. .. seealso:: For a general overview of SCons usage for Godot, see
  29. :ref:`doc_introduction_to_the_buildsystem`.
  30. Setting up SCons
  31. ----------------
  32. To install SCons, open the command prompt and run the following command::
  33. python -m pip install scons
  34. If you are prompted with the message
  35. ``Defaulting to user installation because normal site-packages is not
  36. writeable``, you may have to run that command again using elevated
  37. permissions. Open a new command prompt as an Administrator then run the command
  38. again to ensure that SCons is available from the ``PATH``.
  39. To check whether you have installed Python and SCons correctly, you can
  40. type ``python --version`` and ``scons --version`` into a command prompt
  41. (``cmd.exe``).
  42. If the commands above don't work, make sure to add Python to your ``PATH``
  43. environment variable after installing it, then check again.
  44. You can do so by running the Python installer again and enabling the option
  45. to add Python to the ``PATH``.
  46. If SCons cannot detect your Visual Studio installation, it might be that your
  47. SCons version is too old. Update it to the latest version with
  48. ``python -m pip install --upgrade scons``.
  49. .. _doc_compiling_for_windows_install_vs:
  50. Installing Visual Studio caveats
  51. --------------------------------
  52. If installing Visual Studio 2017 or 2019, make sure to enable **C++** in
  53. the list of workflows to install.
  54. If installing Visual Studio 2015, make sure to run a **Custom**
  55. installation instead of **Typical** and select **C++** as a language there.
  56. If you've already made the mistake of installing Visual Studio without
  57. C++ support, run the installer again; it should present you a **Modify** button.
  58. Running the installer from *Add/Remove Programs* will only give you
  59. a **Repair** option, which won't let you install C++ tools.
  60. Downloading Godot's source
  61. --------------------------
  62. Godot's source code is `hosted on GitHub <https://github.com/godotengine/godot>`_.
  63. Downloading it (cloning) using `Git <https://git-scm.com/>`_ is recommended.
  64. The tutorial will assume from now on that you placed the source code in
  65. ``C:\godot``.
  66. .. warning::
  67. To prevent slowdowns caused by continuous virus scanning during compilation,
  68. add the Godot source folder to the list of exceptions in your antivirus
  69. software.
  70. For Windows Defender, hit the :kbd:`Windows` key, type
  71. "Windows Defender Settings" then hit :kbd:`Enter`.
  72. Under **Virus & threat protection**, go to **Virus & threat protection setting**
  73. and scroll down to **Exclusions**. Click **Add or remove exclusions** then
  74. add the Godot source folder.
  75. Compiling
  76. ---------
  77. Selecting a compiler
  78. ~~~~~~~~~~~~~~~~~~~~
  79. SCons will automatically find and use an existing Visual Studio installation.
  80. If you do not have Visual Studio installed, it will attempt to use
  81. MinGW instead. If you already have Visual Studio installed and want to
  82. use MinGW, pass ``use_mingw=yes`` to the SCons command line. Note that MSVC
  83. builds cannot be performed from the MSYS2 or MinGW shells. Use either
  84. ``cmd.exe`` or PowerShell instead.
  85. During development, using the Visual Studio compiler is usually a better idea,
  86. as it links the Godot binary much faster than MinGW. However, MinGW can
  87. produce more optimized binaries using link-time optimization (see below),
  88. making it a better choice for production use.
  89. Running SCons
  90. ~~~~~~~~~~~~~
  91. After opening a command prompt, change to the root directory of
  92. the engine source code (using ``cd``) and type::
  93. C:\godot> scons platform=windows
  94. You can specify a number of CPU threads to use to speed up the build::
  95. C:\godot> scons -j6 platform=windows
  96. In general, it is OK to have at least as many threads compiling Godot as you
  97. have cores in your CPU, if not one or two more. Feel free to add the ``-j``
  98. option to any SCons command you see below.
  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.tools.32.exe`` or
  103. ``godot.windows.tools.64.exe``. By default, SCons will build a binary matching
  104. your CPU architecture, but this can be overridden using ``bits=64`` or
  105. ``bits=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=release_debug``.
  111. If you are compiling Godot with MinGW, you can make the binary
  112. even smaller and faster by adding the SCons option ``use_lto=yes``.
  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 or other IDEs
  120. ------------------------------------------
  121. For most projects, using only scripting is enough but when development
  122. in C++ is needed, for creating modules or extending the engine, working
  123. with an IDE is usually desirable.
  124. You can create a Visual Studio solution via SCons by running SCons with
  125. the ``vsproj=yes`` parameter, like this::
  126. scons p=windows vsproj=yes
  127. You will be able to open Godot's source in a Visual Studio solution now,
  128. and able to build Godot using Visual Studio's **Build** button.
  129. If you need to edit the build commands, they are located in
  130. "Godot" project settings, NMAKE sheet. SCons is called at the end of
  131. the commands. If you make a mistake, copy the command from one of the
  132. other build configurations (debug, release_debug, release) or
  133. architectures (Win32/x64); they are equivalent.
  134. Cross-compiling for Windows from other operating systems
  135. --------------------------------------------------------
  136. If you are a Linux or macOS user, you need to install
  137. `MinGW-w64 <https://mingw-w64.org/doku.php>`__, which typically comes in 32-bit
  138. and 64-bit variants. The package names may differ based on your distribution,
  139. here are some known ones:
  140. +----------------+--------------------------------------------------------------+
  141. | **Arch Linux** | :: |
  142. | | |
  143. | | pacman -Sy mingw-w64 |
  144. +----------------+--------------------------------------------------------------+
  145. | **Debian** / | :: |
  146. | **Ubuntu** | |
  147. | | apt install mingw-w64 |
  148. +----------------+--------------------------------------------------------------+
  149. | **Fedora** | :: |
  150. | | |
  151. | | dnf install mingw64-gcc-c++ mingw64-winpthreads-static \ |
  152. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  153. +----------------+--------------------------------------------------------------+
  154. | **macOS** | :: |
  155. | | |
  156. | | brew install mingw-w64 |
  157. +----------------+--------------------------------------------------------------+
  158. | **Mageia** | :: |
  159. | | |
  160. | | urpmi mingw64-gcc-c++ mingw64-winpthreads-static \ |
  161. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  162. +----------------+--------------------------------------------------------------+
  163. Before attempting the compilation, SCons will check for
  164. the following binaries in your ``PATH`` environment variable::
  165. i686-w64-mingw32-gcc
  166. x86_64-w64-mingw32-gcc
  167. If the binaries are not located in the ``PATH`` (e.g. ``/usr/bin``),
  168. you can define the following environment variables to give a hint to
  169. the build system::
  170. export MINGW32_PREFIX="/path/to/i686-w64-mingw32-"
  171. export MINGW64_PREFIX="/path/to/x86_64-w64-mingw32-"
  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. ${MINGW32_PREFIX}gcc --version
  176. # i686-w64-mingw32-gcc (GCC) 6.1.0 20160427 (Mageia MinGW 6.1.0-1.mga6)
  177. Troubleshooting
  178. ~~~~~~~~~~~~~~~
  179. Cross-compiling from some Ubuntu versions may lead to
  180. `this bug <https://github.com/godotengine/godot/issues/9258>`_,
  181. due to a default configuration lacking support for POSIX threading.
  182. You can change that configuration following those instructions,
  183. for 64-bit::
  184. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  185. <choose x86_64-w64-mingw32-gcc-posix from the list>
  186. sudo update-alternatives --config x86_64-w64-mingw32-g++
  187. <choose x86_64-w64-mingw32-g++-posix from the list>
  188. And for 32-bit::
  189. sudo update-alternatives --config i686-w64-mingw32-gcc
  190. <choose i686-w64-mingw32-gcc-posix from the list>
  191. sudo update-alternatives --config i686-w64-mingw32-g++
  192. <choose i686-w64-mingw32-g++-posix from the list>
  193. Creating Windows export templates
  194. ---------------------------------
  195. Windows export templates are created by compiling Godot without the editor,
  196. with the following flags::
  197. C:\godot> scons platform=windows tools=no target=release_debug bits=32
  198. C:\godot> scons platform=windows tools=no target=release bits=32
  199. C:\godot> scons platform=windows tools=no target=release_debug bits=64
  200. C:\godot> scons platform=windows tools=no target=release bits=64
  201. If you plan on replacing the standard export templates, copy these to the
  202. following location, replacing ``<version>`` with the version identifier
  203. (such as ``3.1.1.stable`` or ``3.2.dev``)::
  204. %USERPROFILE%\AppData\Roaming\Godot\templates\<version>\
  205. With the following names::
  206. windows_32_debug.exe
  207. windows_32_release.exe
  208. windows_64_debug.exe
  209. windows_64_release.exe
  210. However, if you are using custom modules or custom engine code, you
  211. may instead want to configure your binaries as custom export templates
  212. here:
  213. .. image:: img/wintemplates.png
  214. You don't need to copy them in this case, just reference the resulting
  215. files in the ``bin\`` directory of your Godot source folder, so the next
  216. time you build, you will automatically have the custom templates referenced.