build: Add Werror and warning flags to builds
authorJeremy Kniager <jeremyk@lunarg.com>
Thu, 10 Jun 2021 21:00:11 +0000 (15:00 -0600)
committerjeremyk-lunarg <jeremyk@lunarg.com>
Mon, 14 Jun 2021 16:21:12 +0000 (10:21 -0600)
Added the Werror, Wall, and Wextra flags to builds in CMake.

Change-Id: I58ff7c7d175b512906141b0e691f42b8eb4bf68f

CMakeLists.txt

index fea4515..90523ae 100644 (file)
@@ -125,10 +125,22 @@ if(APPLE)
     set(CMAKE_MACOSX_RPATH 1)
 endif()
 
+option(BUILD_WERROR "Treat compiler warnings as errors" ON)
 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
     set(COMMON_COMPILE_FLAGS "-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
     set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -fno-strict-aliasing -fno-builtin-memcmp")
 
+    if(MAKE_C_COMPILER_ID MATCHES "Clang")
+        set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -Wno-sign-conversion -Wno-shorten-64-to-32 -Wno-string-conversion -Wno-implicit-in-conversion -Wno-enum-enum-conversion")
+    endif()
+
+    if(BUILD_WERROR)
+        if((CMAKE_COMPILER_IS_GNUCXX AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.3.0)) OR
+          (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0.0)))
+          set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -Werror")
+        endif()
+    endif()
+
     # For GCC version 7.1 or greater, we need to disable the implicit fallthrough warning since there's no consistent way to satisfy
     # all compilers until they all accept the C++17 standard
     if(CMAKE_COMPILER_IS_GNUCC AND NOT (CMAKE_CXX_COMPILER_VERSION LESS 7.1))
@@ -145,6 +157,19 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
     endif()
+elseif(MSVC)
+    if(BUILD_WERROR)
+        add_compile_options("/WX")
+    endif()
+    # Warn about nested declarations
+    add_compile_options("/w34456")
+    # Warn about potentially uninitialized variables
+    add_compile_options("/w34701")
+    add_compile_options("/w34703")
+    # Warn about different indirection types.
+    add_compile_options("/w34057")
+    # Warn about signed/unsigned mismatch.
+    add_compile_options("/w34245")
 endif()
 
 # Optional codegen target