From fd49ac277fce90dbd4987342c74d34aaa7335f74 Mon Sep 17 00:00:00 2001
From: Christopher Faylor <me@cgf.cx>
Date: Sun, 12 Oct 2008 03:06:12 +0000
Subject: [PATCH] * Makefile.in: Tweak to work better with new utils/mingw
 front-end. In utils:

---
 winsup/ChangeLog         |  4 ++
 winsup/Makefile.in       | 19 +++++----
 winsup/utils/ChangeLog   |  6 +++
 winsup/utils/Makefile.in | 11 +++--
 winsup/utils/mingw       | 89 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 114 insertions(+), 15 deletions(-)
 create mode 100755 winsup/utils/mingw

diff --git a/winsup/ChangeLog b/winsup/ChangeLog
index c6441b4bb..035cf4c1d 100644
--- a/winsup/ChangeLog
+++ b/winsup/ChangeLog
@@ -1,3 +1,7 @@
+2008-10-11  Christopher Faylor  <me+cygwin@cgf.cx>
+
+	* Makefile.in: Tweak to work better with new utils/mingw front-end.
+
 2008-05-16  Corinna Vinschen  <corinna@vinschen.de>
 
 	* Makefile.in (install-license): Install COPYING file as well.
diff --git a/winsup/Makefile.in b/winsup/Makefile.in
index 4417a9585..90f9e9ac2 100644
--- a/winsup/Makefile.in
+++ b/winsup/Makefile.in
@@ -1,5 +1,5 @@
 # Makefile.in for windows stuff
-# Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2006
+# Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2006, 2007
 # Red Hat, Inc.
 #
 # This file is part of Cygwin.
@@ -15,15 +15,19 @@ VPATH:=@srcdir@
 srcdir:=@srcdir@
 objdir:=.
 
+DESTDIR=
 target_alias:=@target_alias@
 build_alias:=@build_alias@
 host_alias:=@host_alias@
-prefix:=@prefix@
-
+prefix:=${DESTDIR}@prefix@
+exec_prefix:=${DESTDIR}@exec_prefix@
+bindir:=${DESTDIR}@bindir@
+libdir:=${DESTDIR}@libdir@
+datadir:=${DESTDIR}@datadir@
+infodir:=${DESTDIR}@infodir@
+includedir:=${DESTDIR}@includedir@
 program_transform_name:=@program_transform_name@
-exec_prefix:=@exec_prefix@
-bindir:=@bindir@
-libdir:=@libdir@
+
 ifeq ($(target_alias),$(host_alias))
 ifeq ($(build_alias),$(host_alias))
 tooldir:=$(exec_prefix)
@@ -33,9 +37,6 @@ endif
 else
 tooldir:=$(exec_prefix)/$(target_alias)
 endif
-datadir:=@datadir@
-infodir:=@infodir@
-includedir:=@includedir@
 
 INSTALL:=@INSTALL@
 INSTALL_PROGRAM:=@INSTALL_PROGRAM@
diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog
index 217923921..374cc8efc 100644
--- a/winsup/utils/ChangeLog
+++ b/winsup/utils/ChangeLog
@@ -1,3 +1,9 @@
+2008-10-11  Christopher Faylor  <me+cygwin@cgf.cx>
+
+	* mingw: New front-end script to control building of MinGW files
+	without using the deprecated -mno-cygwin option.
+	* Makefile.in: Use mingw front-end.
+
 2008-09-12  Christopher Faylor  <me+cygwin@cgf.cx>
 
 	* cygcheck.cc (pathlike::check_existence): Remove class name from
diff --git a/winsup/utils/Makefile.in b/winsup/utils/Makefile.in
index 3715f0aab..96b79edff 100644
--- a/winsup/utils/Makefile.in
+++ b/winsup/utils/Makefile.in
@@ -48,8 +48,7 @@ ALL_DEP_LDLIBS := $(cygwin_build)/libcygwin.a ${patsubst -l%,\
 MINGW_LIB        := $(mingw_build)/libmingw32.a
 MINGW_LDLIBS     := $(ALL_LDLIBS) $(MINGW_LIB)
 MINGW_DEP_LDLIBS := $(ALL_DEP_LDLIBS) $(MINGW_LIB)
-MINGW_CXX        := ${patsubst %/cygwin/include,%/mingw/include,\
-                      ${filter-out -I$(newlib_source)/%,$(COMPILE_CXX)}} -I$(updir)
+MINGW_CXX        := ${srcdir}/mingw ${CXX} -I${updir}
 
 # List all binaries to be linked in Cygwin mode.  Each binary on this list
 # must have a corresponding .o of the same name.
@@ -90,7 +89,7 @@ all: warn_dumper
 endif
 
 # Check for availability of a MinGW libz and enable for cygcheck.
-libz:=${shell x=$$($(CC) -mno-cygwin --print-file-name=libz.a); cd $$(dirname $$x); dir=$$(pwd); case "$$dir" in *mingw*) echo $$dir/libz.a ;; esac}
+libz:=${shell x=$$(${MINGW_CXX} --print-file-name=libz.a); cd $$(dirname $$x); dir=$$(pwd); case "$$dir" in *mingw*) echo $$dir/libz.a ;; esac}
 ifdef libz
 zlib_h  := -include ${patsubst %/lib/mingw/libz.a,%/include/zlib.h,${patsubst %/lib/libz.a,%/include/zlib.h,$(libz)}}
 zconf_h := ${patsubst %/zlib.h,%/zconf.h,$(zlib_h)}
