From a4125753a47ed0aaaf9cfe17c60818dccd88cba3 Mon Sep 17 00:00:00 2001
From: Jason Hood <jadoxa@yahoo.com.au>
Date: Tue, 21 Nov 2017 11:46:58 +1000
Subject: [PATCH] Fix compiling with MinGW and TDM

MinGW lacks some definitions; TDM wants WINVER defined.

Assume a default `CC=cc` should really be `CC=gcc`.
---
 ANSI.c       |  1 -
 ansicon.c    |  1 -
 ansicon.h    | 33 +++++++++++++++++++++++++++++++++
 makefile.gcc | 11 ++++++++---
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/ANSI.c b/ANSI.c
index 3bcd73d..5b04cb6 100644
--- a/ANSI.c
+++ b/ANSI.c
@@ -166,7 +166,6 @@
 
 #include "ansicon.h"
 #include "version.h"
-#include <tlhelp32.h>
 #include <mmsystem.h>
 
 #ifndef SND_SENTRY
diff --git a/ansicon.c b/ansicon.c
index b4181bd..a34725c 100644
--- a/ansicon.c
+++ b/ansicon.c
@@ -94,7 +94,6 @@
 
 #include "ansicon.h"
 #include "version.h"
-#include <tlhelp32.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <io.h>
diff --git a/ansicon.h b/ansicon.h
index 4fbbf7b..a4ed9f8 100644
--- a/ansicon.h
+++ b/ansicon.h
@@ -17,7 +17,9 @@
 #else
 #define _WIN32_WINNT 0x0500	// at least Windows 2000 required
 #endif
+#define WINVER _WIN32_WINNT
 #include <windows.h>
+#include <tlhelp32.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -27,6 +29,37 @@
 #ifndef LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE
 #define LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE 0x20
 #endif
+#ifndef TH32CS_SNAPMODULE32
+#define TH32CS_SNAPMODULE32 0x10
+#endif
+#if !defined(HandleToULong) && !defined(_WIN64)
+#define HandleToULong HandleToUlong
+#endif
+
+#ifndef __IMAGE_COR20_HEADER_DEFINED__
+#define COMIMAGE_FLAGS_ILONLY	     1
+#define COMIMAGE_FLAGS_32BITREQUIRED 2
+
+// CLR 2.0 header structure.
+typedef struct IMAGE_COR20_HEADER
+{
+    DWORD                   cb;
+    WORD                    MajorRuntimeVersion;
+    WORD                    MinorRuntimeVersion;
+    IMAGE_DATA_DIRECTORY    MetaData;
+    DWORD                   Flags;
+    union {
+        DWORD               EntryPointToken;
+        DWORD               EntryPointRVA;
+    } DUMMYUNIONNAME;
+    IMAGE_DATA_DIRECTORY    Resources;
+    IMAGE_DATA_DIRECTORY    StrongNameSignature;
+    IMAGE_DATA_DIRECTORY    CodeManagerTable;
+    IMAGE_DATA_DIRECTORY    VTableFixups;
+    IMAGE_DATA_DIRECTORY    ExportAddressTableJumps;
+    IMAGE_DATA_DIRECTORY    ManagedNativeHeader;
+} IMAGE_COR20_HEADER, *PIMAGE_COR20_HEADER;
+#endif
 
 #define lenof(array) (sizeof(array)/sizeof(*(array)))
 #define TSIZE(size)  ((size) * sizeof(TCHAR))
diff --git a/makefile.gcc b/makefile.gcc
index ec090f7..394f4b4 100644
--- a/makefile.gcc
+++ b/makefile.gcc
@@ -21,6 +21,11 @@
 # * MinGW-builds x64-4.8.1-release-posix-seh-rev1.
 
 CC ?= gcc
+ifeq ($(origin CC),default)
+ifeq ($(CC),cc)
+CC = gcc
+endif
+endif
 WINDRES ?= windres
 CFLAGS = -O2 -Wall -Wno-multichar
 
@@ -101,7 +106,7 @@ x86/ansicon.exe: x86/ansicon.o $(X86OBJS) x86/procrva.o x86/ansiconv.o
 	$(LDmsg)$(CC) -m32 $+ -s -o $@ $(IVER)
 
 x86/ANSI32.dll: x86/ANSI.o $(X86OBJS) x86/ansiv.o
-	$(LDmsg)$(CC) -m32 $+ -s -o $@ -mdll -Wl,-shared,--image-base,0xAC0000
+	$(LDmsg)$(CC) -m32 $+ -lwinmm -s -o $@ -mdll -Wl,-shared,--image-base,0xAC0000
 
 x64:
 	mkdir x64
@@ -110,10 +115,10 @@ x64/ansicon.exe: x64/ansicon.o $(X64OBJS) x64/ansiconv.o
 	$(LDmsg)$(CC) -m64 $+ -s -o $@ $(IVER)
 
 x64/ANSI64.dll: x64/ANSI.o $(X64OBJS) x64/ansiv.o
-	$(LDmsg)$(CC) -m64 $+ -s -o $@ -mdll -Wl,-shared,--image-base,0xAC000000
+	$(LDmsg)$(CC) -m64 $+ -lwinmm -s -o $@ -mdll -Wl,-shared,--image-base,0xAC000000
 
 x64/ANSI32.dll: x64/ANSI32.o x64/proctype32.o x86/injdll.o x86/util.o x86/ansiv.o
-	$(LDmsg)$(CC) -m32 $+ -s -o $@ -mdll \
+	$(LDmsg)$(CC) -m32 $+ -lwinmm -s -o $@ -mdll \
 		      -Wl,-shared,--image-base,0xAC0000,--large-address-aware
 
 x86/ansicon.o:	version.h