VS: Fix C language standard in target with C++ sources

Add C-language standard to target-wide C++ settings.

Fixes: #21195
This commit is contained in:
Brad King
2020-09-28 16:26:12 -04:00
parent 39677de5e2
commit b325484928
4 changed files with 49 additions and 0 deletions

View File

@@ -2868,6 +2868,23 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions(
this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
}
// Add C-specific flags expressible in a ClCompile meant for C++.
if (langForClCompile == "CXX") {
std::set<std::string> languages;
this->GeneratorTarget->GetLanguages(languages, configName);
if (languages.count("C")) {
std::string flagsC;
this->LocalGenerator->AddCompileOptions(flagsC, this->GeneratorTarget,
"C", configName);
Options optC(this->LocalGenerator, Options::Compiler,
gg->GetClFlagTable());
optC.Parse(flagsC);
if (const char* stdC = optC.GetFlag("LanguageStandard_C")) {
clOptions.AddFlag("LanguageStandard_C", stdC);
}
}
}
// Add a definition for the configuration name.
std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
clOptions.AddDefine(configDefine);

View File

@@ -0,0 +1,23 @@
set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/foo.vcxproj")
if(NOT EXISTS "${vcProjectFile}")
set(RunCMake_TEST_FAILED "Project file ${vcProjectFile} does not exist.")
return()
endif()
set(found_LanguageStandard_stdcpp17 0)
set(found_LanguageStandard_C_stdc11 0)
file(STRINGS "${vcProjectFile}" lines)
foreach(line IN LISTS lines)
if(line MATCHES "<LanguageStandard>stdcpp17</LanguageStandard>")
set(found_LanguageStandard_stdcpp17 1)
endif()
if(line MATCHES "<LanguageStandard_C>stdc11</LanguageStandard_C>")
set(found_LanguageStandard_C_stdc11 1)
endif()
endforeach()
if(NOT found_LanguageStandard_stdcpp17)
string(APPEND RunCMake_TEST_FAILED "LanguageStandard stdcpp17 not found in\n ${vcProjectFile}\n")
endif()
if(NOT found_LanguageStandard_C_stdc11)
string(APPEND RunCMake_TEST_FAILED "LanguageStandard_C stdc11 not found in\n ${vcProjectFile}\n")
endif()

View File

@@ -0,0 +1,5 @@
enable_language(C)
enable_language(CXX)
add_library(foo empty.c empty.cxx)
target_compile_features(foo PRIVATE c_std_11 cxx_std_17)

View File

@@ -3,6 +3,10 @@ cmake_policy(SET CMP0057 NEW)
include(RunCMake)
cmake_policy(SET CMP0054 NEW)
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.27)
run_cmake(LanguageStandard)
endif()
run_cmake(VsCsharpSourceGroup)
run_cmake(VsCSharpCompilerOpts)
run_cmake(ExplicitCMakeLists)