smanders / externpro

build external projects with cmake
MIT License
13 stars 12 forks source link

xpSetFlagsMsvc needs to set /W3 #303

Closed smanders closed 3 years ago

smanders commented 3 years ago

cmake >= 3.15 no longer contains warning flags like /W3 by default https://cmake.org/cmake/help/v3.17/release/3.15.html#other-changes

With MSVC-like compilers the value of CMAKE_<LANG>_FLAGS no longer contains warning flags like /W3 by default. See policy CMP0092.

https://cmake.org/cmake/help/v3.17/policy/CMP0092.html

MSVC warning flags are not in CMAKE_<LANG>_FLAGS by default.

When using MSVC-like compilers in CMake 3.14 and below, warning flags like /W3 are added to CMAKE_<LANG>_FLAGS by default. This is problematic for projects that want to choose a different warning level programmatically. In particular, it requires string editing of the CMAKE_<LANG>_FLAGS variables with knowledge of the CMake builtin defaults so they can be replaced.

CMake 3.15 and above prefer to leave out warning flags from the value of CMAKE_<LANG>_FLAGS by default.

I suspect that developers who are seeing the warning treated as an error have not cleaned out their cmake cache in some time and probably have /W3 set from a cmake run with an earlier version of cmake

I will be adding /W3 to externpro's xpSetFlagsMsvc() https://github.com/smanders/externpro/blob/21.02/modules/xpfunmac.cmake#L2007-L2044 -- until projects are using externpro >= 21.03 they might consider adding it temporarily

smanders commented 3 years ago

Background

CMake shouldn't add /W3 in Windows-MSVC.cmake https://gitlab.kitware.com/cmake/cmake/-/issues/18317

It is impossible to reliably use target_compile_options() on MSVC https://gitlab.kitware.com/cmake/cmake/-/issues/19084

How to set compiler options with CMake in Visual Studio 2017 https://stackoverflow.com/questions/45995784/how-to-set-compiler-options-with-cmake-in-visual-studio-2017

How to set warning level in CMake? https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake

Modern way to set compiler flags in cross-platform cmake project https://stackoverflow.com/questions/45955272/modern-way-to-set-compiler-flags-in-cross-platform-cmake-project

smanders commented 3 years ago

completed with commit to dev branch referenced above