blob: a965ec0ea4e28f6873ab60d745039a8d7a2d73e6 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000027void HostInfoWindows::Initialize() {
28 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
29 HostInfoBase::Initialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000030}
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032void HostInfoWindows::Terminate() {
33 HostInfoBase::Terminate();
34 ::CoUninitialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000035}
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037size_t HostInfoWindows::GetPageSize() {
38 SYSTEM_INFO systemInfo;
39 GetNativeSystemInfo(&systemInfo);
40 return systemInfo.dwPageSize;
Zachary Turner97a14e62014-08-19 17:18:29 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043bool HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor,
44 uint32_t &update) {
45 OSVERSIONINFOEX info;
Zachary Turner97a14e62014-08-19 17:18:29 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
48 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
Zachary Turner97a14e62014-08-19 17:18:29 +000049#pragma warning(push)
50#pragma warning(disable : 4996)
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in
52 // favor of the
53 // new Windows Version Helper APIs. Since we don't specify a minimum SDK
54 // version, it's easier
55 // to simply disable the warning rather than try to support both APIs.
56 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0) {
57 return false;
58 }
Zachary Turner97a14e62014-08-19 17:18:29 +000059#pragma warning(pop)
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 major = info.dwMajorVersion;
62 minor = info.dwMinorVersion;
63 update = info.wServicePackMajor;
Zachary Turner97a14e62014-08-19 17:18:29 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 return true;
Zachary Turner97a14e62014-08-19 17:18:29 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068bool HostInfoWindows::GetOSBuildString(std::string &s) {
69 s.clear();
70 uint32_t major, minor, update;
71 if (!GetOSVersion(major, minor, update))
Zachary Turner190fadc2016-03-22 17:58:09 +000072 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000073
74 llvm::raw_string_ostream stream(s);
75 stream << "Windows NT " << major << "." << minor << "." << update;
76 return true;
77}
78
79bool HostInfoWindows::GetOSKernelDescription(std::string &s) {
80 return GetOSBuildString(s);
81}
82
83bool HostInfoWindows::GetHostname(std::string &s) {
84 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
85 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
86 if (!::GetComputerNameW(buffer, &dwSize))
87 return false;
88
89 return llvm::convertWideToUTF8(buffer, s);
90}
91
92FileSpec HostInfoWindows::GetProgramFileSpec() {
93 static std::once_flag g_once_flag;
94 std::call_once(g_once_flag, []() {
95 std::vector<wchar_t> buffer(PATH_MAX);
96 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
97 std::string path;
98 llvm::convertWideToUTF8(buffer.data(), path);
99 m_program_filespec.SetFile(path, false);
100 });
101 return m_program_filespec;
102}
103
104FileSpec HostInfoWindows::GetDefaultShell() {
105 std::string shell;
106 GetEnvironmentVar("ComSpec", shell);
107 return FileSpec(shell, false);
108}
109
110bool HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec) {
111 FileSpec lldb_file_spec;
112 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
113 return false;
114 llvm::SmallString<64> path(lldb_file_spec.GetDirectory().AsCString());
115 llvm::sys::path::remove_filename(path);
116 llvm::sys::path::append(path, "lib", "site-packages");
117 std::replace(path.begin(), path.end(), '\\', '/');
118 file_spec.GetDirectory().SetString(path.c_str());
119 return true;
120}
121
122bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name,
123 std::string &var) {
124 std::wstring wvar_name;
125 if (!llvm::ConvertUTF8toWide(var_name, wvar_name))
126 return false;
127
128 if (const wchar_t *wvar = _wgetenv(wvar_name.c_str()))
129 return llvm::convertWideToUTF8(wvar, var);
130 return false;
Zachary Turner190fadc2016-03-22 17:58:09 +0000131}