Skip to content

Commit 461fa63

Browse files
committed
new cmake/sources.cmake to collect all the pre-existing source files, because globing is evil.
Moved all flags to generator expressions Moved generator expression helpers to common_compiler_flags.cmake Renamed helper variables to shorten to ease readability Moved find package for python to root CMakeLists.txt Silenced warning for unused CMAKE_C_COMPILER Update test/CmakeLists.txt to use target_link_libraries to pull in required things from godot-cpp target updated ci to build the godot-cpp-test target from the root directory using cmake Changed the visibility of GENERATED_FILES_LIST to PRIVATE Changed the MSVC warning flags to PUBLIC to hide warning in consumers changed godot-cpp compile feature cxx_std_17 to PUBLIC so as to propagate to consumers
1 parent 355e16a commit 461fa63

File tree

6 files changed

+228
-268
lines changed

6 files changed

+228
-268
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,11 @@ jobs:
221221
- name: Build godot-cpp
222222
run: |
223223
cmake -DCMAKE_BUILD_TYPE=Release .
224-
make -j $(nproc) VERBOSE=1
224+
cmake --build . --verbose -j $(nproc) -t godot-cpp
225225
226226
- name: Build test GDExtension library
227227
run: |
228-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." .
229-
make -j $(nproc) VERBOSE=1
228+
cmake --build . --verbose -j $(nproc) -t godot-cpp-test
230229
231230
linux-cmake-ninja:
232231
name: 🐧 Build (Linux, GCC, CMake Ninja)
@@ -245,12 +244,11 @@ jobs:
245244
- name: Build godot-cpp
246245
run: |
247246
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .
248-
cmake --build . -j $(nproc) --verbose
247+
cmake --build . --verbose -j $(nproc) -t godot-cpp
249248
250249
- name: Build test GDExtension library
251250
run: |
252-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -GNinja .
253-
cmake --build . -j $(nproc) --verbose
251+
cmake --build . --verbose -j $(nproc) -t godot-cpp-test
254252
255253
windows-msvc-cmake:
256254
name: 🏁 Build (Windows, MSVC, CMake)
@@ -264,9 +262,8 @@ jobs:
264262
- name: Build godot-cpp
265263
run: |
266264
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
267-
cmake --build . --verbose
265+
cmake --build . --verbose -t godot-cpp
268266
269267
- name: Build test GDExtension library
270268
run: |
271-
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -G"Visual Studio 16 2019" .
272-
cmake --build . --verbose
269+
cmake --build . --verbose -t godot-cpp-test

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cmake_minimum_required(VERSION 3.13)
22
project(godot-cpp LANGUAGES CXX)
33

4+
# Get Python
5+
find_package(Python3 3.4 REQUIRED) # pathlib should be present
6+
47
# Configure CMake
58
# https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965
69
# https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake
@@ -11,6 +14,11 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
1114
endif ()
1215
endif ()
1316

17+
if( CMAKE_C_COMPILER)
18+
#Silence warning from unused CMAKE_C_COMPILER from toolchain
19+
endif ()
20+
21+
# Main Library
1422
include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
1523

1624
# I know this doesn't look like a typical CMakeLists.txt, but as we are
@@ -22,3 +30,6 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
2230
godotcpp_options()
2331

2432
godotcpp_generate()
33+
34+
# Test Example
35+
add_subdirectory( test )

