# Makefile fragment for AmigaOS target.

# We generate four additional things:
#
# libb/libgcc.a
#	A base relative version of libgcc.a which is used when compiling and
#	linking with the '-resident' option.
#
# libb/libm020/libgcc.a
#	An accelerated base relative version of libgcc.a which is used when
#	compiling and linking with the '-mc68020' & '-resident' options.
#
# libm020/libgcc.a
#	An accelerated version of libgcc.a which is used when compiling and
#	linking with the '-mc68020' option.
#
# libb32/libm020/libgcc.a
#	A base relative version of libgcc.a which is used when compiling and
#	linking with the '-resident32' and '-mc68020' flags.
#

XGCC = xgcc $(PIPE)
GCC_FOR_TARGET = ./$(XGCC) -B./

# The standard additional target flags for the compiler.

T_CFLAGS =

# Each compilation environment (Manx, Dice, GCC, SAS/C, etc) provides its
# own equivalent of the UNIX /usr/include tree.  For gcc, the standard headers
# are in /gg/include and system specific headers are in /gg/os-include.
# Use these paths for fixincludes.

SYSTEM_HEADER_DIR = /gg/include
OTHER_FIXINCLUDES_DIRS = /gg/os-include

# Extra objects that get compiled and linked to collect2

EXTRA_COLLECT2_OBJS = amigacollect2.o

# Build supplimentary AmigaOS target support file for collect2
amigacollect2.o: amigacollect2.c
	$(CC) -c $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
	-DA2IXDIR_PREFIX=\"$(prefix)/share/a2ixlibrary\" $(srcdir)/amigacollect2.c

# We don't need a libgcc1, it's all in ixemul.library

LIBGCC1 = libgcc1.null
CROSS_LIBGCC1 = libgcc1.null

# Flags to use when compiling the base relative version of libgcc.a.
# Explicitly force -resident in the compilation flags.

LIBBGCC2_CFLAGS = $(LIBGCC2_CFLAGS) -resident

# Flags to use when compiling the 68020 versions of libgcc.a.
# Explicitly force -mc68020 in the compilation flags.

LIBM020GCC2_CFLAGS = $(LIBGCC2_CFLAGS) -mc68020

# Flags to use when compiling the base relative 68020 versions of libgcc.a.
# Explicitly force -mc68020 & -resident in the compilation flags.

LIBBM020GCC2_CFLAGS = $(LIBGCC2_CFLAGS) -resident -mc68020

# Flags to use when compiling the large base relative 68020 versions of libgcc.a.
# Explicitly force -mc68020 & -resident32 in the compilation flags.

LIBB32M020GCC2_CFLAGS = $(LIBGCC2_CFLAGS) -resident32 -mc68020

# Additional target dependent options for compiling libgcc.a.  This just
# ensures that we don't compile libgcc* with anything other than a
# fixed stack.

TARGET_LIBGCC2_CFLAGS = -mfixedstack

# Build the base relative library and accelerated libraries.
# It is later copied into /gg/lib/gcc-lib/m68k-amigaos/<version>/libb/libgcc.a, whereas
# libgcc.a is copied into /gg/lib/gcc-lib/m68k-amigaos/<version>/libgcc.a.
# Accelerated libraries are copied into same directories, with an extra dir level
# libm020, thus libb/libm020. It doesn't work very well to define one of the
# EXTRA_* macros to contain libb/libgcc.a, particularly for doing "make stageN"
# or "make install".

LIBGCC = libgcc.a libb/libgcc.a libm020/libgcc.a libb/libm020/libgcc.a \
	 libb32/libm020/libgcc.a

# Add install_libbgcc to normal define of INSTALL_LIBGCC.

INSTALL_LIBGCC = install-libgcc install-libbgcc install-libm020gcc \
		 install-libbm020gcc install-libb32m020gcc

# This includes the knowledge that target amigaos doesn't need libgcc1.a

libb/libgcc.a: libgcc1.null libgcc2.c libgcc2.ready $(CONFIG_H) \
   $(LIB2FUNCS_EXTRA) machmode.h longlong.h gbl-ctors.h config.status
# Actually build it in tmplibbgcc.a, then rename at end,
# so that libb/libgcc.a itself remains nonexistent if compilation is aborted.
	-rm -f tmplibbgcc.a
