Skip to content

Commit e7cce7b

Browse files
committed
{cmake} Use the same compiler warnings as godot
1 parent 860182f commit e7cce7b

File tree

2 files changed

+123
-33
lines changed

2 files changed

+123
-33
lines changed

CMakeLists.txt

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# CMAKE_BUILD_TYPE: Compilation target (Debug or Release defaults to Debug)
33
#
44
# 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 supress warnings in projects including this one.
7-
# GODOT_CUSTOM_API_FILE: Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)
8-
# FLOAT_PRECISION: Floating-point precision level ("single", "double")
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 supress warnings in projects including this one.
7+
# GODOT_CPP_WARNING_AS_ERROR Treat any warnings as errors
8+
# GODOT_CUSTOM_API_FILE: Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)
9+
# FLOAT_PRECISION: Floating-point precision level ("single", "double")
910
#
1011
# Android cmake arguments
1112
# CMAKE_TOOLCHAIN_FILE: The path to the android cmake toolchain ($ANDROID_NDK/build/cmake/android.toolchain.cmake)
@@ -41,6 +42,15 @@ cmake_minimum_required(VERSION 3.12)
4142

4243
option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node." ON)
4344
option(GODOT_CPP_SYSTEM_HEADERS "Expose headers as SYSTEM." OFF)
45+
option(GODOT_CPP_WARNING_AS_ERROR "Treat warnings as errors" OFF)
46+
47+
# Add path to modules
48+
list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" )
49+
50+
# Check if we are building ourself or being included
51+
if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
52+
set(GODOT_CPP_BUILDING_SELF ON)
53+
endif()
4454

4555
# Default build type is Debug in the SConstruct
4656
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
@@ -72,7 +82,7 @@ set(GODOT_LINKER_FLAGS )
7282

7383
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
7484
# using Visual Studio C++
75-
set(GODOT_COMPILE_FLAGS "/EHsc /WX") # /GF /MP
85+
set(GODOT_COMPILE_FLAGS "/EHsc") # /GF /MP
7686

7787
if(CMAKE_BUILD_TYPE MATCHES Debug)
7888
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
@@ -82,39 +92,14 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
8292
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
8393
endif(CMAKE_BUILD_TYPE MATCHES Debug)
8494

85-
# Disable conversion warning, truncation, unreferenced var, signed mismatch, different type
86-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /wd4244 /wd4305 /wd4101 /wd4018 /wd4267 /wd4099")
87-
8895
add_definitions(-DNOMINMAX)
89-
90-
# Unkomment for warning level 4
91-
#if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
92-
# string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
93-
#endif()
94-
9596
else() # GCC/Clang
9697
set(GODOT_LINKER_FLAGS "-static-libgcc -static-libstdc++ -Wl,-R,'$$ORIGIN'")
9798

9899
if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
99100
set(GODOT_COMPILE_FLAGS "-fPIC")
100101
endif()
101-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -g -Wwrite-strings")
102-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wchar-subscripts -Wcomment -Wdisabled-optimization")
103-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wformat -Wformat=2 -Wformat-security -Wformat-y2k")
104-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wimport -Winit-self -Winline -Winvalid-pch")
105-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wmissing-braces -Wmissing-format-attribute")
106-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wpointer-arith")
107-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wredundant-decls -Wreturn-type -Wsequence-point")
108-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wswitch -Wswitch-enum -Wtrigraphs")
109-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused-label")
110-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wunused-value -Wvariadic-macros -Wvolatile-register-var -Wno-error=attributes")
111-
112-
# -Wshadow -Wextra -Wall -Weffc++ -Wfloat-equal -Wstack-protector -Wunused-parameter -Wsign-compare -Wunused-variable -Wcast-align
113-
# -Wunused-function -Wstrict-aliasing -Wstrict-aliasing=2 -Wmissing-field-initializers
114-
115-
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
116-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -Wno-ignored-attributes")
117-
endif()
102+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -g")
118103

119104
if(CMAKE_BUILD_TYPE MATCHES Debug)
120105
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0")
@@ -157,6 +142,14 @@ add_library(${PROJECT_NAME}
157142
)
158143
add_library(godot::cpp ALIAS ${PROJECT_NAME})
159144

