blob: 6b2190294081f08693ca02f9f18ccc39edb7d9e5 [file] [log] [blame]
Zachary Turner97a14e62014-08-19 17:18:29 +00001//===-- HostInfoWindows.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/windows/windows.h"
11
Zachary Turner74e08ca2016-03-02 22:05:52 +000012#include <objbase.h>
13
Greg Claytonff48e4b2015-02-03 02:05:44 +000014#include <mutex> // std::once
15
Zachary Turner97a14e62014-08-19 17:18:29 +000016#include "lldb/Host/windows/HostInfoWindows.h"
17#include "llvm/ADT/SmallString.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000018#include "llvm/Support/ConvertUTF.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000019#include "llvm/Support/FileSystem.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000020#include "llvm/Support/Path.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000021#include "llvm/Support/raw_ostream.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000022
23using namespace lldb_private;
24
Zachary Turnera21fee02014-08-21 21:49:24 +000025FileSpec HostInfoWindows::m_program_filespec;
26
Zachary Turner74e08ca2016-03-02 22:05:52 +000027void
28HostInfoWindows::Initialize()
29{
30 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
31 HostInfoBase::Initialize();
32}
33
34void
35HostInfoWindows::Terminate()
36{
37 HostInfoBase::Terminate();
38 ::CoUninitialize();
39}
40
Zachary Turner97a14e62014-08-19 17:18:29 +000041size_t
42HostInfoWindows::GetPageSize()
43{
44 SYSTEM_INFO systemInfo;
45 GetNativeSystemInfo(&systemInfo);
46 return systemInfo.dwPageSize;
47}
48
49bool
50HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
51{
52 OSVERSIONINFOEX info;
53
54 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
55 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
56#pragma warning(push)
57#pragma warning(disable : 4996)
58 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in favor of the
59 // new Windows Version Helper APIs. Since we don't specify a minimum SDK version, it's easier
60 // to simply disable the warning rather than try to support both APIs.
61 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0)
62 {
63 return false;
64 }
65#pragma warning(pop)
66
67 major = info.dwMajorVersion;
68 minor = info.dwMinorVersion;
69 update = info.wServicePackMajor;
70
71 return true;
72}
73
74bool
75HostInfoWindows::GetOSBuildString(std::string &s)
76{
77 s.clear();
78 uint32_t major, minor, update;
79 if (!GetOSVersion(major, minor, update))
80 return false;
81
82 llvm::raw_string_ostream stream(s);
83 stream << "Windows NT " << major << "." << minor << "." << update;
84 return true;
85}
86
87bool
88HostInfoWindows::GetOSKernelDescription(std::string &s)
89{
90 return GetOSBuildString(s);
91}
92
93bool
94HostInfoWindows::GetHostname(std::string &s)
95{
Zachary Turner190fadc2016-03-22 17:58:09 +000096 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
Zachary Turner97a14e62014-08-19 17:18:29 +000097 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
Zachary Turner190fadc2016-03-22 17:58:09 +000098 if (!::GetComputerNameW(buffer, &dwSize))
Zachary Turner97a14e62014-08-19 17:18:29 +000099 return false;
100
Zachary Turner190fadc2016-03-22 17:58:09 +0000101 return llvm::convertWideToUTF8(buffer, s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000102}
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000103
Zachary Turnera21fee02014-08-21 21:49:24 +0000104FileSpec
105HostInfoWindows::GetProgramFileSpec()
106{
Greg Claytonff48e4b2015-02-03 02:05:44 +0000107 static std::once_flag g_once_flag;
Zachary Turner190fadc2016-03-22 17:58:09 +0000108 std::call_once(g_once_flag, []() {
109 std::vector<wchar_t> buffer(PATH_MAX);
110 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
111 std::string path;
112 llvm::convertWideToUTF8(buffer.data(), path);
113 m_program_filespec.SetFile(path, false);
Zachary Turner02624e42015-02-03 18:26:00 +0000114 });
Zachary Turnera21fee02014-08-21 21:49:24 +0000115 return m_program_filespec;
116}
117
Zachary Turner10687b02014-10-20 17:46:43 +0000118FileSpec
119HostInfoWindows::GetDefaultShell()
120{
Zachary Turner190fadc2016-03-22 17:58:09 +0000121 std::string shell;
122 GetEnvironmentVar("ComSpec", shell);
123 return FileSpec(shell, false);
Zachary Turner10687b02014-10-20 17:46:43 +0000124}
125
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000126bool
127HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec)
128{
129 FileSpec lldb_file_spec;
130 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
131 return false;
Zachary Turnerff33eef2015-05-13 19:44:57 +0000132 llvm::SmallString<64> path(lldb_file_spec.GetDirectory().AsCString());
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000133 llvm::sys::path::remove_filename(path);
134 llvm::sys::path::append(path, "lib", "site-packages");
135 std::replace(path.begin(), path.end(), '\\', '/');
136 file_spec.GetDirectory().SetString(path.c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000137 return true;
138}
Zachary Turner190fadc2016-03-22 17:58:09 +0000139
140bool
141HostInfoWindows::GetEnvironmentVar(const std::string &var_name, std::string &var)
142{
143 std::wstring wvar_name;
144 if (!llvm::ConvertUTF8toWide(var_name, wvar_name))
145 return false;
146
147 if (const wchar_t *wvar = _wgetenv(wvar_name.c_str()))
148 return llvm::convertWideToUTF8(wvar, var);
149 return false;
150}