# -e causes any failing command to make this rule fail.
# -e doesn't work in certain shells, so we test $$? as well.
# lynx has a broken ar, it always complains when the initial library is
# empty, thus this command works only if we don't do -e
# There is a trailing backslash (\) deleted from the following line.
#	set -e;
	for name in $(LIB2FUNCS); \
	do \
	  echo $${name}; \
	  $(GCC_FOR_TARGET) $(LIBBGCC2_CFLAGS) $(INCLUDES) -c -DL$${name} \
	      $(srcdir)/libgcc2.c -o $${name}$(objext); \
	  if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	  $(AR) $(AR_FLAGS) tmplibbgcc.a $${name}$(objext); \
	  rm -f $${name}$(objext); \
	done
# Some shells crash when a loop has no items.
# So make sure there is always at least one--`..'.
# Then ignore it.
# We don't use -e here because there are if statements
# that should not make the command give up when the if condition is false.
# Instead, we test for failure after each command where it matters.
	for file in .. $(LIB2FUNCS_EXTRA); \
	do \
	  if [ x$${file} != x.. ]; then \
	    name=`echo $${file} | sed -e 's/[.][cS]$$//' -e 's/[.]asm$$//'`; \
	    oname=` echo $${name} | sed -e 's,.*/,,'`; \
	    echo $${name}; \
	    if [ $${name}.asm = $${file} ]; then \
	      cp $${file} $${name}.s || exit 1; file=$${name}.s; \
	    else true; fi; \
	    $(GCC_FOR_TARGET) $(LIBBGCC2_CFLAGS) $(INCLUDES) -c $${file}; \
	    if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	    $(AR) $(AR_FLAGS) tmplibbgcc.a $${oname}$(objext); \
	    rm -f $${name}.s $${oname}$(objext); \
	  else true; \
	  fi; \
	done
	-if $(RANLIB_TEST) ; then $(RANLIB) tmplibbgcc.a; else true; fi
	-if [ -d libb ] ; then true ; else mkdir libb ; fi
	mv tmplibbgcc.a libb/libgcc.a


install-libbgcc: libb/libgcc.a install-dir
	-if [ -d $(libsubdir)/libb ] ; then true ; else mkdir $(libsubdir)/libb ; fi
	-if [ -f libb/libgcc.a ] ; then \
	  rm -f $(libsubdir)/libb/libgcc.a; \
	  $(INSTALL_DATA) libb/libgcc.a $(libsubdir)/libb/libgcc.a; \
	  if $(RANLIB_TEST) ; then \
	    (cd $(libsubdir)/libb; $(RANLIB) libgcc.a); else true; fi; \
	  chmod a-x $(libsubdir)/libb/libgcc.a; \
	else true; fi

libm020/libgcc.a: libgcc1.null libgcc2.c libgcc2.ready $(CONFIG_H) \
   $(LIB2FUNCS_EXTRA) machmode.h longlong.h gbl-ctors.h config.status
# Actually build it in tmplibm020gcc.a, then rename at end,
# so that libm020/libgcc.a itself remains nonexistent if compilation is aborted.
	-rm -f tmplibm020gcc.a
# -e causes any failing command to make this rule fail.
# -e doesn't work in certain shells, so we test $$? as well.
# lynx has a broken ar, it always complains when the initial library is
# empty, thus this command works only if we don't do -e
# There is a trailing backslash (\) deleted from the following line.
#	set -e;
	for name in $(LIB2FUNCS); \
	do \
	  echo $${name}; \
	  $(GCC_FOR_TARGET) $(LIBM020GCC2_CFLAGS) $(INCLUDES) -c -DL$${name} \
	      $(srcdir)/libgcc2.c -o $${name}$(objext); \
	  if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	  $(AR) $(AR_FLAGS) tmplibm020gcc.a $${name}$(objext); \
	  rm -f $${name}$(objext); \
	done
# Some shells crash when a loop has no items.
# So make sure there is always at least one--`..'.
# Then ignore it.
# We don't use -e here because there are if statements
# that should not make the command give up when the if condition is false.
# Instead, we test for failure after each command where it matters.
	for file in .. $(LIB2FUNCS_EXTRA); \
	do \
	  if [ x$${file} != x.. ]; then \
	    name=`echo $${file} | sed -e 's/[.][cS]$$//' -e 's/[.]asm$$//'`; \
	    oname=` echo $${name} | sed -e 's,.*/,,'`; \
	    echo $${name}; \
	    if [ $${name}.asm = $${file} ]; then \
	      cp $${file} $${name}.s || exit 1; file=$${name}.s; \
	    else true; fi; \
	    $(GCC_FOR_TARGET) $(LIBM020GCC2_CFLAGS) $(INCLUDES) -c $${file}; \
	    if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	    $(AR) $(AR_FLAGS) tmplibm020gcc.a $${oname}$(objext); \
	    rm -f $${name}.s $${oname}$(objext); \
	  else true; \
	  fi; \
	done
	-if $(RANLIB_TEST) ; then $(RANLIB) tmplibm020gcc.a; else true; fi
	-if [ -d libm020 ] ; then true ; else mkdir libm020 ; fi
	mv tmplibm020gcc.a libm020/libgcc.a


