Skip to content

Commit 8534e21

Browse files
committed
Modernise Existing CMakeLists.txt
- Added to .gitignore CMakeUserPresets.json ### Configuration: - Changed python command to use single quotes to make build output log more legible. - Added GODOT_DEV_BUILD to allow differentiation of debug or Release builds. - Added find logic for macos Cocoa library ### godot-cpp Changes - godot-cpp-test is changed to be incorporated into the cmake build as a target. - Duplicated godot-cpp target into [template_release, template_debug, editor] - Created {platform}.cmake files mirroring the style of the SCons build. CMake has a feature called generator expressions for its configuration variables that are evaluated at build time. This allows multi-configuration build systems to properly evaulate options. for msvc, xcode and nijna multi-config. - Moved configuration options to generator expressions with the notable exclusion of OSX_ARCHITECTURES. - Remove CMAKE_BUILD_TYPE from msvc CI target as Multi-Config generators ignore it ### godot-cpp-test Changes - Removed majority of the cmake code, now that the godot-cpp project is setup, the majority of the flags will be propagated as transient dependencies - Marked with EXCLUDE_FROM_ALL so that it isn't built as part of the 'all' target - Updated ci to build the godot-cpp-test target from the root directory using cmake - Tests passing for Windows, Linux, and Macos builds. ### Documentation Updated with new information Added Emscripten example Added Android example
1 parent c20a84e commit 8534e21

15 files changed

+1153
-496
lines changed

.github/workflows/ci.yml

+8-38
Original file line numberDiff line numberDiff line change
@@ -207,30 +207,6 @@ jobs:
207207
path: ${{ matrix.artifact-path }}
208208
if-no-files-found: error
209209

210-
linux-cmake:
211-
name: 🐧 Build (Linux, GCC, CMake)
212-
runs-on: ubuntu-22.04
213-
steps:
214-
- name: Checkout
215-
uses: actions/checkout@v4
216-
with:
217-
submodules: recursive
218-
219-
- name: Install dependencies
220-
run: |
221-
sudo apt-get update -qq
222-
sudo apt-get install -qqq build-essential pkg-config cmake
223-
224-
- name: Build godot-cpp
225-
run: |
226-
cmake -DCMAKE_BUILD_TYPE=Release .
227-
make -j $(nproc) VERBOSE=1
228-
229-
- name: Build test GDExtension library
230-
run: |
231-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." .
232-
make -j $(nproc) VERBOSE=1
233-
234210
linux-cmake-ninja:
235211
name: 🐧 Build (Linux, GCC, CMake Ninja)
236212
runs-on: ubuntu-22.04
@@ -245,15 +221,12 @@ jobs:
245221
sudo apt-get update -qq
246222
sudo apt-get install -qqq build-essential pkg-config cmake ninja-build
247223
248-
- name: Build godot-cpp
249-
run: |
250-
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .
251-
cmake --build . -j $(nproc) --verbose
252-
253224
- name: Build test GDExtension library
254225
run: |
255-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -GNinja .
256-
cmake --build . -j $(nproc) --verbose
226+
mkdir cmake-build
227+
cd cmake-build
228+
cmake ../ -DTEST_TARGET=template_release
229+
cmake --build . --verbose -j $(nproc) -t godot-cpp-test --config Release
257230
258231
windows-msvc-cmake:
259232
name: 🏁 Build (Windows, MSVC, CMake)
@@ -264,12 +237,9 @@ jobs:
264237
with:
265238
submodules: recursive
266239

267-
- name: Build godot-cpp
268-
run: |
269-
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
270-
cmake --build . --verbose --config Release
271-
272240
- name: Build test GDExtension library
273241
run: |
274-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -G"Visual Studio 16 2019" .
275-
cmake --build . --verbose --config Release
242+
mkdir cmake-build
243+
cd cmake-build
244+
cmake ../ -DTEST_TARGET=template_release
245+
cmake --build . --verbose -t godot-cpp-test --config Release

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,7 @@ venv
198198

199199
# Clion Configuration
200200
.idea/
201-
cmake-build-*
201+
cmake-build*/
202+
203+
# CMake related
204+
CMakeUserPresets.json

CMakeLists.txt

