build: many compilation fixes for win32

This commit is contained in:
Danny Robson 2019-01-04 17:11:53 +11:00
parent c92f450ff8
commit 9e5cfc0dc6
20 changed files with 190 additions and 75 deletions

View File

@ -13,6 +13,9 @@
#include "win32/except.hpp"
#include "win32/windows.hpp"
// windows.h needs to be included here so that it defines some symbols that
// dbghelp.h requires.
#include <windows.h>
#include <dbghelp.h>
#include <ostream>

View File

@ -191,20 +191,11 @@ namespace cruft::cast {
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#if defined(COMPILER_GCC)
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
// If we're casting a pointer then we may need to first use a
// const_cast if we want to avoid any warnings about casting, eg in
// the case of `const ValueT*` to `ValueT*`
if constexpr (std::is_pointer_v<SrcT>) {
using value_type = std::remove_pointer_t<SrcT>;
return reinterpret_cast<DstT> (
const_cast<std::remove_cv_t<value_type>*> (src)
);
} else {
return reinterpret_cast<DstT> (src);
}
return (DstT)src;
#pragma GCC diagnostic pop
}

View File

@ -14,6 +14,8 @@
#include <iostream>
#include <winbase.h>
///////////////////////////////////////////////////////////////////////////////
void
@ -38,9 +40,13 @@ await_debugger (void)
void
prepare_debugger (void)
{
// Try to load the DrMinGW debugger if it's available.
//
// We explicitly let the resource leak as we'd like it to survive as long
// as possible.
if (nullptr == LoadLibrary("exchndl.dll")) {
auto code = GetLastError ();
LOG_WARNING("Emergency debugger not loaded: %s", cruft::win32::error::code_string (code));
LOG_WARNING("Emergency debugger not loaded, %s", cruft::win32::error::code_string (code));
}
}
@ -71,6 +77,8 @@ disable_fpe (void)
#include <io.h>
#include <fcntl.h>
#include <wincon.h>
void
force_console (void)
{

View File

@ -14,6 +14,8 @@
#include <filesystem>
#include <vector>
#include <libloaderapi.h>
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
@ -22,7 +24,11 @@ cruft::image_path (void)
std::vector<char> resolved (256);
retry:
const auto written = GetModuleFileName (nullptr, resolved.data (), static_cast<DWORD> (resolved.size ()));
const auto written = GetModuleFileName (
nullptr,
resolved.data (),
static_cast<DWORD> (resolved.size ())
);
if (written == 0)
win32::error::throw_code ();

View File

@ -149,6 +149,6 @@ ieee_float<E, S>::almost_equal (floating_t _a,
///////////////////////////////////////////////////////////////////////////////
template class cruft::ieee_float< 5, 10>; // ieee_half
template class cruft::ieee_float< 8, 23>; // ieee_single;
template class cruft::ieee_float<11, 52>; // ieee_double;
template class cruft::ieee_float< 5, 10>; // ieee_half
template class cruft::ieee_float< 8, 23>; // ieee_single;
template class cruft::ieee_float<11, 52>; // ieee_double;

View File

@ -69,6 +69,9 @@ namespace cruft {
typedef ieee_float< 8, 23> ieee_single;
typedef ieee_float<11, 52> ieee_double;
extern template class ieee_float< 5,10>;
extern template class ieee_float< 8,23>;
extern template class ieee_float<11,52>;
static_assert (sizeof(ieee_half ) == 2, "ieee_half must be 2 bytes");
static_assert (sizeof(ieee_single ) == 4, "ieee_single must be 4 bytes");

View File

@ -12,6 +12,8 @@
#include "win32/except.hpp"
#include "win32/windows.hpp"
#include <winbase.h>
using cruft::detail::win32::mapped_file;
@ -59,17 +61,17 @@ mflags_to_protect (int mflags) {
///////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (::cruft::win32::handle &&src,
mapped_file::mapped_file (::cruft::win32::file &&src,
int fflags,
int mflags):
m_file (std::forward<::cruft::win32::handle> (src)),
m_file (std::forward<::cruft::win32::file> (src)),
m_data (nullptr, UnmapViewOfFile)
{
// I would rather perform checks on filesize after mapping, but mapping
// requires a check for empty files before we perform the mapping to
// detect errors it throws in that specific situation.
DWORD hi_size;
DWORD lo_size = GetFileSize (m_file, &hi_size);
DWORD lo_size = GetFileSize (m_file.native (), &hi_size);
if (lo_size == INVALID_FILE_SIZE)
::cruft::win32::error::throw_code ();
@ -119,17 +121,12 @@ mapped_file::mapped_file (
int mflags
)
: mapped_file (
::cruft::win32::handle (
::cruft::win32::error::try_call (
::CreateFile,
path.u8string ().c_str (),
fflags_to_generic (fflags),
fflags == O_RDONLY ? FILE_SHARE_READ : 0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
nullptr
)
::cruft::win32::file (
path,
fflags_to_generic (fflags),
fflags == O_RDONLY ? FILE_SHARE_READ : 0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN
),
fflags,
mflags
@ -141,7 +138,7 @@ mapped_file::mapped_file (
mapped_file::mapped_file (cruft::posix::fd const &src,
int fflags,
int mflags):
mapped_file (cruft::win32::handle (src.dup ()),
mapped_file (cruft::win32::file (src.dup ()),
fflags,
mflags)
{ };

View File

@ -12,6 +12,7 @@
#include "pointer.hpp"
#include "io.hpp"
#include "win32/file.hpp"
#include "win32/handle.hpp"
#include "win32/windows.hpp"
#include "view.hpp"
@ -56,7 +57,7 @@ namespace cruft {
using difference_type = std::iterator_traits<iterator>::difference_type;
using size_type = std::size_t;
mapped_file (::cruft::win32::handle &&,
mapped_file (::cruft::win32::file &&,
int fflags = O_RDONLY,
int mflags = PROT_READ);
mapped_file (const std::filesystem::path &path,
@ -90,7 +91,7 @@ namespace cruft {
{
return {
reinterpret_cast<const T*> (cbegin ()),
reinterpret_cast<const T*> (align (cend (), alignof (T)))
reinterpret_cast<const T*> (align::up (cend (), alignof (T)))
};
}
@ -100,12 +101,12 @@ namespace cruft {
{
return {
reinterpret_cast<T *> (begin ()),
reinterpret_cast<T *> (align (end (), alignof(T)))
reinterpret_cast<T *> (align::up (end (), alignof(T)))
};
}
private:
::cruft::win32::handle m_file;
::cruft::win32::file m_file;
::cruft::win32::handle m_mapping;
std::unique_ptr<uint8_t,BOOL(*)(LPCVOID)> m_data;

View File

@ -10,16 +10,15 @@
#include "win32/except.hpp"
#include <winbase.h>
using cruft::detail::win32::library;
///////////////////////////////////////////////////////////////////////////////
library::library (const std::filesystem::path &path):
m_handle (LoadLibraryA (path.u8string ().c_str ()))
{
if (!m_handle)
::cruft::win32::error::throw_code ();
}
m_handle (cruft::win32::error::try_call (LoadLibraryW, path.c_str ()))
{ ; }
//-----------------------------------------------------------------------------

View File

@ -14,6 +14,7 @@
#include <cruft/util/cast.hpp>
#include <filesystem>
#include <libloaderapi.h>
namespace cruft {
namespace detail::win32 {
@ -28,14 +29,16 @@ namespace cruft {
~library ();
template <typename FunctionT>
FunctionT
symbol (const char *name)
FunctionT const
symbol (const char *name) const
{
return cast::ffs<FunctionT> (
GetProcAddress (m_handle, name)
);
}
operator HMODULE&() { return m_handle; }
private:
HMODULE m_handle;
};

View File

@ -6,9 +6,22 @@
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_PLATFORM_HPP
#define __UTIL_PLATFORM_HPP
#pragma once
///////////////////////////////////////////////////////////////////////////////
// Determine what compiler we're running
#if defined(__clang__)
#define COMPILER_CLANG
#elif defined(__GNUC__)
#define COMPILER_GCC
#else
#error "Unknown compiler"
#endif
///////////////////////////////////////////////////////////////////////////////
// Determine what operating system we're targetting
#if defined(__ANDROID__)
#define PLATFORM_ANDROID
#define PLATFORM_SUFFIX "android"
@ -26,13 +39,16 @@
#define PLATFORM_SUFFIX "unknown"
#endif
// Clang needs to be checked before GCC as it pretends to be GCC
#if defined(__clang__)
#define COMPILER_CLANG
#elif defined(__GNUC__)
#define COMPILER_GCC
#else
#error "Unknown compiler"
#endif
#endif
///////////////////////////////////////////////////////////////////////////////
#if defined(COMPILER_GCC) || defined(COMPILER_CLANG)
#if defined(__x86_64__)
#define PROCESSOR_AMD64 1
#elif defined(__arm__)
#define PROCESSOR_ARM 1
#else
#error "Unknown processor"
#endif
#else
#error "Unsupported compiler"
#endif

View File

@ -3,11 +3,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2011-2017 Danny Robson <danny@nerdcruft.net>
* Copyright 2011-2019 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include <type_traits>
#include <cstddef>
#include <cstdint>

View File

@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
* Copyright 2016-2019 Danny Robson <danny@nerdcruft.net>
*/
#include "fd.hpp"
@ -167,13 +167,29 @@ fd::write (const void *buffer, size_t count)
}
#if defined(PLATFORM_WIN32)
#include "../win32/windows.hpp"
#endif
//-----------------------------------------------------------------------------
ssize_t
fd::pwrite (const void *buf, size_t count, size_t offset)
{
#if !defined(PLATFORM_WIN32)
return error::try_value (
::pwrite (native (), buf, count, offset)
);
#else
DWORD written;
OVERLAPPED overlapped;
memset (&overlapped, 0, sizeof (overlapped));
overlapped.Offset = offset & 0xffffffff;
overlapped.OffsetHigh = offset >> 32u;
if (!WriteFile (reinterpret_cast<HANDLE> (_get_osfhandle (m_fd)), buf, count, &written, &overlapped))
error::throw_code (EINVAL);
return written;
#endif
}

View File

@ -13,6 +13,8 @@
#include "../debug.hpp"
#include <winbase.h>
using cruft::thread::semaphore;

View File

@ -9,8 +9,14 @@
#include "except.hpp"
#include "windows.hpp"
#include "../annotation.hpp"
#include "../debug.hpp"
#include <winerror.h>
#include <winbase.h>
using cruft::win32::error;
@ -46,16 +52,37 @@ error::last_code (void)
///////////////////////////////////////////////////////////////////////////////
HMODULE
error::try_value (HMODULE value)
{
if (likely (value != nullptr))
return value;
throw_code ();
}
//-----------------------------------------------------------------------------
HANDLE
error::try_value (HANDLE value)
{
if (value != INVALID_HANDLE_VALUE)
if (likely (value != INVALID_HANDLE_VALUE))
return value;
throw_code ();
}
//-----------------------------------------------------------------------------
BOOL
error::try_value (BOOL value)
{
if (likely (value))
return value;
throw_code ();
}
///////////////////////////////////////////////////////////////////////////////
void
error::try_code (void)
@ -68,7 +95,7 @@ error::try_code (void)
void
error::try_code (DWORD id)
{
if (__builtin_expect (id != ERROR_SUCCESS, false))
if (unlikely (id != ERROR_SUCCESS))
throw_code (id);
}

View File

@ -37,7 +37,9 @@ namespace cruft::win32 {
);
}
static HANDLE try_value (HANDLE);
static HMODULE try_value (HMODULE);
static HANDLE try_value (HANDLE);
static BOOL try_value (BOOL);
static void try_code (void);
static void try_code (DWORD);

View File

@ -6,6 +6,8 @@
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "windows.hpp"
#include "../posix/fd.hpp"
@ -13,10 +15,13 @@
namespace cruft::win32 {
struct handle {
handle ();
explicit handle (posix::fd&&);
explicit handle (HANDLE&&);
handle (handle&&);
handle (const handle&) = delete;
handle (posix::fd&&);
handle& operator= (handle const&) = delete;
~handle ();
operator HANDLE& (void) &;

View File

@ -12,6 +12,8 @@
#include "../debug.hpp"
#include "except.hpp"
#include <winerror.h>
#include <string>
#include <cstdint>

View File

@ -11,6 +11,8 @@
#include "windows.hpp"
#include <winreg.h>
#include "../view.hpp"
#include <set>

View File

@ -1,31 +1,62 @@
#pragma once
// include the windows.h header in a way that won't break anything that isn't
// aware of microsoft's propensity to grab large swathes of common terms.
#include "../platform.hpp"
// Include various useful Windows headers in a way that won't break anything
// that isn't aware of Microsoft's propensity to define away large swathes of
// frequently used symbols.
#if defined(_WINDOWS_)
#error "windows headers have already been included"
#endif
// Based off the approach outlined at:
// https://aras-p.info/blog/2018/01/12/Minimizing-windows.h/
// Predefine the hardware platform macros that the headers will expect.
#if defined(PROCESSOR_AMD64)
#if !defined(_AMD64_)
#define _AMD64_
#endif
#elif defined(PROCESSOR_X86)
#if !defined(_X86_)
#define _X86_
#endif
#elif defined(PROCESSOR_ARM)
#if !defined(_ARM_)
#define _ARM_
#endif
#else
#error "Unsupported processr"
#endif
// Request a smaller header. It probably won't help too much, but it shouldn't
// hurt us either.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// undefine a bunch of conflicting names.
#if defined(near)
#include <windef.h>
#include <fileapi.h>
#include <synchapi.h>
#include <debugapi.h>
#include <memoryapi.h>
#include <handleapi.h>
#include <processthreadsapi.h>
#include <errhandlingapi.h>
// GDI isn't used in many locations but it's easier to include it hear and
// undefine various pieces of trash than let unsuspecting code get trampled
// later.
#include <wingdi.h>
#undef near
#endif
#if defined(far)
#undef far
#endif
#if defined(NEAR)
#undef NEAR
#endif
#if defined(FAR)
#undef FAR
#endif
#if defined(OPAQUE)
#undef TRANSPARENT
#undef OPAQUE
#endif