blob: b1c880f0ffecffc67cec7413a6848e91d5073fff [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
Greg Claytonff48e4b2015-02-03 02:05:44 +000012#include <mutex> // std::once
13
Zachary Turner97a14e62014-08-19 17:18:29 +000014#include "lldb/Host/windows/HostInfoWindows.h"
15#include "llvm/ADT/SmallString.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000016#include "llvm/Support/FileSystem.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000017#include "llvm/Support/raw_ostream.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000018#include "llvm/Support/Path.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000019
20using namespace lldb_private;
21
Zachary Turnera21fee02014-08-21 21:49:24 +000022FileSpec HostInfoWindows::m_program_filespec;
23
Zachary Turner97a14e62014-08-19 17:18:29 +000024size_t
25HostInfoWindows::GetPageSize()
26{
27 SYSTEM_INFO systemInfo;
28 GetNativeSystemInfo(&systemInfo);
29 return systemInfo.dwPageSize;
30}
31
32bool
33HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
34{
35 OSVERSIONINFOEX info;
36
37 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
38 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
39#pragma warning(push)
40#pragma warning(disable : 4996)
41 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in favor of the
42 // new Windows Version Helper APIs. Since we don't specify a minimum SDK version, it's easier
43 // to simply disable the warning rather than try to support both APIs.
44 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0)
45 {
46 return false;
47 }
48#pragma warning(pop)
49
50 major = info.dwMajorVersion;
51 minor = info.dwMinorVersion;
52 update = info.wServicePackMajor;
53
54 return true;
55}
56
57bool
58HostInfoWindows::GetOSBuildString(std::string &s)
59{
60 s.clear();
61 uint32_t major, minor, update;
62 if (!GetOSVersion(major, minor, update))
63 return false;
64
65 llvm::raw_string_ostream stream(s);
66 stream << "Windows NT " << major << "." << minor << "." << update;
67 return true;
68}
69
70bool
71HostInfoWindows::GetOSKernelDescription(std::string &s)
72{
73 return GetOSBuildString(s);
74}
75
76bool
77HostInfoWindows::GetHostname(std::string &s)
78{
79 char buffer[MAX_COMPUTERNAME_LENGTH + 1];
80 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
81 if (!::GetComputerName(buffer, &dwSize))
82 return false;
83
84 s.assign(buffer, buffer + dwSize);
85 return true;
86}
Zachary Turner42ff0ad2014-08-21 17:29:12 +000087
Zachary Turnera21fee02014-08-21 21:49:24 +000088FileSpec
89HostInfoWindows::GetProgramFileSpec()
90{
Greg Claytonff48e4b2015-02-03 02:05:44 +000091 static std::once_flag g_once_flag;
92 std::call_once(g_once_flag, []() {
93 char buffer[PATH_MAX];
94 ::GetModuleFileName(NULL, buffer, sizeof(buffer));
95 m_program_filespec.SetFile(buffer, false);
Zachary Turner02624e42015-02-03 18:26:00 +000096 });
Zachary Turnera21fee02014-08-21 21:49:24 +000097 return m_program_filespec;
98}
99
Zachary Turner10687b02014-10-20 17:46:43 +0000100FileSpec
101HostInfoWindows::GetDefaultShell()
102{
103 return FileSpec(::getenv("ComSpec"), false);
104}
105
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000106bool
107HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec)
108{
109 FileSpec lldb_file_spec;
110 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
111 return false;
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000112 llvm::SmallString<64> path;
113 lldb_file_spec.GetPath(path);
114 llvm::sys::path::remove_filename(path);
115 llvm::sys::path::append(path, "lib", "site-packages");
116 std::replace(path.begin(), path.end(), '\\', '/');
117 file_spec.GetDirectory().SetString(path.c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000118 return true;
119}