Skip to content

Commit cc25ef6

Browse files
committed
Modernise Existing CMakeLists.txt
== godot-cpp == Moved find package for python to root CMakeLists.txt Silenced warning for unused CMAKE_C_COMPILER when specified in toolchain Changed godot-cpp compile feature cxx_std_17 to PUBLIC to propagate to consumers Changed the MSVC warning flags to PUBLIC to propagate to consumers Turn on WINDOWS_ENABLED based on WIN32, not IS_MSVC New cmake/sources.cmake to collect all the pre-existing source files, because globing is evil. Made all godot-cpp sources PRIVATE Exposed headers as INTERFACE using target_include_directories for consumers ### Generator Expressions Renamed generator expression helper variables to ease readability Moved all flags to generator expressions Moved generator expression helpers to common_compiler_flags.cmake Refactor SYSTEM_NAME and BUILD_TYPE to use generator expressions Refactor HOT_RELOAD to use generator expressions CMAKE_BUILD_TYPE is no longer depended upon, eliminating config errors for multi-config builds like Visual Studio, and Ninja-MultiConfig Now that we are specifying the config at build time, the hack to re-write CMAKE_CXX_FLAGS(_DEBUG) flags is no longer needed. Remove CMAKE_BUILD_TYPE from msvc CI target as it is a Multi-Config generator and ignores it ### test as a target Update test/CmakeLists.txt to use target_link_libraries to pull in required things from godot-cpp target Add EXCLUDE_FROM_ALL to godot-cpp-test so that it isn't built as part of the 'all' target == godot-cpp-test == updated ci to build the godot-cpp-test target from the root directory using cmake Adding the LANGUAGES CXX to the test project maintains the existing behavior of not checking for C compiler during configure. Removed majority of the cmake code as it was duplicating things that propagate as transient dependency from godot-cpp
1 parent 355e16a commit cc25ef6

File tree

6 files changed

+267
-284
lines changed

6 files changed

+267
-284
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 10 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)
@@ -263,10 +261,9 @@ jobs:
263261

264262
- name: Build godot-cpp
265263
run: |
266-
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
267-
cmake --build . --verbose
264+
cmake -G"Visual Studio 16 2019" .
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: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
cmake_minimum_required(VERSION 3.13)
22
project(godot-cpp LANGUAGES CXX)
33

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 ()
4+
# Get Python
5+
find_package(Python3 3.4 REQUIRED) # pathlib should be present
6+
7+
if( CMAKE_C_COMPILER)
8+
#Silence warning from unused CMAKE_C_COMPILER from toolchain
129
endif ()
1310

11+
# Main Library
1412
include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
1513

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

2422
godotcpp_generate()
23+
24+
# Test Example
25+
add_subdirectory( test )

cmake/common_compiler_flags.cmake

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
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+
14+
set( NOT_RELEASE "$<NOT:$<CONFIG:Release>>")
15+
16+
set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")
17+
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${NOT_RELEASE},$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
18+
19+
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
20+
21+
target_compile_features(${PROJECT_NAME}
22+
PUBLIC
23+
cxx_std_17
24+
)
825

