introduction_to_the_buildsystem.rst 16 KB

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