#!/bin/bash
#	$Id: patch-gcc,v 1.11 2003/03/04 19:40:09 mrustad Exp $
# Modify gcc.
# Patch gcc to recognize mipsisa32-brecis-elf and mipsisa32-brecis-uclinux.

echo Patching gcc/version.c
sed -e "s/\(version_string.*\)\";/\1 `date +%Y%m%d` ${tool_build}\";/" \
	gcc/version.c > tmp-ver.c
mv gcc/version.c gcc/xversion.c
mv tmp-ver.c gcc/version.c

echo Patching config.sub
patch config.sub << "END_OF_PATCH"
*** config.sub.orig	Fri Mar 15 14:06:58 2002
--- config.sub	Fri Mar 15 14:08:02 2002
***************
*** 297,306 ****
--- 297,307 ----
  	| m88110-* | m88k-* | mcore-* \
  	| mips-* | mips16-* | mips64-* | mips64el-* | mips64orion-* \
  	| mips64orionel-* | mips64vr4100-* | mips64vr4100el-* \
  	| mips64vr4300-* | mips64vr4300el-* | mipsbe-* | mipseb-* \
  	| mipsle-* | mipsel-* | mipstx39-* | mipstx39el-* \
+ 	| mipsisa32-* \
  	| none-* | np1-* | ns16k-* | ns32k-* \
  	| orion-* \
  	| pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \
  	| powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* | ppcbe-* \
  	| pyramid-* \
END_OF_PATCH

echo Patching gcc/config.gcc
patch gcc/config.gcc << "END_OF_PATCH"
*** config.gcc.orig	Fri Mar 15 13:07:58 2002
--- config.gcc	Fri Mar 15 13:22:41 2002
***************
*** 2592,2601 ****
--- 2592,2620 ----
  	if test x$stabs = xyes; then
  		tm_file="${tm_file} dbx.h"
  	fi
  	tmake_file=mips/t-ecoff
  	;;
+ mipsisa32-brecis-uclinux*)
+ 	tm_file="elfos.h mips/isa3264.h mips/abi64.h mips/brecisuclinux.h"
+ 	xm_defines=POSIX
+ 	tmake_file=mips/t-brecisuclinux
+ 	extra_parts="crtbegin.o crtend.o"
+ 	gnu_ld=yes
+ 	gas=yes
+# 	use_collect2=yes
+ 	target_cpu_default="MASK_GAS|MASK_SPLIT_ADDR"
+ 	case x${enable_threads} in
+ 		x | xyes | xposix) thread_file='posix'
+ 		;;
+ 	esac
+ 	;;
+ mipsisa32-brecis*-elf*)
+ 	tm_file="mips/isa3264.h mips/abi64.h mips/brecis.h"
+ 	tmake_file="mips/t-isa3264 mips/t-brecis"
+ 	target_cpu_default="MASK_SOFT_FLOAT"
+ 	;;
  mipsisa32-*-elf*)
  	tm_file="mips/isa3264.h mips/abi64.h"
  	tmake_file=mips/t-isa3264
  	target_cpu_default="MASK_SOFT_FLOAT"
  	;;
END_OF_PATCH

cat > gcc/config/mips/t-brecisuclinux << "END_OF_FILE"
CONFIG2_H = $(srcdir)/config/mips/isa3264.h $(srcdir)/config/mips/elf.h \
$(srcdir)/config/mips/mips.h $(srcdir)/config/mips/brecisuclinux.h

STMP_FIXPROTO =

# Suppress building libgcc1.a, since the MIPS compiler port is complete
# and does not need anything from libgcc1.a.
LIBGCC1 =

# Don't let CTOR_LIST end up in sdata section.
CRTSTUFF_T_CFLAGS = -G 0 -Os

# Assemble startup files.
$(T)crti.o: $(srcdir)/config/mips/crti.asm $(GCC_PASSES)
	$(GCC_FOR_TARGET) $(GCC_CFLAGS) $(MULTILIB_CFLAGS) $(INCLUDES) \
	-c -o $(T)crti.o -x assembler-with-cpp $(srcdir)/config/mips/crti.asm

$(T)crtn.o: $(srcdir)/config/mips/crtn.asm $(GCC_PASSES)
	$(GCC_FOR_TARGET) $(GCC_CFLAGS) $(MULTILIB_CFLAGS) $(INCLUDES) \
	-c -o $(T)crtn.o -x assembler-with-cpp $(srcdir)/config/mips/crtn.asm

# We must build libgcc2.a with -G 0, in case the user wants to link
# without the $gp register.
TARGET_LIBGCC2_CFLAGS := -G 0 -Os

# fp-bit and dp-bit are really part of libgcc1, but this will cause
# them to be built correctly, so... [taken from t-sparclite]
# We want fine grained libraries, so use the new code to build the
# floating point emulation libraries.
FPBIT = fp-bit.c
DPBIT = dp-bit.c

