Skip to content

Commit a9d0c66

Browse files
committed
Add "Build System" section to godot-cpp docs (SCons and CMake).
1 parent a1632ef commit a9d0c66

File tree

4 files changed

+438
-0
lines changed

4 files changed

+438
-0
lines changed
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
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 I've seen 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 representable 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 vua 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+
The testing target ``godot-cpp-test`` is guarded by ``GODOTCPP_ENABLE_TESTING`` which is off by default.
91+
92+
To configure and build the godot-cpp project to enable the integration
93+
testing targets the command will look something like:
94+
95+
.. code-block::
96+
97+
# Assuming our current directory is the godot-cpp source root
98+
cmake -S . -B cmake-build -DGODOTCPP_ENABLE_TESTING=YES
99+
cmake --build cmake-build --target godot-cpp-test
100+
101+
Basic walkthrough
102+
-----------------
103+
104+
.. topic:: Clone the git repository
105+
106+
.. code-block::
107+
108+
git clone https://github.com/godotengine/godot-cpp.git
109+
Cloning into 'godot-cpp'...
110+
...
111+
cd godot-cpp
112+
113+
.. topic:: Options
114+
115+
To list the available options CMake use the ``-L[AH]`` option. ``A`` is for
116+
advanced, and ``H`` is for help strings.
117+
118+
.. code-block::
119+
120+
cmake .. -LH
121+
122+
Options are specified on the command line when configuring eg.
123+
124+
.. code-block::
125+
126+
cmake .. -DGODOTCPP_USE_HOT_RELOAD:BOOL=ON \
127+
-DGODOTCPP_PRECISION:STRING=double \
128+
-DCMAKE_BUILD_TYPE:STRING=Debug
129+
130+
Review setting-build-variables_ and build-configurations_ for more information.
131+
132+
.. _setting-build-variables: https://cmake.org/cmake/help/latest/guide/user-interaction/index.html#setting-build-variables
133+
.. _build-configurations: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations
134+
135+
A non-exhaustive list of options:
136+
137+
.. code-block::
138+
139+
// Path to a custom GDExtension API JSON file (takes precedence over `GODOTCPP_GDEXTENSION_DIR`) ( /path/to/custom_api_file )
140+
GODOTCPP_CUSTOM_API_FILE:FILEPATH=
141+
142+
// Force disabling exception handling code (ON|OFF)
143+
GODOTCPP_DISABLE_EXCEPTIONS:BOOL=ON
144+
145+
// Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )
146+
GODOTCPP_GDEXTENSION_DIR:PATH=gdextension
147+
148+
// Set the floating-point precision level (single|double)
149+
GODOTCPP_PRECISION:STRING=single
150+
151+
// Enable the extra accounting required to support hot reload. (ON|OFF)
152+
GODOTCPP_USE_HOT_RELOAD:BOOL=
153+
154+
.. topic:: Configure the build
155+
156+
.. code-block::
157+
158+
cmake -S . -B cmake-build -G Ninja
159+
160+
``-S .`` Specifies the source directory
161+
162+
``-B cmake-build`` Specifies the build directory
163+
164+
``-G Ninja`` Specifies the Generator
165+
166+
The source directory in this example is the source code for godot-cpp.
167+
The build directory is so that generated files do not clutter up the source tree.
168+
CMake doesn't build the code, it generates the files that another tool uses
169+
to build the code, in this case Ninja.
170+
To see the list of generators run ``cmake --help``.
171+
172+
.. topic:: Compiling
173+
174+
Tell cmake to invoke the build system it generated in the specified directory.
175+
The default target is template_debug and the default build configuration is Debug.
176+
177+
.. code-block::
178+
179+
cmake --build cmake-build
180+
181+
Examples
182+
--------
183+
184+
Windows and MSVC - Release
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~
186+
So long as CMake is installed from the `CMake Downloads`_ page and in the PATH,
187+
and Microsoft Visual Studio is installed with C++ support, CMake will detect
188+
the MSVC compiler.
189+
190+
Note that Visual Studio is a Multi-Config Generator so the build configuration
191+
needs to be specified at build time ie ``--config Release``
192+
193+
.. _CMake downloads: https://cmake.org/download/
194+
195+
.. code-block::
196+
197+
# Assuming our current directory is the godot-cpp source root
198+
cmake -S . -B cmake-build -DGODOTCPP_ENABLE_TESTING=YES
199+
cmake --build cmake-build -t godot-cpp-test --config Release
200+
201+
202+
MSys2/clang64, "Ninja" - Debug
203+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204+
Assumes the ming-w64-clang-x86_64-toolchain is installed
205+
206+
Note that Ninja is a Single-Config Generator so the build type
207+
needs to be specified at Configure time.
208+
209+
Using the msys2/clang64 shell
210+
211+
.. code-block::
212+
213+
# Assuming our current directory is the godot-cpp source root
214+
cmake -S . -B cmake-build -G"Ninja" -DGODOTCPP_ENABLE_TESTING=YES -DCMAKE_BUILD_TYPE=Release
215+
cmake --build cmake-build -t godot-cpp-test
216+
217+
MSys2/clang64, "Ninja Multi-Config" - dev_build, Debug Symbols
218+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219+
Assumes the ming-w64-clang-x86_64-toolchain is installed
220+
221+
This time we are choosing the 'Ninja Multi-Config' generator, so the build
222+
type is specified at build time.
223+
224+
Using the msys2/clang64 shell
225+
226+
.. code-block::
227+
228+
# Assuming our current directory is the godot-cpp source root
229+
cmake -S . -B cmake-build -G"Ninja Multi-Config" -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_DEV_BUILD:BOOL=ON
230+
cmake --build cmake-build -t godot-cpp-test --config Debug
231+
232+
Emscripten for web platform
233+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
234+
This has only been tested on Windows so far. You can use this example workflow:
235+
236+
- Clone and install the latest Emscripten tools to ``c:\emsdk``.
237+
- Use ``C:\emsdk\emsdk.ps1 activate latest`` to enable the environment from powershell in the current shell.
238+
- The ``emcmake.bat`` utility adds the emscripten toolchain to the CMake command. It can also be added manually;
239+
the location is listed inside the emcmake.bat file.
240+
241+
.. code-block::
242+
243+
# Assuming our current directory is the godot-cpp source root
244+
C:\emsdk\emsdk.ps1 activate latest
245+
emcmake.bat cmake -S . -B cmake-build-web -DCMAKE_BUILD_TYPE=Release
246+
cmake --build cmake-build-web
247+
248+
Android Cross Compile from Windows
249+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250+
There are two separate paths you can choose when configuring for android.
251+
252+
Use the ``CMAKE_ANDROID_*`` variables specified on the commandline or in your
253+
own toolchain file as listed in the cmake-toolchains_ documentation
254+
255+
.. _cmake-toolchains: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android-with-the-ndk
256+
257+
Or use the toolchain and scripts provided by the Android SDK and make changes
258+
using the ``ANDROID_*`` variables listed there. Where ``<version>`` is whatever
259+
NDK version you have installed (tested with `28.1.13356709`) and ``<platform>``
260+
is for android sdk platform, (tested with ``android-29``)
261+
262+
.. warning::
263+
264+
The Android SDK website explicitly states that they do not support using
265+
the CMake built-in method, and recommends you stick with their toolchain
266+
files.
267+
268+
.. topic:: Using your own toolchain file as described in the CMake documentation
269+
270+
.. code-block::
271+
272+
# Assuming our current directory is the godot-cpp source root
273+
cmake -S . -B cmake-build --toolchain my_toolchain.cmake
274+
cmake --build cmake-build -t template_release
275+
276+
Doing the equivalent on just using the command line:
277+
278+
.. code-block::
279+
280+
# Assuming our current directory is the godot-cpp source root
281+
cmake -S . -B cmake-build \
282+
-DCMAKE_SYSTEM_NAME=Android \
283+
-DCMAKE_SYSTEM_VERSION=<platform> \
284+
-DCMAKE_ANDROID_ARCH_ABI=<arch> \
285+
-DCMAKE_ANDROID_NDK=/path/to/android-ndk
286+
cmake --build cmake-build
287+
288+
.. topic:: Using the toolchain file from the Android SDK
289+
290+
This defaults to minimum supported version and armv7-a:
291+
292+
.. code-block::
293+
294+
# Assuming our current directory is the godot-cpp source root
295+
cmake -S . -B cmake-build --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake
296+
cmake --build cmake-build
297+
298+
Specifying the Android platform and ABI:
299+
300+
.. code-block::
301+
302+
# Assuming our current directory is the godot-cpp source root
303+
cmake -S . -B cmake-build --toolchain $ANDROID_HOME/ndk/<version>/build/cmake/android.toolchain.cmake \
304+
-DANDROID_PLATFORM:STRING=android-29 \
305+
-DANDROID_ABI:STRING=armeabi-v7a
306+
cmake --build cmake-build
307+
308+
309+
Toolchains
310+
----------
311+
This section attempts to list the host and target combinations that have been
312+
at tested.
313+
314+
Linux Host
315+
~~~~~~~~~~
316+
317+
macOS Host
318+
~~~~~~~~~~
319+
320+
:System: Mac Mini
321+
:OS Name: Sequoia 15.0.1
322+
:Processor: Apple M2
323+
324+
* AppleClang
325+
326+
Windows Host
327+
~~~~~~~~~~~~
328+
329+
:OS Name: Windows 11
330+
:Processor: AMD Ryzen 7 6800HS Creator Edition
331+
332+
333+
* `Microsoft Visual Studio 17 2022 <https://visualstudio.microsoft.com/vs/>`_
334+
* `LLVM <https://llvm.org/>`_
335+
* `LLVM-MinGW <https://github.com/mstorsjo/llvm-mingw/releases>`_
336+
337+
* aarch64-w64-mingw32
338+
* armv7-w64-mingw32
339+
* i686-w64-mingw32
340+
* x86_64-w64-mingw32
341+
342+
* `AndroidSDK <https://developer.android.com/studio/#command-tools>`_
343+
* `Emscripten <https://emscripten.org/>`_
344+
* `MinGW-W64-builds <https://github.com/niXman/mingw-builds-binaries/releases>`_
345+
* `Jetbrains-CLion <https://www.jetbrains.com/clion/>`_
346+
347+
Jetbrains builtin compiler is just the MingW64 above.
348+
349+
* `MSYS2 <https://www.msys2.org/>`_
350+
Necessary reading about MSYS2 `environments <https://www.msys2.org/docs/environments/>`_
351+
352+
* ucrt64
353+
* clang64
354+
* mingw32
355+
* mingw64
356+
* clangarm64
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Build system
2+
============
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
:name: toc-godot-cpp-build-system
7+
8+
scons
9+
cmake

0 commit comments

Comments
 (0)