Skip to content

Commit

Permalink
minicargo - Fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Sep 15, 2023
1 parent e2a1257 commit df71e16
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 38 deletions.
4 changes: 4 additions & 0 deletions tools/minicargo/build2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* build.cpp
* - Logic related to invoking the compiler
*/
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS // Allows use of getenv (this program doesn't set env vars)
#endif

#include "manifest.h"
#include "cfg.hpp"
#include "build.h"
Expand Down
3 changes: 1 addition & 2 deletions tools/minicargo/file_timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Timestamp Timestamp::for_file(const ::helpers::path& path)
return Timestamp::infinite_past();
}
CloseHandle(handle);
//DEBUG(Timestamp{out} << " " << path);
return Timestamp { out };
return Timestamp { (static_cast<uint64_t>(out.dwHighDateTime) << 32) | static_cast<uint64_t>(out.dwLowDateTime) };
#else
struct stat s;
if( stat(path.str().c_str(), &s) == 0 )
Expand Down
8 changes: 2 additions & 6 deletions tools/minicargo/file_timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Timestamp
#if _WIN32
uint64_t m_val;

Timestamp(FILETIME ft):
m_val( (static_cast<uint64_t>(ft.dwHighDateTime) << 32) | static_cast<uint64_t>(ft.dwLowDateTime) )
Timestamp(uint64_t val):
m_val(val)
{
}
#else
Expand All @@ -22,11 +22,7 @@ class Timestamp
public:
static Timestamp for_file(const ::helpers::path& p);
static Timestamp infinite_past() {
#if _WIN32
return Timestamp { FILETIME { 0, 0 } };
#else
return Timestamp { 0 };
#endif
}

