e17c6d766317b155d72227ec0eccc4c75b7e35b4
[platform/upstream/cmake.git] / Source / cmGlobalVisualStudio14Generator.cxx
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"
4
5 #include <cm/vector>
6
7 #include "cmDocumentationEntry.h"
8 #include "cmLocalVisualStudio10Generator.h"
9 #include "cmMakefile.h"
10
11 static const char vs14generatorName[] = "Visual Studio 14 2015";
12
13 // Map generator name without year to name with year.
14 static const char* cmVS14GenName(const std::string& name, std::string& genName)
15 {
16   if (strncmp(name.c_str(), vs14generatorName,
17               sizeof(vs14generatorName) - 6) != 0) {
18     return 0;
19   }
20   const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
21   if (cmHasLiteralPrefix(p, " 2015")) {
22     p += 5;
23   }
24   genName = std::string(vs14generatorName) + p;
25   return p;
26 }
27
28 class cmGlobalVisualStudio14Generator::Factory
29   : public cmGlobalGeneratorFactory
30 {
31 public:
32   std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
33     const std::string& name, bool allowArch, cmake* cm) const override
34   {
35     std::string genName;
36     const char* p = cmVS14GenName(name, genName);
37     if (!p) {
38       return std::unique_ptr<cmGlobalGenerator>();
39     }
40     if (!*p) {
41       return std::unique_ptr<cmGlobalGenerator>(
42         new cmGlobalVisualStudio14Generator(cm, genName, ""));
43     }
44     if (!allowArch || *p++ != ' ') {
45       return std::unique_ptr<cmGlobalGenerator>();
46     }
47     if (strcmp(p, "Win64") == 0) {
48       return std::unique_ptr<cmGlobalGenerator>(
49         new cmGlobalVisualStudio14Generator(cm, genName, "x64"));
50     }
51     if (strcmp(p, "ARM") == 0) {
52       return std::unique_ptr<cmGlobalGenerator>(
53         new cmGlobalVisualStudio14Generator(cm, genName, "ARM"));
54     }
55     return std::unique_ptr<cmGlobalGenerator>();
56   }
57
58   void GetDocumentation(cmDocumentationEntry& entry) const override
59   {
60     entry.Name = std::string(vs14generatorName) + " [arch]";
61     entry.Brief = "Generates Visual Studio 2015 project files.  "
62                   "Optional [arch] can be \"Win64\" or \"ARM\".";
63   }
64
65   std::vector<std::string> GetGeneratorNames() const override
66   {
67     std::vector<std::string> names;
68     names.push_back(vs14generatorName);
69     return names;
70   }
71
72   std::vector<std::string> GetGeneratorNamesWithPlatform() const override
73   {
74     std::vector<std::string> names;
75     names.push_back(vs14generatorName + std::string(" ARM"));
76     names.push_back(vs14generatorName + std::string(" Win64"));
77     return names;
78   }
79
80   bool SupportsToolset() const override { return true; }
81   bool SupportsPlatform() const override { return true; }
82
83   std::vector<std::string> GetKnownPlatforms() const override
84   {
85     std::vector<std::string> platforms;
86     platforms.emplace_back("x64");
87     platforms.emplace_back("Win32");
88     platforms.emplace_back("ARM");
89     return platforms;
90   }
91
92   std::string GetDefaultPlatformName() const override { return "Win32"; }
93 };
94
95 std::unique_ptr<cmGlobalGeneratorFactory>
96 cmGlobalVisualStudio14Generator::NewFactory()
97 {
98   return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
99 }
100
101 cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
102   cmake* cm, const std::string& name,
103   std::string const& platformInGeneratorName)
104   : cmGlobalVisualStudio12Generator(cm, name, platformInGeneratorName)
105 {
106   std::string vc14Express;
107   this->ExpressEdition = cmSystemTools::ReadRegistryValue(
108     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
109     "ProductDir",
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;
120 }
121
122 bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
123   const std::string& name) const
124 {
125   std::string genName;
126   if (cmVS14GenName(name, genName)) {
127     return genName == this->GetName();
128   }
129   return false;
130 }
131
132 bool cmGlobalVisualStudio14Generator::InitializeWindows(cmMakefile* mf)
133 {
134   if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
135     return this->SelectWindows10SDK(mf, false);
136   }
137   return true;
138 }
139
140 bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
141 {
142   std::ostringstream e;
143   if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
144     if (this->DefaultPlatformToolset.empty()) {
145       e << this->GetName()
146         << " supports Windows Store '8.0', '8.1' and "
147            "'10.0', but not '"
148         << this->SystemVersion << "'.  Check CMAKE_SYSTEM_VERSION.";
149     } else {
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";
153     }
154     mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
155     return false;
156   }
157   if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
158     return this->SelectWindows10SDK(mf, true);
159   }
160   return true;
161 }
162
163 bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
164 {
165   return true;
166 }
167
168 bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
169                                                          bool required)
170 {
171   // Find the default version of the Windows 10 SDK.
172   std::string const version = this->GetWindows10SDKVersion(mf);
173
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());
179     return false;
180   }
181   this->SetWindowsTargetPlatformVersion(version, mf);
182   return true;
183 }
184
185 void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
186   std::string const& version, cmMakefile* mf)
187 {
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);
195   }
196   mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
197                     this->WindowsTargetPlatformVersion);
198 }
199
200 bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
201   std::string& toolset) const
202 {
203   if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
204     if (this->IsWindowsStoreToolsetInstalled() &&
205         this->IsWindowsDesktopToolsetInstalled()) {
206       toolset = "v140";
207       return true;
208     } else {
209       return false;
210     }
211   }
212   return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
213     toolset);
214 }
215
216 bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
217 {
218   const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
219                               "VisualStudio\\14.0\\VC\\Runtimes";
220
221   std::vector<std::string> vc14;
222   return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
223                                            cmSystemTools::KeyWOW64_32);
224 }
225
226 bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
227 {
228   const char universal10Key[] =
229     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
230     "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
231
232   std::string win10SDK;
233   return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
234                                           cmSystemTools::KeyWOW64_32);
235 }
236
237 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
238   cmMakefile* mf) const
239 {
240   // if the given value is set, it can either be OFF/FALSE or a valid SDK
241   // string
242   if (cmProp value = mf->GetDefinition(
243         "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
244
245     // If the value is some off/false value, then there is NO maximum set.
246     if (cmIsOff(value)) {
247       return std::string();
248     }
249     // If the value is something else, trust that it is a valid SDK value.
250     else if (value) {
251       return *value;
252     }
253     // If value is an invalid pointer, leave result unchanged.
254   }
255
256   // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
257   //
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";
262 }
263
264 #if defined(_WIN32) && !defined(__CYGWIN__)
265 struct NoWindowsH
266 {
267   bool operator()(std::string const& p)
268   {
269     return !cmSystemTools::FileExists(p + "/um/windows.h", true);
270   }
271 };
272 class WindowsSDKTooRecent
273 {
274   std::string const& MaxVersion;
275
276 public:
277   WindowsSDKTooRecent(std::string const& maxVersion)
278     : MaxVersion(maxVersion)
279   {
280   }
281   bool operator()(std::string const& v)
282   {
283     return cmSystemTools::VersionCompareGreater(v, MaxVersion);
284   }
285 };
286 #endif
287
288 std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
289   cmMakefile* mf)
290 {
291 #if defined(_WIN32) && !defined(__CYGWIN__)
292   std::vector<std::string> win10Roots;
293
294   {
295     std::string win10Root;
296     if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
297       cmSystemTools::ConvertToUnixSlashes(win10Root);
298       win10Roots.push_back(win10Root);
299     }
300   }
301
302   {
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);
316     }
317   }
318
319   if (win10Roots.empty()) {
320     return std::string();
321   }
322
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);
328   }
329
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());
333
334   // Only use the filename, which will be the SDK version.
335   for (std::string& i : sdks) {
336     i = cmSystemTools::GetFilenameName(i);
337   }
338
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
341   // documentation.
342   std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
343   if (!maxVersion.empty()) {
344     cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
345   }
346
347   // Sort the results to make sure we select the most recent one.
348   std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
349
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)) {
353       return i;
354     }
355   }
356
357   if (!sdks.empty()) {
358     // Use the latest Windows 10 SDK since the exact version is not available.
359     return sdks.at(0);
360   }
361 #endif
362   // Return an empty string
363   return std::string();
364 }