compiling_for_windows.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 Sourceforge. <https://sourceforge.net/projects/pywin32/files/pywin32/>`__
  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. How the prompts actually work
  148. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  149. The Visual Studio command prompts are just shortcuts that call the
  150. standard Command Prompt and have it run a batch file before giving you
  151. control. The batch file itself is called **vcvarsall.bat** and it sets up
  152. environment variables, including the PATH variable, so that the correct
  153. version of the compiler can be run. The Developer Command Prompt calls a
  154. different file called **VsDevCmd.bat** but none of the other tools that
  155. this batch file enables are needed by Godot/SCons.
  156. Since you are probably using Visual Studio 2013 or 2015, if you need to
  157. recreate them manually, use the below folders, or place them on the
  158. desktop/taskbar:
  159. ::
  160. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools
  161. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2013\Visual Studio Tools
  162. Start the creation of the shortcut by pressing the ``right mouse
  163. button/New/Shortcut`` in an empty place in your desired location.
  164. Then copy one of these commands below for the corresponding tool you
  165. need into the "Path" and "Name" sections of the shortcut creation
  166. wizard, and fix the path to the batch file if needed.
  167. * Visual Studio 2013 is in the "Microsoft Visual Studio 12.0" folder.
  168. * Visual Studio 2015 is in the "Microsoft Visual Studio 14.0" folder.
  169. * etc.
  170. ::
  171. Name: Developer Command Prompt for VS2013
  172. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat""
  173. Name: VS2013 x64 Cross Tools Command Prompt
  174. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" x86_amd64
  175. Name: VS2013 x64 Native Tools Command Prompt
  176. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" amd64
  177. Name: VS2013 x86 Native Tools Command Prompt
  178. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" x86
  179. Name: VS2013 x86 Cross Tools Command Prompt
  180. Path: %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"" amd64_x86
  181. After you create the shortcut, in the shortcut's properties, that you
  182. can access by right clicking with your mouse on the shortcut itself, you
  183. can choose the starting directory of the command prompt ("Start in"
  184. field).
  185. Some of these shortcuts (namely the 64-bit compilers) seem to not be
  186. available in the Express edition of Visual Studio or Visual C++. Before
  187. recreating the commands, make sure that ``cl.exe`` executables are present
  188. in one of these locations, they are the actual compilers for the
  189. architecture you want to build from the command prompt.
  190. ::
  191. x86 (32-bit) cl.exe
  192. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe
  193. x86 (32-bit) cl.exe for cross-compiling for 64-bit Windows.
  194. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe
  195. x64 (64-bit) cl.exe
  196. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\cl.exe
  197. x64 (64-bit) cl.exe for cross-compiling for 32-bit Windows.
  198. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe
  199. In case you are wondering what these prompt shortcuts do, they call ``cmd.exe``
  200. with the ``\k`` option and have it run a Batch file.
  201. ::
  202. %comspec% - path to cmd.exe
  203. \k - keep alive option of the command prompt
  204. remainder - command to run via cmd.exe
  205. cmd.exe \k(eep cmd.exe alive after commands behind this option run) ""runme.bat"" with_this_option
  206. How to run an automated build of Godot
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. If you just need to run the compilation process via a Batch file or
  209. directly in the Windows Command Prompt you need to use the
  210. following command:
  211. ::
  212. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
  213. with one of the following parameters:
  214. * x86 (32-bit cl.exe to compile for the 32-bit architecture)
  215. * amd64 (64-bit cl.exe to compile for the 64-bit architecture)
  216. * x86_amd64 (32-bit cl.exe to compile for the 64-bit architecture)
  217. * amd64_x86 (64-bit cl.exe to compile for the 32-bit architecture)
  218. and after that one, you can run SCons:
  219. ::
  220. scons platform=windows
  221. or you can run them together:
  222. ::
  223. 32-bit Godot
  224. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86 && scons platform=windows
  225. 64-bit Godot
  226. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" amd64 && scons platform=windows
  227. Development in Visual Studio or other IDEs
  228. ------------------------------------------
  229. For most projects, using only scripting is enough but when development
  230. in C++ is needed, for creating modules or extending the engine, working
  231. with an IDE is usually desirable.
  232. You can create a Visual Studio solution via SCons by running SCons with
  233. the ``vsproj=yes`` parameter, like this:
  234. ::
  235. scons p=windows vsproj=yes
  236. You will be able to open Godot's source in a Visual Studio solution now,
  237. and able to build Godot via the Visual Studio **Build** button. However,
  238. make sure that you have installed Pywin32 so that parallel (-j) builds
  239. work properly.
  240. If you need to edit the compilation commands, they are located in
  241. "Godot" project settings, NMAKE sheet. SCons is called at the end of
  242. the commands. If you make a mistake, copy the command from one of the
  243. other build configurations (debug, release_debug, release) or
  244. architectures (Win32/x64). They are equivalent.
  245. Cross-compiling for Windows from other operating systems
  246. --------------------------------------------------------
  247. If you are a Linux or macOS user, you need to install `MinGW-w64 <https://mingw-w64.org/doku.php>`_,
  248. which typically comes in 32-bit and 64-bit variants. The package names
  249. may differ based on your distro, here are some known ones:
  250. +---------------+--------------------------------------------------------+
  251. | **Arch** | :: |
  252. | | |
  253. | | pacman -S scons mingw-w64-gcc |
  254. +---------------+--------------------------------------------------------+
  255. | **Debian** / | :: |
  256. | **Ubuntu** | |
  257. | | apt-get install scons mingw-w64 |
  258. +---------------+--------------------------------------------------------+
  259. | **Fedora** | :: |
  260. | | |
  261. | | dnf install scons mingw32-gcc-c++ mingw64-gcc-c++ |
  262. +---------------+--------------------------------------------------------+
  263. | **macOS** | :: |
  264. | | |
  265. | | brew install scons mingw-w64 |
  266. +---------------+--------------------------------------------------------+
  267. | **Mageia** | :: |
  268. | | |
  269. | | urpmi scons mingw32-gcc-c++ mingw64-gcc-c++ |
  270. +---------------+--------------------------------------------------------+
  271. Before allowing you to attempt the compilation, SCons will check for
  272. the following binaries in your ``$PATH``:
  273. ::
  274. i686-w64-mingw32-gcc
  275. x86_64-w64-mingw32-gcc
  276. If the binaries are not located in the ``$PATH`` (e.g. ``/usr/bin``),
  277. you can define the following environment variables to give a hint to
  278. the build system:
  279. ::
  280. export MINGW32_PREFIX="/path/to/i686-w64-mingw32-"
  281. export MINGW64_PREFIX="/path/to/x86_64-w64-mingw32-"
  282. To make sure you are doing things correctly, executing the following in
  283. the shell should result in a working compiler (the version output may
  284. differ based on your system):
  285. ::
  286. user@host:~$ ${MINGW32_PREFIX}gcc --version
  287. i686-w64-mingw32-gcc (GCC) 6.1.0 20160427 (Mageia MinGW 6.1.0-1.mga6)
  288. Troubleshooting
  289. ~~~~~~~~~~~~~~~
  290. Cross-compiling from some versions of Ubuntu may lead to `this bug <https://github.com/godotengine/godot/issues/9258>`_,
  291. due to a default configuration lacking support for POSIX threading.
  292. You can change that configuration following those instructions,
  293. for 32-bit:
  294. ::
  295. sudo update-alternatives --config i686-w64-mingw32-gcc
  296. <choose i686-w64-mingw32-gcc-posix from the list>
  297. sudo update-alternatives --config i686-w64-mingw32-g++
  298. <choose i686-w64-mingw32-g++-posix from the list>
  299. And for 64-bit:
  300. ::
  301. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  302. <choose x86_64-w64-mingw32-gcc-posix from the list>
  303. sudo update-alternatives --config x86_64-w64-mingw32-g++
  304. <choose x86_64-w64-mingw32-g++-posix from the list>
  305. Creating Windows export templates
  306. ---------------------------------
  307. Windows export templates are created by compiling Godot as release, with
  308. the following flags:
  309. - (using Mingw32 command prompt, using the bits parameter)
  310. ::
  311. C:\godot> scons platform=windows tools=no target=release bits=32
  312. C:\godot> scons platform=windows tools=no target=release_debug bits=32
  313. - (using Mingw-w64 command prompt, using the bits parameter)
  314. ::
  315. C:\godot> scons platform=windows tools=no target=release bits=64
  316. C:\godot> scons platform=windows tools=no target=release_debug bits=64
  317. - (using the Visual Studio command prompts for the correct
  318. architecture, notice the lack of bits parameter)
  319. ::
  320. C:\godot> scons platform=windows tools=no target=release
  321. C:\godot> scons platform=windows tools=no target=release_debug
  322. If you plan on replacing the standard templates, copy these to:
  323. ::
  324. C:\USERS\YOURUSER\AppData\Roaming\Godot\Templates
  325. With the following names:
  326. ::
  327. windows_32_debug.exe
  328. windows_32_release.exe
  329. windows_64_debug.exe
  330. windows_64_release.exe
  331. However, if you are writing your custom modules or custom C++ code, you
  332. might instead want to configure your binaries as custom export templates
  333. here:
  334. .. image:: img/wintemplates.png
  335. You don't even need to copy them, you can just reference the resulting
  336. files in the ``bin\`` directory of your Godot source folder, so the next
  337. time you build you automatically have the custom templates referenced.