# Configure fragment invoked in the post-target section for subdirs
# wanting multilib support.
#
# It is advisable to support a few --enable/--disable options to let the
# user select which libraries s/he really wants.
#
# Subdirectories wishing to use multilib should put the following lines
# in the "post-target" section of configure.in.
#
# if [ "${srcdir}" = "." ] ; then
#   if [ "${with_target_subdir}" != "." ] ; then
#     . ${with_multisrctop}../../config-ml.in
#   else
#     . ${with_multisrctop}../config-ml.in
#   fi
# else
#   . ${srcdir}/../config-ml.in
# fi
#
# See librx/configure.in in the libg++ distribution for an example of how
# to handle autoconf'd libraries.
#
# Things are complicated because 6 separate cases must be handled:
# 2 (native, cross) x 3 (absolute-path, relative-not-dot, dot) = 6.
#
# srcdir=. is special.  It must handle make programs that don't handle VPATH.
# To implement this, a symlink tree is built for each library and for each
# multilib subdir.
#
# The build tree is layed out as
#
# ./
#   libg++
#   newlib
#   m68020/
#          libg++
#          newlib
#          m68881/
#                 libg++
#                 newlib
#
# The nice feature about this arrangement is that inter-library references
# in the build tree work without having to care where you are.  Note that
# inter-library references also work in the source tree because symlink trees
# are built when srcdir=.
#
# Unfortunately, trying to access the libraries in the build tree requires
# the user to manually choose which library to use as GCC won't be able to
# find the right one.  This is viewed as the lesser of two evils.
#
# Configure variables:
# ${with_target_subdir} = "." for native, or ${target_alias} for cross.
# Set by top level Makefile.
# ${with_multisrctop} = how many levels of multilibs there are in the source
# tree.  It exists to handle the case of configuring in the source tree:
# ${srcdir} is not constant.
# ${with_multisubdir} = name of multilib subdirectory (eg: m68020/m68881).
#
# Makefile variables:
# MULTISRCTOP = number of multilib levels in source tree (+1 if cross)
# (FIXME: note that this is different than ${with_multisrctop}.  Check out.).
# MULTIBUILDTOP = number of multilib levels in build tree
# MULTIDIRS = list of multilib subdirs (eg: m68000 m68020 ...)
# (only defined in each library's main Makefile).
# MULTISUBDIR = installed subdirectory name with leading '/' (eg: /m68000)
# (only defined in each multilib subdir).

# FIXME: Multilib is currently disabled by default for everything other than
# newlib.  It is up to each target to turn on multilib support for the other
# libraries as desired.

# We have to handle being invoked by both Cygnus configure and Autoconf.
#
# Cygnus configure incoming variables:
# srcdir, subdir, target, arguments
#
# Autoconf incoming variables:
# srcdir, target, ac_configure_args
#
# We *could* figure srcdir and target out, but we'd have to do work that
# our caller has already done to figure them out and requiring these two
# seems reasonable.

if [ -n "${ac_configure_args}" ]; then
  Makefile=${ac_file-Makefile}
  ml_config_shell=${CONFIG_SHELL-/bin/sh}
  ml_arguments="${ac_configure_args}"
  ml_realsrcdir=${srcdir}
else
  Makefile=${Makefile-Makefile}
  ml_config_shell=${config_shell-/bin/sh}
  ml_arguments="${arguments}"
  if [ -n "${subdir}" -a "${subdir}" != "." ] ; then
    ml_realsrcdir=${srcdir}/${subdir}
  else
    ml_realsrcdir=${srcdir}
  fi
fi

# Scan all the arguments and set all the ones we need.

ml_verbose=--verbose
for option in ${ml_arguments}
do
  case $option in
  --*) ;;
  -*) option=-$option ;;
  esac

  case $option in
  --*=*)
	optarg=`echo $option | sed -e 's/^[^=]*=//'`
	;;
  esac

  case $option in
  --disable-*)
	enableopt=`echo ${option} | sed 's:^--disable-:enable_:;s:-:_:g'`
	eval $enableopt=no
	;;
  --enable-*)
	case "$option" in
	*=*)	;;
	*)	optarg=yes ;;
	esac
	enableopt=`echo ${option} | sed 's:^--::;s:=.*$::;s:-:_:g'`
	eval $enableopt="$optarg"
	;;
  --norecursion | --no*)
	ml_norecursion=yes
	;;
  --silent | --sil* | --quiet | --q*)
	ml_verbose=--silent
	;;
  --verbose | --v | --verb*)
	ml_verbose=--verbose
	;;
  --with-*)
	case "$option" in
	*=*)	;;
	*)	optarg=yes ;;
	esac
	withopt=`echo ${option} | sed 's:^--::;s:=.*$::;s:-:_:g'`
	eval $withopt="$optarg"
	;;
  --without-*)
	withopt=`echo ${option} | sed 's:^--::;s:out::;s:-:_:g'`
	eval $withopt=no
	;;
  esac
