Skip to content

Commit 757d1f7

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

File tree

4 files changed

+451
-0
lines changed

4 files changed

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