introduction_to_the_buildsystem.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. .. _doc_introduction_to_the_buildsystem:
  2. Introduction to the buildsystem
  3. ===============================
  4. .. highlight:: shell
  5. SCons
  6. -----
  7. Godot uses `SCons <https://www.scons.org/>`__ to build. We love it, we are
  8. not changing it for anything else. We are not even sure other build
  9. systems are up to the task of building Godot. We constantly get requests
  10. to move the build system to CMake, or Visual Studio, but this is not
  11. going to happen. There are many reasons why we have chosen SCons over
  12. other alternatives, for example:
  13. - Godot can be compiled for a dozen different platforms: all PC
  14. platforms, all mobile platforms, many consoles, and WebAssembly.
  15. - Developers often need to compile for several of the platforms **at
  16. the same time**, or even different targets of the same platform. They
  17. can't afford reconfiguring and rebuilding the project each time.
  18. SCons can do this with no sweat, without breaking the builds.
  19. - SCons will *never* break a build no matter how many changes,
  20. configurations, additions, removals etc. You have more chances to die
  21. struck by lightning than needing to clean and rebuild in SCons.
  22. - Godot build process is not simple. Several files are generated by
  23. code (binders), others are parsed (shaders), and others need to offer
  24. customization (plugins). This requires complex logic which is easier
  25. to write in an actual programming language (like Python) rather than
  26. using a mostly macro-based language only meant for building.
  27. - Godot build process makes heavy use of cross-compiling tools. Each
  28. platform has a specific detection process, and all these must be
  29. handled as specific cases with special code written for each.
  30. So, please try to keep an open mind and get at least a little familiar with it
  31. if you are planning to build Godot yourself.
  32. Setup
  33. -----
  34. Please refer to the documentation for :ref:`doc_compiling_for_android`,
  35. :ref:`doc_compiling_for_ios`, :ref:`doc_compiling_for_linuxbsd`,
  36. :ref:`doc_compiling_for_macos`, :ref:`doc_compiling_for_uwp`,
  37. :ref:`doc_compiling_for_web`, and :ref:`doc_compiling_for_windows`.
  38. Note that for **Windows/Visual Studio**, you need to use ``x86_x64 Cross Tools
  39. Command Prompt for VS 2017`` or similar, depending on your install, instead of
  40. the standard Windows command prompt to enter the commands below.
  41. Platform selection
  42. ------------------
  43. Godot's build system will begin by detecting the platforms it can build
  44. for. If not detected, the platform will simply not appear on the list of
  45. available platforms. The build requirements for each platform are
  46. described in the rest of this tutorial section.
  47. SCons is invoked by just calling ``scons``. If no platform is specified,
  48. SCons will detect the target platform automatically based on the host platform.
  49. It will then start building for the target platform right away.
  50. To list the available target platforms, use ``scons platform=list``::
  51. scons platform=list
  52. scons: Reading SConscript files ...
  53. The following platforms are available:
  54. android
  55. javascript
  56. linuxbsd
  57. server
  58. windows
  59. Please run SCons again and select a valid platform: platform=<string>
  60. To build for a platform (for example, ``linuxbsd``), run with the ``platform=``
  61. (or ``p=`` to make it short) argument:
  62. ::
  63. scons platform=linuxbsd
  64. This will start the build process, which will take a while. By default, Godot's
  65. SCons setup is configured to use all CPU threads but one (to keep the system
  66. responsive during compilation). If you want to adjust how many CPU threads SCons
  67. will use, use the ``-j <threads>`` parameter to specify how many threads will be
  68. used for the build.
  69. Example for using 4 threads:
  70. ::
  71. scons platform=linuxbsd -j4
  72. .. _doc_introduction_to_the_buildsystem_resulting_binary:
  73. Resulting binary
  74. ----------------
  75. The resulting binaries will be placed in the ``bin/`` subdirectory,
  76. generally with this naming convention::
  77. godot.<platform>.<target>[.dev][.double].<arch>[.<extra_suffix>][.<ext>]
  78. For the previous build attempt, the result would look like this::
  79. ls bin
  80. bin/godot.linuxbsd.editor.x86_64
  81. This means that the binary is for Linux *or* \*BSD (*not* both), is not optimized, has the
  82. whole editor compiled in, and is meant for 64 bits.
  83. A Windows binary with the same configuration will look like this:
  84. .. code-block:: console
  85. C:\godot> dir bin/
  86. godot.windows.editor.64.exe
  87. Copy that binary to any location you like, as it contains the Project Manager,
  88. editor and all means to execute the game. However, it lacks the data to export
  89. it to the different platforms. For that the export templates are needed (which
  90. can be either downloaded from `godotengine.org <https://godotengine.org/>`__, or
  91. you can build them yourself).
  92. Aside from that, there are a few standard options that can be set in all
  93. build targets, and which will be explained below.
  94. .. _doc_introduction_to_the_buildsystem_target:
  95. Target
  96. ------
  97. Target controls if the editor is contained and debug flags are used.
  98. All builds are optimized. Each mode means:
  99. - **editor**: Build with editor, optimized, with debugging code (defines: ``TOOLS_ENABLED``, ``DEBUG_ENABLED``, ``-O2``/``/O2``)
  100. - **template_debug**: Build with C++ debugging symbols (defines: ``DEBUG_ENABLED``, ``-O2``/``/O2``)
  101. - **template_release**: Build without symbols (defines: ``-O3``/``/O2``)
  102. The editor is enabled by default in all PC targets (Linux, Windows, macOS),
  103. disabled for everything else. Disabling the editor produces a binary that can
  104. run projects but that does not include the editor or the Project Manager.
  105. ::
  106. scons platform=<platform> target=editor/template_debug/template_release
  107. Dev build
  108. ---------
  109. When doing engine development the ``dev_build`` option can be used together
  110. with ``target`` to enable dev-specific code. ``dev_build`` defines ``DEV_ENABLED``,
  111. disables optimization (``-O0``/``/0d``), enables generating debug symbols, and
  112. does not define ``NDEBUG`` (so ``assert()`` works in thirdparty libraries).
  113. ::
  114. scons platform=<platform> dev_build=yes
  115. This flag appends the ``.dev`` suffix (for development) to the generated
  116. binary name.
  117. Architecture
  118. ------------
  119. The ``arch`` option is meant to control the CPU or OS version intended to run the
  120. binaries. It is focused mostly on desktop platforms and ignored everywhere
  121. else.
  122. Supported values for the ``arch`` option are **auto**, **x86_32**, **x86_64**,
  123. **arm32**, **arm64**, **rv64**, **ppc32**, **ppc64** and **wasm32**.
  124. ::
  125. scons platform=<platform> arch={auto|x86_32|x86_64|arm32|arm64|rv64|ppc32|ppc64|wasm32}
  126. This flag appends the value of ``arch`` to resulting binaries when
  127. relevant. The default value ``arch=auto`` detects the architecture
  128. that matches the host platform.
  129. .. _doc_buildsystem_custom_modules:
  130. Custom modules
  131. --------------
  132. It's possible to compile modules residing outside of Godot's directory
  133. tree, along with the built-in modules.
  134. A ``custom_modules`` build option can be passed to the command line before
  135. compiling. The option represents a comma-separated list of directory paths
  136. containing a collection of independent C++ modules that can be seen as C++
  137. packages, just like the built-in ``modules/`` directory.
  138. For instance, it's possible to provide both relative, absolute, and user
  139. directory paths containing such modules:
  140. ::
  141. scons custom_modules="../modules,/abs/path/to/modules,~/src/godot_modules"
  142. .. note::
  143. If there's any custom module with the exact directory name as a built-in
  144. module, the engine will only compile the custom one. This logic can be used
  145. to override built-in module implementations.
  146. .. seealso::
  147. :ref:`doc_custom_modules_in_c++`
  148. Cleaning generated files
  149. ------------------------
  150. Sometimes, you may encounter an error due to generated files being present. You
  151. can remove them by using ``scons --clean <options>``, where ``<options>`` is the
  152. list of build options you've used to build Godot previously.
  153. Alternatively, you can use ``git clean -fixd`` which will clean build artifacts
  154. for all platforms and configurations. Beware, as this will remove all untracked
  155. and ignored files in the repository. Don't run this command if you have
  156. uncommitted work!
  157. Other build options
  158. -------------------
  159. There are several other build options that you can use to configure the
  160. way Godot should be built (compiler, debug options, etc.) as well as the
  161. features to include/disable.
  162. Check the output of ``scons --help`` for details about each option for
  163. the version you are willing to compile.
  164. .. _doc_overriding_build_options:
  165. Overriding the build options
  166. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. Using a file
  168. ^^^^^^^^^^^^
  169. The default ``custom.py`` file can be created at the root of the Godot Engine
  170. source to initialize any SCons build options passed via the command line:
  171. .. code-block:: python
  172. # custom.py
  173. optimize = "size"
  174. module_mono_enabled = "yes"
  175. use_llvm = "yes"
  176. extra_suffix = "game_title"
  177. You can also disable some of the builtin modules before compiling, saving some
  178. time it takes to build the engine. See :ref:`doc_optimizing_for_size` page for more details.
  179. .. seealso::
  180. You can use the online
  181. `Godot build options generator <https://godot-build-options-generator.github.io/>`__
  182. to generate a ``custom.py`` file containing SCons options.
  183. You can then save this file and place it at the root of your Godot source directory.
  184. Another custom file can be specified explicitly with the ``profile`` command
  185. line option, both overriding the default build configuration:
  186. .. code-block:: shell
  187. scons profile=path/to/custom.py
  188. .. note:: Build options set from the file can be overridden by the command line
  189. options.
  190. It's also possible to override the options conditionally:
  191. .. code-block:: python
  192. # custom.py
  193. import version
  194. # Override options specific for Godot 3.x and 4.x versions.
  195. if version.major == 3:
  196. pass
  197. elif version.major == 4:
  198. pass
  199. Using the SCONSFLAGS
  200. ^^^^^^^^^^^^^^^^^^^^
  201. ``SCONSFLAGS`` is an environment variable which is used by the SCons to set the
  202. options automatically without having to supply them via the command line.
  203. For instance, you may want to force a number of CPU threads with the
  204. aforementioned ``-j`` option for all future builds:
  205. .. tabs::
  206. .. code-tab:: bash Linux/macOS
  207. export SCONSFLAGS="-j4"
  208. .. code-tab:: bat Windows (cmd)
  209. set SCONSFLAGS=-j4
  210. .. code-tab:: powershell Windows (PowerShell)
  211. $env:SCONSFLAGS="-j4"
  212. Export templates
  213. ----------------
  214. Official export templates are downloaded from the Godot Engine site:
  215. `godotengine.org <https://godotengine.org/>`__. However, you might want
  216. to build them yourself (in case you want newer ones, you are using custom
  217. modules, or simply don't trust your own shadow).
  218. If you download the official export templates package and unzip it, you
  219. will notice that most files are optimized binaries or packages for each
  220. platform:
  221. .. code-block:: none
  222. android_debug.apk
  223. android_release.apk
  224. web_debug.zip
  225. web_release.zip
  226. linux_server_32
  227. linux_server_64
  228. linux_x11_32_debug
  229. linux_x11_32_release
  230. linux_x11_64_debug
  231. linux_x11_64_release
  232. macos.zip
  233. version.txt
  234. windows_32_debug.exe
  235. windows_32_release.exe
  236. windows_64_debug.exe
  237. windows_64_release.exe
  238. To create those yourself, follow the instructions detailed for each
  239. platform in this same tutorial section. Each platform explains how to
  240. create its own template.
  241. The ``version.txt`` file should contain the corresponding Godot version
  242. identifier. This file is used to install export templates in a version-specific
  243. directory to avoid conflicts. For instance, if you are building export templates
  244. for Godot 3.1.1, ``version.txt`` should contain ``3.1.1.stable`` on the first
  245. line (and nothing else). This version identifier is based on the ``major``,
  246. ``minor``, ``patch`` (if present) and ``status`` lines of the
  247. `version.py file in the Godot Git repository <https://github.com/godotengine/godot/blob/master/version.py>`__.
  248. If you are developing for multiple platforms, macOS is definitely the most
  249. convenient host platform for cross-compilation, since you can cross-compile for
  250. almost every target (except for UWP). Linux and Windows come in second place,
  251. but Linux has the advantage of being the easier platform to set this up.