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