926
# These compiler options reflect what is in godot/SConstruct.
10-
target_compile_options( ${PROJECT_NAME} PRIVATE
27+
target_compile_options( ${PROJECT_NAME}
28+
PUBLIC
1129
# MSVC only
12-
$<${compiler_is_msvc}:
30+
$<${IS_MSVC}:
1331
/W4
1432

1533
# Disable warnings which we don't plan to fix.
@@ -23,72 +41,97 @@ target_compile_options( ${PROJECT_NAME} PRIVATE
2341
/wd4514 # C4514 (unreferenced inline function has been removed)
2442
/wd4714 # C4714 (function marked as __forceinline not inlined)
2543
/wd4820 # C4820 (padding added after construct)
44+
$<${WARNING_AS_ERROR}:/WX>
2645
>
46+
PRIVATE
2747

2848
# Clang and GNU common options
29-
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:
49+
$<$<OR:${IS_CLANG},${IS_GNU}>:
3050
-Wall
3151
-Wctor-dtor-privacy
3252
-Wextra
3353
-Wno-unused-parameter
3454
-Wnon-virtual-dtor
3555
-Wwrite-strings
56+
57+
$<${WARNING_AS_ERROR}:-Werror>
3658
>
3759

3860
# Clang only
39-
$<${compiler_is_clang}:
61+
$<${IS_CLANG}:
4062
-Wimplicit-fallthrough
4163
-Wno-ordered-compare-function-pointers
4264
>
4365

4466
# GNU only
45-
$<${compiler_is_gnu}:
67+
$<${IS_GNU}:
4668
-Walloc-zero
4769
-Wduplicated-branches
4870
-Wduplicated-cond
4971
-Wno-misleading-indentation
5072
-Wplacement-new=1
5173
-Wshadow-local
5274
-Wstringop-overflow=4
53-
>
54-
$<$<AND:${compiler_is_gnu},${compiler_less_than_v8}>:
75+
5576
# 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}>:
77+
$<${GNU_LT_V8}:-Wno-strict-overflow>
78+
79+
$<${GNU_GE_V9}:-Wattribute-alias=2>
80+
6281
# Broke on MethodBind templates before GCC 11.
63-
-Wlogical-op
64-
>
65-
$<$<AND:${compiler_is_gnu},${compiler_less_than_v11}>:
82+
$<${GNU_GT_V11}:-Wlogical-op>
83+
6684
# 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}>:
85+
$<${GNU_LT_V11}:-Wno-type-limits>
86+
7087
# False positives in our error macros, see GH-58747.
71-
-Wno-return-type
88+
$<${GNU_GE_V12}:-Wno-return-type>
89+
>
90+
PUBLIC
91+
$<${IS_MSVC}:
92+
/utf-8
93+
$<IF:$<CONFIG:Debug>,/MDd,/MD /O2>
94+
$<$<NOT:${DISABLE_EXCEPTIONS}>:/EHsc>
95+
>
96+
97+
$<$<OR:${IS_CLANG},${IS_GNU}>:
98+
$<${DISABLE_EXCEPTIONS}:-fno-exceptions>
99+
100+
$<IF:$<CONFIG:Debug>,-fno-omit-frame-pointer -O0 -g,-O3>
72101
>
102+
$<${IS_GNU}:$<${HOT_RELOAD}:-fno-gnu-unique>>
103+
)
104+
105+
target_compile_definitions(${PROJECT_NAME}
106+
PUBLIC
107+
GDEXTENSION
108+
109+
$<$<BOOL:${WIN32}>:WINDOWS_ENABLED>
110+
111+
$<${IS_MSVC}:
112+
TYPED_METHOD_BIND
113+
NOMINMAX
114+
$<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0>
115+
>
116+
117+
$<IF:$<CONFIG:Debug>,DEBUG_ENABLED DEBUG_METHODS_ENABLED,NDEBUG>
118+
119+
$<${HOT_RELOAD}:HOT_RELOAD_ENABLED>
120+
121+
$<$<STREQUAL:${GODOT_PRECISION},double>:REAL_T_IS_DOUBLE>
73122
)
74123

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()
124+
target_link_options(${PROJECT_NAME} PRIVATE
125+
$<$<OR:${IS_CLANG},${IS_GNU}>:
126+
-static-libgcc
127+
-static-libstdc++
128+
>
129+
# reading up on RPATH this is only relevant for unix based systems
130+
# https://stackoverflow.com/questions/107888/is-there-a-windows-msvc-equivalent-to-the-rpath-linker-flag
131+
# Is probably worth putting it behind another guard in the future.
132+
# It appears that gcc silently ignores it on windows
133+
# It's also probably unnecessary to be explicitly stated as CMake has a target property for it.
134+
$<${IS_GNU}:
135+
-Wl,-R,'$$ORIGIN'
136+
>
137+
)

0 commit comments

Comments
 (0)