Skip to content

Commit 21bbc11

Browse files
committed
Enable warnings
1 parent d0345ca commit 21bbc11

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

CMakeLists.txt

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cmake_minimum_required(VERSION 3.16...3.27)
22

3-
project(rerun_vrs_example LANGUAGES CXX)
3+
set(CMAKE_COMPILE_WARNING_AS_ERROR ON) # Treat warnings as errors?
4+
5+
set(TARGET_NAME rerun_vrs_example)
6+
project(${TARGET_NAME} LANGUAGES CXX)
47

58
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
69

@@ -22,6 +25,93 @@ FetchContent_MakeAvailable(vrslib)
2225

2326
find_package(fmt REQUIRED)
2427

25-
add_executable(rerun_vrs_example src/main.cpp src/frame_player.cpp src/imu_player.cpp)
26-
target_link_libraries(rerun_vrs_example rerun_sdk vrslib vrs_utils fmt)
27-
target_include_directories(rerun_vrs_example PRIVATE src)
28+
add_executable(${TARGET_NAME} src/main.cpp src/frame_player.cpp src/imu_player.cpp)
29+
target_link_libraries(${TARGET_NAME} rerun_sdk vrslib vrs_utils fmt)
30+
target_include_directories(${TARGET_NAME} PRIVATE src)
31+
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${vrslib_SOURCE_DIR}) # Ignore warnings in VRS headers
32+
33+
34+
if(MSVC)
35+
# TODO(andreas): Try to enable /Wall
36+
target_compile_options(${TARGET_NAME} PRIVATE /W4)
37+
38+
target_compile_options(${TARGET_NAME} PRIVATE /we4996) # Using deprecated functions is an error
39+
40+
if(BUILD_SHARED_LIBS)
41+
# If we are building as shared libs, we are going to have to disable the C4251
42+
# warning, as it would trigger for any datatype derived from a STL class
43+
# See also https://github.com/protocolbuffers/protobuf/blob/v26.1/cmake/README.md#notes-on-compiler-warnings
44+
# We need also to make it public, otherwise downstream code will be flooded by c4251 warnings
45+
target_compile_options(${TARGET_NAME} PUBLIC /wd4251)
46+
endif()
47+
48+
# CMAKE_COMPILE_WARNING_AS_ERROR is only directly supported starting in CMake `3.24`
49+
# https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_WARNING_AS_ERROR.html
50+
if(CMAKE_COMPILE_WARNING_AS_ERROR)
51+
target_compile_options(${TARGET_NAME} PRIVATE /WX
52+
/w15038 # Initialization order. https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/c5038
53+
)
54+
endif()
55+
else()
56+
# Enabled warnings.
57+
target_compile_options(${TARGET_NAME} PRIVATE
58+
-Wall
59+
-Wcast-align
60+
-Wcast-qual
61+
-Werror=deprecated-declarations
62+
-Wextra
63+
-Wformat=2
64+
-Wmissing-include-dirs
65+
-Wnull-dereference
66+
-Wold-style-cast
67+
-Wpedantic
68+
-Wpointer-arith
69+
-Wshadow
70+
-Wswitch-enum
71+
-Wunreachable-code
72+
-Wvla
73+
)
74+
75+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") # match both "Clang" and "AppleClang"
76+
# TODO(emilk): enable some hardening flags from https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
77+
target_compile_options(${TARGET_NAME} PRIVATE
78+
-Wc++17-compat-pedantic
79+
-Wc++20-compat-pedantic
80+
-Wc99-extensions
81+
-Weverything
82+
-Wgnu
83+
-Wnon-gcc
84+
-Wpre-c2x-compat-pedantic
85+
-Wshadow-all
86+
87+
# Turn off some warning that -Weverything turns on:
88+
-Wno-c++98-compat
89+
-Wno-c++98-compat-pedantic
90+
-Wno-covered-switch-default # We often add a `default:` case out of paranoia
91+
-Wno-ctad-maybe-unsupported
92+
-Wno-disabled-macro-expansion
93+
-Wno-documentation
94+
-Wno-documentation-unknown-command
95+
-Wno-double-promotion # float->double is nothing to whine about
96+
-Wno-exit-time-destructors
97+
-Wno-float-equal # comparing floats is fine
98+
-Wno-global-constructors
99+
-Wno-missing-prototypes
100+
-Wno-padded
101+
-Wno-reserved-id-macro
102+
-Wno-reserved-identifier
103+
-Wno-unused-macros
104+
)
105+
endif()
106+
107+
# CMAKE_COMPILE_WARNING_AS_ERROR is only directly supported starting in CMake `3.24`
108+
# https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_WARNING_AS_ERROR.html
109+
if(CMAKE_COMPILE_WARNING_AS_ERROR)
110+
target_compile_options(${TARGET_NAME} PRIVATE -Werror)
111+
endif()
112+
113+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
114+
# Improve stack traces:
115+
target_compile_options(${TARGET_NAME} PRIVATE -g -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fno-optimize-sibling-calls)
116+
endif()
117+
endif()

0 commit comments

Comments
 (0)