From 8157e3a0b38942dadb90a63de73e855d7c75e293 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Sat, 19 Jan 2019 13:03:30 +0200 Subject: [PATCH 1/2] Fix builds with MingW. --- TracyClientDLL.cpp | 2 +- client/TracyProfiler.cpp | 50 ++++++++++++++++++++++++---------------- client/TracyProfiler.hpp | 10 ++++---- common/TracySocket.cpp | 26 +++++++++++---------- common/TracySocket.hpp | 2 +- common/TracySystem.cpp | 11 ++++++++- common/TracySystem.hpp | 2 +- server/TracyPopcnt.hpp | 2 +- server/TracyStorage.cpp | 2 +- server/TracyView.cpp | 3 +++ server/TracyWorker.cpp | 4 ++-- 11 files changed, 69 insertions(+), 45 deletions(-) diff --git a/TracyClientDLL.cpp b/TracyClientDLL.cpp index 497bd110..87eaddac 100644 --- a/TracyClientDLL.cpp +++ b/TracyClientDLL.cpp @@ -22,7 +22,7 @@ namespace tracy { -#ifdef _MSC_VER +#ifdef _WIN32 # define DLL_IMPORT __declspec(dllimport) #else # define DLL_IMPORT diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index cef41a27..6f52afa7 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -1,6 +1,6 @@ #ifdef TRACY_ENABLE -#ifdef _MSC_VER +#ifdef _WIN32 # ifndef NOMINMAX # define NOMINMAX # endif @@ -57,7 +57,7 @@ # include #endif -#if defined _MSC_VER || defined __CYGWIN__ +#if defined _WIN32 || defined __CYGWIN__ # include extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW ); # if _WIN32_WINNT >= _WIN32_WINNT_VISTA @@ -193,7 +193,7 @@ static int64_t SetupHwTimer() static const char* GetProcessName() { const char* processName = "unknown"; -#if defined _MSC_VER +#ifdef _WIN32 static char buf[_MAX_PATH]; GetModuleFileNameA( nullptr, buf, _MAX_PATH ); const char* ptr = buf; @@ -216,7 +216,7 @@ static const char* GetHostInfo() { static char buf[1024]; auto ptr = buf; -#if defined _MSC_VER || defined __CYGWIN__ +#if defined _WIN32 || defined __CYGWIN__ # ifdef UNICODE t_RtlGetVersion RtlGetVersion = (t_RtlGetVersion)GetProcAddress( GetModuleHandle( L"ntdll.dll" ), "RtlGetVersion" ); # else @@ -225,10 +225,12 @@ static const char* GetHostInfo() if( !RtlGetVersion ) { -# ifndef __CYGWIN__ - ptr += sprintf( ptr, "OS: Windows\n" ); -# else +# ifdef __CYGWIN__ 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 } else @@ -236,10 +238,12 @@ static const char* GetHostInfo() RTL_OSVERSIONINFOW ver = { sizeof( RTL_OSVERSIONINFOW ) }; RtlGetVersion( &ver ); -# ifndef __CYGWIN__ - ptr += sprintf( ptr, "OS: Windows %i.%i.%i\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber ); -# else +# ifdef __CYGWIN__ 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 } #elif defined __linux__ @@ -280,7 +284,7 @@ static const char* GetHostInfo() ptr += sprintf( ptr, "Compiler: unknown\n" ); #endif -#if defined _MSC_VER || defined __CYGWIN__ +#ifdef _WIN32 # ifndef __CYGWIN__ InitWinSock(); # endif @@ -332,7 +336,7 @@ static const char* GetHostInfo() auto modelPtr = cpuModel; for( uint32_t i=0x80000002; i<0x80000005; ++i ) { -# if defined _MSC_VER || defined __CYGWIN__ +# if defined _WIN32 || defined __CYGWIN__ __cpuidex( (int*)regs, i, 0 ); # else int zero = 0; @@ -346,7 +350,7 @@ static const char* GetHostInfo() ptr += sprintf( ptr, "CPU: unknown\n" ); #endif -#if defined _MSC_VER || defined __CYGWIN__ +#if defined _WIN32 || defined __CYGWIN__ MEMORYSTATUSEX statex; statex.dwLength = sizeof( statex ); GlobalMemoryStatusEx( &statex ); @@ -366,13 +370,13 @@ static const char* GetHostInfo() return buf; } -#ifdef _MSC_VER +#ifdef _WIN32 static DWORD s_profilerThreadId = 0; static char s_crashText[1024]; LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp ) { - const auto ec = pExp->ExceptionRecord->ExceptionCode; + const unsigned ec = pExp->ExceptionRecord->ExceptionCode; auto msgPtr = s_crashText; switch( ec ) { @@ -381,13 +385,13 @@ LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp ) switch( pExp->ExceptionRecord->ExceptionInformation[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; 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; 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; default: break; @@ -752,7 +756,7 @@ thread_local LuaZoneState init_order(104) s_luaZoneState { 0, false }; static Profiler init_order(105) s_profilerInstance; Profiler& s_profiler = s_profilerInstance; -#ifdef _MSC_VER +#ifdef _WIN32 # define DLL_EXPORT __declspec(dllexport) #else # define DLL_EXPORT __attribute__((visibility("default"))) @@ -850,8 +854,14 @@ Profiler::Profiler() new(s_thread) Thread( LaunchWorker, this ); 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() ); +#endif +#if defined _WIN32 AddVectoredExceptionHandler( 1, CrashFilter ); #endif diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 2947ef67..040fa47f 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -17,13 +17,13 @@ #include "../common/TracyMutex.hpp" #include "../common/TracySystem.hpp" -#if defined _MSC_VER || defined __CYGWIN__ +#if defined _WIN32 || defined __CYGWIN__ # include #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 -# 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. // This comes at the cost of an unaligned memory write. # define TRACY_RDTSCP_OPT @@ -93,7 +93,7 @@ public: # if __ARM_ARCH >= 6 cpu = 0xFFFFFFFF; return GetTimeImpl(); -# elif defined _MSC_VER || defined __CYGWIN__ +# elif defined _WIN32 || defined __CYGWIN__ const auto t = int64_t( __rdtscp( &cpu ) ); return t; # elif defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 @@ -112,7 +112,7 @@ public: #ifdef TRACY_HW_TIMER # if __ARM_ARCH >= 6 return GetTimeImpl(); -# elif defined _MSC_VER || defined __CYGWIN__ +# elif defined _WIN32 || defined __CYGWIN__ unsigned int dontcare; const auto t = int64_t( __rdtscp( &dontcare ) ); return t; diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index 4a3e4261..7cd07b67 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -9,11 +9,13 @@ #include "TracyAlloc.hpp" #include "TracySocket.hpp" -#ifdef _MSC_VER +#ifdef _WIN32 # include # include -# pragma warning(disable:4244) -# pragma warning(disable:4267) +# ifdef _MSC_VER +# pragma warning(disable:4244) +# pragma warning(disable:4267) +# endif #else # include # include @@ -27,13 +29,13 @@ namespace tracy { -#ifdef _MSC_VER +#ifdef _WIN32 typedef SOCKET socket_t; #else typedef int socket_t; #endif -#ifdef _MSC_VER +#ifdef _WIN32 struct __wsinit { __wsinit() @@ -59,7 +61,7 @@ Socket::Socket() , m_bufPtr( nullptr ) , m_bufLeft( 0 ) { -#ifdef _MSC_VER +#ifdef _WIN32 InitWinSock(); #endif } @@ -103,7 +105,7 @@ bool Socket::Connect( const char* addr, const char* port ) #endif if( connect( sock, ptr->ai_addr, ptr->ai_addrlen ) == -1 ) { -#ifdef _MSC_VER +#ifdef _WIN32 closesocket( sock ); #else close( sock ); @@ -122,7 +124,7 @@ bool Socket::Connect( const char* addr, const char* port ) void Socket::Close() { assert( m_sock != -1 ); -#ifdef _MSC_VER +#ifdef _WIN32 closesocket( m_sock ); #else close( m_sock ); @@ -183,7 +185,7 @@ int Socket::Recv( void* _buf, int len, const timeval* tv ) FD_ZERO( &fds ); FD_SET( static_cast(m_sock), &fds ); -#ifndef _WIN32 +#if !defined _WIN32 || defined __MINGW32__ timeval _tv = *tv; select( m_sock+1, &fds, nullptr, nullptr, &_tv ); #else @@ -260,7 +262,7 @@ bool Socket::HasData() ListenSocket::ListenSocket() : m_sock( -1 ) { -#ifdef _MSC_VER +#ifdef _WIN32 InitWinSock(); #endif } @@ -284,7 +286,7 @@ bool ListenSocket::Listen( const char* port, int backlog ) if( getaddrinfo( nullptr, port, &hints, &res ) != 0 ) return false; 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; setsockopt( m_sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&val, sizeof( val ) ); #else @@ -337,7 +339,7 @@ Socket* ListenSocket::Accept() void ListenSocket::Close() { assert( m_sock != -1 ); -#ifdef _MSC_VER +#ifdef _WIN32 closesocket( m_sock ); #else close( m_sock ); diff --git a/common/TracySocket.hpp b/common/TracySocket.hpp index 7a08d797..3a5aaafe 100644 --- a/common/TracySocket.hpp +++ b/common/TracySocket.hpp @@ -8,7 +8,7 @@ struct timeval; namespace tracy { -#ifdef _MSC_VER +#ifdef _WIN32 void InitWinSock(); #endif diff --git a/common/TracySystem.cpp b/common/TracySystem.cpp index 235b2cfc..385b6c82 100644 --- a/common/TracySystem.cpp +++ b/common/TracySystem.cpp @@ -21,6 +21,9 @@ # include #endif +#ifdef __MINGW32__ +# define __STDC_FORMAT_MACROS +#endif #include #include @@ -51,7 +54,7 @@ void SetThreadName( std::thread& thread, 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 wchar_t buf[256]; mbstowcs( buf, name, 256 ); @@ -108,7 +111,13 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name ) buf[sz+1] = '\0'; auto data = (ThreadNameData*)tracy_malloc( sizeof( ThreadNameData ) ); # ifdef _WIN32 +# if defined PTW32_VERSION + data->id = pthread_getw32threadid_np( static_cast( handle ) ); +# elif defined __WINPTHREADS_VERSION + data->id = GetThreadId( pthread_gethandle( static_cast( handle ) ) ); +# else data->id = GetThreadId( static_cast( handle ) ); +# endif # elif defined __APPLE__ pthread_threadid_np( handle, &data->id ); # else diff --git a/common/TracySystem.hpp b/common/TracySystem.hpp index 38d67095..77917c9a 100644 --- a/common/TracySystem.hpp +++ b/common/TracySystem.hpp @@ -2,7 +2,7 @@ #define __TRACYSYSTEM_HPP__ #ifdef TRACY_ENABLE -# if defined __ANDROID__ || defined __CYGWIN__ || defined __APPLE__ || defined _GNU_SOURCE || ( defined _WIN32 && ( !defined NTDDI_WIN10_RS2 || NTDDI_VERSION < NTDDI_WIN10_RS2 ) ) +# if defined __ANDROID__ || defined __CYGWIN__ || defined __MINGW32__ || defined __APPLE__ || defined _GNU_SOURCE || ( defined _WIN32 && ( !defined NTDDI_WIN10_RS2 || NTDDI_VERSION < NTDDI_WIN10_RS2 ) ) # define TRACY_COLLECT_THREAD_NAMES # endif #endif diff --git a/server/TracyPopcnt.hpp b/server/TracyPopcnt.hpp index 57f5c9ee..ec8dd197 100644 --- a/server/TracyPopcnt.hpp +++ b/server/TracyPopcnt.hpp @@ -1,7 +1,7 @@ #ifndef __TRACYPOPCNT_HPP__ #define __TRACYPOPCNT_HPP__ -#ifdef _MSC_VER +#ifdef _WIN32 # include # define TracyCountBits __popcnt64 #else diff --git a/server/TracyStorage.cpp b/server/TracyStorage.cpp index b8698a72..e7b1a116 100644 --- a/server/TracyStorage.cpp +++ b/server/TracyStorage.cpp @@ -8,10 +8,10 @@ #else # include # include -# include # include # include #endif +#include #include "TracyStorage.hpp" diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 3b577faf..02db1b29 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -1,3 +1,6 @@ +#ifdef __MINGW32__ +# define __STDC_FORMAT_MACROS +#endif #include #include #include diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index 296d48e0..e9961c39 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -1,10 +1,10 @@ -#ifdef _MSC_VER +#ifdef _WIN32 # include #else # include #endif -#if defined _MSC_VER || defined __MINGW32__ +#ifdef _WIN32 # include #else # include From 36c76456f7323a31a39e79175727a7b5217b8416 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Sat, 19 Jan 2019 15:03:43 +0200 Subject: [PATCH 2/2] Fix mistakes from MingW support commit. --- client/TracyProfiler.cpp | 2 +- common/TracySystem.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 6f52afa7..de1b7be4 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -284,7 +284,7 @@ static const char* GetHostInfo() ptr += sprintf( ptr, "Compiler: unknown\n" ); #endif -#ifdef _WIN32 +#if defined _WIN32 || defined __CYGWIN__ # ifndef __CYGWIN__ InitWinSock(); # endif diff --git a/common/TracySystem.hpp b/common/TracySystem.hpp index 77917c9a..38d67095 100644 --- a/common/TracySystem.hpp +++ b/common/TracySystem.hpp @@ -2,7 +2,7 @@ #define __TRACYSYSTEM_HPP__ #ifdef TRACY_ENABLE -# if defined __ANDROID__ || defined __CYGWIN__ || defined __MINGW32__ || defined __APPLE__ || defined _GNU_SOURCE || ( defined _WIN32 && ( !defined NTDDI_WIN10_RS2 || NTDDI_VERSION < NTDDI_WIN10_RS2 ) ) +# if defined __ANDROID__ || defined __CYGWIN__ || defined __APPLE__ || defined _GNU_SOURCE || ( defined _WIN32 && ( !defined NTDDI_WIN10_RS2 || NTDDI_VERSION < NTDDI_WIN10_RS2 ) ) # define TRACY_COLLECT_THREAD_NAMES # endif #endif