install-libm020gcc: libm020/libgcc.a install-dir
	-if [ -d $(libsubdir)/libm020 ] ; then true ; else mkdir $(libsubdir)/libm020 ; fi
	-if [ -f libm020/libgcc.a ] ; then \
	  rm -f $(libsubdir)/libm020/libgcc.a; \
	  $(INSTALL_DATA) libm020/libgcc.a $(libsubdir)/libm020/libgcc.a; \
	  if $(RANLIB_TEST) ; then \
	    (cd $(libsubdir)/libm020; $(RANLIB) libgcc.a); else true; fi; \
	  chmod a-x $(libsubdir)/libm020/libgcc.a; \
	else true; fi

libb/libm020/libgcc.a: libgcc1.null libgcc2.c libgcc2.ready $(CONFIG_H) \
   $(LIB2FUNCS_EXTRA) machmode.h longlong.h gbl-ctors.h config.status
# Actually build it in tmplibbm020gcc.a, then rename at end,
# so that libb/libm020/libgcc.a itself remains nonexistent if compilation is aborted.
	-rm -f tmplibbm020gcc.a
# -e causes any failing command to make this rule fail.
# -e doesn't work in certain shells, so we test $$? as well.
# lynx has a broken ar, it always complains when the initial library is
# empty, thus this command works only if we don't do -e
# There is a trailing backslash (\) deleted from the following line.
#	set -e;
	for name in $(LIB2FUNCS); \
	do \
	  echo $${name}; \
	  $(GCC_FOR_TARGET) $(LIBBM020GCC2_CFLAGS) $(INCLUDES) -c -DL$${name} \
	      $(srcdir)/libgcc2.c -o $${name}$(objext); \
	  if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	  $(AR) $(AR_FLAGS) tmplibbm020gcc.a $${name}$(objext); \
	  rm -f $${name}$(objext); \
	done
# Some shells crash when a loop has no items.
# So make sure there is always at least one--`..'.
# Then ignore it.
# We don't use -e here because there are if statements
# that should not make the command give up when the if condition is false.
# Instead, we test for failure after each command where it matters.
	for file in .. $(LIB2FUNCS_EXTRA); \
	do \
	  if [ x$${file} != x.. ]; then \
	    name=`echo $${file} | sed -e 's/[.][cS]$$//' -e 's/[.]asm$$//'`; \
	    oname=` echo $${name} | sed -e 's,.*/,,'`; \
	    echo $${name}; \
	    if [ $${name}.asm = $${file} ]; then \
	      cp $${file} $${name}.s || exit 1; file=$${name}.s; \
	    else true; fi; \
	    $(GCC_FOR_TARGET) $(LIBBM020GCC2_CFLAGS) $(INCLUDES) -c $${file}; \
	    if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	    $(AR) $(AR_FLAGS) tmplibbm020gcc.a $${oname}$(objext); \
	    rm -f $${name}.s $${oname}$(objext); \
	  else true; \
	  fi; \
	done
	-if $(RANLIB_TEST) ; then $(RANLIB) tmplibbm020gcc.a; else true; fi
	-if [ -d libb ] ; then true ; else mkdir libb ; fi
	-if [ -d libb/libm020 ] ; then true ; else mkdir libb/libm020 ; fi
	mv tmplibbm020gcc.a libb/libm020/libgcc.a


install-libbm020gcc: libb/libm020/libgcc.a install-dir
	-if [ -d $(libsubdir)/libb ] ; then true ; else mkdir $(libsubdir)/libb ; fi
	-if [ -d $(libsubdir)/libb/libm020 ] ; then true ; else mkdir $(libsubdir)/libb/libm020 ; fi
	-if [ -f libb/libm020/libgcc.a ] ; then \
	  rm -f $(libsubdir)/libb/libm020/libgcc.a; \
	  $(INSTALL_DATA) libb/libm020/libgcc.a $(libsubdir)/libb/libm020/libgcc.a; \
	  if $(RANLIB_TEST) ; then \
	    (cd $(libsubdir)/libb/libm020; $(RANLIB) libgcc.a); else true; fi; \
	  chmod a-x $(libsubdir)/libb/libm020/libgcc.a; \
	else true; fi

