2
0

cmake.rst 12 KB

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