cmake.rst 12 KB

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