resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmVSSetupHelper.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #ifndef NOMINMAX
6 #  define NOMINMAX // Undefine min and max defined by windows.h
7 #endif
8
9 // Published by Visual Studio Setup team
10 #include <cm3p/Setup.Configuration.h>
11 #include <string>
12 #include <vector>
13
14 #include <windows.h>
15
16 template <class T>
17 class SmartCOMPtr
18 {
19 public:
20   SmartCOMPtr() { ptr = NULL; }
21   SmartCOMPtr(T* p)
22   {
23     ptr = p;
24     if (ptr != NULL)
25       ptr->AddRef();
26   }
27   SmartCOMPtr(const SmartCOMPtr<T>& sptr)
28   {
29     ptr = sptr.ptr;
30     if (ptr != NULL)
31       ptr->AddRef();
32   }
33   T** operator&() { return &ptr; }
34   T* operator->() { return ptr; }
35   T* operator=(T* p)
36   {
37     if (*this != p) {
38       ptr = p;
39       if (ptr != NULL)
40         ptr->AddRef();
41     }
42     return *this;
43   }
44   operator T*() const { return ptr; }
45   template <class I>
46   HRESULT QueryInterface(REFCLSID rclsid, I** pp)
47   {
48     if (pp != NULL) {
49       return ptr->QueryInterface(rclsid, (void**)pp);
50     } else {
51       return E_FAIL;
52     }
53   }
54   HRESULT CoCreateInstance(REFCLSID clsid, IUnknown* pUnknown,
55                            REFIID interfaceId, DWORD dwClsContext = CLSCTX_ALL)
56   {
57     HRESULT hr = ::CoCreateInstance(clsid, pUnknown, dwClsContext, interfaceId,
58                                     (void**)&ptr);
59     return hr;
60   }
61   ~SmartCOMPtr()
62   {
63     if (ptr != NULL)
64       ptr->Release();
65   }
66
67 private:
68   T* ptr;
69 };
70
71 class SmartBSTR
72 {
73 public:
74   SmartBSTR() { str = NULL; }
75   SmartBSTR(const SmartBSTR& src) = delete;
76   SmartBSTR& operator=(const SmartBSTR& src) = delete;
77   operator BSTR() const { return str; }
78   BSTR* operator&() throw() { return &str; }
79   ~SmartBSTR() throw() { ::SysFreeString(str); }
80
81 private:
82   BSTR str;
83 };
84
85 struct VSInstanceInfo
86 {
87   std::string VSInstallLocation;
88   std::string Version;
89   std::string VCToolsetVersion;
90   bool IsWin10SDKInstalled = false;
91   bool IsWin81SDKInstalled = false;
92
93   std::string GetInstallLocation() const;
94 };
95
96 class cmVSSetupAPIHelper
97 {
98 public:
99   cmVSSetupAPIHelper(unsigned int version);
100   ~cmVSSetupAPIHelper();
101
102   bool SetVSInstance(std::string const& vsInstallLocation,
103                      std::string const& vsInstallVersion);
104
105   bool IsVSInstalled();
106   bool GetVSInstanceInfo(std::string& vsInstallLocation);
107   bool GetVSInstanceVersion(std::string& vsInstanceVersion);
108   bool GetVCToolsetVersion(std::string& vsToolsetVersion);
109   bool IsWin10SDKInstalled();
110   bool IsWin81SDKInstalled();
111
112 private:
113   bool Initialize();
114   bool GetVSInstanceInfo(SmartCOMPtr<ISetupInstance2> instance2,
115                          VSInstanceInfo& vsInstanceInfo);
116   bool CheckInstalledComponent(SmartCOMPtr<ISetupPackageReference> package,
117                                bool& bWin10SDK, bool& bWin81SDK);
118   int ChooseVSInstance(const std::vector<VSInstanceInfo>& vecVSInstances);
119   bool EnumerateAndChooseVSInstance();
120   bool LoadSpecifiedVSInstanceFromDisk();
121   bool EnumerateVSInstancesWithVswhere(
122     std::vector<VSInstanceInfo>& VSInstances);
123   bool EnumerateVSInstancesWithCOM(std::vector<VSInstanceInfo>& VSInstances);
124
125   unsigned int Version;
126
127   // COM ptrs to query about VS instances
128   SmartCOMPtr<ISetupConfiguration> setupConfig;
129   SmartCOMPtr<ISetupConfiguration2> setupConfig2;
130   SmartCOMPtr<ISetupHelper> setupHelper;
131   // used to indicate failure in Initialize(), so we don't have to call again
132   bool initializationFailure;
133   // indicated if COM initialization is successful
134   HRESULT comInitialized;
135   // current best instance of VS selected
136   VSInstanceInfo chosenInstanceInfo;
137   bool IsEWDKEnabled();
138
139   std::string SpecifiedVSInstallLocation;
140   std::string SpecifiedVSInstallVersion;
141 };