newlib/winsup/utils/mingw
2008-10-13 00:56:09 +00:00

95 lines
2.8 KiB
Bash
Executable File

#!/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
dir=$(cd $(dirname $("$compiler" -print-prog-name=ld))/../..; pwd)
#
# The mingw32 directory should live somewhere close by to the
# compiler. Search for it.
#
[ "$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; }
sawshared() { return 1; }
sawnostdinc() { return 1; }
sawnostdlib() { 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|-mno-cygwin) continue ;;
-c|-E) sawcomp() { return 0; } ;;
-xc*) sawcfile() { return 0; } ;;
-isystem) eatnext() { return 0; }; continue ;;
-o) pushnext() { return 0; } ;;
-nostdinc*) sawnostdinc() { return 0; } ;;
-nostdlib) sawnostdlib() { return 0; } ;;
-shared|-Wl,-shared) sawshared() { return 0; } ;;
-*) ;;
*.cc|*.c|*.s|*.S|*.i|*.ii) sawcfile() { return 0; } ;;
*.o) sawofile() { return 0; };;
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))
sawnostdinc || set -- -isystem "$ccdir"/include -I"${mingw_dir}"/include "$@"
set -- -D__MINGW32__ -D__MSVCRT__ -DWIN32 -D_WIN32 -D__WIN32 -D__WIN32__ -U__CYGWIN__ -Uunix -U__unix__ -U__unix -U __CYGWIN32__ "$@"
if ! sawnostdinc; then
case "$compiler" in
*++*) set -- -nostdinc++ "$@" ;;
*) set -- -nostdinc "$@" ;;
esac
fi
fi
if sawofile || ! sawcfile || ! sawcomp; then
w32api=$($compiler -print-file-name=libc.a)
w32api=$(cd $(dirname "$w32api")/w32api; pwd)
set -- -Wl,-nostdlib -L"${w32api}" "$@"
! sawnostdlib && set -- -nostdlib "$@" -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lmingw32 -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt
! sawnostdlib && ! sawshared && { sawofile || sawcfile; } && set -- "$mingw_dir"/lib/crt2.o "$@"
fi
# Execute the compiler with new mingw-specific options.
exec $compiler "$@"