compiling_for_windows.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 2019 or later. Visual Studio 2022 is recommended.
  13. **Make sure to enable C++ in the list of workflows to install.**
  14. If you've already installed Visual Studio without C++ support, run the installer
  15. again; it should present you a **Modify** button.
  16. - `MinGW-w64 <https://mingw-w64.org/>`_ with GCC can be used as an alternative to
  17. Visual Studio. Be sure to install/configure it to use the ``posix`` thread model.
  18. **Important:** When using MinGW to compile the ``master`` branch, you need GCC 9 or later.
  19. - `Python 3.6+ <https://www.python.org/downloads/windows/>`_.
  20. **Make sure to enable the option to add Python to the ``PATH`` in the installer.**
  21. - `SCons 3.1.2+ <https://scons.org/pages/download.html>`_ build system. Using the
  22. latest release is recommended, especially for proper support of recent Visual
  23. Studio releases.
  24. .. note:: If you have `Scoop <https://scoop.sh/>`_ installed, you can easily
  25. install MinGW and other dependencies using the following command::
  26. scoop install gcc python scons make mingw
  27. .. note:: If you have `MSYS2 <https://www.msys2.org/>`_ installed, you can easily
  28. install MinGW and other dependencies using the following command::
  29. pacman -S mingw-w64-x86_64-python3-pip mingw-w64-x86_64-gcc \
  30. mingw-w64-i686-python3-pip mingw-w64-i686-gcc make
  31. For each MSYS2 MinGW subsystem, you should then run
  32. `pip3 install scons` in its shell.
  33. .. seealso:: To get the Godot source code for compiling, see
  34. :ref:`doc_getting_source`.
  35. For a general overview of SCons usage for Godot, see
  36. :ref:`doc_introduction_to_the_buildsystem`.
  37. Setting up SCons
  38. ----------------
  39. To install SCons, open the command prompt and run the following command::
  40. python -m pip install scons
  41. If you are prompted with the message
  42. ``Defaulting to user installation because normal site-packages is not
  43. writeable``, you may have to run that command again using elevated
  44. permissions. Open a new command prompt as an Administrator then run the command
  45. again to ensure that SCons is available from the ``PATH``.
  46. To check whether you have installed Python and SCons correctly, you can
  47. type ``python --version`` and ``scons --version`` into a command prompt
  48. (``cmd.exe``).
  49. If the commands above don't work, make sure to add Python to your ``PATH``
  50. environment variable after installing it, then check again.
  51. You can do so by running the Python installer again and enabling the option
  52. to add Python to the ``PATH``.
  53. If SCons cannot detect your Visual Studio installation, it might be that your
  54. SCons version is too old. Update it to the latest version with
  55. ``python -m pip install --upgrade scons``.
  56. .. _doc_compiling_for_windows_install_vs:
  57. Downloading Godot's source
  58. --------------------------
  59. Refer to :ref:`doc_getting_source` for detailed instructions.
  60. The tutorial will assume from now on that you placed the source code in
  61. ``C:\godot``.
  62. .. warning::
  63. To prevent slowdowns caused by continuous virus scanning during compilation,
  64. add the Godot source folder to the list of exceptions in your antivirus
  65. software.
  66. For Windows Defender, hit the :kbd:`Windows` key, type "Windows Security"
  67. then hit :kbd:`Enter`. Click on **Virus & threat protection** on the left
  68. panel. Under **Virus & threat protection settings** click on **Manage Settings**
  69. and scroll down to **Exclusions**. Click **Add or remove exclusions** then
  70. add the Godot source folder.
  71. Compiling
  72. ---------
  73. Selecting a compiler
  74. ~~~~~~~~~~~~~~~~~~~~
  75. SCons will automatically find and use an existing Visual Studio installation.
  76. If you do not have Visual Studio installed, it will attempt to use
  77. MinGW instead. If you already have Visual Studio installed and want to
  78. use MinGW, pass ``use_mingw=yes`` to the SCons command line. Note that MSVC
  79. builds cannot be performed from the MSYS2 or MinGW shells. Use either
  80. ``cmd.exe`` or PowerShell instead.
  81. .. tip::
  82. During development, using the Visual Studio compiler is usually a better
  83. idea, as it links the Godot binary much faster than MinGW. However, MinGW
  84. can produce more optimized binaries using link-time optimization (see
  85. below), making it a better choice for production use. This is particularly
  86. the case for the GDScript VM which performs much better with MinGW compared
  87. to MSVC. Therefore, it's recommended to use MinGW to produce builds that you
  88. distribute to players.
  89. All official Godot binaries are built in
  90. `custom containers <https://github.com/godotengine/build-containers>`__
  91. using MinGW.
  92. Running SCons
  93. ~~~~~~~~~~~~~
  94. After opening a command prompt, change to the root directory of
  95. the engine source code (using ``cd``) and type:
  96. .. code-block:: doscon
  97. C:\godot> scons platform=windows
  98. .. note:: When compiling with multiple CPU threads, SCons may warn about
  99. pywin32 being missing. You can safely ignore this warning.
  100. If all goes well, the resulting binary executable will be placed in
  101. ``C:\godot\bin\`` with the name ``godot.windows.editor.x86_32.exe`` or
  102. ``godot.windows.editor.x86_64.exe``. By default, SCons will build a binary matching
  103. your CPU architecture, but this can be overridden using ``arch=x86_64`` or
  104. ``arch=x86_32``.
  105. This executable file contains the whole engine and runs without any
  106. dependencies. Running it will bring up the Project Manager.
  107. .. tip:: If you are compiling Godot for production use, you can
  108. make the final executable smaller and faster by adding the
  109. SCons option ``production=yes``. This enables additional compiler
  110. optimizations and link-time optimization.
  111. LTO takes some time to run and requires about 7 GB of available RAM
  112. while compiling. If you're running out of memory with the above option,
  113. use ``production=yes lto=none`` or ``production=yes lto=thin`` for a
  114. lightweight but less effective form of LTO.
  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. Compiling with support for Direct3D 12
  120. --------------------------------------
  121. By default, builds of Godot do not contain support for the Direct3D 12 graphics
  122. API.
  123. To compile Godot with Direct3D 12 support you need at least the following:
  124. - `The DirectX Shader Compiler <https://github.com/Microsoft/DirectXShaderCompiler/releases>`_.
  125. The zip folder will be named "dxc\_" followed by the date of release. Download
  126. it anywhere, unzip it and remember the path to the unzipped folder, you will
  127. need it below.
  128. - `godot-nir-static library <https://github.com/godotengine/godot-nir-static/releases/>`_.
  129. We compile the Mesa libraries you will need into a static library. Download it
  130. anywhere, unzip it and remember the path to the unzipped folder, you will
  131. need it below.
  132. .. note:: You can optionally build the godot-nir-static libraries yourself with
  133. the following steps:
  134. 1. Install the Python package `mako <https://www.makotemplates.org>`_
  135. which is needed to generate some files.
  136. 2. Clone the `godot-nir-static <https://github.com/godotengine/godot-nir-static>`_
  137. directory and navigate to it.
  138. 3. Run the following::
  139. git submodule update --init
  140. ./update_mesa.sh
  141. scons
  142. If you are building with MinGW, add ``use_mingw=yes`` to the ``scons``
  143. command, you can also specify build architecture using ``arch={architecture}``.
  144. Mesa static library should be built using the same compiler you are
  145. using for building Godot.
  146. Optionally, you can compile with the following for additional features:
  147. - `PIX <https://devblogs.microsoft.com/pix/download>`_ is a performance tuning
  148. and debugging application for Direct3D12 applications. If you compile-in
  149. support for it, you can get much more detailed information through PIX that
  150. will help you optimize your game and troubleshoot graphics bugs. To use it,
  151. download the WinPixEventRuntime package. You will be taken to a NuGet package
  152. page where you can click "Download package" to get it. Once downloaded, change
  153. the file extension to .zip and unzip the file to some path.
  154. - `Agility SDK <https://devblogs.microsoft.com/directx/directx12agility>`_ can
  155. be used to provide access to the latest Direct3D 12 features without relying
  156. on driver updates. To use it, download the latest Agility SDK package. You
  157. will be taken to a NuGet package page where you can click "Download package"
  158. to get it. Once downloaded, change the file extension to .zip and unzip the
  159. file to some path.
  160. .. note:: If you use a preview version of the Agility SDK, remember to enable
  161. developer mode in Windows; otherwise it won't be used.
  162. .. note:: If you want to use a PIX with MinGW build, navigate to PIX runtime
  163. directory and use the following commands to generate import library::
  164. # For x86-64:
  165. gendef ./bin/x64/WinPixEventRuntime.dll
  166. dlltool --machine i386:x86-64 --no-leading-underscore -d WinPixEventRuntime.def -D WinPixEventRuntime.dll -l ./bin/x64/libWinPixEventRuntime.a
  167. # For ARM64:
  168. gendef ./bin/ARM64/WinPixEventRuntime.dll
  169. dlltool --machine arm64 --no-leading-underscore -d WinPixEventRuntime.def -D WinPixEventRuntime.dll -l ./bin/ARM64/libWinPixEventRuntime.a
  170. When building Godot, you will need to tell SCons to use Direct3D 12 and where to
  171. look for the additional libraries:
  172. .. code-block:: doscon
  173. C:\godot> scons platform=windows d3d12=yes dxc_path=<...> mesa_libs=<...>
  174. Or, with all options enabled:
  175. .. code-block:: doscon
  176. C:\godot> scons platform=windows d3d12=yes dxc_path=<...> mesa_libs=<...> agility_sdk_path=<...> pix_path=<...>
  177. .. note:: The build process will copy ``dxil.dll`` from the ``bin/<arch>/``
  178. directory in the DXC folder to the Godot binary directory and the
  179. appropriate ``bin/<arch>`` file in the Godot binary directory.
  180. Direct3D 12-enabled Godot packages for distribution to end users must
  181. include the ``dxil.dll`` (and relevant folders if using multi-arch),
  182. both for the editor and games. At runtime, the renderer will try to
  183. load the DLL from the arch-specific folders, and will fall back to the
  184. same directory as the Godot executable if the appropriate arch isn't
  185. found.
  186. .. note:: For the Agility SDK's DLLs you have to explicitly choose the kind of
  187. workflow. Single-arch is the default (DLLs copied to ``bin/``). If you
  188. pass ``agility_sdk_multi_arch=yes`` to SCons, you'll opt-in for
  189. multi-arch. DLLs will be copied to the appropriate ``bin/<arch>/``
  190. subdirectories and at runtime the right one will be loaded.
  191. Compiling with ANGLE support
  192. ----------------------------
  193. ANGLE provides a translation layer from OpenGL ES 3.x to Direct3D 11 and can be used
  194. to improve support for the Compatibility renderer on some older GPUs with outdated
  195. OpenGL drivers and on Windows for ARM.
  196. By default, Godot is built with dynamically linked ANGLE, you can use it by placing
  197. ``libEGL.dll`` and ``libGLESv2.dll`` alongside the executable.
  198. .. note:: You can use dynamically linked ANGLE with export templates as well, rename
  199. aforementioned DLLs to ``libEGL.{architecture}.dll`` and ``libGLESv2.{architecture}.dll``
  200. and place them alongside export template executables, and libraries will
  201. be automatically copied during the export process.
  202. To compile Godot with statically linked ANGLE:
  203. - Download pre-built static libraries from `godot-angle-static library <https://github.com/godotengine/godot-angle-static/releases>`_, and unzip them.
  204. - When building Godot, add ``angle_libs={path}`` to tell SCons where to look for the ANGLE libraries::
  205. scons platform=windows angle_libs=<...>
  206. .. note:: You can optionally build the godot-angle-static libraries yourself with
  207. the following steps:
  208. 1. Clone the `godot-angle-static <https://github.com/godotengine/godot-angle-static>`_
  209. directory and navigate to it.
  210. 2. Run the following command::
  211. scons
  212. If you are buildng with MinGW, add ``use_mingw=yes`` to the command,
  213. you can also specify build architecture using ``arch={architecture}``.
  214. ANGLE static library should be built using the same compiler you are
  215. using for building Godot.
  216. Development in Visual Studio
  217. ----------------------------
  218. Using an IDE is not required to compile Godot, as SCons takes care of everything.
  219. But if you intend to do engine development or debugging of the engine's C++ code,
  220. you may be interested in configuring a code editor or an IDE.
  221. Folder-based editors don't require any particular setup to start working with Godot's
  222. codebase. To edit projects with Visual Studio they need to be set up as a solution.
  223. You can create a Visual Studio solution via SCons by running SCons with
  224. the ``vsproj=yes`` parameter, like this::
  225. scons p=windows vsproj=yes
  226. You will be able to open Godot's source in a Visual Studio solution now,
  227. and able to build Godot using Visual Studio's **Build** button.
  228. .. seealso:: See :ref:`doc_configuring_an_ide_vs` for further details.
  229. Cross-compiling for Windows from other operating systems
  230. --------------------------------------------------------
  231. If you are a Linux or macOS user, you need to install
  232. `MinGW-w64 <https://mingw-w64.org/doku.php>`__, which typically comes in 32-bit
  233. and 64-bit variants. The package names may differ based on your distribution,
  234. here are some known ones:
  235. +----------------+--------------------------------------------------------------+
  236. | **Arch Linux** | :: |
  237. | | |
  238. | | pacman -Sy mingw-w64 |
  239. +----------------+--------------------------------------------------------------+
  240. | **Debian** / | :: |
  241. | **Ubuntu** | |
  242. | | apt install mingw-w64 |
  243. +----------------+--------------------------------------------------------------+
  244. | **Fedora** | :: |
  245. | | |
  246. | | dnf install mingw64-gcc-c++ mingw64-winpthreads-static \ |
  247. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  248. +----------------+--------------------------------------------------------------+
  249. | **macOS** | :: |
  250. | | |
  251. | | brew install mingw-w64 |
  252. +----------------+--------------------------------------------------------------+
  253. | **Mageia** | :: |
  254. | | |
  255. | | urpmi mingw64-gcc-c++ mingw64-winpthreads-static \ |
  256. | | mingw32-gcc-c++ mingw32-winpthreads-static |
  257. +----------------+--------------------------------------------------------------+
  258. Before attempting the compilation, SCons will check for
  259. the following binaries in your ``PATH`` environment variable::
  260. i686-w64-mingw32-gcc
  261. x86_64-w64-mingw32-gcc
  262. If the binaries are not located in the ``PATH`` (e.g. ``/usr/bin``),
  263. you can define the following environment variable to give a hint to
  264. the build system::
  265. export MINGW_PREFIX="/path/to/mingw"
  266. Where ``/path/to/mingw`` is the path containing the ``bin`` directory where
  267. ``i686-w64-mingw32-gcc`` and ``x86_64-w64-mingw32-gcc`` are located (e.g.
  268. ``/opt/mingw-w64`` if the binaries are located in ``/opt/mingw-w64/bin``).
  269. To make sure you are doing things correctly, executing the following in
  270. the shell should result in a working compiler (the version output may
  271. differ based on your system)::
  272. ${MINGW_PREFIX}/bin/x86_64-w64-mingw32-gcc --version
  273. # x86_64-w64-mingw32-gcc (GCC) 13.2.0
  274. .. note:: When cross-compiling for Windows using MinGW-w64, keep in mind only
  275. ``x86_64`` and ``x86_32`` architectures are supported. Be sure to
  276. specify the right ``arch=`` option when invoking SCons if building
  277. from a different architecture.
  278. Troubleshooting
  279. ~~~~~~~~~~~~~~~
  280. Cross-compiling from some Ubuntu versions may lead to
  281. `this bug <https://github.com/godotengine/godot/issues/9258>`_,
  282. due to a default configuration lacking support for POSIX threading.
  283. You can change that configuration following those instructions,
  284. for 64-bit::
  285. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  286. <choose x86_64-w64-mingw32-gcc-posix from the list>
  287. sudo update-alternatives --config x86_64-w64-mingw32-g++
  288. <choose x86_64-w64-mingw32-g++-posix from the list>
  289. And for 32-bit::
  290. sudo update-alternatives --config i686-w64-mingw32-gcc
  291. <choose i686-w64-mingw32-gcc-posix from the list>
  292. sudo update-alternatives --config i686-w64-mingw32-g++
  293. <choose i686-w64-mingw32-g++-posix from the list>
  294. Creating Windows export templates
  295. ---------------------------------
  296. Windows export templates are created by compiling Godot without the editor,
  297. with the following flags:
  298. .. code-block:: doscon
  299. C:\godot> scons platform=windows target=template_debug arch=x86_32
  300. C:\godot> scons platform=windows target=template_release arch=x86_32
  301. C:\godot> scons platform=windows target=template_debug arch=x86_64
  302. C:\godot> scons platform=windows target=template_release arch=x86_64
  303. If you plan on replacing the standard export templates, copy these to the
  304. following location, replacing ``<version>`` with the version identifier
  305. (such as ``4.2.1.stable`` or ``4.3.dev``):
  306. .. code-block:: none
  307. %APPDATA%\Godot\export_templates\<version>\
  308. With the following names::
  309. windows_debug_x86_32_console.exe
  310. windows_debug_x86_32.exe
  311. windows_debug_x86_64_console.exe
  312. windows_debug_x86_64.exe
  313. windows_release_x86_32_console.exe
  314. windows_release_x86_32.exe
  315. windows_release_x86_64_console.exe
  316. windows_release_x86_64.exe
  317. However, if you are using custom modules or custom engine code, you
  318. may instead want to configure your binaries as custom export templates
  319. here:
  320. .. image:: img/wintemplates.png
  321. You don't need to copy them in this case, just reference the resulting
  322. files in the ``bin\`` directory of your Godot source folder, so the next
  323. time you build, you will automatically have the custom templates referenced.