cmake/Source/cmVSSetupHelper.h

145 lines
3.7 KiB
C
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2017-04-14 19:02:05 +02:00
#ifndef NOMINMAX
2018-08-09 18:06:22 +02:00
# define NOMINMAX // Undefine min and max defined by windows.h
2017-04-14 19:02:05 +02:00
#endif
// Published by Visual Studio Setup team
2020-08-30 11:54:41 +02:00
#include <cm3p/Setup.Configuration.h>
2017-04-14 19:02:05 +02:00
#include <string>
#include <vector>
#include <windows.h>
template <class T>
class SmartCOMPtr
{
public:
2023-05-23 16:38:00 +02:00
SmartCOMPtr() = default;
2017-04-14 19:02:05 +02:00
SmartCOMPtr(T* p)
{
ptr = p;
2023-05-23 16:38:00 +02:00
if (ptr != nullptr) {
2017-04-14 19:02:05 +02:00
ptr->AddRef();
2023-05-23 16:38:00 +02:00
}
2017-04-14 19:02:05 +02:00
}
SmartCOMPtr(const SmartCOMPtr<T>& sptr)
{
ptr = sptr.ptr;
2023-05-23 16:38:00 +02:00
if (ptr != nullptr) {
2017-04-14 19:02:05 +02:00
ptr->AddRef();
2023-05-23 16:38:00 +02:00
}
2017-04-14 19:02:05 +02:00
}
T** operator&() { return &ptr; }
T* operator->() { return ptr; }
T* operator=(T* p)
{
if (*this != p) {
ptr = p;
2023-05-23 16:38:00 +02:00
if (ptr != nullptr) {
2017-04-14 19:02:05 +02:00
ptr->AddRef();
2023-05-23 16:38:00 +02:00
}
2017-04-14 19:02:05 +02:00
}
return *this;
}
operator T*() const { return ptr; }
template <class I>
HRESULT QueryInterface(REFCLSID rclsid, I** pp)
{
2023-05-23 16:38:00 +02:00
if (pp != nullptr) {
2017-04-14 19:02:05 +02:00
return ptr->QueryInterface(rclsid, (void**)pp);
}
2023-05-23 16:38:00 +02:00
return E_FAIL;
2017-04-14 19:02:05 +02:00
}
HRESULT CoCreateInstance(REFCLSID clsid, IUnknown* pUnknown,
REFIID interfaceId, DWORD dwClsContext = CLSCTX_ALL)
{
HRESULT hr = ::CoCreateInstance(clsid, pUnknown, dwClsContext, interfaceId,
(void**)&ptr);
return hr;
}
~SmartCOMPtr()
{
2023-05-23 16:38:00 +02:00
if (ptr != nullptr) {
2017-04-14 19:02:05 +02:00
ptr->Release();
2023-05-23 16:38:00 +02:00
}
2017-04-14 19:02:05 +02:00
}
private:
2023-05-23 16:38:00 +02:00
T* ptr = nullptr;
2017-04-14 19:02:05 +02:00
};
class SmartBSTR
{
public:
2023-05-23 16:38:00 +02:00
SmartBSTR() = default;
2020-02-01 23:06:01 +01:00
SmartBSTR(const SmartBSTR& src) = delete;
SmartBSTR& operator=(const SmartBSTR& src) = delete;
2017-04-14 19:02:05 +02:00
operator BSTR() const { return str; }
BSTR* operator&() throw() { return &str; }
~SmartBSTR() throw() { ::SysFreeString(str); }
2018-08-09 18:06:22 +02:00
2017-04-14 19:02:05 +02:00
private:
2023-05-23 16:38:00 +02:00
BSTR str = nullptr;
2017-04-14 19:02:05 +02:00
};
struct VSInstanceInfo
{
2022-03-29 21:10:50 +02:00
std::string VSInstallLocation;
std::string Version;
2018-08-09 18:06:22 +02:00
std::string VCToolsetVersion;
bool IsWin10SDKInstalled = false;
bool IsWin81SDKInstalled = false;
2017-04-14 19:02:05 +02:00
2018-01-26 17:06:56 +01:00
std::string GetInstallLocation() const;
2017-04-14 19:02:05 +02:00
};
class cmVSSetupAPIHelper
{
public:
2019-11-11 23:01:05 +01:00
cmVSSetupAPIHelper(unsigned int version);
2017-04-14 19:02:05 +02:00
~cmVSSetupAPIHelper();
2022-03-29 21:10:50 +02:00
bool SetVSInstance(std::string const& vsInstallLocation,
std::string const& vsInstallVersion);
2018-04-23 21:13:27 +02:00
2019-11-11 23:01:05 +01:00
bool IsVSInstalled();
2017-04-14 19:02:05 +02:00
bool GetVSInstanceInfo(std::string& vsInstallLocation);
2021-09-14 00:13:48 +02:00
bool GetVSInstanceVersion(std::string& vsInstanceVersion);
2018-08-09 18:06:22 +02:00
bool GetVCToolsetVersion(std::string& vsToolsetVersion);
2017-04-14 19:02:05 +02:00
bool IsWin10SDKInstalled();
bool IsWin81SDKInstalled();
private:
bool Initialize();
bool GetVSInstanceInfo(SmartCOMPtr<ISetupInstance2> instance2,
VSInstanceInfo& vsInstanceInfo);
bool CheckInstalledComponent(SmartCOMPtr<ISetupPackageReference> package,
2018-01-26 17:06:56 +01:00
bool& bWin10SDK, bool& bWin81SDK);
2017-04-14 19:02:05 +02:00
int ChooseVSInstance(const std::vector<VSInstanceInfo>& vecVSInstances);
bool EnumerateAndChooseVSInstance();
2022-03-29 21:10:50 +02:00
bool LoadSpecifiedVSInstanceFromDisk();
2022-08-04 22:12:04 +02:00
bool EnumerateVSInstancesWithVswhere(
std::vector<VSInstanceInfo>& VSInstances);
bool EnumerateVSInstancesWithCOM(std::vector<VSInstanceInfo>& VSInstances);
2017-04-14 19:02:05 +02:00
2019-11-11 23:01:05 +01:00
unsigned int Version;
2017-04-14 19:02:05 +02:00
// COM ptrs to query about VS instances
SmartCOMPtr<ISetupConfiguration> setupConfig;
SmartCOMPtr<ISetupConfiguration2> setupConfig2;
SmartCOMPtr<ISetupHelper> setupHelper;
// used to indicate failure in Initialize(), so we don't have to call again
2023-05-23 16:38:00 +02:00
bool initializationFailure = false;
2017-04-14 19:02:05 +02:00
// indicated if COM initialization is successful
HRESULT comInitialized;
// current best instance of VS selected
VSInstanceInfo chosenInstanceInfo;
2019-11-11 23:01:05 +01:00
bool IsEWDKEnabled();
2018-04-23 21:13:27 +02:00
std::string SpecifiedVSInstallLocation;
2022-03-29 21:10:50 +02:00
std::string SpecifiedVSInstallVersion;
2017-04-14 19:02:05 +02:00
};