diff --unified '--color=auto' -r gnuplot-5.4.0.orig/config/mingw/Makefile gnuplot-5.4.0/config/mingw/Makefile
--- gnuplot-5.4.0.orig/config/mingw/Makefile	2020-04-10 09:15:40.000000000 +0530
+++ gnuplot-5.4.0/config/mingw/Makefile	2020-09-06 20:38:04.140186600 +0530
@@ -784,19 +784,19 @@
 $(HELPFILE): $(BUILDHELPFILE)
 	$(CP) $^ $@
 
-htmlhelp.def: /c/Windows/System32/hhctrl.ocx
-ifndef MINGW64
-	pexports $^ > $@
-else
-	gendef - $^ > $@
-endif
+#htmlhelp.def: /c/Windows/System32/hhctrl.ocx
+#ifndef MINGW64
+#	pexports $^ > $@
+#else
+#	gendef - $^ > $@
+#endif
 
-prntvpt.def: /c/Windows/System32/prntvpt.dll
-ifndef MINGW64
-	pexports $^ > $@
-else
-	gendef - $^ > $@
-endif
+#prntvpt.def: /c/Windows/System32/prntvpt.dll
+#ifndef MINGW64
+#	pexports $^ > $@
+#else
+#	gendef - $^ > $@
+#endif
 
 # Working directory for patched Japanese sources 
 JA = ja/
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/configure.ac gnuplot-5.4.0/configure.ac
--- gnuplot-5.4.0.orig/configure.ac	2020-07-12 05:59:47.000000000 +0530
+++ gnuplot-5.4.0/configure.ac	2020-09-06 20:04:48.475405900 +0530
@@ -37,6 +37,8 @@
 AC_C_STRINGIZE
 AC_PROG_LN_S
 
+AC_CANONICAL_HOST
+
 if test "${build}" != "${host}"
 then
   CC=${CC-${host_alias-gcc}}
@@ -50,6 +52,8 @@
   CC_FOR_BUILD="${CC}"
   AC_PROG_CC
 
+  build_mingw=no
+
   # We must set the default linker to the linker used by gcc for the correct
   # operation of libtool.  If LD is not defined and we are using gcc, try to
   # set the LD default to the ld used by gcc.
@@ -59,7 +63,10 @@
     then
       case $build in
       *-*-mingw*)
-        gcc_prog_ld=`$CC -print-prog-name=ld 2>&1 | tr -d '\015'` ;;
+        gcc_prog_ld=`$CC -print-prog-name=ld 2>&1 | tr -d '\015'`
+        AC_MSG_NOTICE([MINGW environment detected, executing MINGW type build])
+        #AC_CONFIG_LINKS([src/config.h:config/config.mgw])
+        build_mingw=yes ;;
       *)
         gcc_prog_ld=`$CC -print-prog-name=ld 2>&1` ;;
       esac
@@ -955,6 +962,8 @@
 
 AM_CONDITIONAL(BUILD_GPCAIRO, test "${build_gpcairo}" = yes)
 
