Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 355e16a

Browse files
committedSep 18, 2024·
Re-Structure cmake solution to be closer to the scons solution.
This is just a single step, re-arranging the code without actually changing its functionality. new docs/cmake.md moved the block of comments from the start of the CMakeLists.txt into the cmake.md file and converted content to markdown. new cmake/godotcpp.cmake Moved all exposed options into a new function godotcpp_options() Moved configuration and generation code into godotcpp_generate() To get all the options into the godotcpp_options() I changed the logic of GODOT_USE_HOT_RELOAD which I believe is a closer match to scons, that if the options is not set, and the build type is not release, then it defaults to ON. I msvc builds require the default flags to be modified or it will throw errors. I have added the links to articles in the commit, but its about removing the runtime error checks /RTC1 from the CMAKE_CXX_FLAGS_DEBUG variable. This needs to happen before the files are included. https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965 Renamed GodotCompilerWarnings.cmake to common_compiler_flags.cmake to match scons Included files explicitly by path, as we dont need to append to the CMAKE_MODULES_PATH which effects the whole build tree. This prevents consumers of the library from clobbering the names of the cmake include files and breaking the build.
1 parent b93d6e8 commit 355e16a

File tree

4 files changed

+312
-239
lines changed

4 files changed

+312
-239
lines changed
 

‎CMakeLists.txt

Lines changed: 15 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -1,248 +1,24 @@
1-
# cmake arguments
2-
# CMAKE_BUILD_TYPE: Compilation target (Debug or Release defaults to Debug)
3-
#
4-
# godot-cpp cmake arguments
5-
# GODOT_GDEXTENSION_DIR: Path to the directory containing GDExtension interface header and API JSON file
6-
# GODOT_SYSTEM_HEADERS: Mark the header files as SYSTEM. This may be useful to suppress warnings in projects including this one.
7-
# GODOT_WARNING_AS_ERROR: Treat any warnings as errors
8-
# GODOT_USE_HOT_RELOAD: Build with hot reload support. Defaults to YES for Debug-builds and NO for Release-builds.
9-
# GODOT_CUSTOM_API_FILE: Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)
10-
# GODOT_PRECISION: Floating-point precision level ("single", "double")
11-
#
12-
# Android cmake arguments
13-
# CMAKE_TOOLCHAIN_FILE: The path to the android cmake toolchain ($ANDROID_NDK/build/cmake/android.toolchain.cmake)
14-
# ANDROID_NDK: The path to the android ndk root folder
15-
# ANDROID_TOOLCHAIN_NAME: The android toolchain (arm-linux-androideabi-4.9 or aarch64-linux-android-4.9 or x86-4.9 or x86_64-4.9)
16-
# ANDROID_PLATFORM: The android platform version (android-23)
17-
# More info here: https://godot.readthedocs.io/en/latest/development/compiling/compiling_for_android.html
18-
#
19-
# Examples
20-
#
21-
# Builds a debug version:
22-
# cmake .
23-
# cmake --build .
24-
#
25-
# Builds a release version with clang
26-
# CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" .
27-
# cmake --build .
28-
#
29-
# Builds an android armeabi-v7a debug version:
30-
# cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK \
31-
# -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
32-
# cmake --build .
33-
#
34-
# Protip
35-
# Generate the buildfiles in a sub directory to not clutter the root directory with build files:
36-
# mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
37-
#
38-
# Ensure that you avoid exposing godot-cpp symbols - this might lead to hard to debug errors if you ever load multiple
39-
# plugins using difference godot-cpp versions. Use visibility hidden whenever possible:
40-
# set_target_properties(<all-my-plugin-related-targets> PROPERTIES CXX_VISIBILITY_PRESET hidden)
41-
#
42-
# Todo
43-
# Test build for Windows, Mac and mingw.
44-
451
cmake_minimum_required(VERSION 3.13)
462
project(godot-cpp LANGUAGES CXX)
473

