2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_System.h"
|
|
|
|
|
2017-01-06 04:47:08 +08:00
|
|
|
namespace vcpkg::System
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
fs::path get_exe_path_of_current_process()
|
|
|
|
{
|
|
|
|
wchar_t buf[_MAX_PATH ];
|
|
|
|
int bytes = GetModuleFileNameW(nullptr, buf, _MAX_PATH);
|
|
|
|
if (bytes == 0)
|
|
|
|
std::abort();
|
|
|
|
return fs::path(buf, buf + bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_execute(const wchar_t* cmd_line)
|
|
|
|
{
|
|
|
|
// Basically we are wrapping it in quotes
|
2016-09-30 10:28:00 +08:00
|
|
|
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);
|
2016-09-19 11:50:08 +08:00
|
|
|
int exit_code = _wsystem(actual_cmd_line.c_str());
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_code_and_output cmd_execute_and_capture_output(const wchar_t* cmd_line)
|
|
|
|
{
|
2016-09-30 10:28:00 +08:00
|
|
|
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
std::string output;
|
|
|
|
char buf[1024];
|
|
|
|
auto pipe = _wpopen(actual_cmd_line.c_str(), L"r");
|
|
|
|
if (pipe == nullptr)
|
|
|
|
{
|
2017-02-15 05:26:21 +08:00
|
|
|
return { 1, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
while (fgets(buf, 1024, pipe))
|
|
|
|
{
|
|
|
|
output.append(buf);
|
|
|
|
}
|
|
|
|
if (!feof(pipe))
|
|
|
|
{
|
2017-02-15 05:26:21 +08:00
|
|
|
return { 1, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
auto ec = _pclose(pipe);
|
2017-02-15 05:26:21 +08:00
|
|
|
return { ec, output };
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void print(const char* message)
|
|
|
|
{
|
|
|
|
std::cout << message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void println(const char* message)
|
|
|
|
{
|
|
|
|
print(message);
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
2016-12-13 07:08:26 +08:00
|
|
|
void print(const color c, const char* message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo{};
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &consoleScreenBufferInfo);
|
|
|
|
auto original_color = consoleScreenBufferInfo.wAttributes;
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hConsole, static_cast<int>(c) | (original_color & 0xF0));
|
|
|
|
std::cout << message;
|
|
|
|
SetConsoleTextAttribute(hConsole, original_color);
|
|
|
|
}
|
|
|
|
|
2016-12-13 07:08:26 +08:00
|
|
|
void println(const color c, const char* message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
print(c, message);
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:35:34 +08:00
|
|
|
optional<std::wstring> get_environmental_variable(const wchar_t* varname) noexcept
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
wchar_t* buffer;
|
|
|
|
_wdupenv_s(&buffer, nullptr, varname);
|
2017-02-15 07:35:34 +08:00
|
|
|
|
|
|
|
if (buffer == nullptr)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-02-15 07:35:34 +08:00
|
|
|
return nullptr;
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2017-02-15 10:28:41 +08:00
|
|
|
std::unique_ptr<wchar_t, void(__cdecl *)(void*)> bufptr(buffer, free);
|
|
|
|
return std::make_unique<std::wstring>(buffer);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 05:26:21 +08:00
|
|
|
void set_environmental_variable(const wchar_t* varname, const wchar_t* varvalue) noexcept
|
|
|
|
{
|
|
|
|
_wputenv_s(varname, varvalue);
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:46:40 +08:00
|
|
|
void Stopwatch2::start()
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
static_assert(sizeof(start_time) == sizeof(LARGE_INTEGER), "");
|
|
|
|
|
|
|
|
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&start_time));
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:46:40 +08:00
|
|
|
void Stopwatch2::stop()
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_time));
|
|
|
|
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq));
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:46:40 +08:00
|
|
|
double Stopwatch2::microseconds() const
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
return (reinterpret_cast<const LARGE_INTEGER*>(&end_time)->QuadPart -
|
2017-01-06 04:47:08 +08:00
|
|
|
reinterpret_cast<const LARGE_INTEGER*>(&start_time)->QuadPart) * 1000000.0 / reinterpret_cast<const LARGE_INTEGER*>(&freq)->QuadPart;
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2017-01-06 04:47:08 +08:00
|
|
|
}
|