summaryrefslogtreecommitdiffstats
path: root/Tests
diff options
context:
space:
mode:
authorMartin Duffy <martin.duffy@kitware.com>2022-04-21 19:29:05 (GMT)
committerBrad King <brad.king@kitware.com>2022-05-06 16:14:37 (GMT)
commit76a08cd25332b74627802df8817068219b89161d (patch)
treea349756534c11634ac1fc919793801c78fbec612 /Tests
parentde802fc5a35780581948f7638f0f22c3974922dd (diff)
downloadCMake-76a08cd25332b74627802df8817068219b89161d.zip
CMake-76a08cd25332b74627802df8817068219b89161d.tar.gz
CMake-76a08cd25332b74627802df8817068219b89161d.tar.bz2
COMPILE_WARNING_AS_ERROR: Add options to treat warnings as errors
Add `COMPILE_WARNING_AS_ERROR` target property and supporting `CMAKE_COMPILE_WARNING_AS_ERROR` variable. `COMPILE_WARNING_AS_ERROR` is initialized by `CMAKE_COMPILE_WARNING_AS_ERROR`. It is a boolean variable. If it is true, it expands to a different flag depending on the compiler such that any warnings at compile will be treated as errors. Supports compiler ids that I could find a relevant flag for.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/RunCMake/CMakeLists.txt1
-rw-r--r--Tests/RunCMake/CompileWarningAsError/CMakeLists.txt3
-rw-r--r--Tests/RunCMake/CompileWarningAsError/RunCMakeTest.cmake12
-rw-r--r--Tests/RunCMake/CompileWarningAsError/WarningAsErrorOptions.cmake18
-rw-r--r--Tests/RunCMake/CompileWarningAsError/WerrorOff.cmake8
-rw-r--r--Tests/RunCMake/CompileWarningAsError/WerrorOn-Build-result.txt1
-rw-r--r--Tests/RunCMake/CompileWarningAsError/WerrorOn.cmake13
-rw-r--r--Tests/RunCMake/CompileWarningAsError/err.cxx0
-rw-r--r--Tests/RunCMake/CompileWarningAsError/warn.cxx17
9 files changed, 73 insertions, 0 deletions
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index dbff293..7d521fb 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -365,6 +365,7 @@ add_RunCMake_test(TargetProperties)
add_RunCMake_test(ToolchainFile)
add_RunCMake_test(find_dependency)
add_RunCMake_test(CompileDefinitions)
+add_RunCMake_test(CompileWarningAsError)
add_RunCMake_test(CompileFeatures -DCMake_NO_C_STANDARD=${CMake_NO_C_STANDARD} -DCMake_NO_CXX_STANDARD=${CMake_NO_CXX_STANDARD})
add_RunCMake_test(Policy)
add_RunCMake_test(PolicyScope)
diff --git a/Tests/RunCMake/CompileWarningAsError/CMakeLists.txt b/Tests/RunCMake/CompileWarningAsError/CMakeLists.txt
new file mode 100644
index 0000000..5ff8d3e
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.23)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/CompileWarningAsError/RunCMakeTest.cmake b/Tests/RunCMake/CompileWarningAsError/RunCMakeTest.cmake
new file mode 100644
index 0000000..059c80f
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/RunCMakeTest.cmake
@@ -0,0 +1,12 @@
+include(RunCMake)
+
+function(run_compile_warn test)
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build)
+ set(RunCMake_TEST_OUTPUT_MERGE 1)
+ run_cmake(${test})
+ set(RunCMake_TEST_NO_CLEAN 1)
+ run_cmake_command(${test}-Build ${CMAKE_COMMAND} --build . ${verbose_args})
+endfunction()
+
+run_compile_warn(WerrorOn)
+run_compile_warn(WerrorOff)
diff --git a/Tests/RunCMake/CompileWarningAsError/WarningAsErrorOptions.cmake b/Tests/RunCMake/CompileWarningAsError/WarningAsErrorOptions.cmake
new file mode 100644
index 0000000..ccc6cc5
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/WarningAsErrorOptions.cmake
@@ -0,0 +1,18 @@
+# add compile options to warning_options to ensure unused-function throws a warning
+# if warning_options is NOT DEFINED, assume compiler doesn't support warning as error
+macro(get_warning_options warning_options)
+ if (CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang|XLClang|IBMClang|LCC|NVCC|IntelLLVM)$")
+ set(${warning_options} "-Wall")
+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"
+ OR (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC"))
+ set(${warning_options} "-W4")
+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
+ set(${warning_options} "-w3")
+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "XL")
+ set(${warning_options} "-qinfo=all")
+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
+ set(${warning_options} "+w;+w2")
+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Fujitsu")
+ set(${warning_options} "SHELL:-w 8")
+ endif()
+endmacro()
diff --git a/Tests/RunCMake/CompileWarningAsError/WerrorOff.cmake b/Tests/RunCMake/CompileWarningAsError/WerrorOff.cmake
new file mode 100644
index 0000000..b05d65e
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/WerrorOff.cmake
@@ -0,0 +1,8 @@
+enable_language(CXX)
+
+include(WarningAsErrorOptions.cmake)
+get_warning_options(warning_options)
+
+add_executable(WerrorOff warn.cxx)
+target_compile_options(WerrorOff PUBLIC "${warning_options}")
+set_target_properties(WerrorOff PROPERTIES COMPILE_WARNING_AS_ERROR OFF)
diff --git a/Tests/RunCMake/CompileWarningAsError/WerrorOn-Build-result.txt b/Tests/RunCMake/CompileWarningAsError/WerrorOn-Build-result.txt
new file mode 100644
index 0000000..d197c91
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/WerrorOn-Build-result.txt
@@ -0,0 +1 @@
+[^0]
diff --git a/Tests/RunCMake/CompileWarningAsError/WerrorOn.cmake b/Tests/RunCMake/CompileWarningAsError/WerrorOn.cmake
new file mode 100644
index 0000000..4310333
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/WerrorOn.cmake
@@ -0,0 +1,13 @@
+enable_language(CXX)
+
+include(WarningAsErrorOptions.cmake)
+get_warning_options(warning_options)
+
+if (DEFINED warning_options)
+ add_executable(WerrorOn warn.cxx)
+ target_compile_options(WerrorOn PUBLIC "${warning_options}")
+ set_target_properties(WerrorOn PROPERTIES COMPILE_WARNING_AS_ERROR ON)
+else()
+ # if no werror option is set for the environment, use err.cxx so that build fails as expected
+ add_executable(WerrorOn err.cxx)
+endif()
diff --git a/Tests/RunCMake/CompileWarningAsError/err.cxx b/Tests/RunCMake/CompileWarningAsError/err.cxx
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/err.cxx
diff --git a/Tests/RunCMake/CompileWarningAsError/warn.cxx b/Tests/RunCMake/CompileWarningAsError/warn.cxx
new file mode 100644
index 0000000..64a245a
--- /dev/null
+++ b/Tests/RunCMake/CompileWarningAsError/warn.cxx
@@ -0,0 +1,17 @@
+static void unused_function();
+
+#ifdef __SUNPRO_CC
+struct A
+{
+ virtual ~A() throw();
+};
+struct B : public A
+{
+ virtual ~B() throw(int);
+};
+#endif
+
+int main(int unused_argument, char* [])
+{
+ return 1;
+}