48-
option(GODOT_GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node. (ON|OFF)" ON)
49-
option(GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
50-
option(GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF)
51-
52-
set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING "Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)")
53-
set_property( CACHE GODOT_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
54-
55-
# CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal
56-
# which is inline with the gcc -fvisibility=
57-
# https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
58-
# To match the scons options we need to change the text to match the -fvisibility flag
59-
# it is probably worth another PR which changes both to use the flag options
60-
if( ${GODOT_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOT_SYMBOL_VISIBILITY} STREQUAL "visible" )
61-
set( GODOT_SYMBOL_VISIBILITY "default" )
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 ()
6212
endif ()
6313

64-
# Add path to modules
65-
list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" )
66-
67-
# Set some helper variables for readability
68-
set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
69-
set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
70-
set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )
71-
72-
# Default build type is Debug in the SConstruct
73-
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
74-
set(CMAKE_BUILD_TYPE Debug)
75-
endif()
76-
77-
# Hot reload is enabled by default in Debug-builds
78-
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
79-
option(GODOT_USE_HOT_RELOAD "Enable the extra accounting required to support hot reload. (ON|OFF)" ON)
80-
else()
81-
option(GODOT_USE_HOT_RELOAD "Enable the extra accounting required to support hot reload. (ON|OFF)" OFF)
82-
endif()
83-
84-
if(NOT DEFINED BITS)
85-
set(BITS 32)
86-
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
87-
set(BITS 64)
88-
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
89-
endif()
90-
91-
# Input from user for GDExtension interface header and the API JSON file
92-
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
93-
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
94-
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
95-
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
96-
97-
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
98-
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
99-
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
100-
endif()
101-
102-
set(GODOT_PRECISION "single" CACHE STRING "Set the floating-point precision level (single|double)")
103-
if ("${GODOT_PRECISION}" STREQUAL "double")
104-
add_definitions(-DREAL_T_IS_DOUBLE)
105-
endif()
106-
107-
set( GODOT_COMPILE_FLAGS )
108-
109-
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
110-
# using Visual Studio C++
111-
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
112-
113-
if(CMAKE_BUILD_TYPE MATCHES Debug)
114-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
115-
else()
116-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy
117-
STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
118-
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
119-
endif(CMAKE_BUILD_TYPE MATCHES Debug)
120-
121-
add_definitions(-DNOMINMAX)
122-
else() # GCC/Clang
123-
if(CMAKE_BUILD_TYPE MATCHES Debug)
124-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
125-
else()
126-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
127-
endif(CMAKE_BUILD_TYPE MATCHES Debug)
128-
endif()
129-
130-
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
131-
# saves around 20% of binary size and very significant build time (GH-80513).
132-
option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
133-
if (GODOT_DISABLE_EXCEPTIONS)
134-
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
135-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
136-
else()
137-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
138-
endif()
139-
else()
140-
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
141-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
142-
endif()
143-
endif()
144-
145-
# Generate source from the bindings file
146-
find_package(Python3 3.4 REQUIRED) # pathlib should be present
147-
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
148-
set(GENERATE_BINDING_PARAMETERS "True")
149-
else()
150-
set(GENERATE_BINDING_PARAMETERS "False")
151-
endif()
152-
153-
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.print_file_list(\"${GODOT_GDEXTENSION_API_FILE}\", \"${CMAKE_CURRENT_BINARY_DIR}\", headers=True, sources=True)"
154-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
155-
OUTPUT_VARIABLE GENERATED_FILES_LIST
156-
OUTPUT_STRIP_TRAILING_WHITESPACE
157-
)
158-
159-
add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
160-
COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings(\"${GODOT_GDEXTENSION_API_FILE}\", \"${GENERATE_BINDING_PARAMETERS}\", \"${BITS}\", \"${GODOT_PRECISION}\", \"${CMAKE_CURRENT_BINARY_DIR}\")"
161-
VERBATIM
162-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
163-
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
164-
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
165-
COMMENT "Generating bindings"
166-
)
167-
168-
# Get Sources
169-
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
170-
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)
171-
172-
# Define our godot-cpp library
173-
add_library(${PROJECT_NAME} STATIC
174-
${SOURCES}
175-
${HEADERS}
176-
${GENERATED_FILES_LIST}
177-
)
178-
add_library(godot::cpp ALIAS ${PROJECT_NAME})
179-
180-
include(GodotCompilerWarnings)
181-
182-
target_compile_features(${PROJECT_NAME}
183-
PRIVATE
184-
cxx_std_17
185-
)
186-
187-
if(GODOT_USE_HOT_RELOAD)
188-
target_compile_definitions(${PROJECT_NAME} PUBLIC HOT_RELOAD_ENABLED)
189-
target_compile_options(${PROJECT_NAME} PUBLIC $<${compiler_is_gnu}:-fno-gnu-unique>)
190-
endif()
191-
192-
target_compile_definitions(${PROJECT_NAME} PUBLIC
193-
$<$<CONFIG:Debug>:
194-
DEBUG_ENABLED
195-
DEBUG_METHODS_ENABLED
196-
>
197-
$<${compiler_is_msvc}:
198-
TYPED_METHOD_BIND
199-
>
200-
)
201-
202-
target_link_options(${PROJECT_NAME} PRIVATE
203-
$<$<NOT:${compiler_is_msvc}>:
204-
-static-libgcc
205-
-static-libstdc++
206-
-Wl,-R,'$$ORIGIN'
207-
>
208-
)
209-
210-
# Optionally mark headers as SYSTEM
211-
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE "")
212-
if (GODOT_SYSTEM_HEADERS)
213-
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
214-
endif ()
215-
216-
target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
217-
include
218-
${CMAKE_CURRENT_BINARY_DIR}/gen/include
219-
${GODOT_GDEXTENSION_DIR}
220-
)
221-
222-
# Add the compile flags
223-
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
14+
include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
22415

