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"
7 #include "cmDocumentationEntry.h"
8 #include "cmLocalVisualStudio10Generator.h"
9 #include "cmMakefile.h"
11 static const char vs14generatorName[] = "Visual Studio 14 2015";
13 // Map generator name without year to name with year.
14 static const char* cmVS14GenName(const std::string& name, std::string& genName)
16 if (strncmp(name.c_str(), vs14generatorName,
17 sizeof(vs14generatorName) - 6) != 0) {
20 const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
21 if (cmHasLiteralPrefix(p, " 2015")) {
24 genName = std::string(vs14generatorName) + p;
28 class cmGlobalVisualStudio14Generator::Factory
29 : public cmGlobalGeneratorFactory
32 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
33 const std::string& name, bool allowArch, cmake* cm) const override
36 const char* p = cmVS14GenName(name, genName);
38 return std::unique_ptr<cmGlobalGenerator>();
41 return std::unique_ptr<cmGlobalGenerator>(
42 new cmGlobalVisualStudio14Generator(cm, genName, ""));
44 if (!allowArch || *p++ != ' ') {
45 return std::unique_ptr<cmGlobalGenerator>();
47 if (strcmp(p, "Win64") == 0) {
48 return std::unique_ptr<cmGlobalGenerator>(
49 new cmGlobalVisualStudio14Generator(cm, genName, "x64"));
51 if (strcmp(p, "ARM") == 0) {
52 return std::unique_ptr<cmGlobalGenerator>(
53 new cmGlobalVisualStudio14Generator(cm, genName, "ARM"));
55 return std::unique_ptr<cmGlobalGenerator>();
58 void GetDocumentation(cmDocumentationEntry& entry) const override
60 entry.Name = std::string(vs14generatorName) + " [arch]";
61 entry.Brief = "Generates Visual Studio 2015 project files. "
62 "Optional [arch] can be \"Win64\" or \"ARM\".";
65 std::vector<std::string> GetGeneratorNames() const override
67 std::vector<std::string> names;
68 names.push_back(vs14generatorName);
72 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
74 std::vector<std::string> names;
75 names.push_back(vs14generatorName + std::string(" ARM"));
76 names.push_back(vs14generatorName + std::string(" Win64"));
80 bool SupportsToolset() const override { return true; }
81 bool SupportsPlatform() const override { return true; }
83 std::vector<std::string> GetKnownPlatforms() const override
85 std::vector<std::string> platforms;
86 platforms.emplace_back("x64");
87 platforms.emplace_back("Win32");
88 platforms.emplace_back("ARM");
92 std::string GetDefaultPlatformName() const override { return "Win32"; }
95 std::unique_ptr<cmGlobalGeneratorFactory>
96 cmGlobalVisualStudio14Generator::NewFactory()
98 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
101 cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
102 cmake* cm, const std::string& name,
103 std::string const& platformInGeneratorName)
104 : cmGlobalVisualStudio12Generator(cm, name, platformInGeneratorName)
106 std::string vc14Express;
107 this->ExpressEdition = cmSystemTools::ReadRegistryValue(
108 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
110 vc14Express, cmSystemTools::KeyWOW64_32);
111 this->DefaultPlatformToolset = "v140";
112 this->DefaultAndroidToolset = "Clang_3_8";
113 this->DefaultCLFlagTableName = "v140";
114 this->DefaultCSharpFlagTableName = "v140";
115 this->DefaultLibFlagTableName = "v14";
116 this->DefaultLinkFlagTableName = "v140";
117 this->DefaultMasmFlagTableName = "v14";
118 this->DefaultRCFlagTableName = "v14";
119 this->Version = VS14;
122 bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
123 const std::string& name) const
126 if (cmVS14GenName(name, genName)) {
127 return genName == this->GetName();
132 bool cmGlobalVisualStudio14Generator::InitializeWindows(cmMakefile* mf)
134 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
135 return this->SelectWindows10SDK(mf, false);
140 bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
142 std::ostringstream e;
143 if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
144 if (this->DefaultPlatformToolset.empty()) {
146 << " supports Windows Store '8.0', '8.1' and "
148 << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
150 e << "A Windows Store component with CMake requires both the Windows "
151 << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
152 << "' SDK. Please make sure that you have both installed";
154 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
157 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
158 return this->SelectWindows10SDK(mf, true);
163 bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
168 bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
171 // Find the default version of the Windows 10 SDK.
172 std::string const version = this->GetWindows10SDKVersion(mf);
174 if (required && version.empty()) {
175 std::ostringstream e;
176 e << "Could not find an appropriate version of the Windows 10 SDK"
177 << " installed on this machine";
178 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
181 this->SetWindowsTargetPlatformVersion(version, mf);
185 void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
186 std::string const& version, cmMakefile* mf)
188 this->WindowsTargetPlatformVersion = version;
189 if (!cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
190 this->SystemVersion)) {
191 std::ostringstream e;
192 e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
193 << " to target Windows " << this->SystemVersion << ".";
194 mf->DisplayStatus(e.str(), -1);
196 mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
197 this->WindowsTargetPlatformVersion);
200 bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
201 std::string& toolset) const
203 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
204 if (this->IsWindowsStoreToolsetInstalled() &&
205 this->IsWindowsDesktopToolsetInstalled()) {
212 return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
216 bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
218 const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
219 "VisualStudio\\14.0\\VC\\Runtimes";
221 std::vector<std::string> vc14;
222 return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
223 cmSystemTools::KeyWOW64_32);
226 bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
228 const char universal10Key[] =
229 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
230 "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
232 std::string win10SDK;
233 return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
234 cmSystemTools::KeyWOW64_32);
237 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
238 cmMakefile* mf) const
240 // if the given value is set, it can either be OFF/FALSE or a valid SDK
242 if (cmProp value = mf->GetDefinition(
243 "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
245 // If the value is some off/false value, then there is NO maximum set.
246 if (cmIsOff(value)) {
247 return std::string();
249 // If the value is something else, trust that it is a valid SDK value.
253 // If value is an invalid pointer, leave result unchanged.
256 // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
258 // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
259 // officially only supported for VS 2017." From:
260 // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
261 return "10.0.14393.0";
264 #if defined(_WIN32) && !defined(__CYGWIN__)
267 bool operator()(std::string const& p)
269 return !cmSystemTools::FileExists(p + "/um/windows.h", true);
272 class WindowsSDKTooRecent
274 std::string const& MaxVersion;
277 WindowsSDKTooRecent(std::string const& maxVersion)
278 : MaxVersion(maxVersion)
281 bool operator()(std::string const& v)
283 return cmSystemTools::VersionCompareGreater(v, MaxVersion);
288 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
291 #if defined(_WIN32) && !defined(__CYGWIN__)
292 std::vector<std::string> win10Roots;
295 std::string win10Root;
296 if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
297 cmSystemTools::ConvertToUnixSlashes(win10Root);
298 win10Roots.push_back(win10Root);
303 // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
304 // Try HKLM and then HKCU.
305 std::string win10Root;
306 if (cmSystemTools::ReadRegistryValue(
307 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
308 "Windows Kits\\Installed Roots;KitsRoot10",
309 win10Root, cmSystemTools::KeyWOW64_32) ||
310 cmSystemTools::ReadRegistryValue(
311 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
312 "Windows Kits\\Installed Roots;KitsRoot10",
313 win10Root, cmSystemTools::KeyWOW64_32)) {
314 cmSystemTools::ConvertToUnixSlashes(win10Root);
315 win10Roots.push_back(win10Root);
319 if (win10Roots.empty()) {
320 return std::string();
323 std::vector<std::string> sdks;
324 // Grab the paths of the different SDKs that are installed
325 for (std::string const& i : win10Roots) {
326 std::string path = i + "/Include/*";
327 cmSystemTools::GlobDirs(path, sdks);
330 // Skip SDKs that do not contain <um/windows.h> because that indicates that
331 // only the UCRT MSIs were installed for them.
332 cm::erase_if(sdks, NoWindowsH());
334 // Only use the filename, which will be the SDK version.
335 for (std::string& i : sdks) {
336 i = cmSystemTools::GetFilenameName(i);
339 // Skip SDKs that cannot be used with our toolset, unless the user does not
340 // want to limit the highest supported SDK according to the Microsoft
342 std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
343 if (!maxVersion.empty()) {
344 cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
347 // Sort the results to make sure we select the most recent one.
348 std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
350 // Look for a SDK exactly matching the requested target version.
351 for (std::string const& i : sdks) {
352 if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
358 // Use the latest Windows 10 SDK since the exact version is not available.
362 // Return an empty string
363 return std::string();