145+
include(GodotCompilerWarnings)
146+
147+
# Treat warnings as errors if we are building ourself
148+
if(GODOT_CPP_BUILDING_SELF)
149+
unset( GODOT_CPP_WARNING_AS_ERROR )
150+
set_warning_as_error()
151+
endif()
152+
160153
target_compile_features(${PROJECT_NAME}
161154
PRIVATE
162155
cxx_std_17
@@ -182,8 +175,6 @@ target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE}
182175
${CMAKE_CURRENT_BINARY_DIR}/gen/include
183176
)
184177

185-
unset( GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE )
186-
187178
# Put godot headers as SYSTEM PUBLIC to exclude warnings from irrelevant headers
188179
target_include_directories(${PROJECT_NAME}
189180
SYSTEM PUBLIC

cmake/GodotCompilerWarnings.cmake

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Add warnings based on compiler & version
2+
# Set some helper variables for readability
3+
set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
4+
set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
5+
set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )
6+
7+
set( compiler_less_than_v8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
8+
set( compiler_greater_than_or_equal_v9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
9+
set( compiler_greater_than_or_equal_v11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
10+
set( compiler_less_than_v11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
11+
set( compiler_less_than_or_equal_v12 "$<VERSION_LESS_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
12+
13+
# These compiler options reflect what is in godot/SConstruct.
14+
target_compile_options( ${PROJECT_NAME} PRIVATE
15+
# MSVC only
16+
$<${compiler_is_msvc}:
17+
/W4
18+
19+
# Disable warnings which we don't plan to fix.
20+
/wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
21+
/wd4127 # C4127 (conditional expression is constant)
22+
/wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
23+
/wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
24+
/wd4245
25+
/wd4267
26+
/wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
27+
/wd4514 # C4514 (unreferenced inline function has been removed)
28+
/wd4714 # C4714 (function marked as __forceinline not inlined)
29+
/wd4820 # C4820 (padding added after construct)
30+
>
31+
32+
# Clang and GNU common options
33+
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:
34+
-Wall
35+
-Wctor-dtor-privacy
36+
-Wextra
37+
-Wno-unused-parameter
38+
-Wnon-virtual-dtor
39+
-Wwrite-strings
40+
>
41+
42+
# Clang only
43+
$<${compiler_is_clang}:
44+
-Wimplicit-fallthrough
45+
-Wno-ordered-compare-function-pointers
46+
>
47+
48+
# GNU only
49+
$<${compiler_is_gnu}:
50+
-Walloc-zero
51+
-Wduplicated-branches
52+
-Wduplicated-cond
53+
-Wno-misleading-indentation
54+
-Wplacement-new=1
55+
-Wshadow-local
56+
-Wstringop-overflow=4
57+
>
58+
$<$<AND:${compiler_is_gnu},${compiler_less_than_v8}>:
59+
# Bogus warning fixed in 8+.
60+
-Wno-strict-overflow
61+
>
62+
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v9}>:
63+
# Bogus warning fixed in 8+.
64+
-Wattribute-alias=2
65+
>
66+
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v11}>:
67+
# Broke on MethodBind templates before GCC 11.
68+
-Wlogical-op
69+
>
70+
$<$<AND:${compiler_is_gnu},${compiler_less_than_v11}>:
71+
# Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
72+
-Wno-type-limits
73+
>
74+
$<$<AND:${compiler_is_gnu},${compiler_less_than_or_equal_v12}>:
75+
# False positives in our error macros, see GH-58747.
76+
-Wno-return-type
77+
>
78+
)
79+
80+
# Treat warnings as errors
81+
function( set_warning_as_error )
82+
message( STATUS "[${PROJECT_NAME}] Treating warnings as errors")
83+
if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" )
84+
set_target_properties( ${PROJECT_NAME}
85+
PROPERTIES
86+
COMPILE_WARNING_AS_ERROR ON
87+
)
88+
else()
89+
target_compile_options( ${PROJECT_NAME}
90+
PRIVATE
91+
$<$<CXX_COMPILER_ID:MSVC>:/WX>
92+
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:-Werror>
93+
)
94+
endif()
95+
endfunction()
96+
97+
if ( GODOT_CPP_WARNING_AS_ERROR )
98+
set_warning_as_error()
99+
endif()

0 commit comments

Comments
 (0)