introduction_to_the_buildsystem.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. .. _doc_introduction_to_the_buildsystem:
  2. Introduction to the buildsystem
  3. ===============================
  4. .. highlight:: shell
  5. Godot is a primarily C++ project and it :ref:`uses the SCons build system. <doc_faq_why_scons>`
  6. We love SCons for how maintainable and easy to set up it makes our buildsystem. And thanks to
  7. that compiling Godot from source can be as simple as running:
  8. ::
  9. scons
  10. This produces an editor build for your current platform, operating system, and architecture.
  11. You can change what gets built by specifying a target, a platform, and/or an architecture.
  12. For example, to build an export template used for running exported games, you can run:
  13. ::
  14. scons target=template_release
  15. If you plan to debug or develop the engine, then you might want to enable the ``dev_build``
  16. option to enable dev-only debugging code:
  17. ::
  18. scons dev_build=yes
  19. Following sections in the article will explain these and other universal options in more detail. But
  20. before you can compile Godot, you need to install a few prerequisites. Please refer to the platform
  21. documentation to learn more:
  22. - :ref:`doc_compiling_for_android`
  23. - :ref:`doc_compiling_for_ios`
  24. - :ref:`doc_compiling_for_linuxbsd`
  25. - :ref:`doc_compiling_for_macos`
  26. - :ref:`doc_compiling_for_web`
  27. - :ref:`doc_compiling_for_windows`
  28. These articles cover in great detail both how to setup your environment to compile Godot on a specific
  29. platform, and how to compile for that platform. Please feel free to go back and forth between them and
  30. this article to reference platform-specific and universal configuration options.
  31. Using multi-threading
  32. ---------------------
  33. The build process may take a while, depending on how powerful your system is. By default, Godot's
  34. SCons setup is configured to use all CPU threads but one (to keep the system responsive during
  35. compilation). If the system has 4 CPU threads or fewer, it will use all threads by default.
  36. If you want to adjust how many CPU threads SCons will use, use the ``-j<threads>``
  37. parameter to specify how many threads will be used for the build.
  38. Example for using 12 threads:
  39. ::
  40. scons -j12
  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. .. code:: text
  52. scons platform=list
  53. scons: Reading SConscript files ...
  54. The following platforms are available:
  55. android
  56. ios
  57. linuxbsd
  58. macos
  59. web
  60. windows
  61. Please run SCons again and select a valid platform: platform=<string>
  62. To build for a platform (for example, ``linuxbsd``), run with the ``platform=``
  63. (or ``p=`` to make it short) argument:
  64. ::
  65. scons platform=linuxbsd
  66. .. _doc_introduction_to_the_buildsystem_resulting_binary:
  67. Resulting binary
  68. ----------------
  69. The resulting binaries will be placed in the ``bin/`` subdirectory,
  70. generally with this naming convention:
  71. ::
  72. godot.<platform>.<target>[.dev][.double].<arch>[.<extra_suffix>][.<ext>]
  73. For the previous build attempt, the result would look like this:
  74. .. code-block:: console
  75. ls bin
  76. bin/godot.linuxbsd.editor.x86_64
  77. This means that the binary is for Linux *or* \*BSD (*not* both), is not optimized, has the
  78. whole editor compiled in, and is meant for 64 bits.
  79. A Windows binary with the same configuration will look like this:
  80. .. code-block:: doscon
  81. C:\godot> dir bin/
  82. godot.windows.editor.64.exe
  83. Copy that binary to any location you like, as it contains the Project Manager,
  84. editor and all means to execute the game. However, it lacks the data to export
  85. it to the different platforms. For that the export templates are needed (which
  86. can be either downloaded from `godotengine.org <https://godotengine.org/>`__, or
  87. you can build them yourself).
  88. Aside from that, there are a few standard options that can be set in all
  89. build targets, and which will be explained below.
  90. .. _doc_introduction_to_the_buildsystem_target:
  91. Target
  92. ------
  93. The ``target`` option controls if the editor is compiled and debug flags are used.
  94. Optimization levels (``optimize``) and whether each build contains debug symbols
  95. (``debug_symbols``) is controlled separately from the target. Each mode means:
  96. - ``target=editor``: Build an editor binary (defines ``TOOLS_ENABLED`` and ``DEBUG_ENABLED``)
  97. - ``target=template_debug``: Build a debug export template (defines ``DEBUG_ENABLED``)
  98. - ``target=template_release``: Build a release export template
  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. The list of :ref:`command line arguments <doc_command_line_tutorial>`
  103. available varies depending on the build type.
  104. ::
  105. scons platform=<platform> target=editor|template_debug|template_release
  106. .. _doc_introduction_to_the_buildsystem_development_and_production_aliases:
  107. Development and production aliases
  108. ----------------------------------
  109. When creating builds for development (running debugging/:ref:`profiling <doc_using_cpp_profilers>`
  110. tools), you often have different goals compared to production builds
  111. (making binaries as fast and small as possible).
  112. Godot provides two aliases for this purpose:
  113. - ``dev_mode=yes`` is an alias for ``verbose=yes warnings=extra werror=yes
  114. tests=yes``. This enables warnings-as-errors behavior (similar to Godot's
  115. continuous integration setup) and also builds :ref:`unit tests
  116. <doc_unit_testing>` so you can run them locally.
  117. - ``production=yes`` is an alias for ``use_static_cpp=yes debug_symbols=no
  118. lto=auto``. Statically linking libstdc++ allows for better binary portability
  119. when compiling for Linux. This alias also enables link-time optimization when
  120. compiling for Linux, Web and Windows with MinGW, but keeps LTO disabled when
  121. compiling for macOS, iOS or Windows with MSVC. This is because LTO on those
  122. platforms is very slow to link or has issues with the generated code.
  123. You can manually override options from those aliases by specifying them on the
  124. same command line with different values. For example, you can use ``scons
  125. production=yes debug_symbols=yes`` to create production-optimized binaries with
  126. debugging symbols included.
  127. Dev build
  128. ---------
  129. .. note::
  130. ``dev_build`` should **not** be confused with ``dev_mode``, which is an
  131. alias for several development-related options (see above).
  132. When doing engine development the ``dev_build`` option can be used together
  133. with ``target`` to enable dev-specific code. ``dev_build`` defines ``DEV_ENABLED``,
  134. disables optimization (``-O0``/``/0d``), enables generating debug symbols, and
  135. does not define ``NDEBUG`` (so ``assert()`` works in thirdparty libraries).
  136. ::
  137. scons platform=<platform> dev_build=yes
  138. This flag appends the ``.dev`` suffix (for development) to the generated
  139. binary name.
  140. .. seealso::
  141. There are additional SCons options to enable *sanitizers*, which are tools
  142. you can enable at compile-time to better debug certain engine issues.
  143. See :ref:`doc_using_sanitizers` for more information.
  144. .. _doc_introduction_to_the_buildsystem_debugging_symbols:
  145. Debugging symbols
  146. -----------------
  147. By default, ``debug_symbols=no`` is used, which means **no** debugging symbols
  148. are included in compiled binaries. Use ``debug_symbols=yes`` to include debug
  149. symbols within compiled binaries, which allows debuggers and profilers to work
  150. correctly. Debugging symbols are also required for Godot's crash stacktraces to
  151. display with references to source code files and lines.
  152. The downside is that debugging symbols are large files (significantly larger
  153. than the binaries themselves). As a result, official binaries currently do not
  154. include debugging symbols. This means you need to compile Godot yourself to have
  155. access to debugging symbols.
  156. When using ``debug_symbols=yes``, you can also use
  157. ``separate_debug_symbols=yes`` to put debug information in a separate file with
  158. a ``.debug`` suffix. This allows distributing both files independently. Note
  159. that on Windows, when compiling with MSVC, debugging information is *always*
  160. written to a separate ``.pdb`` file regardless of ``separate_debug_symbols``.
  161. .. tip::
  162. Use the ``strip <path/to/binary>`` command to remove debugging symbols from
  163. a binary you've already compiled.
  164. Optimization level
  165. ------------------
  166. Several compiler optimization levels can be chosen from:
  167. - ``optimize=speed_trace`` *(default when targeting non-Web platforms)*: Favors
  168. execution speed at the cost of larger binary size. Optimizations may sometimes
  169. negatively impact debugger usage (stack traces may be less accurate. If this
  170. occurs to you, use ``optimize=debug`` instead.
  171. - ``optimize=speed``: Favors even more execution speed, at the cost of even
  172. larger binary size compared to ``optimize=speed_trace``. Even less friendly to
  173. debugging compared to ``optimize=debug``, as this uses the most aggressive
  174. optimizations available.
  175. - ``optimize=size`` *(default when targeting the Web platform)*: Favors small
  176. binaries at the cost of slower execution speed.
  177. - ``optimize=size_extra``: Favors even smaller binaries, at the cost of even
  178. slower execution speed compared to ``optimize=size``.
  179. - ``optimize=debug``: Only enables optimizations that do not impact debugging in
  180. any way. This results in faster binaries than ``optimize=none``, but slower
  181. binaries than ``optimize=speed_trace``.
  182. - ``optimize=none``: Do not perform any optimization. This provides the fastest
  183. build times, but the slowest execution times.
  184. - ``optimize=custom`` *(advanced users only)*: Do not pass optimization
  185. arguments to the C/C++ compilers. You will have to pass arguments manually
  186. using the ``cflags``, ``ccflags`` and ``cxxflags`` SCons options.
  187. Architecture
  188. ------------
  189. The ``arch`` option is meant to control the CPU or OS version intended to run the
  190. binaries. It is focused mostly on desktop platforms and ignored everywhere
  191. else.
  192. Supported values for the ``arch`` option are **auto**, **x86_32**, **x86_64**,
  193. **arm32**, **arm64**, **rv64**, **ppc32**, **ppc64** and **wasm32**.
  194. ::
  195. scons platform=<platform> arch={auto|x86_32|x86_64|arm32|arm64|rv64|ppc32|ppc64|wasm32}
  196. This flag appends the value of ``arch`` to resulting binaries when
  197. relevant. The default value ``arch=auto`` detects the architecture
  198. that matches the host platform.
  199. .. _doc_buildsystem_custom_modules:
  200. Custom modules
  201. --------------
  202. It's possible to compile modules residing outside of Godot's directory
  203. tree, along with the built-in modules.
  204. A ``custom_modules`` build option can be passed to the command line before
  205. compiling. The option represents a comma-separated list of directory paths
  206. containing a collection of independent C++ modules that can be seen as C++
  207. packages, just like the built-in ``modules/`` directory.
  208. For instance, it's possible to provide both relative, absolute, and user
  209. directory paths containing such modules:
  210. ::
  211. scons custom_modules="../modules,/abs/path/to/modules,~/src/godot_modules"
  212. .. note::
  213. If there's any custom module with the exact directory name as a built-in
  214. module, the engine will only compile the custom one. This logic can be used
  215. to override built-in module implementations.
  216. .. seealso::
  217. :ref:`doc_custom_modules_in_cpp`
  218. Cleaning generated files
  219. ------------------------
  220. Sometimes, you may encounter an error due to generated files being present. You
  221. can remove them by using ``scons --clean <options>``, where ``<options>`` is the
  222. list of build options you've used to build Godot previously.
  223. Alternatively, you can use ``git clean -fixd`` which will clean build artifacts
  224. for all platforms and configurations. Beware, as this will remove all untracked
  225. and ignored files in the repository. Don't run this command if you have
  226. uncommitted work!
  227. Other build options
  228. -------------------
  229. There are several other build options that you can use to configure the
  230. way Godot should be built (compiler, debug options, etc.) as well as the
  231. features to include/disable.
  232. Check the output of ``scons --help`` for details about each option for
  233. the version you are willing to compile.
  234. .. _doc_overriding_build_options:
  235. Overriding the build options
  236. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  237. Using a file
  238. ^^^^^^^^^^^^
  239. The default ``custom.py`` file can be created at the root of the Godot Engine
  240. source to initialize any SCons build options passed via the command line:
  241. .. code-block:: python
  242. :caption: custom.py
  243. optimize = "size"
  244. module_mono_enabled = "yes"
  245. use_llvm = "yes"
  246. extra_suffix = "game_title"
  247. You can also disable some of the built-in modules before compiling, saving some
  248. time it takes to build the engine. See :ref:`doc_optimizing_for_size` page for more details.
  249. .. seealso::
  250. You can use the online
  251. `Godot build options generator <https://godot-build-options-generator.github.io/>`__
  252. to generate a ``custom.py`` file containing SCons options.
  253. You can then save this file and place it at the root of your Godot source directory.
  254. Another custom file can be specified explicitly with the ``profile`` command
  255. line option, both overriding the default build configuration:
  256. .. code-block:: shell
  257. scons profile=path/to/custom.py
  258. .. note:: Build options set from the file can be overridden by the command line
  259. options.
  260. It's also possible to override the options conditionally:
  261. .. code-block:: python
  262. :caption: custom.py
  263. import version
  264. # Override options specific for Godot 3.x and 4.x versions.
  265. if version.major == 3:
  266. pass
  267. elif version.major == 4:
  268. pass
  269. Using the SCONSFLAGS
  270. ^^^^^^^^^^^^^^^^^^^^
  271. ``SCONSFLAGS`` is an environment variable which is used by the SCons to set the
  272. options automatically without having to supply them via the command line.
  273. For instance, you may want to force a number of CPU threads with the
  274. aforementioned ``-j`` option for all future builds:
  275. .. tabs::
  276. .. code-tab:: bash Linux/macOS
  277. export SCONSFLAGS="-j4"
  278. .. code-tab:: bat Windows (cmd)
  279. set SCONSFLAGS=-j4
  280. .. code-tab:: powershell Windows (PowerShell)
  281. $env:SCONSFLAGS="-j4"
  282. SCU (single compilation unit) build
  283. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  284. Regular builds tend to be bottlenecked by including large numbers of headers
  285. in each compilation translation unit. Primarily to speed up development (rather
  286. than for production builds), Godot offers a "single compilation unit" build
  287. (aka "Unity / Jumbo" build).
  288. For the folders accelerated by this option, multiple ``.cpp`` files are
  289. compiled in each translation unit, so headers can be shared between multiple
  290. files, which can dramatically decrease build times.
  291. To perform an SCU build, use the ``scu_build=yes`` SCons option.
  292. .. note:: When developing a Pull Request using SCU builds, be sure to make a
  293. regular build prior to submitting the PR. This is because SCU builds
  294. by nature include headers from earlier ``.cpp`` files in the
  295. translation unit, therefore won't catch all the includes you will
  296. need in a regular build. The CI will catch these errors, but it will
  297. usually be faster to catch them on a local build on your machine.
  298. Export templates
  299. ----------------
  300. Official export templates are downloaded from the Godot Engine site:
  301. `godotengine.org <https://godotengine.org/>`__. However, you might want
  302. to build them yourself (in case you want newer ones, you are using custom
  303. modules, or simply don't trust your own shadow).
  304. If you download the official export templates package and unzip it, you
  305. will notice that most files are optimized binaries or packages for each
  306. platform:
  307. .. code-block:: none
  308. android_debug.apk
  309. android_release.apk
  310. android_source.zip
  311. ios.zip
  312. linux_debug.arm32
  313. linux_debug.arm64
  314. linux_debug.x86_32
  315. linux_debug.x86_64
  316. linux_release.arm32
  317. linux_release.arm64
  318. linux_release.x86_32
  319. linux_release.x86_64
  320. macos.zip
  321. version.txt
  322. web_debug.zip
  323. web_dlink_debug.zip
  324. web_dlink_nothreads_debug.zip
  325. web_dlink_nothreads_release.zip
  326. web_dlink_release.zip
  327. web_nothreads_debug.zip
  328. web_nothreads_release.zip
  329. web_release.zip
  330. windows_debug_x86_32_console.exe
  331. windows_debug_x86_32.exe
  332. windows_debug_x86_64_console.exe
  333. windows_debug_x86_64.exe
  334. windows_debug_arm64_console.exe
  335. windows_debug_arm64.exe
  336. windows_release_x86_32_console.exe
  337. windows_release_x86_32.exe
  338. windows_release_x86_64_console.exe
  339. windows_release_x86_64.exe
  340. windows_release_arm64_console.exe
  341. windows_release_arm64.exe
  342. To create those yourself, follow the instructions detailed for each
  343. platform in this same tutorial section. Each platform explains how to
  344. create its own template.
  345. The ``version.txt`` file should contain the corresponding Godot version
  346. identifier. This file is used to install export templates in a version-specific
  347. directory to avoid conflicts. For instance, if you are building export templates
  348. for Godot 3.1.1, ``version.txt`` should contain ``3.1.1.stable`` on the first
  349. line (and nothing else). This version identifier is based on the ``major``,
  350. ``minor``, ``patch`` (if present) and ``status`` lines of the
  351. `version.py file in the Godot Git repository <https://github.com/godotengine/godot/blob/master/version.py>`__.
  352. If you are developing for multiple platforms, macOS is definitely the most
  353. convenient host platform for cross-compilation, since you can cross-compile for
  354. every target. Linux and Windows come in second place,
  355. but Linux has the advantage of being the easier platform to set this up.