done

# Only do this if --enable-multilib.
if [ "${enable_multilib}" = yes ]; then

# Compute whether this is the library's top level directory
# (ie: not a multilib subdirectory, and not a subdirectory like libg++/src).
# ${with_multisubdir} tells us we're in the right branch, but we could be
# in a subdir of that.
# ??? The previous version could void this test by separating the process into
# two files: one that only the library's toplevel configure.in ran (to
# configure the multilib subdirs), and another that all configure.in's ran to
# update the Makefile.  It seemed reasonable to collapse all multilib support
# into one file, but it does leave us with having to perform this test.
ml_toplevel_p=no
if [ -z "${with_multisubdir}" ]; then
  if [ "${srcdir}" = "." ]; then
    # Use ${ml_realsrcdir} instead of ${srcdir} here to account for ${subdir}.
    # ${with_target_subdir} = "." for native, otherwise target alias.
    if [ "${with_target_subdir}" = "." ]; then
      if [ -f ${ml_realsrcdir}/../config-ml.in ]; then
	ml_toplevel_p=yes
      fi
    else
      if [ -f ${ml_realsrcdir}/../../config-ml.in ]; then
	ml_toplevel_p=yes
      fi
    fi
  else
    # Use ${ml_realsrcdir} instead of ${srcdir} here to account for ${subdir}.
    if [ -f ${ml_realsrcdir}/../config-ml.in ]; then
      ml_toplevel_p=yes
    fi
  fi
fi

# If this is the library's top level directory, set multidirs to the
# multilib subdirs to support.  This lives at the top because we need
# `multidirs' set right away.

if [ "${ml_toplevel_p}" = yes ]; then

multidirs=
for i in `${CC-gcc} --print-multi-lib 2>/dev/null`; do
  dir=`echo $i | sed -e 's/;.*$//'`
  if [ "${dir}" = "." ]; then
    true
  else
    if [ -z "${multidirs}" ]; then
      multidirs="${dir}"
    else
      multidirs="${multidirs} ${dir}"
    fi
  fi
done

case "${target}" in
arc-*-elf*)
	if [ x$enable_biendian != xyes ]
	then
	  old_multidirs=${multidirs}
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "${x}" in
	      *be*) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	;;
m68*-*-*)
	if [ x$enable_softfloat = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *soft-float* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_m68881 = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *m68881* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_m68000 = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *m68000* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_m68020 = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *m68020* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	;;
mips*-*-*)
	if [ x$enable_single_float = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *single* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_biendian = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *el* ) : ;;
	      *eb* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_softfloat = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *soft-float* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	;;
