|
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_CPP_SYSTEM_HEADERS Mark the header files as SYSTEM. This may be useful to suppress warnings in projects including this one. |
7 |
| -# GODOT_CPP_WARNING_AS_ERROR Treat any warnings as errors |
8 |
| -# GODOT_ENABLE_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 |
| -# FLOAT_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 |
| -# Todo |
39 |
| -# Test build for Windows, Mac and mingw. |
40 |
| - |
41 | 1 | cmake_minimum_required(VERSION 3.13)
|
42 | 2 | project(godot-cpp LANGUAGES CXX)
|
43 | 3 |
|
44 |
| -option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node." ON) |
45 |
| -option(GODOT_CPP_SYSTEM_HEADERS "Expose headers as SYSTEM." ON) |
46 |
| -option(GODOT_CPP_WARNING_AS_ERROR "Treat warnings as errors" OFF) |
47 |
| - |
48 |
| -# Add path to modules |
49 |
| -list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" ) |
50 |
| - |
51 |
| -# Set some helper variables for readability |
52 |
| -set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" ) |
53 |
| -set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" ) |
54 |
| -set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" ) |
55 |
| - |
56 |
| -# Default build type is Debug in the SConstruct |
57 |
| -if("${CMAKE_BUILD_TYPE}" STREQUAL "") |
58 |
| - set(CMAKE_BUILD_TYPE Debug) |
59 |
| -endif() |
60 |
| - |
61 |
| -# Hot reload is enabled by default in Debug-builds |
62 |
| -if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") |
63 |
| - option(GODOT_ENABLE_HOT_RELOAD "Build with hot reload support" ON) |
64 |
| -else() |
65 |
| - option(GODOT_ENABLE_HOT_RELOAD "Build with hot reload support" OFF) |
66 |
| -endif() |
67 |
| - |
68 |
| -if(NOT DEFINED BITS) |
69 |
| - set(BITS 32) |
70 |
| - if(CMAKE_SIZEOF_VOID_P EQUAL 8) |
71 |
| - set(BITS 64) |
72 |
| - endif(CMAKE_SIZEOF_VOID_P EQUAL 8) |
73 |
| -endif() |
74 |
| - |
75 |
| -# Input from user for GDExtension interface header and the API JSON file |
76 |
| -set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "") |
77 |
| -set(GODOT_CUSTOM_API_FILE "" CACHE STRING "") |
78 |
| - |
79 |
| -set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json") |
80 |
| -if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override. |
81 |
| - set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}") |
82 |
| -endif() |
83 |
| - |
84 |
| -set(FLOAT_PRECISION "single" CACHE STRING "") |
85 |
| -if ("${FLOAT_PRECISION}" STREQUAL "double") |
86 |
| - add_definitions(-DREAL_T_IS_DOUBLE) |
87 |
| -endif() |
88 |
| - |
89 |
| -set(GODOT_COMPILE_FLAGS ) |
90 |
| - |
91 |
| -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
92 |
| - # using Visual Studio C++ |
93 |
| - set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP |
94 |
| - |
95 |
| - if(CMAKE_BUILD_TYPE MATCHES Debug) |
96 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi |
97 |
| - else() |
98 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy |
99 |
| - STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
100 |
| - string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) |
101 |
| - endif(CMAKE_BUILD_TYPE MATCHES Debug) |
102 |
| - |
103 |
| - add_definitions(-DNOMINMAX) |
104 |
| -else() # GCC/Clang |
105 |
| - if(CMAKE_BUILD_TYPE MATCHES Debug) |
106 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g") |
107 |
| - else() |
108 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3") |
109 |
| - endif(CMAKE_BUILD_TYPE MATCHES Debug) |
110 |
| -endif() |
111 |
| - |
112 |
| -# Disable exception handling. Godot doesn't use exceptions anywhere, and this |
113 |
| -# saves around 20% of binary size and very significant build time (GH-80513). |
114 |
| -option(GODOT_DISABLE_EXCEPTIONS ON "Force disabling exception handling code") |
115 |
| -if (GODOT_DISABLE_EXCEPTIONS) |
116 |
| - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
117 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0") |
118 |
| - else() |
119 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions") |
120 |
| - endif() |
121 |
| -else() |
122 |
| - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
123 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc") |
124 |
| - endif() |
125 |
| -endif() |
126 |
| - |
127 |
| -if (GODOT_ENABLE_HOT_RELOAD) |
128 |
| - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D HOT_RELOAD_ENABLED") |
129 |
| -endif() |
130 |
| - |
131 |
| -# Generate source from the bindings file |
132 |
| -find_package(Python3 3.4 REQUIRED) # pathlib should be present |
133 |
| -if(GENERATE_TEMPLATE_GET_NODE) |
134 |
| - set(GENERATE_BINDING_PARAMETERS "True") |
135 |
| -else() |
136 |
| - set(GENERATE_BINDING_PARAMETERS "False") |
137 |
| -endif() |
138 |
| - |
139 |
| -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)" |
140 |
| - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
141 |
| - OUTPUT_VARIABLE GENERATED_FILES_LIST |
142 |
| - OUTPUT_STRIP_TRAILING_WHITESPACE |
143 |
| -) |
144 |
| - |
145 |
| -add_custom_command(OUTPUT ${GENERATED_FILES_LIST} |
146 |
| - COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings(\"${GODOT_GDEXTENSION_API_FILE}\", \"${GENERATE_BINDING_PARAMETERS}\", \"${BITS}\", \"${FLOAT_PRECISION}\", \"${CMAKE_CURRENT_BINARY_DIR}\")" |
147 |
| - VERBATIM |
148 |
| - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
149 |
| - MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE} |
150 |
| - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py |
151 |
| - COMMENT "Generating bindings" |
152 |
| -) |
153 |
| - |
154 |
| -# Get Sources |
155 |
| -file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**) |
156 |
| -file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**) |
157 |
| - |
158 |
| -# Define our godot-cpp library |
159 |
| -add_library(${PROJECT_NAME} STATIC |
160 |
| - ${SOURCES} |
161 |
| - ${HEADERS} |
162 |
| - ${GENERATED_FILES_LIST} |
163 |
| -) |
164 |
| -add_library(godot::cpp ALIAS ${PROJECT_NAME}) |
165 |
| - |
166 |
| -include(GodotCompilerWarnings) |
167 |
| - |
168 |
| -target_compile_features(${PROJECT_NAME} |
169 |
| - PRIVATE |
170 |
| - cxx_std_17 |
171 |
| -) |
172 |
| - |
173 |
| -target_compile_definitions(${PROJECT_NAME} PUBLIC |
174 |
| - $<$<CONFIG:Debug>: |
175 |
| - DEBUG_ENABLED |
176 |
| - DEBUG_METHODS_ENABLED |
177 |
| - > |
178 |
| - $<${compiler_is_msvc}: |
179 |
| - TYPED_METHOD_BIND |
180 |
| - > |
181 |
| -) |
182 |
| - |
183 |
| -target_link_options(${PROJECT_NAME} PRIVATE |
184 |
| - $<$<NOT:${compiler_is_msvc}>: |
185 |
| - -static-libgcc |
186 |
| - -static-libstdc++ |
187 |
| - -Wl,-R,'$$ORIGIN' |
188 |
| - > |
189 |
| -) |
190 |
| - |
191 |
| -# Optionally mark headers as SYSTEM |
192 |
| -set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE "") |
193 |
| -if (GODOT_CPP_SYSTEM_HEADERS) |
194 |
| - set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE SYSTEM) |
| 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 () |
195 | 12 | endif ()
|
196 | 13 |
|
197 |
| -target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC |
198 |
| - include |
199 |
| - ${CMAKE_CURRENT_BINARY_DIR}/gen/include |
200 |
| - ${GODOT_GDEXTENSION_DIR} |
201 |
| -) |
202 |
| - |
203 |
| -# Add the compile flags |
204 |
| -set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS}) |
| 14 | +include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake ) |
205 | 15 |
|
206 |
| -# Create the correct name (godot.os.build_type.system_bits) |
207 |
| -string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME) |
208 |
| -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. |
209 | 19 |
|
210 |
| -if(ANDROID) |
211 |
| - # Added the android abi after system name |
212 |
| - set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI}) |
| 20 | +# The typical target definitions are in ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake |
213 | 21 |
|
214 |
| - # Android does not have the bits at the end if you look at the main godot repo build |
215 |
| - set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}") |
216 |
| -else() |
217 |
| - set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}") |
218 |
| -endif() |
| 22 | +godotcpp_options() |
219 | 23 |
|
220 |
| -set_target_properties(${PROJECT_NAME} |
221 |
| - PROPERTIES |
222 |
| - CXX_EXTENSIONS OFF |
223 |
| - POSITION_INDEPENDENT_CODE ON |
224 |
| - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin" |
225 |
| - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin" |
226 |
| - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin" |
227 |
| - OUTPUT_NAME "${OUTPUT_NAME}" |
228 |
| -) |
| 24 | +godotcpp_generate() |
0 commit comments