# Configure fragment invoked in the post-target section for subdirs
# wanting multilib support.
# The intent is to keep as much of this in one place as possible (and out
# of each subdirectory, eg: newlib, libio, etc.) until the right way to do
# this (ha ha) is decided upon.

# Only do this if --enable-multilib.

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

if [ -z "${with_multisubdir}" ]; then
  multisubdir=
else
  multisubdir="/${with_multisubdir}"
  # The '[^/][^/]*' appears that way to work around a SunOS sed bug.
  dotdot=`echo ${with_multisubdir} | sed -e 's:[^/][^/]*:..:g'`
  # TOP is used by newlib and should not be used elsewhere for this purpose.
  # MULTITOP is the proper one to use.
  # FIXME: newlib needs to be updated to use MULTITOP so we can delete TOP.
  # Newlib may wish to continue to use TOP for its own purposes of course.
  sed -e "s:^TOP[ 	]*=[ 	]*\([./]*\)[ 	]*$:TOP = ${dotdot}/\1:" \
      -e "s:^MULTITOP[ 	]*=.*$:MULTITOP = ${dotdot}:" \
	${Makefile} > Makefile.tem
  rm -f ${Makefile}
  mv Makefile.tem ${Makefile}
fi

# 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:^MULTIDIRS[ 	]*=.*$:MULTIDIRS = ${multidirs}:" \
    -e "s:^MULTISUBDIR[ 	]*=.*$:MULTISUBDIR = ${multisubdir}:" \
    -e 's:^MULTIDO[ 	]*=.*$:MULTIDO = $(MAKE):' \
    -e 's:^MULTICLEAN[ 	]*=.*$:MULTICLEAN = $(MAKE):' \
	${Makefile} > Makefile.tem
rm -f ${Makefile}
mv Makefile.tem ${Makefile}

# ??? May wish to add a check to avoid appending this to Makefiles that
# don't need it.  It's not necessary, but is cleaner.

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; \
	  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} ]; then \
		flags=`echo $$i | sed -e 's/^[^;]*;//' -e 's/@/ -/g'`; \
		if (cd $${dir}; $(MAKE) $(FLAGS_TO_PASS) \
				CFLAGS="$(CFLAGS) $${flags}" \
				CXXFLAGS="$(CXXFLAGS) $${flags}" \
				LIBCFLAGS="$(LIBCFLAGS) $${flags}" \
				LIBCXXFLAGS="$(LIBCXXFLAGS) $${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 \
	  for dir in Makefile $(MULTIDIRS); do \
	    if [ -f ./$$dir/Makefile ]; then \
	      if (cd ./$$dir; $(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 # "${enable_multilib}" = yes