libb32/libm020/libgcc.a: libgcc1.null libgcc2.c libgcc2.ready $(CONFIG_H) \
   $(LIB2FUNCS_EXTRA) machmode.h longlong.h gbl-ctors.h config.status
# Actually build it in tmplibb32m020gcc.a, then rename at end,
# so that libb32/libm020/libgcc.a itself remains nonexistent if compilation is aborted.
	-rm -f tmplibb32m020gcc.a
# -e causes any failing command to make this rule fail.
# -e doesn't work in certain shells, so we test $$? as well.
# lynx has a broken ar, it always complains when the initial library is
# empty, thus this command works only if we don't do -e
# There is a trailing backslash (\) deleted from the following line.
#	set -e;
	for name in $(LIB2FUNCS); \
	do \
	  echo $${name}; \
	  $(GCC_FOR_TARGET) $(LIBB32M020GCC2_CFLAGS) $(INCLUDES) -c -DL$${name} \
	      $(srcdir)/libgcc2.c -o $${name}$(objext); \
	  if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	  $(AR) $(AR_FLAGS) tmplibb32m020gcc.a $${name}$(objext); \
	  rm -f $${name}$(objext); \
	done
# Some shells crash when a loop has no items.
# So make sure there is always at least one--`..'.
# Then ignore it.
# We don't use -e here because there are if statements
# that should not make the command give up when the if condition is false.
# Instead, we test for failure after each command where it matters.
	for file in .. $(LIB2FUNCS_EXTRA); \
	do \
	  if [ x$${file} != x.. ]; then \
	    name=`echo $${file} | sed -e 's/[.][cS]$$//' -e 's/[.]asm$$//'`; \
	    oname=` echo $${name} | sed -e 's,.*/,,'`; \
	    echo $${name}; \
	    if [ $${name}.asm = $${file} ]; then \
	      cp $${file} $${name}.s || exit 1; file=$${name}.s; \
	    else true; fi; \
	    $(GCC_FOR_TARGET) $(LIBB32M020GCC2_CFLAGS) $(INCLUDES) -c $${file}; \
	    if [ $$? -eq 0 ] ; then true; else exit 1; fi; \
	    $(AR) $(AR_FLAGS) tmplibb32m020gcc.a $${oname}$(objext); \
	    rm -f $${name}.s $${oname}$(objext); \
	  else true; \
	  fi; \
	done
	-if $(RANLIB_TEST) ; then $(RANLIB) tmplibb32m020gcc.a; else true; fi
	-if [ -d libb32 ] ; then true ; else mkdir libb32 ; fi
	-if [ -d libb32/libm020 ] ; then true ; else mkdir libb32/libm020 ; fi
	mv tmplibb32m020gcc.a libb32/libm020/libgcc.a


install-libb32m020gcc: libb32/libm020/libgcc.a install-dir
	-if [ -d $(libsubdir)/libb32 ] ; then true ; else mkdir $(libsubdir)/libb32 ; fi
	-if [ -d $(libsubdir)/libb32/libm020 ] ; then true ; else mkdir $(libsubdir)/libb32/libm020 ; fi
	-if [ -f libb32/libm020/libgcc.a ] ; then \
	  rm -f $(libsubdir)/libb32/libm020/libgcc.a; \
	  $(INSTALL_DATA) libb32/libm020/libgcc.a $(libsubdir)/libb32/libm020/libgcc.a; \
	  if $(RANLIB_TEST) ; then \
	    (cd $(libsubdir)/libb32/libm020; $(RANLIB) libgcc.a); else true; fi; \
	  chmod a-x $(libsubdir)/libb32/libm020/libgcc.a; \
	else true; fi

# When making one of the stage<N> dirs, we need to make subdirs for
# additional libraries, and copy them there. base-relative libraries
# and accelerated libraries are preserved through stages.

EXTRA_STAGE1_TARGETS = stage1-libamiga
EXTRA_STAGE2_TARGETS = stage2-libamiga
EXTRA_STAGE3_TARGETS = stage3-libamiga
EXTRA_STAGE4_TARGETS = stage4-libamiga

