Commit fe87dad0 authored by Kenton Varda's avatar Kenton Varda

Merge pull request #122 from joshuawarner32/fix-mingw-build

initial fixes for building kj/main.c++ utilities on mingw
parents 9d7b417e 8a8072e5
......@@ -25,6 +25,10 @@
#include <string.h>
#include <errno.h>
#if _WIN32
#define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
#endif
namespace kj {
namespace _ { // private
......
......@@ -110,6 +110,11 @@
#include "string.h"
#include "exception.h"
#ifdef ERROR
// This is problematic because windows.h #defines ERROR, which we use in an enum here.
#error "Make sure to to undefine ERROR (or just #include <kj/windows-sanity.h>) before this file"
#endif
namespace kj {
#define KJ_LOG(severity, ...) \
......
......@@ -28,7 +28,13 @@
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#if _WIN32
#include <windows.h>
#include <io.h>
#else
#include <sys/uio.h>
#endif
namespace kj {
......@@ -60,12 +66,53 @@ void TopLevelProcessContext::exit() {
static void writeLineToFd(int fd, StringPtr message) {
// Write the given message to the given file descriptor with a trailing newline iff the message
// is non-empty and doesn't already have a trailing newline. We use writev() to do this in a
// single system call without any copying.
// single system call without any copying (OS permitting).
if (message.size() == 0) {
return;
}
#if _WIN32
KJ_STACK_ARRAY(char, newlineExpansionBuffer, 2 * (message.size() + 1), 128, 512);
char* p = newlineExpansionBuffer.begin();
for(char ch : message) {
if(ch == '\n') {
*(p++) = '\r';
}
*(p++) = ch;
}
if(!message.endsWith("\n")) {
*(p++) = '\r';
*(p++) = '\n';
}
size_t newlineExpandedSize = p - newlineExpansionBuffer.begin();
KJ_ASSERT(newlineExpandedSize <= newlineExpansionBuffer.size());
HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
DWORD consoleMode;
bool redirectedToFile = !GetConsoleMode(handle, &consoleMode);
DWORD writtenSize;
if(redirectedToFile) {
WriteFile(handle, newlineExpansionBuffer.begin(), newlineExpandedSize, &writtenSize, nullptr);
} else {
KJ_STACK_ARRAY(wchar_t, buffer, newlineExpandedSize, 128, 512);
size_t finalSize = MultiByteToWideChar(
CP_UTF8,
0,
newlineExpansionBuffer.begin(),
newlineExpandedSize,
buffer.begin(),
buffer.size());
KJ_ASSERT(finalSize <= buffer.size());
WriteConsoleW(handle, buffer.begin(), finalSize, &writtenSize, nullptr);
}
#else
// Unfortunately the writev interface requires non-const pointers even though it won't modify
// the data.
struct iovec vec[2];
......@@ -109,6 +156,7 @@ static void writeLineToFd(int fd, StringPtr message) {
}
}
}
#endif
}
void TopLevelProcessContext::warning(StringPtr message) {
......
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef KJ_WINDOWS_SANITY_H_
#define KJ_WINDOWS_SANITY_H_
#ifndef _INC_WINDOWS
#error "windows.h needs to be included before kj/windows-sanity.h (or perhaps you don't need either?)"
#endif
namespace win32 {
const auto ERROR_ = ERROR;
#undef ERROR
const auto ERROR = ERROR_;
}
using win32::ERROR;
#endif // KJ_WINDOWS_SANITY_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment