Skip to content

Commit f9b7c66

Browse files
committed
VS: Fix mapping of -Qspectre- flag
The mapping for this flag was added by commit 43aa632 (VS: Populate `-Qspectre-` flag table entry for v142, 2019-01-24, v3.14.0-rc1~74^2~7). However, it did not do anything because the special logic added by commit bb60ed6 (VS: Add flag table entry for -Qspectre, 2018-10-08, v3.13.0-rc1~4^2) to move the `SpectreMitigation` element from `ClCompile` to the top level only handled the presence of the setting and not its value. Extend the special logic to carry the value too. Fixes: #19535
1 parent f43a7d7 commit f9b7c66

5 files changed

+50
-5
lines changed

Source/cmVisualStudio10TargetGenerator.cxx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,11 @@ void cmVisualStudio10TargetGenerator::WriteMSToolConfigurationValues(
12291229
if (this->IPOEnabledConfigurations.count(config) > 0) {
12301230
e1.Element("WholeProgramOptimization", "true");
12311231
}
1232-
if (this->SpectreMitigationConfigurations.count(config) > 0) {
1233-
e1.Element("SpectreMitigation", "Spectre");
1232+
{
1233+
auto s = this->SpectreMitigation.find(config);
1234+
if (s != this->SpectreMitigation.end()) {
1235+
e1.Element("SpectreMitigation", s->second);
1236+
}
12341237
}
12351238
}
12361239

@@ -2760,8 +2763,8 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions(
27602763
}
27612764
}
27622765

2763-
if (clOptions.HasFlag("SpectreMitigation")) {
2764-
this->SpectreMitigationConfigurations.insert(configName);
2766+
if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
2767+
this->SpectreMitigation[configName] = s;
27652768
clOptions.RemoveFlag("SpectreMitigation");
27662769
}
27672770

Source/cmVisualStudio10TargetGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class cmVisualStudio10TargetGenerator
215215
unsigned int NsightTegraVersion[4];
216216
bool TargetCompileAsWinRT;
217217
std::set<std::string> IPOEnabledConfigurations;
218-
std::set<std::string> SpectreMitigationConfigurations;
218+
std::map<std::string, std::string> SpectreMitigation;
219219
cmGlobalVisualStudio10Generator* const GlobalGenerator;
220220
cmLocalVisualStudio10Generator* const LocalGenerator;
221221
std::set<std::string> CSharpCustomCommandNames;

Tests/RunCMake/VS10Project/RunCMakeTest.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ run_cmake(VsPackageReferences)
2626
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.05)
2727
run_cmake(VsJustMyCode)
2828
endif()
29+
30+
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.20)
31+
run_cmake(VsSpectreMitigation)
32+
endif()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
macro(VsSpectreMitigation_check tgt spectre_expect)
2+
set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/${tgt}.vcxproj")
3+
if(NOT EXISTS "${vcProjectFile}")
4+
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not exist.")
5+
return()
6+
endif()
7+
8+
set(HAVE_SpectreMitigation 0)
9+
10+
file(STRINGS "${vcProjectFile}" lines)
11+
foreach(line IN LISTS lines)
12+
if(line MATCHES "^ *<SpectreMitigation>([^<>]+)</SpectreMitigation>")
13+
set(spectre_actual "${CMAKE_MATCH_1}")
14+
if(NOT "${spectre_actual}" STREQUAL "${spectre_expect}")
15+
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj has <SpectreMitigation> '${spectre_actual}', not '${spectre_expect}'.")
16+
return()
17+
endif()
18+
set(HAVE_SpectreMitigation 1)
19+
break()
20+
endif()
21+
endforeach()
22+
23+
if(NOT HAVE_SpectreMitigation AND NOT "${spectre_expect}" STREQUAL "")
24+
set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not have a <SpectreMitigation> field.")
25+
return()
26+
endif()
27+
endmacro()
28+
29+
VsSpectreMitigation_check(SpectreMitigationOn-C "Spectre")
30+
VsSpectreMitigation_check(SpectreMitigationOff-C "false")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(CMAKE_CONFIGURATION_TYPES Debug)
2+
enable_language(C)
3+
4+
add_library(SpectreMitigationOn-C empty.c)
5+
target_compile_options(SpectreMitigationOn-C PRIVATE -Qspectre)
6+
7+
add_library(SpectreMitigationOff-C empty.c)
8+
target_compile_options(SpectreMitigationOff-C PRIVATE -Qspectre-)

0 commit comments

Comments
 (0)