cmake.rst 12 KB

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