stage1-libamiga:
	-if [ -d stage1 ] ; then true ; else mkdir stage1 ; fi
	-if [ -d stage1/libb ] ; then true ; else mkdir stage1/libb ; fi
	-if [ -d stage1/libb32 ] ; then true ; else mkdir stage1/libb32 ; fi
	-if [ -d stage1/libb/libm020 ] ; then true ; else mkdir stage1/libb/libm020 ; fi
	-if [ -d stage1/libm020 ] ; then true ; else mkdir stage1/libm020 ; fi
	-if [ -d stage1/libb32/libm020 ] ; then true ; else mkdir stage1/libb32/libm020 ; fi
	-cp libb/libgcc.a stage1/libb/libgcc.a
	-cp libb/libm020/libgcc.a stage1/libb/libm020/libgcc.a
	-cp libm020/libgcc.a stage1/libm020/libgcc.a
	-cp libb32/libm020/libgcc.a stage1/libb32/libm020/libgcc.a
	-if $(RANLIB_TEST) ; then $(RANLIB) stage1/libb/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage1/libb/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage1/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage1/libb32/libm020/libgcc.a; else true; fi

stage2-libamiga:
	-if [ -d stage2 ] ; then true ; else mkdir stage2 ; fi
	-if [ -d stage2/libb ] ; then true ; else mkdir stage2/libb ; fi
	-if [ -d stage2/libb32 ] ; then true ; else mkdir stage2/libb32 ; fi
	-if [ -d stage2/libb/libm020 ] ; then true ; else mkdir stage2/libb/libm020 ; fi
	-if [ -d stage2/libm020 ] ; then true ; else mkdir stage2/libm020 ; fi
	-if [ -d stage2/libb32/libm020 ] ; then true ; else mkdir stage2/libb32/libm020 ; fi
	-cp libb/libgcc.a stage2/libb/libgcc.a
	-cp libb/libm020/libgcc.a stage2/libb/libm020/libgcc.a
	-cp libm020/libgcc.a stage2/libm020/libgcc.a
	-cp libb32/libm020/libgcc.a stage2/libb32/libm020/libgcc.a
	-if $(RANLIB_TEST) ; then $(RANLIB) stage2/libb/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage2/libb/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage2/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage2/libb32/libm020/libgcc.a; else true; fi

stage3-libamiga:
	-if [ -d stage3 ] ; then true ; else mkdir stage3 ; fi
	-if [ -d stage3/libb ] ; then true ; else mkdir stage3/libb ; fi
	-if [ -d stage3/libb32 ] ; then true ; else mkdir stage3/libb32 ; fi
	-if [ -d stage3/libb/libm020 ] ; then true ; else mkdir stage3/libb/libm020 ; fi
	-if [ -d stage3/libm020 ] ; then true ; else mkdir stage3/libm020 ; fi
	-if [ -d stage3/libb32/libm020 ] ; then true ; else mkdir stage3/libb32/libm020 ; fi
	-cp libb/libgcc.a stage3/libb/libgcc.a
	-cp libb/libm020/libgcc.a stage3/libb/libm020/libgcc.a
	-cp libm020/libgcc.a stage3/libm020/libgcc.a
	-cp libb32/libm020/libgcc.a stage3/libb32/libm020/libgcc.a
	-if $(RANLIB_TEST) ; then $(RANLIB) stage3/libb/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage3/libb/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage3/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage3/libb32/libm020/libgcc.a; else true; fi

stage4-libamiga:
	-if [ -d stage4 ] ; then true ; else mkdir stage4 ; fi
	-if [ -d stage4/libb ] ; then true ; else mkdir stage4/libb ; fi
	-if [ -d stage4/libb32 ] ; then true ; else mkdir stage4/libb32 ; fi
	-if [ -d stage4/libb/libm020 ] ; then true ; else mkdir stage4/libb/libm020 ; fi
	-if [ -d stage4/libm020 ] ; then true ; else mkdir stage4/libm020 ; fi
	-if [ -d stage4/libb32/libm020 ] ; then true ; else mkdir stage4/libb32/libm020 ; fi
	-cp libb/libgcc.a stage4/libb/libgcc.a
	-cp libb/libm020/libgcc.a stage4/libb/libm020/libgcc.a
	-cp libm020/libgcc.a stage4/libm020/libgcc.a
	-cp libb32/libm020/libgcc.a stage4/libb32/libm020/libgcc.a
	-if $(RANLIB_TEST) ; then $(RANLIB) stage4/libb/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage4/libb/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage4/libm020/libgcc.a; else true; fi
	-if $(RANLIB_TEST) ; then $(RANLIB) stage4/libb32/libm020/libgcc.a; else true; fi