powerpc*-*-* | rs6000*-*-*)
	if [ x$enable_softfloat = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *soft-float* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_powercpu = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      power | */power | */power/* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_powerpccpu = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *powerpc* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_powerpcos = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *mcall-linux* | *mcall-solaris* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_biendian = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *mlittle* | *mbig* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_sysv = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *mcall-sysv* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	if [ x$enable_aix = xno ]
	then
	  old_multidirs="${multidirs}"
	  multidirs=""
	  for x in ${old_multidirs}; do
	    case "$x" in
	      *mcall-aix* ) : ;;
	      *) multidirs="${multidirs} ${x}" ;;
	    esac
	  done
	fi
	;;
esac

# Remove extraneous blanks from multidirs.
# Tests like `if [ -n "$multidirs" ]' require it.
multidirs=`echo "$multidirs" | sed -e 's/^[ ][ ]*//' -e 's/[ ][ ]*$//' -e 's/[ ][ ]*/ /g'`

# Add code to library's top level makefile to handle building the multilib
# subdirs.

cat > Multi.tem <<\EOF

# FIXME: There should be an @-sign in front of the `if'.
# Leave out until this is tested a bit more.
multi-do:
	if [ -z "$(MULTIDIRS)" ]; then \
	  true; \
	else \
	  rootpre=`pwd`/; export rootpre; \
	  srcrootpre=`cd $(srcdir); pwd`/; export srcrootpre; \
	  lib=`echo $${rootpre} | sed -e 's,^.*/\([^/][^/]*\)/$$,\1,'`; \
	  compiler="$(CC)"; \
	  for i in `$${compiler} --print-multi-lib 2>/dev/null`; do \
	    dir=`echo $$i | sed -e 's/;.*$$//'`; \
	    if [ "$${dir}" = "." ]; then \
	      true; \
	    else \
	      if [ -d ../$${dir}/$${lib} ]; then \
		flags=`echo $$i | sed -e 's/^[^;]*;//' -e 's/@/ -/g'`; \
		if (cd ../$${dir}/$${lib}; $(MAKE) $(FLAGS_TO_PASS) \
				CFLAGS="$(CFLAGS) $${flags}" \
				CXXFLAGS="$(CXXFLAGS) $${flags}" \
				LIBCFLAGS="$(LIBCFLAGS) $${flags}" \
				LIBCXXFLAGS="$(LIBCXXFLAGS) $${flags}" \
				LDFLAGS="$(LDFLAGS) $${flags}" \
				$(DO)); then \
		  true; \
		else \
		  exit 1; \
		fi; \
	      else true; \
	      fi; \
	    fi; \
	  done; \
	fi

# FIXME: There should be an @-sign in front of the `if'.
# Leave out until this is tested a bit more.
multi-clean:
	if [ -z "$(MULTIDIRS)" ]; then \
	  true; \
	else \
	  lib=`pwd | sed -e 's,^.*/\([^/][^/]*\)$$,\1,'`; \
	  for dir in Makefile $(MULTIDIRS); do \
	    if [ -f ../$${dir}/$${lib}/Makefile ]; then \
	      if (cd ../$${dir}/$${lib}; $(MAKE) $(FLAGS_TO_PASS) $(DO)); \
	      then true; \
	      else exit 1; \
	      fi; \
	    else true; \
	    fi; \
	  done; \
	fi
EOF

cat ${Makefile} Multi.tem > Makefile.tem
rm -f ${Makefile} Multi.tem
mv Makefile.tem ${Makefile}

fi # ${ml_toplevel_p} = yes

if [ "${ml_verbose}" = --verbose ]; then
  echo "Adding multilib support to Makefile in ${ml_realsrcdir}"
  if [ "${ml_toplevel_p}" = yes ]; then
    echo "multidirs=${multidirs}"
  fi
  echo "with_multisubdir=${with_multisubdir}"
fi

if [ "${srcdir}" = "." ]; then
  if [ "${with_target_subdir}" != "." ]; then
    ml_srcdotdot="../"
  else
    ml_srcdotdot=""
  fi
else
  ml_srcdotdot=""
fi

if [ -z "${with_multisubdir}" ]; then
  ml_subdir=
  ml_builddotdot=
  : # ml_srcdotdot= # already set
else
  ml_subdir="/${with_multisubdir}"
  # The '[^/][^/]*' appears that way to work around a SunOS sed bug.
  ml_builddotdot=`echo ${with_multisubdir} | sed -e 's:[^/][^/]*:..:g'`/
  if [ "$srcdir" = "." ]; then
    ml_srcdotdot=${ml_srcdotdot}${ml_builddotdot}
  else
    : # ml_srcdotdot= # already set
  fi
fi

if [ "${ml_toplevel_p}" = yes ]; then
  ml_do='$(MAKE)'
  ml_clean='$(MAKE)'
else
  ml_do=true
  ml_clean=true
fi

# TOP is used by newlib and should not be used elsewhere for this purpose.
# MULTI{SRC,BUILD}TOP are the proper ones to use.  MULTISRCTOP is empty
# when srcdir != builddir.  MULTIBUILDTOP is always some number of ../'s.
# FIXME: newlib needs to be updated to use MULTI{SRC,BUILD}TOP so we can
# delete TOP.  Newlib may wish to continue to use TOP for its own purposes
# of course.
# MULTIDIRS is non-empty for the cpu top level Makefile (eg: newlib/Makefile)
# and lists the subdirectories to recurse into.
# MULTISUBDIR is non-empty in each cpu subdirectory's Makefile
# (eg: newlib/h8300h/Makefile) and is the installed subdirectory name with
# a leading '/'.
# MULTIDO is used for targets like all, install, and check where
# $(FLAGS_TO_PASS) augmented with the subdir's compiler option is needed.
# MULTICLEAN is used for the *clean targets.
#
# ??? It is possible to merge MULTIDO and MULTICLEAN into one.  They are
# currently kept separate because we don't want the *clean targets to require
# the existence of the compiler (which MULTIDO currently requires) and
# therefore we'd have to record the directory options as well as names
# (currently we just record the names and use --print-multi-lib to get the
# options).

sed -e "s:^TOP[ 	]*=[ 	]*\([./]*\)[ 	]*$:TOP = ${ml_builddotdot}\1:" \
    -e "s:^MULTISRCTOP[ 	]*=.*$:MULTISRCTOP = ${ml_srcdotdot}:" \
    -e "s:^MULTIBUILDTOP[ 	]*=.*$:MULTIBUILDTOP = ${ml_builddotdot}:" \
    -e "s:^MULTIDIRS[ 	]*=.*$:MULTIDIRS = ${multidirs}:" \
    -e "s:^MULTISUBDIR[ 	]*=.*$:MULTISUBDIR = ${ml_subdir}:" \
    -e "s:^MULTIDO[ 	]*=.*$:MULTIDO = $ml_do:" \
    -e "s:^MULTICLEAN[ 	]*=.*$:MULTICLEAN = $ml_clean:" \
	${Makefile} > Makefile.tem
rm -f ${Makefile}
mv Makefile.tem ${Makefile}

# If this is the library's top level, configure each multilib subdir.
# This is done at the end because this is the loop that runs configure
# in each multilib subdir and it seemed reasonable to finish updating the
# Makefile before going on to configure the subdirs.

if [ "${ml_toplevel_p}" = yes ]; then

# We must freshly configure each subdirectory.  This bit of code is
# actually partially stolen from the main configure script.  FIXME.

if [ -n "${multidirs}" ] && [ -z "${ml_norecursion}" ]; then

  if [ "${ml_verbose}" = --verbose ]; then
    echo "Running configure in multilib subdirs ${multidirs}"
    echo "pwd: `pwd`"
  fi

  ml_origdir=`pwd`
  ml_libdir=`echo $ml_origdir | sed -e 's,^.*/,,'`
  # cd to top-level-build-dir/${with_target_subdir}
  cd ..

  for ml_dir in ${multidirs}; do

    if [ "${ml_verbose}" = --verbose ]; then
      echo "Running configure in multilib subdir ${ml_dir}"
      echo "pwd: `pwd`"
    fi

    if [ -d ${ml_dir} ]; then true; else mkdir ${ml_dir}; fi
    if [ -d ${ml_dir}/${ml_libdir} ]; then true; else mkdir ${ml_dir}/${ml_libdir}; fi

    # Eg: if ${ml_dir} = m68000/m68881, dotdot = ../../
    dotdot=../`echo ${ml_dir} | sed -e 's|[^/]||g' -e 's|/|../|g'`

    case ${srcdir} in
    ".")
      echo Building symlink tree in `pwd`/${ml_dir}/${ml_libdir}
      if [ "${with_target_subdir}" != "." ]; then
	ml_unsubdir="../"
      else
	ml_unsubdir=""
      fi
      (cd ${ml_dir}/${ml_libdir};
       ../${dotdot}${ml_unsubdir}symlink-tree ../${dotdot}${ml_unsubdir}${ml_libdir} "")
      if [ -f ${ml_dir}/${ml_libdir}/Makefile ]; then
	if [ x"${MAKE}" = x ]; then
	  (cd ${ml_dir}/${ml_libdir}; make distclean)
	else
	  (cd ${ml_dir}/${ml_libdir}; ${MAKE} distclean)
	fi
      fi
      ml_newsrcdir="."
      ml_srcdiroption=
      multisrctop=${dotdot}
      ;;
    *)
      case "${srcdir}" in
      /*) # absolute path
        ml_newsrcdir=${srcdir}
        ;;
      *) # otherwise relative
        ml_newsrcdir=${dotdot}${srcdir}
        ;;
      esac
      ml_srcdiroption="-srcdir=${ml_newsrcdir}"
      multisrctop=
      ;;
    esac

    case "${progname}" in
    /*)     ml_recprog=${progname} ;;
    *)      ml_recprog=${dotdot}${progname} ;;
    esac

    # FIXME: POPDIR=${PWD=`pwd`} doesn't work here.
    ML_POPDIR=`pwd`
    cd ${ml_dir}/${ml_libdir}

    if [ -f ${ml_newsrcdir}/configure ]; then
      ml_recprog=${ml_newsrcdir}/configure
    fi
    if eval ${ml_config_shell} ${ml_recprog} \
	--with-multisubdir=${ml_dir} --with-multisrctop=${multisrctop} \
	${ml_arguments} ${ml_srcdiroption} ; then
      true
    else
      exit 1
    fi

    cd ${ML_POPDIR}

  done

  cd ${ml_origdir}
fi

fi # ${ml_toplevel_p} = yes
fi # ${enable_multilib} = yes