+AM_CONDITIONAL(BUILD_MINGW, test "${build_mingw}" = yes)
+
 dnl Enable subsystem to generate statistical summary of file contents
 AC_ARG_ENABLE(stats,dnl
 [  --disable-stats         Omit calculation of statistical summary of data],
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/demo/plugin/Makefile.am gnuplot-5.4.0/demo/plugin/Makefile.am
--- gnuplot-5.4.0.orig/demo/plugin/Makefile.am	2017-12-22 23:02:30.000000000 +0530
+++ gnuplot-5.4.0/demo/plugin/Makefile.am	2020-09-06 20:05:12.659453700 +0530
@@ -4,7 +4,7 @@
 noinst_PROGRAMS = demo_plugin.so
 
 demo_plugin_so_SOURCES = demo_plugin.c gnuplot_plugin.h
-demo_plugin_so_CFLAGS = -rdynamic -fPIC -I$(top_srcdir)/src
+demo_plugin_so_CFLAGS = -fPIC -I$(top_srcdir)/src
 demo_plugin_so_LDFLAGS = -shared
 
 # Note: Solaris SunPro C compiler needs these flags instead
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/docs/Makefile.am gnuplot-5.4.0/docs/Makefile.am
--- gnuplot-5.4.0.orig/docs/Makefile.am	2020-04-10 22:24:28.000000000 +0530
+++ gnuplot-5.4.0/docs/Makefile.am	2020-09-06 20:03:02.506859700 +0530
@@ -50,6 +50,11 @@
 
 AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/term
 
+if BUILD_MINGW
+AM_CPPFLAGS += -DWINDOWS_NO_GUI
+dist_doc_DATA = gnuplot.pdf
+endif
+
 # Documentation will be prepared for all terminals in CORETERM
 # even if the terminal itself is not included in the build.
 
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/Makefile.am gnuplot-5.4.0/Makefile.am
--- gnuplot-5.4.0.orig/Makefile.am	2020-03-31 22:58:16.000000000 +0530
+++ gnuplot-5.4.0/Makefile.am	2020-09-06 20:01:49.907467200 +0530
@@ -7,6 +7,8 @@
 PATCHLEVEL PGPKEYS README RELEASE_NOTES \
 configure.vms win
 
+dist_doc_DATA = FAQ.pdf README RELEASE_NOTES Copyright NEWS BUGS
+
 DISTCLEANFILES = stamp-h
 
 ACLOCAL_AMFLAGS = -I m4
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/src/command.c gnuplot-5.4.0/src/command.c
--- gnuplot-5.4.0.orig/src/command.c	2020-06-11 03:03:34.000000000 +0530
+++ gnuplot-5.4.0/src/command.c	2020-09-06 19:57:28.950915200 +0530
@@ -112,7 +112,7 @@
 int thread_rl_RetCode = -1; /* return code from readline in a thread */
 #endif /* OS2_IPC */
 
-#ifndef _WIN32
+#if !defined (_WIN32) || defined (__MINGW32__)
 # include "help.h"
 #endif
 
@@ -2839,7 +2839,7 @@
 
 
 #ifdef NO_GIH
-#ifdef _WIN32
+#if defined (_WIN32) && !defined (__MINGW32__)
 void
 help_command()
 {
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/src/Makefile.am gnuplot-5.4.0/src/Makefile.am
--- gnuplot-5.4.0.orig/src/Makefile.am	2020-03-31 22:58:16.000000000 +0530
+++ gnuplot-5.4.0/src/Makefile.am	2020-09-06 20:06:11.275225300 +0530
@@ -93,6 +93,27 @@
 # Hercules and original pc graphics driver code
 # corgraph.asm header.mac hrcgraph.asm pcgraph.asm lineproc.mac
 
+# MINGW specific build section
+if BUILD_MINGW
+
+AM_CPPFLAGS += -DHAVE_GDIPLUS -DHAVE_DWRITE -DHAVE_D2D -DHAVE_D2D11 -DHAVE_PRNTVPT -DWIN32
+AM_CPPFLAGS += -O2 -pipe -DUNICODE -D_UNICODE -Wno-unused-function
+AM_CPPFLAGS += -D_Windows -DHAVE_CONFIG_H
+AM_CPPFLAGS += -D__USE_MINGW_ANSI_STDIO=1
+AM_CPPFLAGS += -DUSE_MOUSE=1 -DWIN_IPC
+
+AM_LDFLAGS = 
+
+gnuplot_SOURCES += win/winmain.c win/wgnuplib.c win/wgraph.c win/wprinter.c \
+win/wpause.c win/wgdiplus.cpp win/wd2d.cpp win/wtext.h plot.h\
+gpexecute.c
+
+gnuplot_CPPFLAGS = $(AM_CPPFLAGS) -DPIPES -DWGP_CONSOLE
+gnuplot_LDADD += -lkernel32 -lgdi32 -lwinspool -lcomdlg32 -lcomctl32 \
+ -ladvapi32 -lshell32 -lmsimg32 -lgdiplus -lshlwapi -ld2d1 -ld3d11 -ldwrite \
+ -lprntvpt -lwindowscodecs -lole32 -lgobject-2.0
+gnuplot_LDFLAGS = $(AM_LDFLAGS) -mconsole
+
 if BUILD_WXWIDGETS
 gnuplot_SOURCES += wxterminal/wxt_gui.cpp
 endif
@@ -103,6 +122,41 @@
 
 if BUILD_BITMAP
 gnuplot_SOURCES += bitmap.c
+
+#wgnuplot section
+bin_PROGRAMS += wgnuplot
+
+menudir = $(prefix)/bin
+menu_DATA = win/wgnuplot.mnu
+
+wgnuplot_SOURCES = $(gnuplot_SOURCES)
+wgnuplot_SOURCES += win/wtext.c win/screenbuf.c win/wmenu.c win/wredirect.cpp
+wgnuplot_CPPFLAGS = $(AM_CPPFLAGS) -DPIPES
+wgnuplot_LDADD = $(gnuplot_LDADD) wgplt_res.$(O)
+wgnuplot_LDFLAGS = $(AM_LDFLAGS) -mwindows --enable-auto-import
+
+#wgnuplot_pipes section
+bin_PROGRAMS += wgnuplot_pipes
+
+wgnuplot_pipes_SOURCES = $(gnuplot_SOURCES)
+wgnuplot_pipes_SOURCES += win/wtext.c win/screenbuf.c win/wmenu.c win/wredirect.cpp
+wgnuplot_pipes_CPPFLAGS = $(AM_CPPFLAGS) -DPIPES
+wgnuplot_pipes_LDADD = $(gnuplot_LDADD) wgplt_res.$(O)
+wgnuplot_pipes_LDFLAGS = $(AM_LDFLAGS) -mconsole
+
+RCFLAGS = --include-dir=$(srcdir)/win \
+	--define __WIN32__ --define __WIN95__ \
+	--define __GNUWIN32__ --use-temp-file
+RCOUT = wgplt_res.$(O)
+RES2COFF = echo wgplt_res.$(O)
+
+wgplt_res.$(O): win/wgnuplot.rc win/wgnuplib.rc win/wresourc.h win/texticon.ico win/grpicon.ico win/wgnuplot.exe.manifest win/wgnuplot.exe.manifest64
+	echo Making RESOURCE FILE
+	windres $(RCFLAGS) $< wgplt_res.$(O)
+	echo wgplt_res.$(O)
+
+endif
+
 endif
 
 if DEVELOPMENT_VERSION
diff --unified '--color=auto' -r gnuplot-5.4.0.orig/src/win/winmain.c gnuplot-5.4.0/src/win/winmain.c
--- gnuplot-5.4.0.orig/src/win/winmain.c	2020-04-10 22:24:28.000000000 +0530
+++ gnuplot-5.4.0/src/win/winmain.c	2020-09-06 19:58:17.437576400 +0530
@@ -368,7 +368,8 @@
     else
 	lang = GetLanguageCode();
 
-    filename = (LPTSTR) malloc((_tcslen(szModuleName) + _tcslen(name) + _tcslen(lang) + _tcslen(ext) + 1) * sizeof(TCHAR));
+    //filename = (LPTSTR) malloc((_tcslen(szModuleName) + _tcslen(name) + _tcslen(lang) + _tcslen(ext) + 1) * sizeof(TCHAR));
+    filename = (LPTSTR) malloc(1024 * sizeof(TCHAR));
     if (filename) {
 	_tcscpy(filename, szModuleName);
 	_tcscat(filename, name);
