Skip to content

Commit

Permalink
build: Add Werror and warning flags to builds
Browse files Browse the repository at this point in the history
Added the Werror, Wall, and Wextra flags to builds in CMake.

Change-Id: I58ff7c7d175b512906141b0e691f42b8eb4bf68f
  • Loading branch information
jeremyk-lunarg committed Jun 14, 2021
1 parent ac93933 commit 0143cbe
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 0143cbe

Please sign in to comment.