compiling_for_windows.rst 23 KB

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