2
0

compiling_for_linuxbsd.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. .. _doc_compiling_for_linuxbsd:
  2. Compiling for Linux, \*BSD
  3. ==========================
  4. .. highlight:: shell
  5. .. seealso::
  6. This page describes how to compile Linux editor and export template binaries from source.
  7. If you're looking to export your project to Linux instead, read :ref:`doc_exporting_for_linux`.
  8. Requirements
  9. ------------
  10. For compiling under Linux or other Unix variants, the following is
  11. required:
  12. - GCC 7+ or Clang 6+.
  13. - Python 3.6+ <https://www.python.org/downloads/>`_.
  14. - `SCons 3.0+ <https://scons.org/pages/download.html>`_ build system. If your distribution uses Python 2 by default,
  15. or you are using a version of SCons prior to 3.1.2, you will need to change
  16. the version of Python that SCons uses by changing the shebang (the first line)
  17. of the SCons script file to ``#! /usr/bin/python3``.
  18. Use the command ``which scons`` to find the location of the SCons script file.
  19. - pkg-config (used to detect the dependencies below).
  20. - X11, Xcursor, Xinerama, Xi and XRandR development libraries.
  21. - MesaGL development libraries.
  22. - ALSA development libraries.
  23. - PulseAudio development libraries.
  24. - *Optional* - libudev (build with ``udev=yes``).
  25. .. seealso:: To get the Godot source code for compiling, see
  26. :ref:`doc_getting_source`.
  27. For a general overview of SCons usage for Godot, see
  28. :ref:`doc_introduction_to_the_buildsystem`.
  29. .. _doc_compiling_for_linuxbsd_oneliners:
  30. Distro-specific one-liners
  31. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  32. +------------------+-----------------------------------------------------------------------------------------------------------+
  33. | **Alpine Linux** | :: |
  34. | | |
  35. | | apk add scons pkgconf gcc g++ libx11-dev libxcursor-dev libxinerama-dev libxi-dev libxrandr-dev \ |
  36. | | mesa-dev libexecinfo-dev eudev-dev alsa-lib-dev pulseaudio-dev |
  37. +------------------+-----------------------------------------------------------------------------------------------------------+
  38. | **Arch Linux** | :: |
  39. | | |
  40. | | pacman -S --needed scons pkgconf gcc libxcursor libxinerama libxi libxrandr mesa glu libglvnd \ |
  41. | | alsa-lib pulseaudio |
  42. +------------------+-----------------------------------------------------------------------------------------------------------+
  43. | **Debian** / | :: |
  44. | **Ubuntu** | |
  45. | | sudo apt-get install build-essential scons pkg-config libx11-dev libxcursor-dev libxinerama-dev \ |
  46. | | libgl1-mesa-dev libglu-dev libasound2-dev libpulse-dev libudev-dev libxi-dev libxrandr-dev |
  47. +------------------+-----------------------------------------------------------------------------------------------------------+
  48. | **Fedora** | :: |
  49. | | |
  50. | | sudo dnf install scons pkgconfig libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel \ |
  51. | | libXi-devel mesa-libGL-devel mesa-libGLU-devel alsa-lib-devel pulseaudio-libs-devel \ |
  52. | | libudev-devel gcc-c++ libstdc++-static libatomic-static |
  53. +------------------+-----------------------------------------------------------------------------------------------------------+
  54. | **FreeBSD** | :: |
  55. | | |
  56. | | sudo pkg install py37-scons pkgconf xorg-libraries libXcursor libXrandr libXi xorgproto libGLU \ |
  57. | | alsa-lib pulseaudio |
  58. | | |
  59. +------------------+-----------------------------------------------------------------------------------------------------------+
  60. | **Gentoo** | :: |
  61. | | |
  62. | | emerge -an dev-util/scons x11-libs/libX11 x11-libs/libXcursor x11-libs/libXinerama x11-libs/libXi \ |
  63. | | media-libs/mesa media-libs/glu media-libs/alsa-lib media-sound/pulseaudio |
  64. +------------------+-----------------------------------------------------------------------------------------------------------+
  65. | **Mageia** | :: |
  66. | | |
  67. | | urpmi scons task-c++-devel pkgconfig "pkgconfig(alsa)" "pkgconfig(glu)" "pkgconfig(libpulse)" \ |
  68. | | "pkgconfig(udev)" "pkgconfig(x11)" "pkgconfig(xcursor)" "pkgconfig(xinerama)" "pkgconfig(xi)" \ |
  69. | | "pkgconfig(xrandr)" |
  70. +------------------+-----------------------------------------------------------------------------------------------------------+
  71. | **OpenBSD** | :: |
  72. | | |
  73. | | pkg_add python scons llvm |
  74. +------------------+-----------------------------------------------------------------------------------------------------------+
  75. | **openSUSE** | :: |
  76. | | |
  77. | | sudo zypper install scons pkgconfig libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel \ |
  78. | | libXi-devel Mesa-libGL-devel alsa-devel libpulse-devel libudev-devel gcc-c++ libGLU1 |
  79. +------------------+-----------------------------------------------------------------------------------------------------------+
  80. | **NetBSD** | :: |
  81. | | |
  82. | | pkg_add pkg-config py37-scons |
  83. | | |
  84. | | For audio support, you can optionally install ``pulseaudio``. |
  85. +------------------+-----------------------------------------------------------------------------------------------------------+
  86. | **Solus** | :: |
  87. | | |
  88. | | sudo eopkg install -c system.devel scons libxcursor-devel libxinerama-devel libxi-devel \ |
  89. | | libxrandr-devel mesalib-devel libglu alsa-lib-devel pulseaudio-devel |
  90. +------------------+-----------------------------------------------------------------------------------------------------------+
  91. Compiling
  92. ---------
  93. Start a terminal, go to the root dir of the engine source code and type:
  94. ::
  95. scons -j8 platform=linuxbsd
  96. A good rule of thumb for the ``-j`` (*jobs*) flag, is to have at least as many
  97. threads compiling Godot as you have cores in your CPU, if not one or two more.
  98. Feel free to add the ``-j`` option to any SCons command you see below.
  99. You can automatically use all available CPU cores with command substitution.
  100. On Linux, you can use ``nproc``:
  101. ::
  102. scons -j$(nproc)
  103. On \*BSD, you can use ``sysctl -n hw.ncpu``:
  104. ::
  105. scons -j$(sysctl -n hw.ncpu)
  106. .. note::
  107. Prior to Godot 4.0, the Linux/\*BSD target was called ``x11`` instead of
  108. ``linuxbsd``. If you are looking to compile Godot 3.x, make sure to use the
  109. `stable branch of this documentation <https://docs.godotengine.org/en/stable/development/compiling/compiling_for_x11.html>`__.
  110. If all goes well, the resulting binary executable will be placed in the
  111. "bin" subdirectory. This executable file contains the whole engine and
  112. runs without any dependencies. Executing it will bring up the project
  113. manager.
  114. .. note::
  115. If you wish to compile using Clang rather than GCC, use this command:
  116. ::
  117. scons platform=linuxbsd use_llvm=yes
  118. Using Clang appears to be a requirement for OpenBSD, otherwise fonts
  119. would not build.
  120. .. note:: If you are compiling Godot for production use, then you can
  121. make the final executable smaller and faster by adding the
  122. SCons options ``target=template_release production=yes``.
  123. If you are compiling Godot with GCC, you can make the binary
  124. even smaller and faster by adding the SCons option ``use_lto=yes``.
  125. As link-time optimization is a memory-intensive process,
  126. this will require about 7 GB of available RAM while compiling.
  127. .. note:: If you want to use separate editor settings for your own Godot builds
  128. and official releases, you can enable
  129. :ref:`doc_data_paths_self_contained_mode` by creating a file called
  130. ``._sc_`` or ``_sc_`` in the ``bin/`` folder.
  131. Running a headless/server build
  132. -------------------------------
  133. To run in *headless* mode which provides editor functionality to export
  134. projects in an automated manner, use the normal build::
  135. scons -j8 platform=linuxbsd target=editor
  136. And then use the ``--headless`` command line argument::
  137. ./bin/godot.linuxbsd.editor.x86_64 --headless
  138. To compile a debug *server* build which can be used with
  139. :ref:`remote debugging tools <doc_command_line_tutorial>`, use::
  140. scons -j8 platform=linuxbsd target=template_debug
  141. To compile a *server* build which is optimized to run dedicated game servers,
  142. use::
  143. scons -j8 platform=linuxbsd target=template_release production=yes
  144. Building export templates
  145. -------------------------
  146. .. warning:: Linux binaries usually won't run on distributions that are
  147. older than the distribution they were built on. If you wish to
  148. distribute binaries that work on most distributions,
  149. you should build them on an old distribution such as Ubuntu 16.04.
  150. You can use a virtual machine or a container to set up a suitable
  151. build environment.
  152. To build Linux or \*BSD export templates, run the build system with the
  153. following parameters:
  154. - (32 bits)
  155. ::
  156. scons platform=linuxbsd target=template_release arch=x86_32
  157. scons platform=linuxbsd target=template_debug arch=x86_32
  158. - (64 bits)
  159. ::
  160. scons platform=linuxbsd target=template_release arch=x86_64
  161. scons platform=linuxbsd target=template_debug arch=x86_64
  162. Note that cross-compiling for the opposite bits (64/32) as your host
  163. platform is not always straight-forward and might need a chroot environment.
  164. To create standard export templates, the resulting files in the ``bin/`` folder
  165. must be copied to:
  166. ::
  167. $HOME/.local/share/godot/export_templates/<version>/
  168. and named like this (even for \*BSD which is seen as "Linux/X11" by Godot):
  169. ::
  170. linux_x11_32_debug
  171. linux_x11_32_release
  172. linux_x11_64_debug
  173. linux_x11_64_release
  174. However, if you are writing your custom modules or custom C++ code, you
  175. might instead want to configure your binaries as custom export templates
  176. here:
  177. .. image:: img/lintemplates.png
  178. You don't even need to copy them, you can just reference the resulting
  179. files in the ``bin/`` directory of your Godot source folder, so the next
  180. time you build, you automatically have the custom templates referenced.
  181. Using Clang and LLD for faster development
  182. ------------------------------------------
  183. You can also use Clang and LLD to build Godot. This has two upsides compared to
  184. the default GCC + GNU ld setup:
  185. - LLD links Godot significantly faster compared to GNU ld or gold. This leads to
  186. faster iteration times.
  187. - Clang tends to give more useful error messages compared to GCC.
  188. To do so, install Clang and the ``lld`` package from your distribution's package manager
  189. then use the following SCons command::
  190. scons platform=linuxbsd use_llvm=yes linker=lld
  191. After the build is completed, a new binary with a ``.llvm`` suffix will be
  192. created in the ``bin/`` folder.
  193. It's still recommended to use GCC for production builds as they can be compiled using
  194. link-time optimization, making the resulting binaries smaller and faster.
  195. Using mold for faster development
  196. ---------------------------------
  197. For even faster linking compared to LLD, you can use `mold <https://github.com/rui314/mold>`__.
  198. mold can be used with either GCC or Clang.
  199. As of January 2023, mold is not readily available in Linux distribution
  200. repositories, so you will have to install its binaries manually.
  201. - Download mold binaries from its `releases page <https://github.com/rui314/mold/releases/latest>`__.
  202. - Extract the ``.tar.gz`` file, then move the extraced folder to a location such as ``.local/share/mold``.
  203. - Add ``$HOME/.local/share/mold/bin`` to your user's ``PATH`` environment variable.
  204. For example, you can add the following line at the end of your ``$HOME/.bash_profile`` file:
  205. ::
  206. PATH="$HOME/.local/share/mold/bin:$PATH"
  207. - Open a new terminal (or run ``source "$HOME/.bash_profile"``),
  208. then use the following SCons command when compiling Godot::
  209. scons platform=linuxbsd linker=mold
  210. Using system libraries for faster development
  211. ---------------------------------------------
  212. `Godot bundles the source code of various third-party libraries. <https://github.com/godotengine/godot/tree/master/thirdparty>`__
  213. You can choose to use system versions of third-party libraries instead.
  214. This makes the Godot binary faster to link, as third-party libraries are
  215. dynamically linked. Therefore, they don't need to be statically linked
  216. every time you build the engine (even on small incremental changes).
  217. However, not all Linux distributions have packages for third-party libraries
  218. available (or they may not be up-to-date).
  219. Moving to system libraries can reduce linking times by several seconds on slow
  220. CPUs, but it requires manual testing depending on your Linux distribution. Also,
  221. you may not be able to use system libraries for everything due to bugs in the
  222. system library packages (or in the build system, as this feature is less
  223. tested).
  224. To compile Godot with system libraries, install these dependencies *on top* of the ones
  225. listed in the :ref:`doc_compiling_for_linuxbsd_oneliners`:
  226. +------------------+-----------------------------------------------------------------------------------------------------------+
  227. | **Fedora** | :: |
  228. | | |
  229. | | sudo dnf install embree-devel enet-devel glslang-devel graphite2-devel harfbuzz-devel libicu-devel \ |
  230. | | libsquish-devel libtheora-devel libvorbis-devel libwebp-devel libzstd-devel mbedtls-devel \ |
  231. | | miniupnpc-devel |
  232. +------------------+-----------------------------------------------------------------------------------------------------------+
  233. After installing all required packages, use the following command to build Godot:
  234. .. NOTE: Some `builtin_` options aren't used here because they break the build as of January 2023
  235. (tested on Fedora 37).
  236. ::
  237. scons platform=linuxbsd builtin_embree=no builtin_enet=no builtin_freetype=no builtin_graphite=no builtin_harfbuzz=no builtin_libogg=no builtin_libpng=no builtin_libtheora=no builtin_libvorbis=no builtin_libwebp=no builtin_mbedtls=no builtin_miniupnpc=no builtin_pcre2=no builtin_zlib=no builtin_zstd=no
  238. You can view a list of all built-in libraries that have system alternatives by
  239. running ``scons -h``, then looking for options starting with ``builtin_``.
  240. .. warning::
  241. When using system libraries, the resulting library is **not** portable
  242. across Linux distributions anymore. Do not use this approach for creating
  243. binaries you intend to distribute to others, unless you're creating a
  244. package for a Linux distribution.
  245. Using Pyston for faster development
  246. -----------------------------------
  247. You can use `Pyston <https://www.pyston.org/>`__ to run SCons. Pyston is a JIT-enabled
  248. implementation of the Python language (which SCons is written in). It is currently
  249. only compatible with Linux. Pyston can speed up incremental builds significantly,
  250. often by a factor between 1.5× and 2×. Pyston can be combined with Clang and LLD
  251. to get even faster builds.
  252. - Download the `latest portable Pyston release <https://github.com/pyston/pyston/releases/latest>`__.
  253. - Extract the portable ``.tar.gz`` to a set location, such as ``$HOME/.local/opt/pyston/`` (create folders as needed).
  254. - Use ``cd`` to reach the extracted Pyston folder from a terminal,
  255. then run ``./pyston -m pip install scons`` to install SCons within Pyston.
  256. - To make SCons via Pyston easier to run, create a symbolic link of its wrapper
  257. script to a location in your ``PATH`` environment variable::
  258. ln -s ~/.local/opt/pyston/bin/scons ~/.local/bin/pyston-scons
  259. - Instead of running ``scons <build arguments>``, run ``pyston-scons <build arguments>``
  260. to compile Godot.
  261. If you can't run ``pyston-scons`` after creating the symbolic link,
  262. make sure ``$HOME/.local/bin/`` is part of your user's ``PATH`` environment variable.
  263. .. note::
  264. Alternatively, you can run ``python -m pip install pyston_lite_autoload``
  265. then run SCons as usual. This will automatically load a subset of Pyston's
  266. optimizations in any Python program you run. However, this won't bring as
  267. much of a performance improvement compared to installing "full" Pyston.