Merged in rokups/tracy/mingw-support (pull request #26)

MingW support
This commit is contained in:
Rokas K. (rku) 2019-01-20 00:44:44 +00:00 committed by Bartosz Taudul
commit 31bbdfe2f2
10 changed files with 68 additions and 44 deletions

View File

@ -22,7 +22,7 @@
namespace tracy namespace tracy
{ {
#ifdef _MSC_VER #ifdef _WIN32
# define DLL_IMPORT __declspec(dllimport) # define DLL_IMPORT __declspec(dllimport)
#else #else
# define DLL_IMPORT # define DLL_IMPORT

View File

@ -1,6 +1,6 @@
#ifdef TRACY_ENABLE #ifdef TRACY_ENABLE
#ifdef _MSC_VER #ifdef _WIN32
# ifndef NOMINMAX # ifndef NOMINMAX
# define NOMINMAX # define NOMINMAX
# endif # endif
@ -57,7 +57,7 @@
# include <setjmp.h> # include <setjmp.h>
#endif #endif
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
# include <lmcons.h> # include <lmcons.h>
extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW ); extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW );
# if _WIN32_WINNT >= _WIN32_WINNT_VISTA # if _WIN32_WINNT >= _WIN32_WINNT_VISTA
@ -193,7 +193,7 @@ static int64_t SetupHwTimer()
static const char* GetProcessName() static const char* GetProcessName()
{ {
const char* processName = "unknown"; const char* processName = "unknown";
#if defined _MSC_VER #ifdef _WIN32
static char buf[_MAX_PATH]; static char buf[_MAX_PATH];
GetModuleFileNameA( nullptr, buf, _MAX_PATH ); GetModuleFileNameA( nullptr, buf, _MAX_PATH );
const char* ptr = buf; const char* ptr = buf;
@ -216,7 +216,7 @@ static const char* GetHostInfo()
{ {
static char buf[1024]; static char buf[1024];
auto ptr = buf; auto ptr = buf;
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
# ifdef UNICODE # ifdef UNICODE
t_RtlGetVersion RtlGetVersion = (t_RtlGetVersion)GetProcAddress( GetModuleHandle( L"ntdll.dll" ), "RtlGetVersion" ); t_RtlGetVersion RtlGetVersion = (t_RtlGetVersion)GetProcAddress( GetModuleHandle( L"ntdll.dll" ), "RtlGetVersion" );
# else # else
@ -225,10 +225,12 @@ static const char* GetHostInfo()
if( !RtlGetVersion ) if( !RtlGetVersion )
{ {
# ifndef __CYGWIN__ # ifdef __CYGWIN__
ptr += sprintf( ptr, "OS: Windows\n" );
# else
ptr += sprintf( ptr, "OS: Windows (Cygwin)\n" ); ptr += sprintf( ptr, "OS: Windows (Cygwin)\n" );
# elif defined __MINGW32__
ptr += sprintf( ptr, "OS: Windows (MingW)\n" );
# else
ptr += sprintf( ptr, "OS: Windows\n" );
# endif # endif
} }
else else
@ -236,10 +238,12 @@ static const char* GetHostInfo()
RTL_OSVERSIONINFOW ver = { sizeof( RTL_OSVERSIONINFOW ) }; RTL_OSVERSIONINFOW ver = { sizeof( RTL_OSVERSIONINFOW ) };
RtlGetVersion( &ver ); RtlGetVersion( &ver );
# ifndef __CYGWIN__ # ifdef __CYGWIN__
ptr += sprintf( ptr, "OS: Windows %i.%i.%i\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber );
# else
ptr += sprintf( ptr, "OS: Windows %i.%i.%i (Cygwin)\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber ); ptr += sprintf( ptr, "OS: Windows %i.%i.%i (Cygwin)\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber );
# elif defined __MINGW32__
ptr += sprintf( ptr, "OS: Windows %i.%i.%i (MingW)\n", (int)ver.dwMajorVersion, (int)ver.dwMinorVersion, (int)ver.dwBuildNumber );
# else
ptr += sprintf( ptr, "OS: Windows %i.%i.%i\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber );
# endif # endif
} }
#elif defined __linux__ #elif defined __linux__
@ -280,7 +284,7 @@ static const char* GetHostInfo()
ptr += sprintf( ptr, "Compiler: unknown\n" ); ptr += sprintf( ptr, "Compiler: unknown\n" );
#endif #endif
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
# ifndef __CYGWIN__ # ifndef __CYGWIN__
InitWinSock(); InitWinSock();
# endif # endif
@ -332,7 +336,7 @@ static const char* GetHostInfo()
auto modelPtr = cpuModel; auto modelPtr = cpuModel;
for( uint32_t i=0x80000002; i<0x80000005; ++i ) for( uint32_t i=0x80000002; i<0x80000005; ++i )
{ {
# if defined _MSC_VER || defined __CYGWIN__ # if defined _WIN32 || defined __CYGWIN__
__cpuidex( (int*)regs, i, 0 ); __cpuidex( (int*)regs, i, 0 );
# else # else
int zero = 0; int zero = 0;
@ -346,7 +350,7 @@ static const char* GetHostInfo()
ptr += sprintf( ptr, "CPU: unknown\n" ); ptr += sprintf( ptr, "CPU: unknown\n" );
#endif #endif
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
MEMORYSTATUSEX statex; MEMORYSTATUSEX statex;
statex.dwLength = sizeof( statex ); statex.dwLength = sizeof( statex );
GlobalMemoryStatusEx( &statex ); GlobalMemoryStatusEx( &statex );
@ -366,13 +370,13 @@ static const char* GetHostInfo()
return buf; return buf;
} }
#ifdef _MSC_VER #ifdef _WIN32
static DWORD s_profilerThreadId = 0; static DWORD s_profilerThreadId = 0;
static char s_crashText[1024]; static char s_crashText[1024];
LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp ) LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp )
{ {
const auto ec = pExp->ExceptionRecord->ExceptionCode; const unsigned ec = pExp->ExceptionRecord->ExceptionCode;
auto msgPtr = s_crashText; auto msgPtr = s_crashText;
switch( ec ) switch( ec )
{ {
@ -381,13 +385,13 @@ LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp )
switch( pExp->ExceptionRecord->ExceptionInformation[0] ) switch( pExp->ExceptionRecord->ExceptionInformation[0] )
{ {
case 0: case 0:
msgPtr += sprintf( msgPtr, "Read violation at address 0x%Iu.", pExp->ExceptionRecord->ExceptionInformation[1] ); msgPtr += sprintf( msgPtr, "Read violation at address 0x%" PRIxMAX ".", pExp->ExceptionRecord->ExceptionInformation[1] );
break; break;
case 1: case 1:
msgPtr += sprintf( msgPtr, "Write violation at address 0x%Iu.", pExp->ExceptionRecord->ExceptionInformation[1] ); msgPtr += sprintf( msgPtr, "Write violation at address 0x%" PRIxMAX ".", pExp->ExceptionRecord->ExceptionInformation[1] );
break; break;
case 8: case 8:
msgPtr += sprintf( msgPtr, "DEP violation at address 0x%Iu.", pExp->ExceptionRecord->ExceptionInformation[1] ); msgPtr += sprintf( msgPtr, "DEP violation at address 0x%" PRIxMAX ".", pExp->ExceptionRecord->ExceptionInformation[1] );
break; break;
default: default:
break; break;
@ -752,7 +756,7 @@ thread_local LuaZoneState init_order(104) s_luaZoneState { 0, false };
static Profiler init_order(105) s_profilerInstance; static Profiler init_order(105) s_profilerInstance;
Profiler& s_profiler = s_profilerInstance; Profiler& s_profiler = s_profilerInstance;
#ifdef _MSC_VER #ifdef _WIN32
# define DLL_EXPORT __declspec(dllexport) # define DLL_EXPORT __declspec(dllexport)
#else #else
# define DLL_EXPORT __attribute__((visibility("default"))) # define DLL_EXPORT __attribute__((visibility("default")))
@ -850,8 +854,14 @@ Profiler::Profiler()
new(s_thread) Thread( LaunchWorker, this ); new(s_thread) Thread( LaunchWorker, this );
SetThreadName( s_thread->Handle(), "Tracy Profiler" ); SetThreadName( s_thread->Handle(), "Tracy Profiler" );
#ifdef _MSC_VER #if defined PTW32_VERSION
s_profilerThreadId = pthread_getw32threadid_np( s_thread->Handle() );
#elif defined __WINPTHREADS_VERSION
s_profilerThreadId = GetThreadId( (HANDLE)pthread_gethandle( s_thread->Handle() ) );
#elif defined _MSC_VER
s_profilerThreadId = GetThreadId( s_thread->Handle() ); s_profilerThreadId = GetThreadId( s_thread->Handle() );
#endif
#if defined _WIN32
AddVectoredExceptionHandler( 1, CrashFilter ); AddVectoredExceptionHandler( 1, CrashFilter );
#endif #endif

View File

@ -17,13 +17,13 @@
#include "../common/TracyMutex.hpp" #include "../common/TracyMutex.hpp"
#include "../common/TracySystem.hpp" #include "../common/TracySystem.hpp"
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
# include <intrin.h> # include <intrin.h>
#endif #endif
#if defined _MSC_VER || defined __CYGWIN__ || ( ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 ) && !defined __ANDROID__ ) || __ARM_ARCH >= 6 #if defined _WIN32 || defined __CYGWIN__ || ( ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 ) && !defined __ANDROID__ ) || __ARM_ARCH >= 6
# define TRACY_HW_TIMER # define TRACY_HW_TIMER
# if defined _MSC_VER || defined __CYGWIN__ # if defined _WIN32 || defined __CYGWIN__
// Enable optimization for MSVC __rdtscp() intrin, saving one LHS of a cpu value on the stack. // Enable optimization for MSVC __rdtscp() intrin, saving one LHS of a cpu value on the stack.
// This comes at the cost of an unaligned memory write. // This comes at the cost of an unaligned memory write.
# define TRACY_RDTSCP_OPT # define TRACY_RDTSCP_OPT
@ -93,7 +93,7 @@ public:
# if __ARM_ARCH >= 6 # if __ARM_ARCH >= 6
cpu = 0xFFFFFFFF; cpu = 0xFFFFFFFF;
return GetTimeImpl(); return GetTimeImpl();
# elif defined _MSC_VER || defined __CYGWIN__ # elif defined _WIN32 || defined __CYGWIN__
const auto t = int64_t( __rdtscp( &cpu ) ); const auto t = int64_t( __rdtscp( &cpu ) );
return t; return t;
# elif defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 # elif defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64
@ -112,7 +112,7 @@ public:
#ifdef TRACY_HW_TIMER #ifdef TRACY_HW_TIMER
# if __ARM_ARCH >= 6 # if __ARM_ARCH >= 6
return GetTimeImpl(); return GetTimeImpl();
# elif defined _MSC_VER || defined __CYGWIN__ # elif defined _WIN32 || defined __CYGWIN__
unsigned int dontcare; unsigned int dontcare;
const auto t = int64_t( __rdtscp( &dontcare ) ); const auto t = int64_t( __rdtscp( &dontcare ) );
return t; return t;

View File

@ -9,11 +9,13 @@
#include "TracyAlloc.hpp" #include "TracyAlloc.hpp"
#include "TracySocket.hpp" #include "TracySocket.hpp"
#ifdef _MSC_VER #ifdef _WIN32
# include <winsock2.h> # include <winsock2.h>
# include <ws2tcpip.h> # include <ws2tcpip.h>
# ifdef _MSC_VER
# pragma warning(disable:4244) # pragma warning(disable:4244)
# pragma warning(disable:4267) # pragma warning(disable:4267)
# endif
#else #else
# include <sys/socket.h> # include <sys/socket.h>
# include <netdb.h> # include <netdb.h>
@ -27,13 +29,13 @@
namespace tracy namespace tracy
{ {
#ifdef _MSC_VER #ifdef _WIN32
typedef SOCKET socket_t; typedef SOCKET socket_t;
#else #else
typedef int socket_t; typedef int socket_t;
#endif #endif
#ifdef _MSC_VER #ifdef _WIN32
struct __wsinit struct __wsinit
{ {
__wsinit() __wsinit()
@ -59,7 +61,7 @@ Socket::Socket()
, m_bufPtr( nullptr ) , m_bufPtr( nullptr )
, m_bufLeft( 0 ) , m_bufLeft( 0 )
{ {
#ifdef _MSC_VER #ifdef _WIN32
InitWinSock(); InitWinSock();
#endif #endif
} }
@ -103,7 +105,7 @@ bool Socket::Connect( const char* addr, const char* port )
#endif #endif
if( connect( sock, ptr->ai_addr, ptr->ai_addrlen ) == -1 ) if( connect( sock, ptr->ai_addr, ptr->ai_addrlen ) == -1 )
{ {
#ifdef _MSC_VER #ifdef _WIN32
closesocket( sock ); closesocket( sock );
#else #else
close( sock ); close( sock );
@ -122,7 +124,7 @@ bool Socket::Connect( const char* addr, const char* port )
void Socket::Close() void Socket::Close()
{ {
assert( m_sock != -1 ); assert( m_sock != -1 );
#ifdef _MSC_VER #ifdef _WIN32
closesocket( m_sock ); closesocket( m_sock );
#else #else
close( m_sock ); close( m_sock );
@ -183,7 +185,7 @@ int Socket::Recv( void* _buf, int len, const timeval* tv )
FD_ZERO( &fds ); FD_ZERO( &fds );
FD_SET( static_cast<socket_t>(m_sock), &fds ); FD_SET( static_cast<socket_t>(m_sock), &fds );
#ifndef _WIN32 #if !defined _WIN32 || defined __MINGW32__
timeval _tv = *tv; timeval _tv = *tv;
select( m_sock+1, &fds, nullptr, nullptr, &_tv ); select( m_sock+1, &fds, nullptr, nullptr, &_tv );
#else #else
@ -260,7 +262,7 @@ bool Socket::HasData()
ListenSocket::ListenSocket() ListenSocket::ListenSocket()
: m_sock( -1 ) : m_sock( -1 )
{ {
#ifdef _MSC_VER #ifdef _WIN32
InitWinSock(); InitWinSock();
#endif #endif
} }
@ -284,7 +286,7 @@ bool ListenSocket::Listen( const char* port, int backlog )
if( getaddrinfo( nullptr, port, &hints, &res ) != 0 ) return false; if( getaddrinfo( nullptr, port, &hints, &res ) != 0 ) return false;
m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol ); m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
#if defined _MSC_VER || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
unsigned long val = 0; unsigned long val = 0;
setsockopt( m_sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&val, sizeof( val ) ); setsockopt( m_sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&val, sizeof( val ) );
#else #else
@ -337,7 +339,7 @@ Socket* ListenSocket::Accept()
void ListenSocket::Close() void ListenSocket::Close()
{ {
assert( m_sock != -1 ); assert( m_sock != -1 );
#ifdef _MSC_VER #ifdef _WIN32
closesocket( m_sock ); closesocket( m_sock );
#else #else
close( m_sock ); close( m_sock );

View File

@ -8,7 +8,7 @@ struct timeval;
namespace tracy namespace tracy
{ {
#ifdef _MSC_VER #ifdef _WIN32
void InitWinSock(); void InitWinSock();
#endif #endif

View File

@ -21,6 +21,9 @@
# include <fcntl.h> # include <fcntl.h>
#endif #endif
#ifdef __MINGW32__
# define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
@ -51,7 +54,7 @@ void SetThreadName( std::thread& thread, const char* name )
void SetThreadName( std::thread::native_handle_type handle, const char* name ) void SetThreadName( std::thread::native_handle_type handle, const char* name )
{ {
#ifdef _WIN32 #if defined _WIN32 && !defined PTW32_VERSION && !defined __WINPTHREADS_VERSION
# if defined NTDDI_WIN10_RS2 && NTDDI_VERSION >= NTDDI_WIN10_RS2 # if defined NTDDI_WIN10_RS2 && NTDDI_VERSION >= NTDDI_WIN10_RS2
wchar_t buf[256]; wchar_t buf[256];
mbstowcs( buf, name, 256 ); mbstowcs( buf, name, 256 );
@ -108,7 +111,13 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name )
buf[sz+1] = '\0'; buf[sz+1] = '\0';
auto data = (ThreadNameData*)tracy_malloc( sizeof( ThreadNameData ) ); auto data = (ThreadNameData*)tracy_malloc( sizeof( ThreadNameData ) );
# ifdef _WIN32 # ifdef _WIN32
# if defined PTW32_VERSION
data->id = pthread_getw32threadid_np( static_cast<pthread_t>( handle ) );
# elif defined __WINPTHREADS_VERSION
data->id = GetThreadId( pthread_gethandle( static_cast<pthread_t>( handle ) ) );
# else
data->id = GetThreadId( static_cast<HANDLE>( handle ) ); data->id = GetThreadId( static_cast<HANDLE>( handle ) );
# endif
# elif defined __APPLE__ # elif defined __APPLE__
pthread_threadid_np( handle, &data->id ); pthread_threadid_np( handle, &data->id );
# else # else

View File

@ -1,7 +1,7 @@
#ifndef __TRACYPOPCNT_HPP__ #ifndef __TRACYPOPCNT_HPP__
#define __TRACYPOPCNT_HPP__ #define __TRACYPOPCNT_HPP__
#ifdef _MSC_VER #ifdef _WIN32
# include <intrin.h> # include <intrin.h>
# define TracyCountBits __popcnt64 # define TracyCountBits __popcnt64
#else #else

View File

@ -8,10 +8,10 @@
#else #else
# include <dirent.h> # include <dirent.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h> # include <unistd.h>
# include <errno.h> # include <errno.h>
#endif #endif
#include <sys/stat.h>
#include "TracyStorage.hpp" #include "TracyStorage.hpp"

View File

@ -1,3 +1,6 @@
#ifdef __MINGW32__
# define __STDC_FORMAT_MACROS
#endif
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <chrono> #include <chrono>

View File

@ -1,10 +1,10 @@
#ifdef _MSC_VER #ifdef _WIN32
# include <winsock2.h> # include <winsock2.h>
#else #else
# include <sys/time.h> # include <sys/time.h>
#endif #endif
#if defined _MSC_VER || defined __MINGW32__ #ifdef _WIN32
# include <malloc.h> # include <malloc.h>
#else #else
# include <alloca.h> # include <alloca.h>