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