dp-bit.c: $(srcdir)/config/fp-bit.c
	echo '#ifdef __MIPSEL__' > dp-bit.c
	echo '#define FLOAT_BIT_ORDER_MISMATCH' >> dp-bit.c
	echo '#endif' >> dp-bit.c
	echo '#undef US_SOFTWARE_GOFAST' >> dp-bit.c
	cat $(srcdir)/config/fp-bit.c >> dp-bit.c

fp-bit.c: $(srcdir)/config/fp-bit.c
	echo '#define FLOAT' > fp-bit.c
	echo '#ifdef __MIPSEL__' >> fp-bit.c
	echo '#define FLOAT_BIT_ORDER_MISMATCH' >> fp-bit.c
	echo '#endif' >> fp-bit.c
	echo '#undef US_SOFTWARE_GOFAST' >> fp-bit.c
	cat $(srcdir)/config/fp-bit.c >> fp-bit.c

# Build the libraries for both hard and soft floating point

MULTILIB_OPTIONS := mhard-float/msoft-float
MULTILIB_DIRNAMES := hard-float soft-float
EXTRA_MULTILIB_PARTS = crtbegin.o crtend.o # crti.o crtn.o

LIBGCC = stmp-multilib
INSTALL_LIBGCC = install-multilib

# Add additional dependencies to recompile selected modules whenever the
# tm.h file changes.  The files compiled are:
#
#	gcc.c		(*_SPEC changes)
#	toplev.c	(new switches + assembly output changes)
#	sdbout.c	(debug format changes)
#	dbxout.c	(debug format changes)
#	dwarfout.c	(debug format changes)
#	final.c		(assembly output changes)
#	varasm.c	(assembly output changes)
#	cse.c		(cost functions)
#	insn-output.c	(possible ifdef changes in tm.h)
#	regclass.c	(fixed/call used register changes)
#	cccp.c		(new preprocessor macros, -v version #)
#	explow.c	(GO_IF_LEGITIMATE_ADDRESS)
#	recog.c		(GO_IF_LEGITIMATE_ADDRESS)
#	reload.c	(GO_IF_LEGITIMATE_ADDRESS)

gcc.o: $(CONFIG2_H)
toplev.o: $(CONFIG2_H)
sdbout.o: $(CONFIG2_H)
dbxout.o: $(CONFIG2_H)
dwarfout.o: $(CONFIG2_H)
final.o: $(CONFIG2_H)
varasm.o: $(CONFIG2_H)
cse.o: $(CONFIG2_H)
insn-output.o: $(CONFIG2_H)
regclass.o: $(CONFIG2_H)
cccp.o: $(CONFIG2_H)
explow.o: $(CONFIG2_H)
recog.o: $(CONFIG2_H)
reload.o: $(CONFIG2_H)
END_OF_FILE

cat > gcc/config/mips/t-brecis << "END_OF_FILE"
CONFIG2_H := $(srcdir)/config/mips/isa3264.h $(srcdir)/config/mips/brecis.h
CONFIG2_H += $(srcdir)/config/mips/elf.h $(srcdir)/config/mips/ecoff.h
CONFIG2_H += $(srcdir)/config/mips/mips.h
 
 # Build the libraries for both hard and soft floating point
 
MULTILIB_OPTIONS = msoft-float/mhard-float
MULTILIB_DIRNAMES = soft-float hard-float
END_OF_FILE

cat > gcc/config/mips/brecis.h << "END_OF_FILE"
/*
 * BRECIS-specific modifications.
 */

#undef TARGET_VERSION
#if TARGET_ENDIAN_DEFAULT == 0
#define TARGET_VERSION fprintf (stderr, " (MIPS32el BRECIS with ELF)");
#else
#define TARGET_VERSION fprintf (stderr, " (MIPS32 BRECIS with ELF)");
#endif

END_OF_FILE

cat > gcc/config/mips/brecisuclinux.h << "END_OF_FILE"
/*
 * BRECIS uClinux-specific modifications.
 */

/* Do not assume anything about the header files. */
#undef	NO_IMPLICIT_EXTERN_C
#define NO_IMPLICIT_EXTERN_C

#undef	MIPS_ISA_DEFAULT
#define	MIPS_ISA_DEFAULT	32

#undef MULTIBYTE_CHARS
#define MULTIBYTE_CHARS 0

#undef ASM_APP_ON
#define ASM_APP_ON "#APP\n"

#undef ASM_APP_OFF
#define ASM_APP_OFF "#NO_APP\n"

#undef	MIPS_ABI_DEFAULT
#define	MIPS_ABI_DEFAULT	ABI_32

#ifndef MIPS_ENABLE_EMBEDDED_O32
#define MIPS_ENABLE_EMBEDDED_O32 1
#endif

