Skip to content

Support target_compile_warnings() to set warning like -Wall, -Wextra or /Wall for cmake targets

Introduction

The topic has been discussed several times either directly (e.g. cmake-api-for-warnings)[1] or indirectly (e.g. reliably use target_compile_options()[2])

After some surveys. I found that cmake-api-for-warnings[1] seems to reach a conclusion while there has been no implementation since then. And Add options to enable all compiler warnings and to treat compiler warnings as errors[3] followed up the topic and proposed some implementation thoughts. However the discussion did not end up with final conclusion nor working patches.

Proposed Feature and Design

The following only take clang and gcc into discussion. But the idea is meant to generalized to other compilers.

Two target properties COMPILE_WARNING_MODE and COMPILE_WARNING_AS_ERROR are added.

COMPILE_WARNING_MODE is initialized by CMAKE_COMPILE_WARNING_MODE global variable. It takes the following values: DEFAULT (expands to nothing), DISABLED(expands to "-w" for clang/gcc/msvc), HIGH(expands to "-Wall -Wextra" for clang/gcc and "-W4" for msvc).

COMPILE_WARNING_AS_ERROR is initialized by CMAKE_COMPILE_WARNING_AS_ERROR. It is a boolean variable. If it is true, it expands to "-Werror" for clang/gcc and "-WX" for msvc. It expands to nothing otherwise.

These two target properties do not have INTERFACE_* counterparts thus do not propagate to targets that depend on them.

Reference

Edited by Jiang Yi
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information