Some source specific compiler flags prevent using ninja as a toolchain for MSVC builds when using them to override project wide flags
Summary: It's impossible with Ninja to override /Gz with /Gd for single files if /Gz is set for the target. Visual Studio toolchains handle this fine.
Result: Command line error D8016: '/Gz' and '/Gd' command-line options are incompatible
When using Visual Studio 2019 I am running into the problem of it (apparently) using Ninja as the default toolchain. Normally that would be fine. However in some cases when compiler flags are set project wide with target_compile_options and then I want them overwritten for a few specific files it will fail to compile. This works fine (even without warnings) when using a Visual Studio toolchain. Tested with Visual Studio 16 2019 and Visual Studio 16 2019 Win64.
This is extra annoying because of the fact that it works fine when running CMake from the VS Native Tools command line, (seems to default to a different generator), but not when run from within Visual studio itself. It can be "solved" by changing the CMakeSettings.json file to specify the different toolchains, however that is slightly hidden as a advanced setting, and would require users to be aware of this.
There are a few issues I could find with related problems, but in those cases it only caused warnings, and I have "solved" such a problem myself previously, by having CMake find and replace the optimization flags in each of the CMAKE_CXX_FLAGS_RELEASE_INIT etc... Perhaps there was a cleaner solution in that case, but even then it doesn't solve this case, as here I want the default project/target wide setting, then specific overridden files.
My suggested solution would be to instead of trying to fix each problem individually, to instead add a function that allows the "stripping" of compiler flags from a target/file/something in the same way they can be added.
If it's wanted to only fix this specific issue, then however the Visual studio toolchain handles it should be a possible solution. However that risks other similar issues appearing in the future again.
A couple examples of similar issues I could find:
And code that replicates this issue:
cmake_minimum_required(VERSION 3.26 FATAL_ERROR)
project (hello_cmake)
add_executable(hello_cmake main.cpp Secondary.cpp)
target_compile_options(hello_cmake PRIVATE /Gz)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/Secondary.cpp PROPERTIES COMPILE_OPTIONS /Gd)
Output from failed build:
------ Build All started: Project: NinjaFails, Configuration: x64-Debug ------ [1/2] C:\PROGRA
2\MICROS2\2019\PROFES1\VC\Tools\MSVC\14291.301\bin\Hostx64\x64\cl.exe /TP /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /Gz /Gd /showIncludes /FoCMakeFiles\hello_cmake.dir\Secondary.cpp.obj /FdCMakeFiles\hello_cmake.dir\ /FS -c ......\Secondary.cpp FAILED: CMakeFiles/hello_cmake.dir/Secondary.cpp.obj C:\PROGRA2\MICROS2\2019\PROFES1\VC\Tools\MSVC\14291.301\bin\Hostx64\x64\cl.exe /TP /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /Gz /Gd /showIncludes /FoCMakeFiles\hello_cmake.dir\Secondary.cpp.obj /FdCMakeFiles\hello_cmake.dir\ /FS -c ......\Secondary.cpp Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30151 for x64 Copyright (C) Microsoft Corporation. All rights reserved.C:\Users\username\Desktop\NinjaFails\out\build\x64-Debug\cl : Command line error D8016: '/Gz' and '/Gd' command-line options are incompatible ninja: build stopped: subcommand failed.
Build All failed.