cmake.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. SCons Deviations
  20. ----------------
  21. Not everything from SCons can be perfectly representable in CMake, here are
  22. the notable differences.
  23. - debug_symbols
  24. No longer has an explicit option, and is enabled via Debug-like CMake
  25. build configurations; Debug, RelWithDebInfo.
  26. - dev_build
  27. Does not define NDEBUG when disabled, NDEBUG is set via Release-like
  28. CMake build configurations; Release, MinSizeRel.
  29. Testing Integration
  30. -------------------
  31. When consuming a third party CMake project into yours, an unfortunate side
  32. effect is that the targets of the consumed project appear in the list of
  33. available targets, and are by default included in the ALL meta target
  34. created by most build systems. For this reason, all the targets specified
  35. in godot-cpp are marked with the ``EXCLUDE_FROM_ALL`` tag to prevent
  36. unnecessary compilation. The testing targets ``godot-cpp.test.<target>``
  37. are also guarded by ``GODOTCPP_ENABLE_TESTING`` which is off by default.
  38. To configure and build the godot-cpp project to enable the integration
  39. testing targets the command will look something like:
  40. .. code-block::
  41. # Assuming our current directory is the godot-cpp source root
  42. mkdir cmake-build
  43. cd cmake-build
  44. cmake .. -DGODOTCPP_ENABLE_TESTING=YES
  45. cmake --build . --target godot-cpp.test.template_debug
  46. Basic walkthrough
  47. -----------------
  48. .. topic:: Clone the git repository
  49. .. code-block::
  50. git clone https://github.com/godotengine/godot-cpp.git
  51. Cloning into 'godot-cpp'...
  52. ...
  53. cd godot-cpp
  54. .. topic:: Out-of-tree build directory
  55. Create a build directory for CMake to put caches and build artifacts in and
  56. change directory to it. This is typically as a sub-directory of the project
  57. root but can be outside the source tree. This is so that generated files do
  58. not clutter up the source tree.
  59. .. code-block::
  60. mkdir cmake-build
  61. cd cmake-build
  62. .. topic:: Configure the build
  63. CMake doesn't build the code, it generates the files that another tool uses
  64. to build the code. To see the list of generators run ``cmake --help``. The
  65. first phase of which is running through the configuration scripts.
  66. Configure and generate Ninja build files.
  67. .. code-block::
  68. cmake .. -G "Ninja"
  69. To list the available options CMake use the ``-L[AH]`` option. ``A`` is for
  70. advanced, and ``H`` is for help strings.
  71. .. code-block::
  72. cmake .. -LH
  73. Options are specified on the command line when configuring
  74. .. code-block::
  75. cmake .. -DGODOTCPP_USE_HOT_RELOAD:BOOL=ON \
  76. -DGODOTCPP_PRECISION:STRING=double \
  77. -DCMAKE_BUILD_TYPE:STRING=Debug
  78. Review setting-build-variables_ and build-configurations_ for more information.
  79. .. _setting-build-variables: https://cmake.org/cmake/help/latest/guide/user-interaction/index.html#setting-build-variables
  80. .. _build-configurations: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations
  81. A non-exhaustive list of options:
  82. .. code-block::
  83. // Path to a custom GDExtension API JSON file (takes precedence over `GODOTCPP_GDEXTENSION_DIR`) ( /path/to/custom_api_file )
  84. `GODOTCPP_CUSTOM_API_FILE:FILEPATH=`
  85. // Force disabling exception handling code (ON|OFF)
  86. GODOTCPP_DISABLE_EXCEPTIONS:BOOL=ON
  87. // Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )
  88. GODOTCPP_GDEXTENSION_DIR:PATH=gdextension
  89. // Generate a template version of the Node class's get_node. (ON|OFF)
  90. GODOTCPP_GENERATE_TEMPLATE_GET_NODE:BOOL=ON
  91. // Set the floating-point precision level (single|double)
  92. GODOTCPP_PRECISION:STRING=single
  93. // Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)
  94. GODOTCPP_SYMBOL_VISIBILITY:STRING=hidden
  95. // Expose headers as SYSTEM.
  96. GODOTCPP_SYSTEM_HEADERS:BOOL=ON
  97. // Enable the extra accounting required to support hot reload. (ON|OFF)
  98. GODOTCPP_USE_HOT_RELOAD:BOOL=
  99. // Treat warnings as errors
  100. GODOTCPP_WARNING_AS_ERROR:BOOL=OFF
  101. .. topic:: Compiling
  102. A target and a configuration is required, as the default ``all`` target does
  103. not include anything and when using multi-config generators like ``Ninja
  104. Multi-Config``, ``Visual Studio *`` or ``Xcode`` the build configuration
  105. needs to be specified at build time. Build in Release mode unless you need
  106. debug symbols.
  107. .. code-block::
  108. cmake --build . -t template_debug --config Debug
  109. Examples
  110. --------
  111. Windows and MSVC - Release
  112. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  113. So long as CMake is installed from the `CMake Downloads`_ page and in the PATH,
  114. and Microsoft Visual Studio is installed with c++ support, CMake will detect
  115. the MSVC compiler.
  116. Remembering that Visual Studio is a Multi-Config Generator so the build type
  117. needs to be specified at build time.
  118. .. _CMake downloads: https://cmake.org/download/
  119. .. code-block::
  120. # Assuming our current directory is the godot-cpp source root
  121. mkdir build-msvc
  122. cd build-msvc
  123. cmake .. -DGODOTCPP_ENABLE_TESTING=YES
  124. cmake --build . -t godot-cpp.test.template_debug --config Debug
  125. MSys2/clang64, "Ninja" - Debug
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. Assumes the ming-w64-clang-x86_64-toolchain is installed
  128. Remembering that Ninja is a Single-Config Generator so the build type
  129. needs to be specified at Configure time.
  130. Using the msys2/clang64 shell
  131. .. code-block::
  132. # Assuming our current directory is the godot-cpp source root
  133. mkdir build-clang
  134. cd build-clang
  135. cmake .. -G"Ninja" -DGODOTCPP_ENABLE_TESTING=YES -DCMAKE_BUILD_TYPE=Debug
  136. cmake --build . -t godot-cpp.test.template_debug
  137. MSys2/clang64, "Ninja Multi-Config" - dev_build, Debug Symbols
  138. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139. Assumes the ming-w64-clang-x86_64-toolchain is installed
  140. This time we are choosing the 'Ninja Multi-Config' generator, so the build
  141. type is specified at build time.
  142. Using the msys2/clang64 shell
  143. .. code-block::
  144. # Assuming our current directory is the godot-cpp source root
  145. mkdir build-clang
  146. cd build-clang
  147. cmake .. -G"Ninja Multi-Config" -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_DEV_BUILD:BOOL=ON
  148. cmake --build . -t godot-cpp.test.template_debug --config Debug
  149. Emscripten for web platform
  150. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  151. I've only tested this on windows so far.
  152. I cloned and installed the latest Emscripten tools to ``c:\emsdk``
  153. At the time of writing that was v3.1.69
  154. I've been using ``C:\emsdk\emsdk.ps1 activate latest`` to enable the
  155. environment from powershell in the current shell.
  156. The ``emcmake.bat`` utility adds the emscripten toolchain to the CMake command
  157. .. code-block::
  158. # Assuming our current directory is the godot-cpp source root
  159. C:\emsdk\emsdk.ps1 activate latest
  160. mkdir build-wasm32
  161. cd build-wasm32
  162. emcmake.bat cmake ../
  163. cmake --build . --target template_release
  164. Android Cross Compile from Windows
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. There are two separate paths you can choose when configuring for android.
  167. Use the ``CMAKE_ANDROID_*`` variables specified on the commandline or in your
  168. own toolchain file as listed in the cmake-toolchains_ documentation
  169. .. _cmake-toolchains: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android-with-the-ndk
  170. Or use the toolchain and scripts provided by the Android SDK and make changes
  171. using the ``ANDROID_*`` variables listed there. Where ``<version>`` is whatever
  172. ndk version you have installed (tested with `23.2.8568313`) and ``<platform>``
  173. is for android sdk platform, (tested with ``android-29``)
  174. .. warning::
  175. The Android SDK website explicitly states that they do not support using
  176. the CMake built-in method, and recommends you stick with their toolchain
  177. files.
  178. .. topic:: Using your own toolchain file as described in the CMake documentation
  179. .. code-block::
  180. # Assuming our current directory is the godot-cpp source root
  181. mkdir build-android
  182. cd build-android
  183. cmake .. --toolchain my_toolchain.cmake
  184. cmake --build . -t template_release
  185. Doing the equivalent on just using the command line
  186. .. code-block::
  187. # Assuming our current directory is the godot-cpp source root
  188. mkdir build-android
  189. cd build-android
  190. cmake .. \
  191. -DCMAKE_SYSTEM_NAME=Android \
  192. -DCMAKE_SYSTEM_VERSION=<platform> \
  193. -DCMAKE_ANDROID_ARCH_ABI=<arch> \
  194. -DCMAKE_ANDROID_NDK=/path/to/android-ndk
  195. cmake --build . -t template_release
  196. .. topic:: Using the toolchain file from the Android SDK
  197. Defaults to minimum supported version( android-16 in my case) and armv7-a.
  198. .. code-block::
  199. # Assuming our current directory is the godot-cpp source root
  200. mkdir build-android
  201. cd build-android
  202. cmake .. --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake
  203. cmake --build . -t template_release
  204. Specify Android platform and ABI
  205. .. code-block::
  206. # Assuming our current directory is the godot-cpp source root
  207. mkdir build-android
  208. cd build-android
  209. cmake .. --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake \
  210. -DANDROID_PLATFORM:STRING=android-29 \
  211. -DANDROID_ABI:STRING=armeabi-v7a
  212. cmake --build . -t template_release
  213. Toolchains
  214. ----------
  215. This section attempts to list the host and target combinations that have been
  216. at tested.
  217. Info on cross compiling triplets indicates that the naming is a little more
  218. freeform that expected, and tailored to its use case. Triplets tend to have the
  219. format ``<arch>[sub][-vendor][-OS][-env]``
  220. * `osdev.org <https://wiki.osdev.org/Target_Triplet>`_
  221. * `stack overflow <https://stackoverflow.com/questions/13819857/does-a-list-of-all-known-target-triplets-in-use-exist>`_
  222. * `LLVM <https://llvm.org/doxygen/classllvm_1_1Triple.html>`_
  223. * `clang target triple <https://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_
  224. * `vcpkg <https://learn.microsoft.com/en-us/vcpkg/concepts/triplets>`_
  225. * `wasm32-unknown-emscripten <https://blog.therocode.net/2020/10/a-guide-to-rust-sdl2-emscripten>`_
  226. Linux Host
  227. ~~~~~~~~~~
  228. :Target: x86_64-linux
  229. Macos Host
  230. ~~~~~~~~~~
  231. :System: Mac Mini
  232. :OS Name: Sequoia 15.0.1
  233. :Processor: Apple M2
  234. Windows Host
  235. ~~~~~~~~~~~~
  236. :OS Name: Microsoft Windows 11 Home, 10.0.22631 N/A Build 22631
  237. :Processor: AMD Ryzen 7 6800HS Creator Edition
  238. `Microsoft Visual Studio 17 2022 <https://visualstudio.microsoft.com/vs/>`_
  239. :Target: x86_64-w64
  240. `LLVM <https://llvm.org/>`_
  241. :Target: x86_64-pc-windows-msvc
  242. `AndroidSDK <https://developer.android.com/studio/#command-tools>`_
  243. armv7-none-linux-androideabi16
  244. `Emscripten <https://emscripten.org/>`_
  245. :Compiler: Emscripten
  246. :Target: wasm32-unknown-emscripten
  247. `MinGW-w64 <https://www.mingw-w64.org/>`_ based toolchains
  248. `MSYS2 <https://www.msys2.org/>`_
  249. Necessary reading about MSYS2 `environments <https://www.msys2.org/docs/environments/>`_
  250. ucrt64
  251. :Compiler: gcc version 14.2.0 (Rev1, Built by MSYS2 project)
  252. :Target: x86_64-w64-mingw32
  253. clang64
  254. :Compiler: clang version 18.1.8
  255. :Target: x86_64-w64-windows-gnu
  256. `LLVM-MinGW <https://github.com/mstorsjo/llvm-mingw/releases>`_
  257. `MinGW-W64-builds <https://github.com/niXman/mingw-builds-binaries/releases>`_
  258. :Compiler: gcc
  259. :Target: x86_64-w64-mingw32-ucrt
  260. `Jetbrains-CLion <https://www.jetbrains.com/clion/>`_
  261. :Target: x86_64-w64-mingw32-msvcrt