From 3a25d3e85ecfc7029033a948b099b53a1cb9a734 Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Fri, 11 Jul 2014 23:37:02 +0100
Subject: [PATCH 06/29] Fix building minidump processor for MinGW

- Link minidump_stackwalk, minidump_dump, microdump_stackwalk with
PTHREAD_LIBS as pthread_cancel is used

- Link minidump_stackwalk, microdump_stackwalk, binarystream_unittest with
ws2_32 as ntoh functions are used

- _s function variants should only be used if _MSC_VER, use _r variants with
MinGW (and define _POSIX_C_SOURCE to ensure they are prototyped)

 - Don't try to build upload tools, dump_syms_dwarf for MinGW

(Part of https://breakpad.appspot.com/548002 was commited as svn r1399 to
fix breakpad client compilation for MinGW

This is mainly the configure.ac and Makefile.am changes left over from
https://breakpad.appspot.com/548002/ with a bit of updating, to fix building
of the minidump processor library and tools for MinGW)

v2:
Use _POSIX_C_SOURCE not _POSIX to enable *_r() prototypes in headers

The headers seem to have changed so localtime_r() prototype is now guarded
by _POSIX_C_SOURCE

This seems right anyhow as _POSIX_C_SOURCE is what SUS defines, whereas I
think _POSIX is some left-over Interix thing?

v3:
Drop _snprintf fix, now dealt with centrally and correctly since 48673cdb

Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
---
 Makefile.am               | 20 +++++++++++++++++---
 configure.ac              |  6 ++++++
 src/processor/logging.cc  |  2 +-
 src/processor/minidump.cc |  2 +-
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8d816efc..009aa90d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -56,6 +56,11 @@ if WINDOWS_HOST
 AM_CPPFLAGS += -DNO_STABS_SUPPORT
 endif
 
+if MINGW_HOST
+SOCKET_LIBS = -lws2_32
+AM_CPPFLAGS += -D_POSIX_C_SOURCE
+endif
+
 # Specify include paths for ac macros
 ACLOCAL_AMFLAGS = -I m4
 
@@ -349,11 +354,13 @@ bin_PROGRAMS += \
 	src/processor/minidump_stackwalk
 endif !DISABLE_PROCESSOR
 
+if !MINGW_HOST
 if !DISABLE_TOOLS
 bin_PROGRAMS += \
 	src/tools/linux/symupload/minidump_upload \
 	src/tools/linux/symupload/sym_upload
 endif
+endif
 
 if LINUX_HOST
 EXTRA_PROGRAMS += \
@@ -374,11 +381,13 @@ endif
 endif LINUX_HOST
 
 if WINDOWS_HOST
+if !MINGW_HOST
 if !DISABLE_TOOLS
 bin_PROGRAMS += \
 	src/tools/windows/dump_syms_dwarf/dump_syms
 endif
 endif
+endif
 
 ## Tests
 if !DISABLE_PROCESSOR
@@ -780,6 +789,7 @@ src_tools_linux_md2core_minidump_2_core_unittest_LDADD = \
 endif LINUX_HOST
 
 if WINDOWS_HOST
+if !MINGW_HOST
 if !DISABLE_TOOLS
 src_tools_windows_dump_syms_dwarf_dump_syms_SOURCES = \
 	src/common/dwarf_cfi_to_module.cc \
@@ -797,6 +807,7 @@ src_tools_windows_dump_syms_dwarf_dump_syms_SOURCES = \
 	src/tools/windows/dump_syms_dwarf/dump_syms.cc
 endif
 endif
+endif
 
 if !DISABLE_PROCESSOR
 src_processor_address_map_unittest_SOURCES = \
@@ -1221,7 +1232,8 @@ src_processor_minidump_dump_LDADD = \
 	src/processor/logging.o \
 	src/processor/minidump.o \
 	src/processor/pathname_stripper.o \
-	src/processor/proc_maps_linux.o
+	src/processor/proc_maps_linux.o \
+	$(PTHREAD_CFLAGS) $(PTHREAD_LIBS)
 
 src_processor_microdump_stackwalk_SOURCES = \
 	src/processor/microdump_stackwalk.cc
@@ -1255,7 +1267,8 @@ src_processor_microdump_stackwalk_LDADD = \
 	src/processor/stackwalker_sparc.o \
 	src/processor/stackwalker_x86.o \
 	src/processor/tokenize.o \
-	src/third_party/libdisasm/libdisasm.a
+	src/third_party/libdisasm/libdisasm.a \
+	$(PTHREAD_CFLAGS) $(PTHREAD_LIBS) $(SOCKET_LIBS)
 
 src_processor_minidump_stackwalk_SOURCES = \
 	src/processor/minidump_stackwalk.cc
@@ -1294,7 +1307,8 @@ src_processor_minidump_stackwalk_LDADD = \
 	src/processor/stackwalker_x86.o \
 	src/processor/symbolic_constants_win.o \
 	src/processor/tokenize.o \
-	src/third_party/libdisasm/libdisasm.a
+	src/third_party/libdisasm/libdisasm.a \
+	$(PTHREAD_CFLAGS) $(PTHREAD_LIBS) $(SOCKET_LIBS)
 
 endif !DISABLE_PROCESSOR
 
diff --git a/configure.ac b/configure.ac
index 4e92b291..eec56966 100644
--- a/configure.ac
+++ b/configure.ac
@@ -113,10 +113,16 @@ case $host in
   *-*-cygwin* )
     WINDOWS_HOST=true
     ;;
+  *-*-mingw* )
+    WINDOWS_HOST=true
+    MINGW_HOST=true
+    ;;
 esac
 
 AM_CONDITIONAL(LINUX_HOST, test x$LINUX_HOST = xtrue)
+# WINDOWS_HOST means MINGW or CYGWIN
 AM_CONDITIONAL(WINDOWS_HOST, test x$WINDOWS_HOST = xtrue)
+AM_CONDITIONAL(MINGW_HOST, test x$MINGW_HOST = xtrue)
 
 # Only use Android support headers when compiling for Android
 case $host in
diff --git a/src/processor/logging.cc b/src/processor/logging.cc
index c1eebbc2..edc82f01 100644
--- a/src/processor/logging.cc
+++ b/src/processor/logging.cc
@@ -53,7 +53,7 @@ LogStream::LogStream(std::ostream &stream, Severity severity,
   time_t clock;
   time(&clock);
   struct tm tm_struct;
-#ifdef _WIN32
+#ifdef _MSC_VER
   localtime_s(&tm_struct, &clock);
 #else
   localtime_r(&clock, &tm_struct);
diff --git a/src/processor/minidump.cc b/src/processor/minidump.cc
index 81114e02..df8b640a 100644
--- a/src/processor/minidump.cc
+++ b/src/processor/minidump.cc
@@ -376,7 +376,7 @@ static void PrintValueOrInvalid(bool valid,
 // Converts a time_t to a string showing the time in UTC.
 static string TimeTToUTCString(time_t tt) {
   struct tm timestruct;
-#ifdef _WIN32
+#ifdef _MSC_VER
   gmtime_s(&timestruct, &tt);
 #else
   gmtime_r(&tt, &timestruct);
-- 
2.15.0