225-
# Create the correct name (godot.os.build_type.system_bits)
226-
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
227-
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
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.
22819

229-
if(ANDROID)
230-
# Added the android abi after system name
231-
set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI})
20+
# The typical target definitions are in ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake
23221

233-
# Android does not have the bits at the end if you look at the main godot repo build
234-
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}")
235-
else()
236-
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
237-
endif()
22+
godotcpp_options()
23823

239-
set_target_properties(${PROJECT_NAME}
240-
PROPERTIES
241-
CXX_EXTENSIONS OFF
242-
POSITION_INDEPENDENT_CODE ON
243-
CXX_VISIBILITY_PRESET ${GODOT_SYMBOL_VISIBILITY}
244-
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
245-
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
246-
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
247-
OUTPUT_NAME "${OUTPUT_NAME}"
248-
)
24+
godotcpp_generate()
File renamed without changes.

‎cmake/godotcpp.cmake

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
function( godotcpp_options )
2+
3+
#TODO platform
4+
#TODO target
5+
6+
# Input from user for GDExtension interface header and the API JSON file
7+
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
8+
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
9+
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
10+
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
11+
12+
#TODO generate_bindings
13+
14+
option(GODOT_GENERATE_TEMPLATE_GET_NODE
15+
"Generate a template version of the Node class's get_node. (ON|OFF)" ON)
16+
17+
#TODO build_library
18+
19+
set(GODOT_PRECISION "single" CACHE STRING
20+
"Set the floating-point precision level (single|double)")
21+
22+
#TODO arch
23+
#TODO threads
24+
#TODO compiledb
25+
#TODO compiledb_file
26+
#TODO build_profile aka cmake preset
27+
28+
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
29+
"Enable the extra accounting required to support hot reload. (ON|OFF)")
30+
31+
option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
32+
33+
set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING
34+
"Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)")
35+
set_property( CACHE GODOT_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
36+
37+
#TODO optimize
38+
#TODO debug_symbols
39+
#TODO dev_build
40+
41+
# FIXME These options are not present in SCons, and perhaps should be added there.
42+
option(GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
43+
option(GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF)
44+
45+
# Run options commands on the following to populate cache for all platforms.
46+
# This type of thing is typically done conditionally
47+
# But as scons shows all options so shall we.
48+
#TODO ios_options()
49+
#TODO linux_options()
50+
#TODO macos_options()
51+
#TODO web_options()
52+
#TODO windows_options()
53+
endfunction()
54+
55+
56+
function( godotcpp_generate )
57+
# Set some helper variables for readability
58+
set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
59+
set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
60+
set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )
61+
62+
# CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal
63+
# which is inline with the gcc -fvisibility=
64+
# https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
65+
# To match the scons options we need to change the text to match the -fvisibility flag
66+
# it is probably worth another PR which changes both to use the flag options
67+
if( ${GODOT_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOT_SYMBOL_VISIBILITY} STREQUAL "visible" )
68+
set( GODOT_SYMBOL_VISIBILITY "default" )
69+
endif ()
70+
71+
# Default build type is Debug in the SConstruct
72+
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
73+
set(CMAKE_BUILD_TYPE Debug)
74+
endif()
75+
76+
# Hot reload is enabled by default in Debug-builds
77+
if( GODOT_USE_HOT_RELOAD STREQUAL "" AND NOT CMAKE_BUILD_TYPE STREQUAL "Release")
78+
set(GODOT_USE_HOT_RELOAD ON)
79+
endif()
80+
81+
if(NOT DEFINED BITS)
82+
set(BITS 32)
83+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
84+
set(BITS 64)
85+
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
86+
endif()
87+
88+
89+
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
90+
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
91+
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
92+
endif()
93+
94+
if ("${GODOT_PRECISION}" STREQUAL "double")
95+
add_definitions(-DREAL_T_IS_DOUBLE)
96+
endif()
97+
98+
set( GODOT_COMPILE_FLAGS )
99+
100+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
101+
# using Visual Studio C++
102+
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
103+
104+
if(CMAKE_BUILD_TYPE MATCHES Debug)
105+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
106+
else()
107+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy
108+
endif(CMAKE_BUILD_TYPE MATCHES Debug)
109+
110+
add_definitions(-DNOMINMAX)
111+
else() # GCC/Clang
112+
if(CMAKE_BUILD_TYPE MATCHES Debug)
113+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
114+
else()
115+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
116+
endif(CMAKE_BUILD_TYPE MATCHES Debug)
117+
endif()
118+
119+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
120+
# saves around 20% of binary size and very significant build time (GH-80513).
121+
if (GODOT_DISABLE_EXCEPTIONS)
122+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
123+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
124+
else()
125+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
126+
endif()
127+
else()
128+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
129+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
130+
endif()
131+
endif()
132+
133+
# Generate source from the bindings file
134+
find_package(Python3 3.4 REQUIRED) # pathlib should be present
135+
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
136+
set(GENERATE_BINDING_PARAMETERS "True")
137+
else()
138+
set(GENERATE_BINDING_PARAMETERS "False")
139+
endif()
140+
141+
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.print_file_list(\"${GODOT_GDEXTENSION_API_FILE}\", \"${CMAKE_CURRENT_BINARY_DIR}\", headers=True, sources=True)"
142+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
143+
OUTPUT_VARIABLE GENERATED_FILES_LIST
144+
OUTPUT_STRIP_TRAILING_WHITESPACE
145+
)
146+
147+
add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
148+
COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings(\"${GODOT_GDEXTENSION_API_FILE}\", \"${GENERATE_BINDING_PARAMETERS}\", \"${BITS}\", \"${GODOT_PRECISION}\", \"${CMAKE_CURRENT_BINARY_DIR}\")"
149+
VERBATIM
150+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
151+
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
152+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
153+
COMMENT "Generating bindings"
154+
)
155+
156+
# Get Sources
157+
# As this cmake file was added using 'include(godotcpp)' from the root CMakeLists.txt,
158+
# the ${CMAKE_CURRENT_SOURCE_DIR} is still the root dir.
159+
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
160+
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)
161+
162+
# Define our godot-cpp library
163+
add_library(${PROJECT_NAME} STATIC
164+
${SOURCES}
165+
${HEADERS}
166+
${GENERATED_FILES_LIST}
167+
)
168+
add_library(godot::cpp ALIAS ${PROJECT_NAME})
169+
170+
include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
171+
172+
target_compile_features(${PROJECT_NAME}
173+
PRIVATE
174+
cxx_std_17
175+
)
176+
177+
if(GODOT_USE_HOT_RELOAD)
178+
target_compile_definitions(${PROJECT_NAME} PUBLIC HOT_RELOAD_ENABLED)
179+
target_compile_options(${PROJECT_NAME} PUBLIC $<${compiler_is_gnu}:-fno-gnu-unique>)
180+
endif()
181+
182+
target_compile_definitions(${PROJECT_NAME} PUBLIC
183+
$<$<CONFIG:Debug>:
184+
DEBUG_ENABLED
185+
DEBUG_METHODS_ENABLED
186+
>
187+
$<${compiler_is_msvc}:
188+
TYPED_METHOD_BIND
189+
>
190+
)
191+
192+
target_link_options(${PROJECT_NAME} PRIVATE
193+
$<$<NOT:${compiler_is_msvc}>:
194+
-static-libgcc
195+
-static-libstdc++
196+
-Wl,-R,'$$ORIGIN'
197+
>
198+
)
199+
200+
# Optionally mark headers as SYSTEM
201+
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE "")
202+
if (GODOT_SYSTEM_HEADERS)
203+
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
204+
endif ()
205+
206+
target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
207+
include
208+
${CMAKE_CURRENT_BINARY_DIR}/gen/include
209+
${GODOT_GDEXTENSION_DIR}
210+
)
211+
212+
# Add the compile flags
213+
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
214+
215+
# Create the correct name (godot.os.build_type.system_bits)
216+
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
217+
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
218+
219+
if(ANDROID)
220+
# Added the android abi after system name
221+
set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI})
222+
223+
# Android does not have the bits at the end if you look at the main godot repo build
224+
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}")
225+
else()
226+
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
227+
endif()
228+
229+
set_target_properties(${PROJECT_NAME}
230+
PROPERTIES
231+
CXX_EXTENSIONS OFF
232+
POSITION_INDEPENDENT_CODE ON
233+
CXX_VISIBILITY_PRESET ${GODOT_SYMBOL_VISIBILITY}
234+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
235+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
236+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
237+
OUTPUT_NAME "${OUTPUT_NAME}"
238+
)
239+
240+
endfunction()