cmake/common_compiler_flags.cmake

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
# Add warnings based on compiler & version
2-
# Set some helper variables for readability
3-
set( compiler_less_than_v8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
4-
set( compiler_greater_than_or_equal_v9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
5-
set( compiler_greater_than_or_equal_v11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
6-
set( compiler_less_than_v11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
7-
set( compiler_greater_than_or_equal_v12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
1+
#Generator Expression Helpers
2+
set( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
3+
set( IS_GNU "$<CXX_COMPILER_ID:GNU>" )
4+
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
5+
6+
set( GNU_LT_V8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
7+
set( GNU_GE_V9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
8+
set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
9+
set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
10+
set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
11+
12+
set( WARNING_AS_ERROR "$<BOOL:${GODOT_WARNING_AS_ERROR}>")
13+
set( HOT_RELOAD "$<BOOL:${GODOT_USE_HOT_RELOAD}>")
14+
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
15+
16+
target_compile_features(${PROJECT_NAME}
17+
PUBLIC
18+
cxx_std_17
19+
)
820

921
# These compiler options reflect what is in godot/SConstruct.
10-
target_compile_options( ${PROJECT_NAME} PRIVATE
22+
target_compile_options( ${PROJECT_NAME}
23+
PUBLIC
1124
# MSVC only
12-
$<${compiler_is_msvc}:
25+
$<${IS_MSVC}:
1326
/W4
1427

1528
# Disable warnings which we don't plan to fix.
@@ -23,72 +36,89 @@ target_compile_options( ${PROJECT_NAME} PRIVATE
2336
/wd4514 # C4514 (unreferenced inline function has been removed)
2437
/wd4714 # C4714 (function marked as __forceinline not inlined)
2538
/wd4820 # C4820 (padding added after construct)
39+
$<${WARNING_AS_ERROR}:/WX>
2640
>
41+
PRIVATE
2742

2843
# Clang and GNU common options
29-
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:
44+
$<$<OR:${IS_CLANG},${IS_GNU}>:
3045
-Wall
3146
-Wctor-dtor-privacy
3247
-Wextra
3348
-Wno-unused-parameter
3449
-Wnon-virtual-dtor
3550
-Wwrite-strings
51+
52+
$<${WARNING_AS_ERROR}:-Werror>
3653
>
3754

3855
# Clang only
39-
$<${compiler_is_clang}:
56+
$<${IS_CLANG}:
4057
-Wimplicit-fallthrough
4158
-Wno-ordered-compare-function-pointers
4259
>
4360

4461
# GNU only
45-
$<${compiler_is_gnu}:
62+
$<${IS_GNU}:
4663
-Walloc-zero
4764
-Wduplicated-branches
4865
-Wduplicated-cond
4966
-Wno-misleading-indentation
5067
-Wplacement-new=1
5168
-Wshadow-local
5269
-Wstringop-overflow=4
53-
>
54-
$<$<AND:${compiler_is_gnu},${compiler_less_than_v8}>:
70+
5571
# Bogus warning fixed in 8+.
56-
-Wno-strict-overflow
57-
>
58-
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v9}>:
59-
-Wattribute-alias=2
60-
>
61-
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v11}>:
72+
$<${GNU_LT_V8}:-Wno-strict-overflow>
73+
74+
$<${GNU_GE_V9}:-Wattribute-alias=2>
75+
6276
# Broke on MethodBind templates before GCC 11.
63-
-Wlogical-op
64-
>
65-
$<$<AND:${compiler_is_gnu},${compiler_less_than_v11}>:
77+
$<${GNU_GT_V11}:-Wlogical-op>
78+
6679
# Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
67-
-Wno-type-limits
68-
>
69-
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v12}>:
80+
$<${GNU_LT_V11}:-Wno-type-limits>
81+
7082
# False positives in our error macros, see GH-58747.
71-
-Wno-return-type
83+
$<${GNU_GE_V12}:-Wno-return-type>
84+
>
85+
PUBLIC
86+
$<${IS_MSVC}:
87+
/utf-8
88+
$<IF:$<CONFIG:Debug>,/MDd,/MD /O2>
89+
$<$<NOT:${DISABLE_EXCEPTIONS}>:/EHsc>
7290
>
91+
92+
$<$<OR:${IS_CLANG},${IS_GNU}>:
93+
$<${DISABLE_EXCEPTIONS}:-fno-exceptions>
94+
95+
$<IF:$<CONFIG:Debug>,-fno-omit-frame-pointer -O0 -g,-O3>
96+
>
97+
$<${IS_GNU}:$<${HOT_RELOAD}:-fno-gnu-unique>>
7398
)
7499

75-
# Treat warnings as errors
76-
function( set_warning_as_error )
77-
message( STATUS "[${PROJECT_NAME}] Treating warnings as errors")
78-
if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" )
79-
set_target_properties( ${PROJECT_NAME}
80-
PROPERTIES
81-
COMPILE_WARNING_AS_ERROR ON
82-
)
83-
else()
84-
target_compile_options( ${PROJECT_NAME}
85-
PRIVATE
86-
$<${compiler_is_msvc}:/WX>
87-
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:-Werror>
88-
)
89-
endif()
90-
endfunction()
91-
92-
if ( GODOT_WARNING_AS_ERROR )
93-
set_warning_as_error()
94-
endif()
100+
target_compile_definitions(${PROJECT_NAME}
101+
PUBLIC
102+
GDEXTENSION
103+
104+
$<${IS_MSVC}:
105+
WINDOWS_ENABLED
106+
TYPED_METHOD_BIND
107+
NOMINMAX
108+
$<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0>
109+
>
110+
111+
$<IF:$<CONFIG:Debug>,DEBUG_ENABLED DEBUG_METHODS_ENABLED,NDEBUG>
112+
113+
$<${HOT_RELOAD}:HOT_RELOAD_ENABLED>
114+
115+
$<$<STREQUAL:${GODOT_PRECISION},double>:REAL_T_IS_DOUBLE>
116+
)
117+
118+
target_link_options(${PROJECT_NAME} PRIVATE
119+
$<$<OR:${IS_CLANG},${IS_GNU}>:
120+
-static-libgcc
121+
-static-libstdc++
122+
-Wl,-R,'$$ORIGIN'
123+
>
124+
)

cmake/godotcpp.cmake

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function( godotcpp_options )
2828
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
2929
"Enable the extra accounting required to support hot reload. (ON|OFF)")
3030

31+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
32+
# saves around 20% of binary size and very significant build time (GH-80513).
3133
option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
3234

3335
set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING
@@ -54,11 +56,7 @@ endfunction()
5456

5557

5658
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-
59+
### Configure variables
6260
# CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal
6361
# which is inline with the gcc -fvisibility=
6462
# https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
@@ -78,6 +76,7 @@ function( godotcpp_generate )
7876
set(GODOT_USE_HOT_RELOAD ON)
7977
endif()
8078

79+
### Generate Bindings
8180
if(NOT DEFINED BITS)
8281
set(BITS 32)
8382
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
@@ -91,47 +90,7 @@ function( godotcpp_generate )
9190
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
9291
endif()
9392

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-
13393
# Generate source from the bindings file
134-
find_package(Python3 3.4 REQUIRED) # pathlib should be present
13594
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
13695
set(GENERATE_BINDING_PARAMETERS "True")
13796
else()
@@ -153,48 +112,18 @@ function( godotcpp_generate )
153112
COMMENT "Generating bindings"
154113
)
155114

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-
)
115+
### Define our godot-cpp library
116+
add_library(${PROJECT_NAME} STATIC )
168117
add_library(godot::cpp ALIAS ${PROJECT_NAME})
169118

170-
include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
171-
172-
target_compile_features(${PROJECT_NAME}
119+
# Get Sources
120+
include( cmake/sources.cmake )
121+
target_sources( ${PROJECT_NAME}
122+
PUBLIC
123+
${GODOTCPP_SOURCES}
124+
${GODOTCPP_HEADERS}
173125
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-
>
126+
${GENERATED_FILES_LIST}
198127
)
199128

200129
# Optionally mark headers as SYSTEM
@@ -209,10 +138,10 @@ function( godotcpp_generate )
209138
${GODOT_GDEXTENSION_DIR}
210139
)
211140

212-
# Add the compile flags
213-
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
141+
include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
142+
214143

215-
# Create the correct name (godot.os.build_type.system_bits)
144+
### Create the correct name (godot.os.build_type.system_bits)
216145
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
217146
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
218147

@@ -226,6 +155,7 @@ function( godotcpp_generate )
226155
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
227156
endif()
228157

158+
229159
set_target_properties(${PROJECT_NAME}
230160
PROPERTIES
231161
CXX_EXTENSIONS OFF

0 commit comments

Comments
 (0)