+91-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,98 @@
1-
cmake_minimum_required(VERSION 3.13)
2-
project(godot-cpp LANGUAGES CXX)
3-
4-
# Configure CMake
5-
# https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965
6-
# https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake
7-
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
8-
if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
9-
STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
10-
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
11-
endif ()
12-
endif ()
1+
cmake_minimum_required(VERSION 3.17)
2+
3+
#[=======================================================================[.rst:
4+
5+
CMake Version requirements
6+
--------------------------
7+
8+
To enable use of the emscripten emsdk hack for pseudo shared library support
9+
without polluting options for consumers we need to use the
10+
CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE which was introduced in version 3.17
11+
12+
Scons Compatibility
13+
-------------------
14+
15+
As we are attempting to maintain feature parity, and ease of maintenance, these
16+
CMake scripts are built to resemble the SCons build system.
1317
14-
include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
18+
The file structure and file content are made to match, if not in content then
19+
in spirit. The closer the two build systems look the easier they will be to
20+
maintain.
1521
16-
# I know this doesn't look like a typical CMakeLists.txt, but as we are
17-
# attempting mostly feature parity with SCons, and easy maintenance, the closer
18-
# the two build systems look the easier they will be to keep in lockstep.
22+
Where the SCons additional scripts in the tools directory, The CMake scripts
23+
are in the cmake directory.
1924
20-
# The typical target definitions are in ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake
25+
For example, the tools/godotcpp.py is sourced into SCons, and the 'options'
26+
function is run.
2127
28+
.. highlight:: python
29+
30+
cpp_tool = Tool("godotcpp", toolpath=["tools"])
31+
cpp_tool.options(opts, env)
32+
33+
34+
The CMake equivalent is below.
35+
]=======================================================================]
36+
37+
include( cmake/godotcpp.cmake )
2238
godotcpp_options()
2339

40+
#[=======================================================================[.rst:
41+
42+
Configurations
43+
--------------
44+
45+
There are two build main configurations, 'Debug' and 'Release', these are not
46+
related to godot's DEBUG_FEATURES flag. Build configurations change the default
47+
compiler and linker flags present when building the library, things like debug
48+
symbols, optimization.
49+
50+
The Scons build scripts don't have this concept, you can think of it like the
51+
SCons solution has a single default configuration. In both cases overriding the
52+
defaults is controlled by options on the command line, or in preset files.
53+
54+
Because of this added configuration and that it can be undefined, it becomes
55+
important to set a default, considering the SCons solution that does not enable
56+
debug symbols by default, it seemed appropriate to set the default to 'Release'
57+
if unspecified. This can always be overridden like below.
58+
59+
.. highlight:: shell
60+
61+
cmake <source> -DCMAKE_BUILD_TYPE:STRING=Debug
62+
63+
.. caution::
64+
65+
A complication arises from `Multi-Config Generators`_ that cannot have
66+
their configuration set at configure time. This means that the configuration
67+
must be set on the build command. This is especially important for Visual
68+
Studio Generators which default to 'Debug'
69+
70+
.. highlight:: shell
71+
72+
cmake --build . --config Release
73+
74+
.. _Multi-Config Generators:https://cmake.org/cmake/help/latest/prop_gbl/GENERATOR_IS_MULTI_CONFIG.html
75+
]=======================================================================]
76+
get_property( IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG )
77+
if( NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE )
78+
if( GODOT_DEV_BUILD )
79+
set( CMAKE_BUILD_TYPE Debug )
80+
else ()
81+
set( CMAKE_BUILD_TYPE Release )
82+
endif ()
83+
endif ()
84+
85+
#[[ Python is required for code generation ]]
86+
find_package(Python3 3.4 REQUIRED) # pathlib should be present
87+
88+
# Define our project.
89+
project( godot-cpp
90+
VERSION 4.4
91+
DESCRIPTION "C++ bindings for the Godot Engine's GDExtensions API."
92+
HOMEPAGE_URL "https://github.com/godotengine/godot-cpp"
93+
LANGUAGES CXX)
94+
2495
godotcpp_generate()
96+
97+
# Test Example
98+
add_subdirectory( test )

cmake/android.cmake

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[=======================================================================[.rst:
2+
Android
3+
-------
4+
5+
This file contains functions for options and configuration for targeting the
6+
Android platform
7+
8+
Configuration of the Android toolchain is done using toolchain files,
9+
CMakePresets, or variables on the command line.
10+
11+
The `Android SDK`_ provides toolchain files to help with configuration.
12+
13+
CMake has its own `built-in support`_ for cross compiling to the
14+
Android platforms.
15+
16+
.. warning::
17+
18+
Android does not support or test the CMake built-in workflow, recommend
19+
using their toolchain file.
20+
21+
.. _Android SDK:https://developer.android.com/ndk/guides/cmake
22+
23+
.. _built-in support:https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android
24+
25+
There is further information and examples in the doc/cmake.rst file.
26+
27+
]=======================================================================]
28+
function( android_options )
29+
# Android Options
30+
endfunction()
31+
32+
function( android_generate TARGET_NAME )
33+
34+
target_compile_definitions(${TARGET_NAME}
35+
PUBLIC
36+
ANDROID_ENABLED
37+
UNIX_ENABLED
38+
)
39+
40+
common_compiler_flags( ${TARGET_NAME} )
41+
endfunction()

0 commit comments

Comments
 (0)