From f67450bd3e6b5351945c143c3edf0f22eb226f93 Mon Sep 17 00:00:00 2001 From: Paul Colby Date: Thu, 10 Oct 2024 18:04:26 +1100 Subject: [PATCH] Use `CMAKE_COMPILE_WARNING_AS_ERROR` when available Also make additional warning, and warnings-as-errors optional (but ON by default). --- CMakeLists.txt | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aa1154bdb..b9af0c158 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,12 +62,31 @@ add_compile_definitions( QT_USE_NODISCARD_FILE_OPEN ) -# Enable most compiler warnings, and treat as errors. -# \todo Consider using CMAKE_COMPILE_WARNING_AS_ERROR on CMake 3.24+ -if (MSVC) - add_compile_options(/W3 /WX) -else() - add_compile_options(-Wall -Wextra -Werror) +# Enable most compiler warnings, by default. +option(EXTRA_COMPILE_WARNINGS "Enable additional compile warnings" ON) +if (EXTRA_COMPILE_WARNINGS) + message(STATUS "Enabling additional compile warnings") + if (MSVC) + add_compile_options(/W3) + else() + add_compile_options(-Wall -Wextra) + endif() +endif() + +# Treat warnings as errors, by default. +if (CMAKE_VERSION VERSION_LESS 3.24) + option(COMPILE_WARNING_AS_ERROR "Treat compile compiler warnings as errors" ON) + if (CMAKE_COMPILE_WARNING_AS_ERROR) + message(STATUS "Treating compile warnings as errors") + if (MSVC) + add_compile_options(/WX) + else() + add_compile_options(-Werror) + endif() + endif() +elseif (NOT DEFINED CMAKE_COMPILE_WARNING_AS_ERROR) + message(STATUS "Treating compile warnings as errors") + set(CMAKE_COMPILE_WARNING_AS_ERROR ON) endif() # Optional test coverage reporting (off by default, and only supported for some compilers).