compiling_for_windows.rst 17 KB

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