Discussion:
[CMake] [MSVC] Setting warning level on target feels like long-time bug
Mateusz Loskot
2018-12-08 21:07:15 UTC
Permalink
Hi,

I define a target for a library, and set warning level for MSVC compiler:

target_compile_options(mylib PRIVATE $<$<CXX_COMPILER_ID:MSVC>:-W4>)

Although this works well, the compiler throws this warning:

cl : Command line warning D9025: overriding '/W3' with '/W4'

I want to get rid of this warning, so I fix it this way

if(MSVC)
string(REGEX REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REGEX REPLACE "-W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif()

target_compile_options(mylib PRIVATE $<$<CXX_COMPILER_ID:MSVC>:-W4>)

This works like charm, but leaves me with sour feeling that
something is not right here.The REGEX REPLACE clean-up
has become such a habit, it's almost canonical thing I do in
all my CMake-based projects.

Shouldn't CMake drop the default when target_compile_options is called?
Is this behaviour by design, for MSVC?
Could anyone help me to understand if this is actually a bug
or am I misunderstanding anything?

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Rob Boehne
2018-12-10 16:33:09 UTC
Permalink
I’m going to chime in here and say that IMO, it really isn’t the build system generator’s responsibility to set a warning level.
CMake maintainers should consider these questions when deciding on what to do with this issue:

*) Does CMake set the compiler’s warning flags on all platforms?

*) How does setting the warning flag impact users? (as in this thread, and elsewhere)

This is (to me anyway) not a question as to whether /w3 should be removed from the defaults, but whether the compiler’s default warning level should be silently overridden by using CMake.
The advantages to keeping it, are (IMHO) 1) Users who rely on this won’t be surprised if they care about, but don’t specify warning level for MSVC – and they use a version or configuration where /w3 is not the default. I can’t think of a second advantage of keeping this.

I can think of quite a few reasons why keeping this behavior would be bad – suppose the compiler implementation changes – now CMake is responsible for keeping the same warnings on

From: CMake <cmake-***@cmake.org> on behalf of Ray Donnelly <***@gmail.com>
Date: Sunday, December 9, 2018 at 9:03 AM
To: Mateusz Loskot <***@loskot.net>
Cc: CMake <cmake-***@cmake.org>, "***@cmake.org" <***@cmake.org>
Subject: Re: [CMake] [cmake-developers] [MSVC] Setting warning level on target feels like long-time bug

Cmake is already full of do much hardcoded logic / flags and does new releases so frequently (maybe there's some correlation between these two) that adding this would hardly impact upon its quality or maintainability.

So to that end, hardcoding the default per msvc version so that it's not added unnecessarily, triggering this warning would seem appropriate to me. Either that or disabling this warning with more hard coded flags would also be appropriate.

The current status in not ideal.
I think the discussion is shifting from the initial problem which was unwanted warning « Command line warning D9025: overriding '/W3' with '/W4' ».
I disagree with your opinion.
Fixing just the warning would be a symptomatic treatment.
Changing defaults is not a good idea from my point of view because relying on defaults can be problematic if Microsoft decide to change the default behaviour.
Although I think it is a long shot at something that is (highly)
unlikely to change, YAGNI consideration,
I understand CMake developers may be reluctant to change the long-time defaults.
The real question is how to manage cleanly target specific flags overriding global or directory defaults?
From end-user point of view (I have not checekd CMake implementation),
I'd either do not explicitly set -W defaults or strip any -W option
prior re-setting
with (possibly) new value passed to target_compile_options.

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
--
Powered by www.kitware.com<http://www.kitware.com>

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake-developers
Continue reading on narkive:
Loading...