compiling_for_windows.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 C++, `Visual
  9. Studio Community <https://www.visualstudio.com/vs/community/>`__
  10. (recommended), version 2013 (12.0) or later.
  11. **Make sure you read Installing Visual Studio caveats below or you
  12. will have to run/download the installer again.**
  13. - `Python 2.7+ or Python 3.5+ <https://www.python.org/downloads/>`__.
  14. - `Pywin32 Python Extension <https://github.com/mhammond/pywin32>`__
  15. for parallel builds (which increase the build speed by a great factor).
  16. - `SCons <https://www.scons.org>`__ build system.
  17. .. seealso:: For a general overview of SCons usage for Godot, see
  18. :ref:`doc_introduction_to_the_buildsystem`.
  19. Setting up SCons
  20. ----------------
  21. Python adds the interpreter (python.exe) to the path. It usually
  22. installs in ``C:\Python`` (or ``C:\Python[Version]``). SCons installs
  23. inside the Python install (typically in the ``Scripts`` folder) and
  24. provides a batch file called ``scons.bat``.
  25. The location of this file can be added to the path or it can simply be
  26. copied to ``C:\Python`` together with the interpreter executable.
  27. To check whether you have installed Python and SCons correctly, you can
  28. type ``python --version`` and ``scons --version`` into the
  29. Windows Command Prompt (``cmd.exe``).
  30. If commands above do not work, make sure you add Python to your PATH
  31. environment variable after installing it, and check again.
  32. Setting up Pywin32
  33. ------------------
  34. Pywin32 is required for parallel builds using multiple CPU cores.
  35. If SCons is issuing a warning about Pywin32 after parsing SConstruct
  36. build instructions, when beginning to build, you need to install it properly
  37. from the correct installer executable for your Python version
  38. `located at Github. <https://github.com/mhammond/pywin32/releases>`__
  39. For example, if you installed a 32-bit version of Python 2.7, you would want
  40. to install the latest version of Pywin32 that is built for the mentioned version
  41. of Python. That executable installer would be named ``pywin32-221.win32-py2.7.exe``.
  42. The ``amd64`` version of Pywin32 is for a 64-bit version of Python
  43. ``pywin32-221.win-amd64-py2.7.exe``. Change the ``py`` number to install for
  44. your version of Python (check via ``python --version`` mentioned above).
  45. .. _doc_compiling_for_windows_install_vs:
  46. Installing Visual Studio caveats
  47. --------------------------------
  48. If installing Visual Studio 2015 or later, make sure to run **Custom** installation, not
  49. **Typical** and select C++ as language there (and any other things you might
  50. need). The installer does not install C++ by default. C++ was the
  51. `only language made optional <https://blogs.msdn.microsoft.com/vcblog/2015/07/24/setup-changes-in-visual-studio-2015-affecting-c-developers/>`__
  52. in Visual Studio 2015.
  53. If you have already made the mistake of installing a **Typical**,
  54. installation, rerun the executable installer you downloaded from
  55. internet, it will give you a **Modify** Button option. Running the
  56. install from Add/Remove programs will only give you the "Repair" option,
  57. which will do nothing for your problem.
  58. If you're using Express, make sure you get/have a version that can
  59. compile for ***C++, Desktop***.
  60. Downloading Godot's source
  61. --------------------------
  62. `Godot's <https://github.com/godotengine/godot>`__ source is hosted on
  63. GitHub. Downloading it (cloning) via `Git <https://git-scm.com/>`__ is recommended.
  64. The tutorial will presume from now on that you placed the source into
  65. ``C:\godot``.
  66. Compiling
  67. ---------
  68. SCons will not be able out of the box to compile from the
  69. Windows Command Prompt (``cmd.exe``) because SCons and Visual C++ compiler
  70. will not be able to locate environment variables and executables they
  71. need for compilation.
  72. Therefore, you need to start a Visual Studio command prompt. It sets up
  73. environment variables needed by SCons to locate the compiler.
  74. It should be called similar to one of the below names (for your
  75. respective version of Visual Studio):
  76. * "Developer Command Prompt for VS2013"
  77. * "VS2013 x64 Native Tools Command Prompt"
  78. * "VS2013 x86 Native Tools Command Prompt"
  79. * "VS2013 x64 Cross Tools Command Prompt"
  80. * "VS2013 x86 Cross Tools Command Prompt"
  81. You should be able to find at least the Developer Command Prompt for
  82. your version of Visual Studio in your start menu.
  83. However Visual Studio sometimes seems to not install some of the above
  84. shortcuts, except the Developer Console at these locations that are
  85. automatically searched by the start menu search option:
  86. ::
  87. Win 7:
  88. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools
  89. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2013\Visual Studio Tools
  90. If you found the Developer Console, it will do for now to create a 32-bit
  91. version of Godot, but if you want the 64-bit version, you might need
  92. to setup the prompts manually for easy access.
  93. If you don't see some of the shortcuts, "How the prompts actually work"
  94. section below will explain how to setup these prompts if you need them.
  95. About the Developer/Tools Command Prompts and the Visual C++ compiler
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. There is a few things you need to know about these consoles and the
  98. Visual C++ compiler.
  99. Your Visual Studio installation will ship with several Visual C++
  100. compilers, them being more or less identical, however each ``cl.exe``
  101. (Visual C++ compiler) will compile Godot for a different architecture
  102. (32-bit x86 or 64-bit x86; the ARM compiler is not supported).
  103. The **Developer Command Prompt** will build a 32-bit version of Godot by
  104. using the 32-bit Visual C++ compiler.
  105. **Native Tools** Prompts (mentioned above) are used when you want the
  106. 32-bit cl.exe to compile a 32-bit executable (x86 Native Tools
  107. Command Prompt). For the 64-bit cl.exe, it will compile a 64-bit
  108. executable (x64 Native Tools Command Prompt).
  109. The **Cross Tools** are used when your Windows is using one architecture
  110. (32-bit, for example) and you need to compile to a different
  111. architecture (64-bit). As you might be familiar, 32-bit Windows can not
  112. run 64-bit executables, but you still might need to compile for them.
  113. For example:
  114. * "VS2013 x64 Cross Tools Command Prompt" will use a 32-bit cl.exe that
  115. will compile a 64 bit application.
  116. * "VS2013 x86 Cross Tools Command Prompt" will use a 64-bit cl.exe that
  117. will compile a 32-bit application. This one is useful if you are
  118. running a 32-bit Windows.
  119. On a 64-bit Windows, you can run any of above prompts and compilers
  120. (``cl.exe`` executables) because 64-bit Windows can run any 32-bit
  121. application. 32-bit Windows cannot run 64-bit executables, so the
  122. Visual Studio installer won't even install shortcuts for some of
  123. these prompts.
  124. Note that you need to choose the **Developer Console** or the correct
  125. **Tools Prompt** to build Godot for the correct architecture. Use only
  126. Native Prompts if you are not sure yet what exactly Cross Compile
  127. Prompts do.
  128. Running SCons
  129. ~~~~~~~~~~~~~
  130. Once inside the **Developer Console/Tools Console Prompt**, go to the
  131. root directory of the engine source code and type:
  132. ::
  133. C:\godot> scons platform=windows
  134. Tip: if you installed "Pywin32 Python Extension" you can append the -j
  135. command to instruct SCons to run parallel builds like this:
  136. ::
  137. C:\godot> scons -j6 platform=windows
  138. In general, it is OK to have at least as many threads compiling Godot as
  139. you have cores in your CPU, if not one or two more. Feel free to add the
  140. -j option to any SCons command you see below if you setup the
  141. "Pywin32 Python Extension".
  142. If all goes well, the resulting binary executable will be placed in
  143. ``C:\godot\bin\`` with the name of ``godot.windows.tools.32.exe`` or
  144. ``godot.windows.tools.64.exe``. SCons will automatically detect what
  145. compiler architecture the environment (the prompt) is setup for and will
  146. build a corresponding executable.
  147. This executable file contains the whole engine and runs without any
  148. dependencies. Executing it will bring up the Project Manager.
  149. .. note:: If you are compiling Godot for production use, then you can
  150. make the final executable smaller and faster by adding the
  151. SCons option ``target=release_debug``.
  152. How the prompts actually work
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. The Visual Studio command prompts are just shortcuts that call the
  155. standard Command Prompt and have it run a batch file before giving you
  156. control. The batch file itself is called **vcvarsall.bat** and it sets up
  157. environment variables, including the PATH variable, so that the correct
  158. version of the compiler can be run. The Developer Command Prompt calls a
  159. different file called **VsDevCmd.bat** but none of the other tools that
  160. this batch file enables are needed by Godot/SCons.
  161. Since you are probably using Visual Studio 2013 or 2015, if you need to
  162. recreate them manually, use the below folders, or place them on the
  163. desktop/taskbar:
  164. ::
  165. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools
  166. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2013\Visual Studio Tools
  167. Start the creation of the shortcut by pressing the ``right mouse
  168. button/New/Shortcut`` in an empty place in your desired location.
  169. Then copy one of these commands below for the corresponding tool you
  170. need into the "Path" and "Name" sections of the shortcut creation
  171. wizard, and fix the path to the batch file if needed.
  172. * Visual Studio 2013 is in the "Microsoft Visual Studio 12.0" folder.
  173. * Visual Studio 2015 is in the "Microsoft Visual Studio 14.0" folder.
  174. * etc.
  175. ::
  176. Name: Developer Command Prompt for VS2013
  177. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat""
  178. Name: VS2013 x64 Cross Tools Command Prompt
  179. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" x86_amd64
  180. Name: VS2013 x64 Native Tools Command Prompt
  181. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" amd64
  182. Name: VS2013 x86 Native Tools Command Prompt
  183. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" x86
  184. Name: VS2013 x86 Cross Tools Command Prompt
  185. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" amd64_x86
  186. After you create the shortcut, in the shortcut's properties, that you
  187. can access by right clicking with your mouse on the shortcut itself, you
  188. can choose the starting directory of the command prompt ("Start in"
  189. field).
  190. Some of these shortcuts (namely the 64-bit compilers) seem to not be
  191. available in the Express edition of Visual Studio or Visual C++. Before
  192. recreating the commands, make sure that ``cl.exe`` executables are present
  193. in one of these locations, they are the actual compilers for the
  194. architecture you want to build from the command prompt.
  195. ::
  196. x86 (32-bit) cl.exe
  197. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe
  198. x86 (32-bit) cl.exe for cross-compiling for 64-bit Windows.
  199. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe
  200. x64 (64-bit) cl.exe
  201. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\cl.exe
  202. x64 (64-bit) cl.exe for cross-compiling for 32-bit Windows.
  203. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe
  204. In case you are wondering what these prompt shortcuts do, they call ``cmd.exe``
  205. with the ``\k`` option and have it run a Batch file.
  206. ::
  207. %comspec% - path to cmd.exe
  208. \k - keep alive option of the command prompt
  209. remainder - command to run via cmd.exe
  210. cmd.exe \k(eep cmd.exe alive after commands behind this option run) ""runme.bat"" with_this_option
  211. How to run an automated build of Godot
  212. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. If you just need to run the compilation process via a Batch file or
  214. directly in the Windows Command Prompt you need to use the
  215. following command:
  216. ::
  217. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
  218. with one of the following parameters:
  219. * x86 (32-bit cl.exe to compile for the 32-bit architecture)
  220. * amd64 (64-bit cl.exe to compile for the 64-bit architecture)
  221. * x86_amd64 (32-bit cl.exe to compile for the 64-bit architecture)
  222. * amd64_x86 (64-bit cl.exe to compile for the 32-bit architecture)
  223. and after that one, you can run SCons:
  224. ::
  225. scons platform=windows
  226. or you can run them together:
  227. ::
  228. 32-bit Godot
  229. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86 && scons platform=windows
  230. 64-bit Godot
  231. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" amd64 && scons platform=windows
  232. Development in Visual Studio or other IDEs
  233. ------------------------------------------
  234. For most projects, using only scripting is enough but when development
  235. in C++ is needed, for creating modules or extending the engine, working
  236. with an IDE is usually desirable.
  237. You can create a Visual Studio solution via SCons by running SCons with
  238. the ``vsproj=yes`` parameter, like this:
  239. ::
  240. scons p=windows vsproj=yes
  241. You will be able to open Godot's source in a Visual Studio solution now,
  242. and able to build Godot via the Visual Studio **Build** button. However,
  243. make sure that you have installed Pywin32 so that parallel (-j) builds
  244. work properly.
  245. If you need to edit the compilation commands, they are located in
  246. "Godot" project settings, NMAKE sheet. SCons is called at the end of
  247. the commands. If you make a mistake, copy the command from one of the
  248. other build configurations (debug, release_debug, release) or
  249. architectures (Win32/x64). They are equivalent.
  250. Cross-compiling for Windows from other operating systems
  251. --------------------------------------------------------
  252. If you are a Linux or macOS user, you need to install `MinGW-w64 <https://mingw-w64.org/doku.php>`_,
  253. which typically comes in 32-bit and 64-bit variants. The package names
  254. may differ based on your distro, here are some known ones:
  255. +---------------+--------------------------------------------------------+
  256. | **Arch** | :: |
  257. | | |
  258. | | pacman -S scons mingw-w64-gcc |
  259. +---------------+--------------------------------------------------------+
  260. | **Debian** / | :: |
  261. | **Ubuntu** | |
  262. | | apt-get install scons mingw-w64 |
  263. +---------------+--------------------------------------------------------+
  264. | **Fedora** | :: |
  265. | | |
  266. | | dnf install scons mingw32-gcc-c++ mingw64-gcc-c++ |
  267. +---------------+--------------------------------------------------------+
  268. | **macOS** | :: |
  269. | | |
  270. | | brew install scons mingw-w64 |
  271. +---------------+--------------------------------------------------------+
  272. | **Mageia** | :: |
  273. | | |
  274. | | urpmi scons mingw32-gcc-c++ mingw64-gcc-c++ |
  275. +---------------+--------------------------------------------------------+
  276. Before allowing you to attempt the compilation, SCons will check for
  277. the following binaries in your ``$PATH``:
  278. ::
  279. i686-w64-mingw32-gcc
  280. x86_64-w64-mingw32-gcc
  281. If the binaries are not located in the ``$PATH`` (e.g. ``/usr/bin``),
  282. you can define the following environment variables to give a hint to
  283. the build system:
  284. ::
  285. export MINGW32_PREFIX="/path/to/i686-w64-mingw32-"
  286. export MINGW64_PREFIX="/path/to/x86_64-w64-mingw32-"
  287. To make sure you are doing things correctly, executing the following in
  288. the shell should result in a working compiler (the version output may
  289. differ based on your system):
  290. ::
  291. user@host:~$ ${MINGW32_PREFIX}gcc --version
  292. i686-w64-mingw32-gcc (GCC) 6.1.0 20160427 (Mageia MinGW 6.1.0-1.mga6)
  293. Troubleshooting
  294. ~~~~~~~~~~~~~~~
  295. Cross-compiling from some versions of Ubuntu may lead to `this bug <https://github.com/godotengine/godot/issues/9258>`_,
  296. due to a default configuration lacking support for POSIX threading.
  297. You can change that configuration following those instructions,
  298. for 32-bit:
  299. ::
  300. sudo update-alternatives --config i686-w64-mingw32-gcc
  301. <choose i686-w64-mingw32-gcc-posix from the list>
  302. sudo update-alternatives --config i686-w64-mingw32-g++
  303. <choose i686-w64-mingw32-g++-posix from the list>
  304. And for 64-bit:
  305. ::
  306. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  307. <choose x86_64-w64-mingw32-gcc-posix from the list>
  308. sudo update-alternatives --config x86_64-w64-mingw32-g++
  309. <choose x86_64-w64-mingw32-g++-posix from the list>
  310. Creating Windows export templates
  311. ---------------------------------
  312. Windows export templates are created by compiling Godot as release, with
  313. the following flags:
  314. - (using Mingw32 command prompt, using the bits parameter)
  315. ::
  316. C:\godot> scons platform=windows tools=no target=release bits=32
  317. C:\godot> scons platform=windows tools=no target=release_debug bits=32
  318. - (using Mingw-w64 command prompt, using the bits parameter)
  319. ::
  320. C:\godot> scons platform=windows tools=no target=release bits=64
  321. C:\godot> scons platform=windows tools=no target=release_debug bits=64
  322. - (using the Visual Studio command prompts for the correct
  323. architecture, notice the lack of bits parameter)
  324. ::
  325. C:\godot> scons platform=windows tools=no target=release
  326. C:\godot> scons platform=windows tools=no target=release_debug
  327. If you plan on replacing the standard templates, copy these to:
  328. ::
  329. C:\USERS\YOURUSER\AppData\Roaming\Godot\Templates
  330. With the following names:
  331. ::
  332. windows_32_debug.exe
  333. windows_32_release.exe
  334. windows_64_debug.exe
  335. windows_64_release.exe
  336. However, if you are writing your custom modules or custom C++ code, you
  337. might instead want to configure your binaries as custom export templates
  338. here:
  339. .. image:: img/wintemplates.png
  340. You don't even need to copy them, you can just reference the resulting
  341. files in the ``bin\`` directory of your Godot source folder, so the next
  342. time you build you automatically have the custom templates referenced.