Skip to content

Commit 617c043

Browse files
authored
Enable default warning levels in cmake build (lballabio#1611)
2 parents b7f3dc2 + 96dc6df commit 617c043

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ jobs:
2121
run: |
2222
mkdir build
2323
cd build
24-
cmake .. -GNinja -DBOOST_ROOT=/usr -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
25-
cmake --build .
24+
cmake .. -GNinja -DBOOST_ROOT=/usr -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -L
25+
cmake --build . --verbose
2626
- name: Test
2727
run: |
2828
cd build
@@ -45,8 +45,8 @@ jobs:
4545
cmake-linux-ci-opts-
4646
- name: Compile
4747
run: |
48-
cmake --preset linux-ci-build-with-nonstandard-options
49-
cmake --build --preset linux-ci-build-with-nonstandard-options
48+
cmake --preset linux-ci-build-with-nonstandard-options -L
49+
cmake --build --preset linux-ci-build-with-nonstandard-options --verbose
5050
- name: Test
5151
run: |
5252
cd build
@@ -78,7 +78,7 @@ jobs:
7878
mkdir build
7979
cd build
8080
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Vc\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.3 || exit 1
81-
cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
81+
cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -L
8282
cmake --build . --verbose
8383
dir ql\*.lib
8484
- name: Test
@@ -112,7 +112,7 @@ jobs:
112112
mkdir build
113113
cd build
114114
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Vc\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.3 || exit 1
115-
cmake .. -GNinja -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
115+
cmake .. -GNinja -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -L
116116
cmake --build . --verbose
117117
dir ql\*.lib
118118
- name: Test
@@ -144,8 +144,8 @@ jobs:
144144
shell: cmd
145145
run: |
146146
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Vc\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.3 || exit 1
147-
cmake --preset windows-ci-build-with-nonstandard-options
148-
cmake --build --preset windows-ci-build-with-nonstandard-options
147+
cmake --preset windows-ci-build-with-nonstandard-options -L
148+
cmake --build --preset windows-ci-build-with-nonstandard-options --verbose
149149
- name: Test
150150
run: |
151151
cd build
@@ -171,8 +171,8 @@ jobs:
171171
run: |
172172
mkdir build
173173
cd build
174-
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja
175-
cmake --build .
174+
cmake .. -DCMAKE_BUILD_TYPE=Release -DQL_COMPILE_WARNING_AS_ERROR=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja -L
175+
cmake --build . --verbose
176176
- name: Test
177177
run: |
178178
cd build

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ option(QL_ENABLE_PARALLEL_UNIT_TEST_RUNNER "Enable the parallel unit test runner
4747
option(QL_ENABLE_SESSIONS "Singletons return different instances for different sessions" OFF)
4848
option(QL_ENABLE_THREAD_SAFE_OBSERVER_PATTERN "Enable the thread-safe observer pattern" OFF)
4949
option(QL_ENABLE_TRACING "Tracing messages should be allowed" OFF)
50+
option(QL_ENABLE_DEFAULT_WARNING_LEVEL "Enable the default warning level to pass the ci pipeline" ON)
51+
option(QL_COMPILE_WARNING_AS_ERROR "Specify whether to treat warnings on compile as errors." OFF)
5052
option(QL_ERROR_FUNCTIONS "Error messages should include current function information" OFF)
5153
option(QL_ERROR_LINES "Error messages should include file and line information" OFF)
5254
option(QL_EXTRA_SAFETY_CHECKS "Extra safety checks should be performed" OFF)
@@ -101,6 +103,34 @@ if (CMAKE_CXX_STANDARD LESS 17)
101103
endif()
102104
endif ()
103105

106+
# Set the default warning level we use to pass the GitHub workflows
107+
if (QL_ENABLE_DEFAULT_WARNING_LEVEL)
108+
if (MSVC)
109+
# warning level 3
110+
# There are also specific warnings disabled for MSCV in cmake/Platform.cmake.
111+
add_compile_options(-W3)
112+
else()
113+
# lots of warnings
114+
add_compile_options(-Wall -Wno-unknown-pragmas)
115+
endif()
116+
endif()
117+
118+
# Treat warnings on compile as errors
119+
if (QL_COMPILE_WARNING_AS_ERROR)
120+
# all compiler warnings as errors
121+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
122+
# since v3.24 cmake can set all compiler warnings as errors itself
123+
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
124+
else()
125+
# or set them manually
126+
if (MSVC)
127+
add_compile_options(-WX)
128+
else()
129+
add_compile_options(-Werror)
130+
endif()
131+
endif()
132+
endif()
133+
104134
if (QL_USE_CLANG_TIDY)
105135
if (NOT DEFINED QL_CLANG_TIDY)
106136
set(QL_CLANG_TIDY clang-tidy)

CMakePresets.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
"QL_HIGH_RESOLUTION_DATE": "ON",
117117
"QL_NULL_AS_FUNCTIONS": "ON",
118118
"QL_USE_INDEXED_COUPON": "ON",
119-
"QL_USE_STD_CLASSES": "ON"
119+
"QL_USE_STD_CLASSES": "ON",
120+
"QL_COMPILE_WARNING_AS_ERROR": "ON"
120121
}
121122
},
122123
{
@@ -137,6 +138,7 @@
137138
"QL_NULL_AS_FUNCTIONS": "ON",
138139
"QL_USE_INDEXED_COUPON": "ON",
139140
"QL_USE_STD_CLASSES": "ON",
141+
"QL_COMPILE_WARNING_AS_ERROR": "ON",
140142
"CMAKE_CXX_COMPILER_LAUNCHER": "sccache"
141143
},
142144
"vendor": {

cmake/Platform.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if (MSVC)
1717

1818
add_compile_definitions(NOMINMAX)
1919

20+
# caused by ql\time\date.cpp: warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.
21+
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
22+
2023
# /wd4267
2124
# Suppress warnings: assignment of 64-bit value to 32-bit QuantLib::Integer (x64)
2225

@@ -27,4 +30,11 @@ if (MSVC)
2730
# Suppress warnings: "Prefer enum class over enum" (Enum.3)
2831

2932
add_compile_options(/wd4267 /wd4819 /wd26812)
33+
34+
# prevent warnings when using /std:c++17 and above
35+
if(CMAKE_CXX_STANDARD GREATER_EQUAL 17)
36+
# E.g. caused by #include <boost/numeric/ublas/matrix.hpp>
37+
# In c++17 std::iterator is deprecated. As of boost 1.81 boost::ublas has not provided a fix for this.
38+
add_compile_definitions(_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
39+
endif()
3040
endif()

0 commit comments

Comments
 (0)