Skip to content

Commit

Permalink
[headers]: [MinGW][1/3] Add support for MinGW-w64
Browse files Browse the repository at this point in the history
  • Loading branch information
raedrizqie committed Jul 14, 2024
1 parent d915232 commit 33095f5
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 32 deletions.
6 changes: 3 additions & 3 deletions include/dxc/Support/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) {

#ifndef NDEBUG

#ifdef _WIN32
#ifdef _MSC_VER

// DXASSERT is used to debug break when 'exp' evaluates to false and is only
// intended for internal developer use. It is compiled out in free
Expand Down Expand Up @@ -314,7 +314,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) {

#define DXVERIFY_NOMSG(exp) DXASSERT(exp, "")

#else // _WIN32
#else // _MSC_VER
#include <cassert>

#define DXASSERT_NOMSG assert
Expand All @@ -341,7 +341,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) {
} \
} while (0)

#endif // _WIN32
#endif // _MSC_VER

#else // NDEBUG

Expand Down
2 changes: 2 additions & 0 deletions include/dxc/Support/WinIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ template <class T> void swap(CComHeapPtr<T> &a, CComHeapPtr<T> &b) {

#include "dxc/WinAdapter.h"

#ifndef __MINGW32__
#ifdef __cplusplus
#if !defined(DEFINE_ENUM_FLAG_OPERATORS)
// Define operator overloads to enable bit operations on enum values that are
Expand Down Expand Up @@ -118,6 +119,7 @@ template <class T> struct _ENUM_FLAG_SIZED_INTEGER {
#else
#define DEFINE_ENUM_FLAG_OPERATORS(ENUMTYPE) // NOP, C allows these operators.
#endif
#endif // __MINGW32__

#endif // _MSC_VER

Expand Down
115 changes: 113 additions & 2 deletions include/dxc/WinAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef LLVM_SUPPORT_WIN_ADAPTER_H
#define LLVM_SUPPORT_WIN_ADAPTER_H

#ifndef _WIN32
#ifndef _MSC_VER

#ifdef __cplusplus
#include <atomic>
Expand All @@ -33,6 +33,109 @@
#include <vector>
#endif // __cplusplus

#ifdef __MINGW32__

#undef _WIN32_WINNT
#undef _WIN32_IE

// Require at least Windows 7 (Updated from XP)
#define _WIN32_WINNT 0x0601
#define _WIN32_IE 0x0800

#define WIN32_LEAN_AND_MEAN
#define STRSAFE_NO_DEPRECATE

#include <intsafe.h>
#include <objidl.h>
#include <sal.h>
#include <strsafe.h>
#include <unknwn.h>
#include <windows.h>

#undef EN
#undef IN
#undef OUT
#undef MemoryFence
#undef ReplaceText

#define EventRegisterMicrosoft_Windows_DXCompiler_API()
#define EventUnregisterMicrosoft_Windows_DXCompiler_API()

#define DxcEtw_DXCompilerCompile_Start()
#define DxcEtw_DXCompilerCompile_Stop(hr)
#define DxcEtw_DXCompilerCreateInstance_Start()
#define DxcEtw_DXCompilerCreateInstance_Stop(hr)
#define DxcEtw_DXCompilerDisassemble_Start()
#define DxcEtw_DXCompilerDisassemble_Stop(hr)
#define DxcEtw_DXCompilerInitialization_Start()
#define DxcEtw_DXCompilerInitialization_Stop(hr)
#define DxcEtw_DXCompilerPreprocess_Start()
#define DxcEtw_DXCompilerPreprocess_Stop(hr)
#define DxcEtw_DXCompilerShutdown_Start()
#define DxcEtw_DXCompilerShutdown_Stop(hr)
#define DxcEtw_DxcValidation_Start()
#define DxcEtw_DxcValidation_Stop(hr)

#define ATLASSERT assert

// FIXME: missing from sal.h
#define _In_NLS_string_(x)
#define _Ret_opt_

#ifdef __cplusplus

constexpr uint8_t nybble_from_hex(char c) {
return ((c >= '0' && c <= '9')
? (c - '0')
: ((c >= 'a' && c <= 'f')
? (c - 'a' + 10)
: ((c >= 'A' && c <= 'F') ? (c - 'A' + 10)
: /* Should be an error */ -1)));
}

constexpr uint8_t byte_from_hex(char c1, char c2) {
return nybble_from_hex(c1) << 4 | nybble_from_hex(c2);
}

constexpr uint8_t byte_from_hexstr(const char str[2]) {
return nybble_from_hex(str[0]) << 4 | nybble_from_hex(str[1]);
}

constexpr GUID guid_from_string(const char str[37]) {
return GUID{static_cast<uint32_t>(byte_from_hexstr(str)) << 24 |
static_cast<uint32_t>(byte_from_hexstr(str + 2)) << 16 |
static_cast<uint32_t>(byte_from_hexstr(str + 4)) << 8 |
byte_from_hexstr(str + 6),
static_cast<uint16_t>(
static_cast<uint16_t>(byte_from_hexstr(str + 9)) << 8 |
byte_from_hexstr(str + 11)),
static_cast<uint16_t>(
static_cast<uint16_t>(byte_from_hexstr(str + 14)) << 8 |
byte_from_hexstr(str + 16)),
{byte_from_hexstr(str + 19), byte_from_hexstr(str + 21),
byte_from_hexstr(str + 24), byte_from_hexstr(str + 26),
byte_from_hexstr(str + 28), byte_from_hexstr(str + 30),
byte_from_hexstr(str + 32), byte_from_hexstr(str + 34)}};
}

#define CROSS_PLATFORM_UUIDOF(name, spec) \
struct __declspec(uuid(spec)) name; \
extern "C++" { \
template <> struct __mingw_uuidof_s<name> { \
static constexpr IID __uuid_inst = guid_from_string(spec); \
}; \
template <> constexpr const GUID &__mingw_uuidof<name>() { \
return __mingw_uuidof_s<name>::__uuid_inst; \
} \
template <> constexpr const GUID &__mingw_uuidof<name*>() { \
return __mingw_uuidof_s<name>::__uuid_inst; \
} \
}

#endif // __cplusplus

#else // __MINGW32__

#define COM_NO_WINDOWS_H // needed to inform d3d headers that this isn't windows

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -630,6 +733,12 @@ CROSS_PLATFORM_UUIDOF(ID3D12LibraryReflection,
CROSS_PLATFORM_UUIDOF(ID3D12ShaderReflection,
"5A58797D-A72C-478D-8BA2-EFC6B0EFE88E")

#endif // __cplusplus

#endif // __MINGW32__

#ifdef __cplusplus

//===--------------------- COM Pointer Types ------------------------------===//

class CAllocator {
Expand Down Expand Up @@ -897,6 +1006,7 @@ class CHeapPtr : public CHeapPtrBase<T, Allocator> {

#define CComHeapPtr CHeapPtr

#ifndef __MINGW32__
//===--------------------------- BSTR Allocation --------------------------===//

void SysFreeString(BSTR bstrString);
Expand All @@ -905,6 +1015,7 @@ BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui);

//===--------------------------- BSTR Length ------------------------------===//
unsigned int SysStringLen(const BSTR bstrString);
#endif // __MINGW32__

//===--------------------- UTF-8 Related Types ----------------------------===//

Expand Down Expand Up @@ -1034,6 +1145,6 @@ class WArgV {

#endif // __cplusplus

#endif // _WIN32
#endif // _MSC_VER

#endif // LLVM_SUPPORT_WIN_ADAPTER_H
4 changes: 2 additions & 2 deletions include/dxc/dxcapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef __DXC_API__
#define __DXC_API__

#ifdef _WIN32
#ifdef _MSC_VER
#ifndef DXC_API_IMPORT
#define DXC_API_IMPORT __declspec(dllimport)
#endif
Expand Down Expand Up @@ -1222,7 +1222,7 @@ struct IDxcPdbUtils2 : public IUnknown {
// Note: __declspec(selectany) requires 'extern'
// On Linux __declspec(selectany) is removed and using 'extern' results in link
// error.
#ifdef _MSC_VER
#ifdef _WIN32
#define CLSID_SCOPE __declspec(selectany) extern
#else
#define CLSID_SCOPE
Expand Down
5 changes: 3 additions & 2 deletions include/dxc/dxcdxrfallbackcompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

#ifndef __DXC_DXR_FALLBACK_COMPILER_API__
#define __DXC_DXR_FALLBACK_COMPILER_API__
#include "dxcapi.h"

#include "dxc/dxcapi.h"

enum class ShaderType : unsigned int {
Raygen,
Expand Down Expand Up @@ -94,7 +95,7 @@ struct __declspec(uuid("76bb3c85-006d-4b72-9e10-63cd97df57f0"))
// Note: __declspec(selectany) requires 'extern'
// On Linux __declspec(selectany) is removed and using 'extern' results in link
// error.
#ifdef _MSC_VER
#ifdef _WIN32
#define CLSID_SCOPE __declspec(selectany) extern
#else
#define CLSID_SCOPE
Expand Down
4 changes: 2 additions & 2 deletions include/dxc/dxcisense.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef __DXC_ISENSE__
#define __DXC_ISENSE__

#include "dxcapi.h"
#include "dxc/dxcapi.h"
#ifndef _WIN32
#include "WinAdapter.h"
#endif
Expand Down Expand Up @@ -943,7 +943,7 @@ struct IDxcCompletionString : public IUnknown {
// CLSID_DxcIntelliSense is not visible externally (this is OK in C, since const
// is not by default static in C)

#ifdef _MSC_VER
#ifdef _WIN32
#define CLSID_SCOPE __declspec(selectany) extern
#else
#define CLSID_SCOPE
Expand Down
2 changes: 1 addition & 1 deletion include/dxc/dxctools.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct IDxcRewriter : public IUnknown {
IDxcOperationResult **ppResult) = 0;
};

#ifdef _MSC_VER
#ifdef _WIN32
#define CLSID_SCOPE __declspec(selectany) extern
#else
#define CLSID_SCOPE
Expand Down
1 change: 1 addition & 0 deletions include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <limits>
#include <string>
Expand Down
33 changes: 17 additions & 16 deletions include/llvm/Object/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ struct coff_symbol_generic {
support::ulittle32_t Value;
};

using namespace COFF;

class COFFSymbolRef {
public:
COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {}
Expand Down Expand Up @@ -326,67 +328,66 @@ class COFFSymbolRef {
uint8_t getBaseType() const { return getType() & 0x0F; }

uint8_t getComplexType() const {
return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
return (getType() & 0xF0) >> 4; // SCT_COMPLEX_TYPE_SHIFT
}

bool isAbsolute() const {
return getSectionNumber() == -1;
}

bool isExternal() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
return getStorageClass() == IMAGE_SYM_CLASS_EXTERNAL;
}

bool isCommon() const {
return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
return isExternal() && getSectionNumber() == IMAGE_SYM_UNDEFINED &&
getValue() != 0;
}

bool isUndefined() const {
return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
return isExternal() && getSectionNumber() == IMAGE_SYM_UNDEFINED &&
getValue() == 0;
}

bool isWeakExternal() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
return getStorageClass() == IMAGE_SYM_CLASS_WEAK_EXTERNAL;
}

bool isFunctionDefinition() const {
return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
return isExternal() && getBaseType() == IMAGE_SYM_TYPE_NULL &&
getComplexType() == IMAGE_SYM_DTYPE_FUNCTION &&
!COFF::isReservedSectionNumber(getSectionNumber());
}

bool isFunctionLineInfo() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
return getStorageClass() == IMAGE_SYM_CLASS_FUNCTION;
}

bool isAnyUndefined() const {
return isUndefined() || isWeakExternal();
}

bool isFileRecord() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
return getStorageClass() == IMAGE_SYM_CLASS_FILE;
}

bool isSection() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION;
return getStorageClass() == IMAGE_SYM_CLASS_SECTION;
}

bool isSectionDefinition() const {
// C++/CLI creates external ABS symbols for non-const appdomain globals.
// These are also followed by an auxiliary section definition.
bool isAppdomainGlobal =
getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE;
bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
bool isAppdomainGlobal = getStorageClass() == IMAGE_SYM_CLASS_EXTERNAL &&
getSectionNumber() == IMAGE_SYM_ABSOLUTE;
bool isOrdinarySection = getStorageClass() == IMAGE_SYM_CLASS_STATIC;
if (!getNumberOfAuxSymbols())
return false;
return isAppdomainGlobal || isOrdinarySection;
}

bool isCLRToken() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
return getStorageClass() == IMAGE_SYM_CLASS_CLR_TOKEN;
}

private:
Expand All @@ -411,7 +412,7 @@ struct coff_section {
// Returns true if the actual number of relocations is stored in
// VirtualAddress field of the first relocation table entry.
bool hasExtendedRelocations() const {
return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
return (Characteristics & IMAGE_SCN_LNK_NRELOC_OVFL) &&
NumberOfRelocations == UINT16_MAX;
}
};
Expand Down
Loading

0 comments on commit 33095f5

Please sign in to comment.