1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmGlobalVisualStudio14Generator.h"
10 #include "cmDocumentationEntry.h"
11 #include "cmGlobalGenerator.h"
12 #include "cmGlobalGeneratorFactory.h"
13 #include "cmGlobalVisualStudioGenerator.h"
14 #include "cmMakefile.h"
15 #include "cmMessageType.h"
16 #include "cmStringAlgorithms.h"
17 #include "cmSystemTools.h"
20 static const char vs14generatorName[] = "Visual Studio 14 2015";
22 // Map generator name without year to name with year.
23 static const char* cmVS14GenName(const std::string& name, std::string& genName)
25 if (strncmp(name.c_str(), vs14generatorName,
26 sizeof(vs14generatorName) - 6) != 0) {
29 const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
30 if (cmHasLiteralPrefix(p, " 2015")) {
33 genName = std::string(vs14generatorName) + p;
37 class cmGlobalVisualStudio14Generator::Factory
38 : public cmGlobalGeneratorFactory
41 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
42 const std::string& name, bool allowArch, cmake* cm) const override
45 const char* p = cmVS14GenName(name, genName);
47 return std::unique_ptr<cmGlobalGenerator>();
50 return std::unique_ptr<cmGlobalGenerator>(
51 new cmGlobalVisualStudio14Generator(cm, genName, ""));
53 if (!allowArch || *p++ != ' ') {
54 return std::unique_ptr<cmGlobalGenerator>();
56 if (strcmp(p, "Win64") == 0) {
57 return std::unique_ptr<cmGlobalGenerator>(
58 new cmGlobalVisualStudio14Generator(cm, genName, "x64"));
60 if (strcmp(p, "ARM") == 0) {
61 return std::unique_ptr<cmGlobalGenerator>(
62 new cmGlobalVisualStudio14Generator(cm, genName, "ARM"));
64 return std::unique_ptr<cmGlobalGenerator>();
67 void GetDocumentation(cmDocumentationEntry& entry) const override
69 entry.Name = std::string(vs14generatorName) + " [arch]";
70 entry.Brief = "Generates Visual Studio 2015 project files. "
71 "Optional [arch] can be \"Win64\" or \"ARM\".";
74 std::vector<std::string> GetGeneratorNames() const override
76 std::vector<std::string> names;
77 names.push_back(vs14generatorName);
81 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
83 std::vector<std::string> names;
84 names.push_back(vs14generatorName + std::string(" ARM"));
85 names.push_back(vs14generatorName + std::string(" Win64"));
89 bool SupportsToolset() const override { return true; }
90 bool SupportsPlatform() const override { return true; }
92 std::vector<std::string> GetKnownPlatforms() const override
94 std::vector<std::string> platforms;
95 platforms.emplace_back("x64");
96 platforms.emplace_back("Win32");
97 platforms.emplace_back("ARM");
101 std::string GetDefaultPlatformName() const override { return "Win32"; }
104 std::unique_ptr<cmGlobalGeneratorFactory>
105 cmGlobalVisualStudio14Generator::NewFactory()
107 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
110 cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
111 cmake* cm, const std::string& name,
112 std::string const& platformInGeneratorName)
113 : cmGlobalVisualStudio12Generator(cm, name, platformInGeneratorName)
115 std::string vc14Express;
116 this->ExpressEdition = cmSystemTools::ReadRegistryValue(
117 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
119 vc14Express, cmSystemTools::KeyWOW64_32);
120 this->DefaultPlatformToolset = "v140";
121 this->DefaultAndroidToolset = "Clang_3_8";
122 this->DefaultCLFlagTableName = "v140";
123 this->DefaultCSharpFlagTableName = "v140";
124 this->DefaultLibFlagTableName = "v14";
125 this->DefaultLinkFlagTableName = "v140";
126 this->DefaultMasmFlagTableName = "v14";
127 this->DefaultRCFlagTableName = "v14";
128 this->Version = VSVersion::VS14;
131 bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
132 const std::string& name) const
135 if (cmVS14GenName(name, genName)) {
136 return genName == this->GetName();
141 bool cmGlobalVisualStudio14Generator::InitializeWindows(cmMakefile* mf)
143 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
144 return this->SelectWindows10SDK(mf, false);
149 bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
151 std::ostringstream e;
152 if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
153 if (this->DefaultPlatformToolset.empty()) {
155 << " supports Windows Store '8.0', '8.1' and "
157 << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
159 e << "A Windows Store component with CMake requires both the Windows "
160 << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
161 << "' SDK. Please make sure that you have both installed";
163 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
166 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
167 return this->SelectWindows10SDK(mf, true);
172 bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
177 bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
180 // Find the default version of the Windows 10 SDK.
181 std::string const version = this->GetWindows10SDKVersion(mf);
183 if (required && version.empty()) {
184 std::ostringstream e;
185 e << "Could not find an appropriate version of the Windows 10 SDK"
186 << " installed on this machine";
187 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
190 this->SetWindowsTargetPlatformVersion(version, mf);
194 void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
195 std::string const& version, cmMakefile* mf)
197 this->WindowsTargetPlatformVersion = version;
198 if (!cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
199 this->SystemVersion)) {
200 std::ostringstream e;
201 e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
202 << " to target Windows " << this->SystemVersion << ".";
203 mf->DisplayStatus(e.str(), -1);
205 mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
206 this->WindowsTargetPlatformVersion);
209 bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
210 std::string& toolset) const
212 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
213 if (this->IsWindowsStoreToolsetInstalled() &&
214 this->IsWindowsDesktopToolsetInstalled()) {
221 return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
225 bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
227 const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
228 "VisualStudio\\14.0\\VC\\Runtimes";
230 std::vector<std::string> vc14;
231 return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
232 cmSystemTools::KeyWOW64_32);
235 bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
237 const char universal10Key[] =
238 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
239 "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
241 std::string win10SDK;
242 return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
243 cmSystemTools::KeyWOW64_32);
246 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
247 cmMakefile* mf) const
249 // if the given value is set, it can either be OFF/FALSE or a valid SDK
251 if (cmValue value = mf->GetDefinition(
252 "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
254 // If the value is some off/false value, then there is NO maximum set.
255 if (cmIsOff(value)) {
256 return std::string();
258 // If the value is something else, trust that it is a valid SDK value.
262 // If value is an invalid pointer, leave result unchanged.
265 return this->GetWindows10SDKMaxVersionDefault(mf);
268 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
271 // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
273 // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
274 // officially only supported for VS 2017." From:
275 // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
276 return "10.0.14393.0";
279 #if defined(_WIN32) && !defined(__CYGWIN__)
282 bool operator()(std::string const& p)
284 return !cmSystemTools::FileExists(p + "/um/windows.h", true);
287 class WindowsSDKTooRecent
289 std::string const& MaxVersion;
292 WindowsSDKTooRecent(std::string const& maxVersion)
293 : MaxVersion(maxVersion)
296 bool operator()(std::string const& v)
298 return cmSystemTools::VersionCompareGreater(v, MaxVersion);
303 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
306 #if defined(_WIN32) && !defined(__CYGWIN__)
307 std::vector<std::string> win10Roots;
310 std::string win10Root;
311 if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
312 cmSystemTools::ConvertToUnixSlashes(win10Root);
313 win10Roots.push_back(win10Root);
318 // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
319 // Try HKLM and then HKCU.
320 std::string win10Root;
321 if (cmSystemTools::ReadRegistryValue(
322 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
323 "Windows Kits\\Installed Roots;KitsRoot10",
324 win10Root, cmSystemTools::KeyWOW64_32) ||
325 cmSystemTools::ReadRegistryValue(
326 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
327 "Windows Kits\\Installed Roots;KitsRoot10",
328 win10Root, cmSystemTools::KeyWOW64_32)) {
329 cmSystemTools::ConvertToUnixSlashes(win10Root);
330 win10Roots.push_back(win10Root);
334 if (win10Roots.empty()) {
335 return std::string();
338 std::vector<std::string> sdks;
339 // Grab the paths of the different SDKs that are installed
340 for (std::string const& i : win10Roots) {
341 std::string path = i + "/Include/*";
342 cmSystemTools::GlobDirs(path, sdks);
345 // Skip SDKs that do not contain <um/windows.h> because that indicates that
346 // only the UCRT MSIs were installed for them.
347 cm::erase_if(sdks, NoWindowsH());
349 // Only use the filename, which will be the SDK version.
350 for (std::string& i : sdks) {
351 i = cmSystemTools::GetFilenameName(i);
354 // Skip SDKs that cannot be used with our toolset, unless the user does not
355 // want to limit the highest supported SDK according to the Microsoft
357 std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
358 if (!maxVersion.empty()) {
359 cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
362 // Sort the results to make sure we select the most recent one.
363 std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
365 // Look for a SDK exactly matching the requested target version.
366 for (std::string const& i : sdks) {
367 if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
373 // Use the latest Windows 10 SDK since the exact version is not available.
377 // Return an empty string
378 return std::string();