Skip to content

Commit 2de650b

Browse files
enetherudsnopek
authored andcommitted
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. (cherry picked from commit 2402a04)
1 parent 5fe58bc commit 2de650b

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()

0 commit comments

Comments
 (0)