#undef TARGET_DEFAULT
#define TARGET_DEFAULT (MASK_GAS|MASK_SPLIT_ADDR)

#undef MIPS_DEFAULT_GVALUE
#define MIPS_DEFAULT_GVALUE 8

#undef	MAX_OFILE_ALIGNMENT
#define MAX_OFILE_ALIGNMENT (4096)

#undef TARGET_VERSION
#if TARGET_ENDIAN_DEFAULT == 0
#define TARGET_VERSION fprintf (stderr, " (MIPS32el BRECIS uClinux with ELF)");
#else
#define TARGET_VERSION fprintf (stderr, " (MIPS32 BRECIS uClinux with ELF)");
#endif

/* Specify predefined symbols in preprocessor.  */
#undef CPP_PREDEFINES
#define CPP_PREDEFINES "-DMIPSEB -D_MIPSEB -D__MIPSEB__ -DBRECIS -D__BRECIS__ \
-DEMBED -Dunix -Dmips -D_mips -D_unix -D__unix__ \
-DR3000 -D_R3000 -D__gnu_linux__ -Dlinux -Asystem=posix -Acpu=mips \
-Amachine=mips -D__ELF__ -D__linux__ -D_LINUX -D_uclinux_ -D__uclinux__ \
-DNO_MM -D__NO_MM__"

#if 0
/* SUBTARGET_CC1_SPEC is passed to the compiler proper.  It may be
   overridden by subtargets.  */
#undef SUBTARGET_CC1_SPEC
#define SUBTARGET_CC1_SPEC "-march=4KC"

#undef SUBTARGET_ASM_SPEC
#define SUBTARGET_ASM_SPEC "%{mabi=64: -64} -non_shared"
#endif	/* 0 */

/* Extra switches sometimes passed to the linker.  */
/* ??? The bestGnum will never be passed to the linker, because the gcc driver
  will interpret it as a -b option.  */

#undef LINK_SPEC
#define LINK_SPEC "%(endian_spec) -q -dn -n -Bstatic \
%{G*} %{mips1} %{mips2} %{mips3} %{mips4} %{mips32} %{mips64} \
%{bestGnum} %{non_shared}"

/* Provide a STARTFILE_SPEC appropriate for GNU/Linux.  Here we add
   the GNU/Linux magical crtbegin.o file (see crtstuff.c) which
   provides part of the support for getting C++ file-scope static
   object constructed before entering `main'. */
   
#undef	STARTFILE_SPEC
#define STARTFILE_SPEC \
  "%{pg:gcrt0.o%s} %{!pg:%{p:gcrt0.o%s} \
		       %{!p:%{profile:gcrt0.o%s} \
			 %{!profile:crt0.o%s}}} \
   crti.o%s crtbegin.o%s"

/* Provide a ENDFILE_SPEC appropriate for GNU/Linux.  Here we tack on
   the GNU/Linux magical crtend.o file (see crtstuff.c) which
   provides part of the support for getting C++ file-scope static
   object constructed before entering `main', followed by a normal
   GNU/Linux "finalizer" file, `crtn.o'.  */

#undef	ENDFILE_SPEC
#define ENDFILE_SPEC	"crtend.o%s crtn.o%s"

#undef	LIB_SPEC
#define LIB_SPEC "%{pthread:-lpthread} %{profile:-lc_p} %{!profile: -lc}"

/* Define this so we can compile MS code for use with WINE.  */
#undef HANDLE_PRAGMA_PACK_PUSH_POP
#define HANDLE_PRAGMA_PACK_PUSH_POP
END_OF_FILE

echo Patching libstdc++-v3/include/c_std/std_cstdlib.h
patch libstdc++-v3/include/c_std/std_cstdlib.h << "END_OF_PATCH"
--- libstdc++-v3/include/std_cstdlib.h.orig	Sat Oct 26 20:02:05 2002
+++ libstdc++-v3/include/std_cstdlib.h	Sat Oct 26 20:05:45 2002
@@ -101,9 +101,13 @@
   using ::labs;
   using ::ldiv;
   using ::malloc;
+#ifdef	_GLIBCPP_HAVE_MBRLEN
   using ::mblen;
+#endif
+#ifdef	_GLIBCPP_HAVE_MBSRTOWCS
   using ::mbstowcs;
   using ::mbtowc;
+#endif
   using ::qsort;
   using ::rand;
   using ::realloc;
@@ -112,8 +116,10 @@
   using ::strtol;
   using ::strtoul;
   using ::system;
+#ifdef	_GLIBCPP_HAVE_WCRTOMB
   using ::wcstombs;
   using ::wctomb;
+#endif
 
   inline long 
   abs(long __i) { return labs(__i); }
END_OF_PATCH

echo GCC patches complete.

# End of patch-gcc