‎doc/cmake.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## CMake
2+
3+
### cmake arguments
4+
5+
`CMAKE_BUILD_TYPE`: Compilation target (Debug or Release defaults to Debug)
6+
7+
### godot-cpp cmake arguments
8+
- `GODOT_GDEXTENSION_DIR`: Path to the directory containing GDExtension interface header and API JSON file
9+
- `GODOT_SYSTEM_HEADERS`: Mark the header files as SYSTEM. This may be useful to suppress warnings in projects including this one.
10+
- `GODOT_WARNING_AS_ERROR`: Treat any warnings as errors
11+
- `GODOT_USE_HOT_RELOAD`: Build with hot reload support. Defaults to YES for Debug-builds and NO for Release-builds.
12+
- `GODOT_CUSTOM_API_FILE`: Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)
13+
- `GODOT_PRECISION`: Floating-point precision level ("single", "double")
14+
15+
### Android cmake arguments
16+
- `CMAKE_TOOLCHAIN_FILE`: The path to the android cmake toolchain ($ANDROID_NDK/build/cmake/android.toolchain.cmake)
17+
- `ANDROID_NDK`: The path to the android ndk root folder
18+
- `ANDROID_TOOLCHAIN_NAME`: The android toolchain (arm-linux-androideabi-4.9 or aarch64-linux-android-4.9 or x86-4.9 or x86_64-4.9)
19+
- `ANDROID_PLATFORM`: The android platform version (android-23)
20+
21+
- More info [here](https://godot.readthedocs.io/en/latest/development/compiling/compiling_for_android.html)
22+
23+
## Examples
24+
```shell
25+
Builds a debug version:
26+
cmake .
27+
cmake --build .
28+
```
29+
Builds a release version with clang
30+
31+
```shell
32+
CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" .
33+
cmake --build .
34+
```
35+
Builds an android armeabi-v7a debug version:
36+
37+
``` shell
38+
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK \
39+
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
40+
cmake --build .
41+
```
42+
43+
## Protip
44+
Generate the buildfiles in a sub directory to not clutter the root directory with build files:
45+
46+
```shell
47+
mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
48+
```
49+
50+
Ensure that you avoid exposing godot-cpp symbols - this might lead to hard to debug errors if you ever load multiple
51+
plugins using difference godot-cpp versions. Use visibility hidden whenever possible:
52+
```cmake
53+
set_target_properties(<all-my-plugin-related-targets> PROPERTIES CXX_VISIBILITY_PRESET hidden)
54+
```
55+
56+
## Todo
57+
Test build for Windows, Mac and mingw.

0 commit comments

Comments
 (0)
Please sign in to comment.