2
0

cmake.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. .. _doc_godot_cpp_build_system_cmake:
  2. Secondary build system: Working with CMake
  3. ==========================================
  4. .. seealso::
  5. This page documents how to compile godot-cpp. If you're looking to compile
  6. Godot instead, see :ref:`doc_introduction_to_the_buildsystem`.
  7. Beside the SCons_ based build system, godot-cpp also provides a CMakeLists.txt_
  8. file to support users that prefer using CMake_ over SCons for their build
  9. system.
  10. While actively supported, the CMake system is considered secondary to the
  11. SCons build system. This means it may lack some features that are available to
  12. projects using SCons.
  13. .. _CMakeLists.txt: https://github.com/godotengine/godot-cpp/blob/master/CMakeLists.txt
  14. .. _CMake: http://scons.org
  15. .. _Scons: http://cmake.org
  16. Introduction
  17. ------------
  18. Compiling godot-cpp independently of an extension project is mainly for
  19. godot-cpp developers, package maintainers, and CI/CD.
  20. Examples of how to use CMake to consume the godot-cpp library as part of an
  21. extension project:
  22. * `godot-cpp-template <https://github.com/godotengine/godot-cpp-template/>`__
  23. * `godot_roguelite <https://github.com/vorlac/godot-roguelite/>`__
  24. * `godot-orchestrator <https://github.com/CraterCrash/godot-orchestrator/>`__
  25. Examples for configuring godot-cpp are listed at the bottom of the page, many
  26. of which may help with configuring your project.
  27. CMake's ``Debug`` vs Godot's ``template_debug``
  28. -----------------------------------------------
  29. Something that has come up during many discussions is the conflation of a
  30. compilation of C++ source code with debug symbols enabled, and compiling a
  31. Godot extension with debug features enabled. The two concepts are not mutually
  32. exclusive.
  33. Debug Features
  34. ~~~~~~~~~~~~~~
  35. Enables a pre-processor definition to selectively compile code to help users of
  36. a Godot extension with their own project.
  37. Debug features are enabled in ``editor`` and ``template_debug`` builds, which
  38. can be specified during the configure phase like so:
  39. .. code-block:: shell
  40. cmake -S godot-cpp -B cmake-build -DGODOTCPP_TARGET=<target choice>
  41. Debug
  42. ~~~~~
  43. Sets compiler flags so that debug symbols are generated to help godot extension
  44. developers debug their extension.
  45. ``Debug`` is the default build type for CMake projects, the way to select another
  46. depends on the generator used:
  47. * For single configuration generators, add ``-DCMAKE_BUILD_TYPE=<type>`` to the
  48. configure command.
  49. * For multi-config generators, add ``--config <type>`` to the build command.
  50. Where ``<type>`` is one of ``Debug``, ``Release``, ``RelWithDebInfo``, and
  51. ``MinSizeRel``.
  52. SCons Deviations
  53. ----------------
  54. Not all code from the SCons system can be perfectly represented in CMake, here
  55. are the notable differences:
  56. - ``debug_symbols``
  57. Is no longer an explicit option, and is enabled when using CMake build
  58. configurations; ``Debug``, ``RelWithDebInfo``.
  59. - ``dev_build``
  60. Does not define ``NDEBUG`` when disabled, ``NDEBUG`` is set when using
  61. CMake build configurations; ``Release``, ``MinSizeRel``.
  62. - ``arch``
  63. CMake sets the architecture via the toolchain files, macOS universal is
  64. controlled via the ``CMAKE_OSX_ARCHITECTURES`` property which is copied to
  65. targets when they are defined.
  66. - ``debug_crt``
  67. CMake controls linking to Windows runtime libraries by copying the value of
  68. ``CMAKE_MSVC_RUNTIME_LIBRARIES`` to targets as they are defined. godot-cpp
  69. will set this variable if it isn't already set. So, include it before other
  70. dependencies to have the value propagate across the projects.
  71. Basic Walk-Through
  72. ------------------
  73. Clone the git repository
  74. ~~~~~~~~~~~~~~~~~~~~~~~~
  75. .. code-block:: shell
  76. git clone https://github.com/godotengine/godot-cpp.git
  77. Cloning into 'godot-cpp'...
  78. ...
  79. Configure the build
  80. ~~~~~~~~~~~~~~~~~~~
  81. .. code-block:: shell
  82. cmake -S godot-cpp -B cmake-build -G Ninja
  83. - ``-S`` Specifies the source directory as ``godot-cpp``
  84. - ``-B`` Specifies the build directory as ``cmake-build``
  85. - ``-G`` Specifies the Generator as ``Ninja``
  86. The source directory in this example is the source root for the freshly cloned
  87. godot-cpp. CMake will also interpret the first path in the command as the
  88. source path, or if an existing build path is specified it will deduce the
  89. source path from the build cache.
  90. The following three commands are equivalent:
  91. .. code-block:: shell
  92. # Current working directory is the godot-cpp source root.
  93. cmake . -B build-dir
  94. # Current working directory is an empty godot-cpp/build-dir.
  95. cmake ../
  96. # Current working directory is an existing build path.
  97. cmake .
  98. The build directory is specified so that generated files do not clutter the
  99. source tree with build artifacts.
  100. CMake doesn't build the code, it generates the files that a build tool uses, in
  101. this case the ``Ninja`` generator creates Ninja_ build files.
  102. To see the list of generators run ``cmake --help``.
  103. .. _Ninja: https://ninja-build.org/
  104. Build Options
  105. ~~~~~~~~~~~~~
  106. To list the available options use the ``-L[AH]`` command flags. ``A`` is for
  107. advanced, and ``H`` is for help strings:
  108. .. code-block:: shell
  109. cmake -S godot-cpp -LH
  110. Options are specified on the command line when configuring, for example:
  111. .. code-block:: shell
  112. cmake -S godot-cpp -DGODOTCPP_USE_HOT_RELOAD:BOOL=ON \
  113. -DGODOTCPP_PRECISION:STRING=double \
  114. -DCMAKE_BUILD_TYPE:STRING=Debug
  115. See setting-build-variables_ and build-configurations_ for more information.
  116. .. _setting-build-variables: https://cmake.org/cmake/help/latest/guide/user-interaction/index.html#setting-build-variables
  117. .. _build-configurations: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations
  118. A non-exhaustive list of options:
  119. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  120. .. code-block:: text
  121. // Path to a custom GDExtension API JSON file.
  122. // (takes precedence over GODOTCPP_GDEXTENSION_DIR)
  123. // ( /path/to/custom_api_file )
  124. GODOTCPP_CUSTOM_API_FILE:FILEPATH=
  125. // Force disabling exception handling code. (ON|OFF)
  126. GODOTCPP_DISABLE_EXCEPTIONS:BOOL=ON
  127. // Path to a custom directory containing the GDExtension interface
  128. // header and API JSON file. ( /path/to/gdextension_dir )
  129. GODOTCPP_GDEXTENSION_DIR:PATH=gdextension
  130. // Set the floating-point precision level. (single|double)
  131. GODOTCPP_PRECISION:STRING=single
  132. // Enable the extra accounting required to support hot reload. (ON|OFF)
  133. GODOTCPP_USE_HOT_RELOAD:BOOL=
  134. Compiling
  135. ~~~~~~~~~
  136. Tell CMake to invoke the build system it generated in the specified directory.
  137. The default target is ``template_debug`` and the default build configuration is
  138. Debug.
  139. .. code-block:: shell
  140. cmake --build cmake-build
  141. Examples
  142. --------
  143. These examples, while intended for godot-cpp developers, package maintainers,
  144. and CI/CD may help you configure your own extension project.
  145. Practical examples for how to consume the godot-cpp library as part of an
  146. extension project are listed in the `Introduction`_.
  147. Enabling Integration Testing
  148. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  149. The testing target ``godot-cpp-test`` is guarded by ``GODOTCPP_ENABLE_TESTING``
  150. which is off by default.
  151. To configure and build the godot-cpp project to enable the integration
  152. testing targets the command will look something like:
  153. .. code-block:: shell
  154. cmake -S godot-cpp -B cmake-build -DGODOTCPP_ENABLE_TESTING=YES
  155. cmake --build cmake-build --target godot-cpp-test
  156. Windows and MSVC - Release
  157. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. So long as CMake is installed from the `CMake Downloads`_ page and in the PATH,
  159. and Microsoft Visual Studio is installed with C++ support, CMake will detect
  160. the MSVC compiler.
  161. Note that Visual Studio is a Multi-Config Generator so the build configuration
  162. needs to be specified at build time, for example, ``--config Release``.
  163. .. _CMake downloads: https://cmake.org/download/
  164. .. code-block:: shell
  165. cmake -S godot-cpp -B cmake-build -DGODOTCPP_ENABLE_TESTING=YES
  166. cmake --build cmake-build -t godot-cpp-test --config Release
  167. MSys2/clang64, "Ninja" - Debug
  168. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. Assumes the ``ming-w64-clang-x86_64``-toolchain is installed.
  170. Note that Ninja is a Single-Config Generator so the build type needs to be
  171. specified at configuration time.
  172. Using the ``msys2/clang64`` shell:
  173. .. code-block:: shell
  174. cmake -S godot-cpp -B cmake-build -G"Ninja" \
  175. -DGODOTCPP_ENABLE_TESTING=YES -DCMAKE_BUILD_TYPE=Release
  176. cmake --build cmake-build -t godot-cpp-test
  177. MSys2/clang64, "Ninja Multi-Config" - dev_build, Debug Symbols
  178. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179. Assumes the ``ming-w64-clang-x86_64``-toolchain is installed.
  180. This time we are choosing the 'Ninja Multi-Config' generator, so the build
  181. type is specified at build time.
  182. Using the ``msys2/clang64`` shell:
  183. .. code-block:: shell
  184. cmake -S godot-cpp -B cmake-build -G"Ninja Multi-Config" \
  185. -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_DEV_BUILD:BOOL=ON
  186. cmake --build cmake-build -t godot-cpp-test --config Debug
  187. Emscripten for web platform
  188. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  189. This has only been tested on Windows so far. You can use this example workflow:
  190. - Clone and install the latest Emscripten tools to ``c:\emsdk``.
  191. - Use ``C:\emsdk\emsdk.ps1 activate latest`` to enable the environment from
  192. powershell in the current shell.
  193. - The ``emcmake.bat`` utility adds the emscripten toolchain to the CMake
  194. command. It can also be added manually;
  195. the location is listed inside the ``emcmake.bat`` file
  196. .. code-block:: powershell
  197. C:\emsdk\emsdk.ps1 activate latest
  198. emcmake.bat cmake -S godot-cpp -B cmake-build-web -DCMAKE_BUILD_TYPE=Release
  199. cmake --build cmake-build-web
  200. Android Cross Compile from Windows
  201. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  202. There are two separate paths you can choose when configuring for android.
  203. Use the ``CMAKE_ANDROID_*`` variables specified on the command line or in your
  204. own toolchain file as listed in the cmake-toolchains_ documentation.
  205. .. _cmake-toolchains: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android-with-the-ndk
  206. Or use the toolchain and scripts provided by the Android SDK and make changes
  207. using the ``ANDROID_*`` variables listed there. Where ``<version>`` is whatever
  208. NDK version you have installed (tested with `28.1.13356709`) and ``<platform>``
  209. is for the Android sdk platform, (tested with ``android-29``).
  210. .. warning::
  211. The Android SDK website_ explicitly states that they do not support using
  212. the CMake built-in method, and recommends you stick with their toolchain
  213. files.
  214. .. _website: https://developer.android.com/ndk/guides/cmake
  215. Using your own toolchain file
  216. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  217. As described in the CMake documentation:
  218. .. code-block:: shell
  219. cmake -S godot-cpp -B cmake-build --toolchain my_toolchain.cmake
  220. cmake --build cmake-build -t template_release
  221. Doing the equivalent just using the command line:
  222. .. code-block:: shell
  223. cmake -S godot-cpp -B cmake-build \
  224. -DCMAKE_SYSTEM_NAME=Android \
  225. -DCMAKE_SYSTEM_VERSION=<platform> \
  226. -DCMAKE_ANDROID_ARCH_ABI=<arch> \
  227. -DCMAKE_ANDROID_NDK=/path/to/android-ndk
  228. cmake --build cmake-build
  229. Using the Android SDK toolchain file
  230. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  231. This defaults to the minimum supported version and armv7-a:
  232. .. code-block:: shell
  233. cmake -S godot-cpp -B cmake-build \
  234. --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake
  235. cmake --build cmake-build
  236. Specifying the Android platform and ABI:
  237. .. code-block:: shell
  238. cmake -S godot-cpp -B cmake-build \
  239. --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake \
  240. -DANDROID_PLATFORM:STRING=android-29 \
  241. -DANDROID_ABI:STRING=armeabi-v7a
  242. cmake --build cmake-build