@@ -129,10 +128,10 @@ endif
 # how to link a MinGW binary
 $(MINGW_BINS): %.exe: %.o
 ifdef VERBOSE
-	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${filter %.o,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
+	$(MINGW_CXX) $(MINGW_CXXFLAGS) -o $@ ${filter %.o,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
 else
-	@echo $(CXX) -o $@ ${filter %.o,$^} ${filter-out -B%, $(MINGW_CXXFLAGS) $(MINGW_LDFLAGS)};\
-	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${filter %.o,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
+	@echo $(MINGW_CXX) -o $@ ${filter %.o,$^} ${filter-out -B%, $(MINGW_CXXFLAGS) $(MINGW_LDFLAGS)};\
+	$(MINGW_CXX) $(MINGW_CXXFLAGS) -o $@ ${filter %.o,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
 endif
 
 # how to link a Cygwin binary
diff --git a/winsup/utils/mingw b/winsup/utils/mingw
new file mode 100755
index 000000000..0c7b9b469
--- /dev/null
+++ b/winsup/utils/mingw
@@ -0,0 +1,89 @@
+#!/bin/sh
+# Front-end kludge to cause a Cygwin gcc to default to
+# an installed version of the MinGW components.
+
+#
+# Find the path to the compiler
+#
+compiler=$1; shift
+case "$compiler" in
+	*/*)	;;
+	*)	compiler=($(type "$compiler"))
+		compiler=${compiler[2]} ;;
+esac
+
+#
+# The mingw32 directory should live somewhere close by to the
+# compiler.  Search for it.
+#
+updir=$(dirname "$compiler")
+dir=$(cd "$updir"/..; pwd)
+[ "$dir" = '/' ] && dir=''
+mingw_dir=''
+for d in "$dir"/*-mingw32 "$dir"/usr/*-mingw32 "$dir"/*-mingw* "$dir"/usr/*-mingw*; do
+    case "$d" in
+	*\**)	continue ;;
+	*)	mingw_dir=$d; break;
+    esac
+done
+
+if [ -z "$mingw_dir" ]; then
+    echo "$0: couldn't find i686-pc-mingw32 directory" 1>&2
+    exit 1
+fi
+
+#
+# Inspect each argument throwing away ones that seem to try to include the
+# Cygwin environment.
+#
+newargs=()
+sawcomp() { return 1; }
+sawcfile() { return 1; }
+sawofile() { return 1; }
+eatnext() { return 1; }
+pushnext() { return 1; }
+for f do
+    if eatnext; then
+	eatnext() { return 1; }
+	continue;
+    fi
+    if pushnext; then
+	pushnext() { return 1; }
+    else
+	case "$f" in
+	    *cygwin/include*|*newlib*) continue ;;
+	    -c|-E)		sawcomp() { return 0; } ;;
+	    -isystem)	eatnext() { return 0; }; continue ;;
+	    -o)		pushnext() { return 0; } ; /bin/true ;;
+	    -*)		;;
+	    *.cc|*.c)	sawcfile() { return 0; } ;;
+	    *.o)		sawofile() { return 0; }; /bin/true  ;;
+	esac
+    fi
+    newargs[${#newargs[*]}]="$f"
+done
+
+# Set up a new set of arguments + also search the installed mingw
+# directory.
+set -- -B"$mingw_dir"/lib/ "${newargs[@]}"
+
+# Add some default options depending on whether this looks like
+# an attempt to link, compile, or both.
+if sawcomp || sawcfile; then
+    ccdir=$(dirname $($compiler -print-libgcc-file-name))
+    set -- -isystem "$ccdir"/include -D__MINGW32__ -D__MSVCRT__ -DWIN32 -D_WIN32 -D__WIN32 -D__WIN32__ -U__CYGWIN__ -Uunix -U__unix__ -U__unix -U __CYGWIN32__ -I"${mingw_dir}"/include "$@"
+    case "$compiler" in
+	*++*) set -- -nostdinc++ "$@" ;;
+	*) set -- -nostdinc "$@" ;;
+    esac
+fi
+
+if sawofile || ! sawcfile || ! sawcomp; then
+    w32api=$($compiler -print-file-name=libc.a)
+    w32api=$(cd $(dirname "$w32api")/w32api; pwd)
+    set -- "$@" -nostdlib -Wl,-nostdlib -L"$w32api" -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lmingw32 -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt
+    { sawofile || sawcfile; } && set -- "$mingw_dir"/lib/crt2.o "$@"
+fi
+
+# Execute the compiler with new mingw-specific options.
+exec $compiler "$@"