bool operator==(const Timestamp& x) const {
Expand Down
17 changes: 13 additions & 4 deletions tools/minicargo/jobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* jobs.cpp
* - Logic related to running build tasks
*/
//
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS // Allows use of getenv (this program doesn't set env vars)
#endif

#include <iostream>
#include "jobs.hpp"
#include "debug.h"
Expand Down Expand Up @@ -140,15 +145,19 @@ bool JobList::wait_one()
#ifdef _WIN32
::std::vector<HANDLE> handles;
for(const auto& j : this->running_jobs) {
handles.push_back(j.handle.handle);
handles.push_back(j.handle.m_handle);
}
if(handles.size() > MAXIMUM_WAIT_OBJECTS) {
handles.resize(MAXIMUM_WAIT_OBJECTS);
::std::cerr << "WARNING! Win32's WaitForMultipleObjects only supports up to " << MAXIMUM_WAIT_OBJECTS << " handles, but have " << handles.size() << ::std::endl;
}
auto wait_rv = WaitForMultipleObjects(handles.size(), handles.data(), FALSE, 0);
auto wait_rv = WaitForMultipleObjects(static_cast<DWORD>(handles.size()), handles.data(), FALSE, INFINITE);
if( !(WAIT_OBJECT_0 <= wait_rv && wait_rv < WAIT_OBJECT_0 + handles.size()) ) {
return false;
}

auto idx = wait_rv - WAIT_OBJECT_0;
auto handle = this->running_jobs[idx].handle.handle;
auto handle = this->running_jobs[idx].handle.m_handle;
RunningJob rjob = ::std::move(this->running_jobs[idx]);
this->running_jobs.erase(this->running_jobs.begin() + idx);

Expand All @@ -166,7 +175,7 @@ bool JobList::wait_one()

rv = os_support::Process::handle_status(status);

auto i = ::std::find_if(this->running_jobs.begin(), this->running_jobs.end(), [&](const auto& e){ return e.handle.handle == pid; });
auto i = ::std::find_if(this->running_jobs.begin(), this->running_jobs.end(), [&](const auto& e){ return e.handle.m_handle == pid; });
assert(i != this->running_jobs.end());
RunningJob rjob = ::std::move(*i);
this->running_jobs.erase(i);
Expand Down
43 changes: 27 additions & 16 deletions tools/minicargo/os.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//
//
//
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS // Allows use of getenv (this program doesn't set env vars)
#endif

#include <iostream>
#include "os.hpp"
#include "debug.h"
Expand Down Expand Up @@ -49,13 +53,13 @@ namespace os_support {
Process::~Process()
{
#ifdef _WIN32
if(stderr) {
CloseHandle(stderr);
stderr = nullptr;
if(m_stderr) {
CloseHandle(m_stderr);
m_stderr = nullptr;
}
#else
if(stderr > 0) {
close(stderr);
if(m_stderr > 0) {
close(m_stderr);
stderr = -1;
}
#endif
Expand Down Expand Up @@ -93,8 +97,8 @@ Process Process::spawn(

#ifdef _WIN32
::std::stringstream cmdline;
cmdline << exe_name();
for (const auto& arg : args().get_vec()) {
cmdline << exe_name;
for (const auto& arg : args.get_vec()) {
argv_quote_windows(arg, cmdline);
}
auto cmdline_str = cmdline.str();
Expand Down Expand Up @@ -143,6 +147,7 @@ Process Process::spawn(
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = NULL;
HANDLE stderr_pipe = NULL;
if(capture_stderr)
{
SECURITY_ATTRIBUTES sa = { 0 };
Expand All @@ -160,21 +165,21 @@ Process Process::spawn(
SECURITY_ATTRIBUTES sa = { 0 };
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
si.hStdOutput = CreateFile( static_cast<::std::string>(logfile()).c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
si.hStdOutput = CreateFile( logfile.str().c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if(!si.hStdOutput) throw WinapiError("CreateFile StdOutput");
DWORD tmp;
WriteFile(si.hStdOutput, cmdline_str.data(), static_cast<DWORD>(cmdline_str.size()), &tmp, NULL);
WriteFile(si.hStdOutput, "\n", 1, &tmp, NULL);
}
PROCESS_INFORMATION pi = { 0 };
CreateProcessA(exe_name(),
CreateProcessA(exe_name,
(LPSTR)cmdline_str.c_str(),
NULL, NULL, TRUE, 0, NULL,
(working_directory() != ::helpers::path() ? working_directory().str().c_str() : NULL),
(working_directory != ::helpers::path() ? working_directory.str().c_str() : NULL),
&si, &pi
);
CloseHandle(si.hStdOutput);
return Process { pi.hProcess, nullptr };
return Process { pi.hProcess, stderr_pipe };
#else

class CError: public ::std::exception {
Expand Down Expand Up @@ -291,19 +296,19 @@ Process Process::spawn(
bool Process::wait()
{
#ifdef _WIN32
if( this->stderr ) {
if( this->m_stderr ) {
throw ::std::runtime_error("capture_stderr with an explicit wait");
}
WaitForSingleObject(pi.hProcess, INFINITE);
WaitForSingleObject(m_handle, INFINITE);
DWORD status = 1;
GetExitCodeProcess(pi.hProcess, &status);
GetExitCodeProcess(m_handle, &status);
return handle_status(status);
#else
if( this->stderr > 0 ) {
throw ::std::runtime_error("capture_stderr with an explicit wait");
}
int status = -1;
waitpid(handle, &status, 0);
waitpid(m_handle, &status, 0);
return handle_status(status);
#endif
}
Expand All @@ -313,8 +318,14 @@ bool Process::handle_status(int status)
#ifdef _WIN32
if(status != 0)
{
auto dw_status = static_cast<DWORD>(status);
set_console_colour(std::cerr, TerminalColour::Red);
std::cerr << "Process `" << cmdline_str << "` exited with non-zero exit status " << std::hex << DWORD(status);
if( (dw_status & 0xC000'0000) != 0 ) {
std::cerr << "Process exited with non-zero exit status 0x" << std::hex << dw_status << std::endl;
}
else {
std::cerr << "Process exited with non-zero exit status " << status << std::endl;
}
set_console_colour(std::cerr, TerminalColour::Default);
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions tools/minicargo/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
#include "stringlist.h"
#include <path.h>

#ifdef WIN32
#ifdef _WIN32
#else
#include <unistd.h>
#endif

namespace os_support {
struct Process
{
#ifdef WIN32
void* handle;
void* stderr;
#ifdef _WIN32
void* m_handle;
void* m_stderr;
#else
pid_t handle;
int stderr;
pid_t m_handle;
int m_stderr;
#endif

~Process();
Expand Down
8 changes: 7 additions & 1 deletion vsproject/minicargo/minicargo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,22 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\tools\minicargo\build.cpp" />
<ClCompile Include="..\..\tools\minicargo\build2.cpp" />
<ClCompile Include="..\..\tools\minicargo\cfg.cpp" />
<ClCompile Include="..\..\tools\minicargo\file_timestamp.cpp" />
<ClCompile Include="..\..\tools\minicargo\jobs.cpp" />
<ClCompile Include="..\..\tools\minicargo\main.cpp" />
<ClCompile Include="..\..\tools\minicargo\manifest.cpp" />
<ClCompile Include="..\..\tools\minicargo\os.cpp" />
<ClCompile Include="..\..\tools\minicargo\repository.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\tools\minicargo\build.h" />
<ClInclude Include="..\..\tools\minicargo\cfg.hpp" />
<ClInclude Include="..\..\tools\minicargo\file_timestamp.h" />
<ClInclude Include="..\..\tools\minicargo\jobs.hpp" />
<ClInclude Include="..\..\tools\minicargo\manifest.h" />
<ClInclude Include="..\..\tools\minicargo\os.hpp" />
<ClInclude Include="..\..\tools\minicargo\repository.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
24 changes: 21 additions & 3 deletions vsproject/minicargo/minicargo.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
<ClCompile Include="..\..\tools\minicargo\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\build.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\manifest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand All @@ -30,6 +27,18 @@
<ClCompile Include="..\..\tools\minicargo\cfg.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\build2.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\file_timestamp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\os.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tools\minicargo\jobs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\tools\minicargo\manifest.h">
Expand All @@ -44,5 +53,14 @@
<ClInclude Include="..\..\tools\minicargo\cfg.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\tools\minicargo\file_timestamp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\tools\minicargo\jobs.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\tools\minicargo\os.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit df71e16

Please sign in to comment.