VS: Suppress MSBuild default flags not specified by project or user

`Microsoft.Cl.Common.props` adds some `cl` flags by default, but for
CMake they may not match what's produced by command-line generators.
If the `-Zc:wchar_t`, `-Zc:forScope`, and/or `-Zc:inline` flags are
not specified by the project or user, suppress them.

Fixes: #26964
This commit is contained in:
Brad King
2025-06-02 12:06:15 -04:00
parent e3b7a00af1
commit 7db3dbddb2
4 changed files with 53 additions and 0 deletions

View File

@@ -3632,18 +3632,27 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions(
if (!clOptions.HasFlag("BasicRuntimeChecks")) {
clOptions.AddFlag("BasicRuntimeChecks", "Default");
}
if (!clOptions.HasFlag("ForceConformanceInForLoopScope")) {
clOptions.AddFlag("ForceConformanceInForLoopScope", "");
}
if (!clOptions.HasFlag("MinimalRebuild")) {
clOptions.AddFlag("MinimalRebuild", "");
}
if (!clOptions.HasFlag("Optimization")) {
clOptions.AddFlag("Optimization", "");
}
if (!clOptions.HasFlag("RemoveUnreferencedCodeData")) {
clOptions.AddFlag("RemoveUnreferencedCodeData", "");
}
if (!clOptions.HasFlag("RuntimeLibrary")) {
clOptions.AddFlag("RuntimeLibrary", "");
}
if (!clOptions.HasFlag("SupportJustMyCode")) {
clOptions.AddFlag("SupportJustMyCode", "");
}
if (!clOptions.HasFlag("TreatWChar_tAsBuiltInType")) {
clOptions.AddFlag("TreatWChar_tAsBuiltInType", "");
}
}
this->ClOptions[configName] = std::move(pOptions);

View File

@@ -34,6 +34,7 @@ run_cmake(VsCSharpReferenceProps)
run_cmake(VsCSharpWithoutSources)
run_cmake(VsCSharpDeployFiles)
run_cmake(VSCSharpDefines)
run_cmake(VsDefaultFlags)
run_cmake(VsSdkDirectories)
run_cmake(VsGlobals)
run_cmake(VsProjectImport)

View File

@@ -0,0 +1,41 @@
macro(VsDefaultFlags_check tgt)
set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/${tgt}.vcxproj")
if(NOT EXISTS "${vcProjectFile}")
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not exist.")
return()
endif()
set(HAVE_ForceConformanceInForLoopScope 0)
set(HAVE_RemoveUnreferencedCodeData 0)
set(HAVE_TreatWChar_tAsBuiltInType 0)
file(STRINGS "${vcProjectFile}" lines)
foreach(line IN LISTS lines)
if(line MATCHES "^ *<ForceConformanceInForLoopScope></ForceConformanceInForLoopScope>")
set(HAVE_ForceConformanceInForLoopScope 1)
endif()
if(line MATCHES "^ *<RemoveUnreferencedCodeData></RemoveUnreferencedCodeData>")
set(HAVE_RemoveUnreferencedCodeData 1)
endif()
if(line MATCHES "^ *<TreatWChar_tAsBuiltInType></TreatWChar_tAsBuiltInType>")
set(HAVE_TreatWChar_tAsBuiltInType 1)
endif()
endforeach()
if(NOT HAVE_ForceConformanceInForLoopScope)
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not have a <ForceConformanceInForLoopScope> property.")
return()
endif()
if(NOT HAVE_RemoveUnreferencedCodeData)
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not have a <RemoveUnreferencedCodeData> property.")
return()
endif()
if(NOT HAVE_TreatWChar_tAsBuiltInType)
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not have a <TreatWChar_tAsBuiltInType> property.")
return()
endif()
endmacro()
VsDefaultFlags_check(empty)

View File

@@ -0,0 +1,2 @@
enable_language(C)
add_library(empty empty.c)