compiling_for_windows.rst 18 KB

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