# Top level makefile fragment for GNU Ada (GNAT).
#   Copyright (C) 1994 Free Software Foundation, Inc.

#This file is part of GNU CC.

#GNU CC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.

#GNU CC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with GNU CC; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

# This file provides the language dependent support in the main Makefile.
# Each language makefile fragment must provide the following targets:
#
# foo.all.build, foo.all.cross, foo.start.encap, foo.rest.encap,
# foo.info, foo.dvi,
# foo.install-normal, foo.install-common, foo.install-info, foo.install-man,
# foo.uninstall, foo.distdir,
# foo.mostlyclean, foo.clean, foo.distclean, foo.extraclean,
# foo.maintainer-clean, foo.stage1, foo.stage2, foo.stage3, foo.stage4
#
# where `foo' is the name of the language.
#
# It should also provide rules for:
#
# - making any compiler driver (eg: g++)
# - the compiler proper (eg: cc1plus)
# - define the names for selecting the language in LANGUAGES.

# Extra flags to pass to recursive makes.
BOOT_ADAFLAGS= $(ADAFLAGS)
ADAFLAGS= -gnatpg -gnata
GNATBIND= gnatbind
GNATLIBFLAGS= -gnatpg
ADA_FLAGS_TO_PASS = \
	"ADA_FOR_BUILD=$(ADA_FOR_BUILD)" \
	"ADAFLAGS=$(ADAFLAGS)" \
	"ADA_FOR_TARGET=$(ADA_FOR_TARGET)" \
	"GNATBIND=$(GNATBIND)" \
	"INSTALL_DATA=$(INSTALL_DATA)"

# Define the names for selecting Ada in LANGUAGES.
Ada ada: gnat1 gnatbind

# There are too many Ada sources to check against here.  Let's
# always force the recursive make.
gnat1: $(LIBDEPS) stamp-objlist force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnat1

gnatbind: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatbind

gnatf: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatf

gnatk8: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatk8

gnatmake: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatmake

gnatchp: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatchp

gnatbl: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ../gnatbl

# use host-gcc
gnattools: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) \
		GNATLIBFLAGS=$(GNATLIBFLAGS) \
		../gnatf ../gnatk8 ../gnatmake ../gnatchp ../gnatbl

# use cross-gcc
gnatlib: force
	cd ada; $(MAKE) GNATLIBFLAGS=$(GNATLIBFLAGS) gnatlib

# use cross-gcc
gnatgthreads: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) \
		GNATLIBFLAGS=$(GNATLIBFLAGS) gnatgthreads

# use only for native compiler
gnatlib_and_tools: force
	cd ada; \
	$(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS)\
	    CC="../xgcc -B../" GNATBIND="../gnatbind"\
	    ../gnatf ../gnatk8 ../gnatmake ../gnatchp ../gnatbl; \
	$(MAKE) GNATLIBFLAGS=$(GNATLIBFLAGS) gnatlib
	  

gnat-cross: force
	cd ada; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) \
		GNATLIBFLAGS=$(GNATLIBFLAGS) gnat-cross

# Build hooks:

ada.all.build: 
ada.all.cross: gnat1 gnattools
	cp gnat1     gnat1-cross
	cp gnatbind  gnatbind-cross
	cp gnatf     gnatf-cross
	cp gnatk8    gnatk8-cross
	cp gnatmake  gnatmake-cross
	cp gnatchp   gnatchp-cross
	cp gnatbl    gnatbl-cross

ada.start.encap:
ada.rest.encap:
ada.info:
ada.dvi:

# Install hooks:
# gnat1 is installed elsewhere as part of $(COMPILERS).

ada.install-normal:

# Install the binder program as $(target)-gnatbind
# and also as either gnatbind (if native) or $(tooldir)/bin/gnatbind
# likewise for gnatf, gnatchp, and gnatk8, gnatmake gnatbl.
ada.install-common:
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatbind-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatbind; \
	    $(INSTALL_PROGRAM) gnatbind-cross $(bindir)/$(target)-gnatbind; \
	    chmod a+x $(bindir)/$(target)-gnatbind; \
	  else \
	    rm -f $(bindir)/gnatbind; \
	    $(INSTALL_PROGRAM) gnatbind $(bindir)/gnatbind; \
	    chmod a+x $(bindir)/gnatbind; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatf-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatf; \
	    $(INSTALL_PROGRAM) gnatf-cross $(bindir)/$(target)-gnatf; \
	    chmod a+x $(bindir)/$(target)-gnatf; \
	  else \
	    rm -f $(bindir)/gnatf; \
	    $(INSTALL_PROGRAM) gnatf $(bindir)/gnatf; \
	    chmod a+x $(bindir)/gnatf; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatk8-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatk8; \
	    $(INSTALL_PROGRAM) gnatk8-cross $(bindir)/$(target)-gnatk8; \
	    chmod a+x $(bindir)/$(target)-gnatk8; \
	  else \
	    rm -f $(bindir)/gnatk8; \
	    $(INSTALL_PROGRAM) gnatk8 $(bindir)/gnatk8; \
	    chmod a+x $(bindir)/gnatk8; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gcc-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatmake; \
	    $(INSTALL_PROGRAM) gnatmake-cross $(bindir)/$(target)-gnatmake; \
	    chmod a+x $(bindir)/$(target)-gnatmake; \
	  else \
	    rm -f $(bindir)/gnatmake; \
	    $(INSTALL_PROGRAM) gnatmake $(bindir)/gnatmake; \
	    chmod a+x $(bindir)/gnatmake; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatchp-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatchp; \
	    $(INSTALL_PROGRAM) gnatchp-cross $(bindir)/$(target)-gnatchp; \
	    chmod a+x $(bindir)/$(target)-gnatchp; \
	  else \
	    rm -f $(bindir)/gnatchp; \
	    $(INSTALL_PROGRAM) gnatchp $(bindir)/gnatchp; \
	    chmod a+x $(bindir)/gnatchp; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatchop-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatchop; \
	    $(INSTALL_PROGRAM) gnatchop-cross $(bindir)/$(target)-gnatchop; \
	    chmod a+x $(bindir)/$(target)-gnatchop; \
	  else \
	    rm -f $(bindir)/gnatchop; \
	    $(INSTALL_PROGRAM) $(srcdir)/ada/gnatchop $(bindir)/gnatchop; \
	    chmod a+x $(bindir)/gnatchop; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  if [ -f gnatbl-cross ] ; then \
	    rm -f $(bindir)/$(target)-gnatbl; \
	    $(INSTALL_PROGRAM) gnatbl-cross $(bindir)/$(target)-gnatbl; \
	    chmod a+x $(bindir)/$(target)-gnatbl; \
	  else \
	    rm -f $(bindir)/gnatbl; \
	    $(INSTALL_PROGRAM) gnatbl $(bindir)/gnatbl; \
	    chmod a+x $(bindir)/gnatbl; \
	  fi ; \
	fi
	-if [ -f gnat1 ] ; then \
	  $(MAKE) INSTALL_DATA="$(INSTALL_DATA)" install-gnatlib; \
	fi

install-gnatlib:
	(cd ada; \
	 $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) install-gnatlib);

ada.install-info:
ada.install-man:

ada.uninstall:
	-rm -rf $(bindir)/gnatbind $(bindir)/gnatf
	-rm -rf $(bindir)/$(target)-gnatbind $(bindir)/$(target)-gnatf

# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

ada.mostlyclean:
	-rm -f ada/*.o ada/*.ali ada/b_*.c ada/sdefault.adb ada/ttypef.ads
	-rm -f ada/stamp-sdefault
ada.clean:
ada.distclean:
	-rm -f ada/config.status ada/Makefile
ada.extraclean:
ada.maintainer-clean:
	-rm -f ada/a-[se]info.h
	-rm -f ada/nmake.ad[bs]
	-rm -f ada/treeprs.ads
	-rm -f cp/parse.c cp/parse.h

# Stage hooks:
# The main makefile has already created stage?/ada

ada.stage1:
	-mv ada/*.o ada/*.ali ada/mkttypef ada/b_*.c stage1/ada
	-mv ada/stamp-* stage1/ada
ada.stage2:
	-mv ada/*.o ada/*.ali ada/mkttypef ada/b_*.c stage2/ada
	-mv ada/stamp-* stage2/ada
ada.stage3:
	-mv ada/*.o ada/*.ali ada/mkttypef ada/b_*.c stage3/ada
	-mv ada/stamp-* stage3/ada
ada.stage4:
	-mv ada/*.o ada/*.ali ada/mkttypef ada/b_*.c stage4/ada
	-mv ada/stamp-* stage4/ada

# Maintenance hooks:

# This target creates the files that can be rebuilt, but go in the
# distribution anyway.  It then copies the files to the distdir directory.
ada.distdir:
	mdkdir tmp/ada
	cd ada ; $(MAKE) $(FLAGS_TO_PASS) $(ADA_FLAGS_TO_PASS) ada_extra_files
	cd ada; \
	for file in *[0-9a-zA-Z+]; do \
	  ln $$file ../tmp/ada >/dev/null 2>&1 || cp $$file ../tmp/ada; \
	done

# Bootstrapping targets for just GNAT - use the same stage directories
gnatboot: force
	$(MAKE) gnat1 gnatbind CC="$(CC)" CFLAGS="$(CFLAGS)"
	$(MAKE) gnatstage1
	$(MAKE) gnatboot2 BOOT_CFLAGS="$(BOOT_CFLAGS)" \
	                  BOOT_ADAFLAGS="$(BOOT_ADAFLAGS)" \
	                  LDFLAGS="$(BOOT_LDFLAGS)"

gnatboot2: force
	$(MAKE) gnat1 gnatbind  CC="gcc -B../stage1/"\
				CFLAGS="$(BOOT_CFLAGS)" \
	        		ADAFLAGS="$(BOOT_ADAFLAGS)"\
				LDFLAGS="$(BOOT_LDFLAGS)" \
				GNATBIND=../stage1/gnatbind 
	-rm -f stamp-cmp-failed
	for file in ada/*.o ; do \
	  tail +16c ./$$file > tmp-foo1 ; \
	  tail +16c ./stage1/$$file > tmp-foo2 2>/dev/null && \
	  (cmp tmp-foo1 tmp-foo2>/dev/null \
           || (touch stamp-cmp-failed \
                && echo ./$$file ./stage1/$$file differs \
	        && true)) ; \
	done
	-rm -f tmp-foo* 
	@if [ ! -f stamp-cmp-failed ] ; then \
	  echo Bootstrap succeded after Stage1; \
	 else \
	  $(MAKE) gnatstage2 ; \
	  $(MAKE) gnatboot3 BOOT_CFLAGS="$(BOOT_CFLAGS)" \
			    BOOT_ADAFLAGS="$(BOOT_ADAFLAGS)" \
			    LDFLAGS="$(BOOT_LDFLAGS)"; \
	 fi

gnatboot3: force
	$(MAKE) gnat1 gnatbind  CC="gcc -B../stage2/"\
				CFLAGS="$(BOOT_CFLAGS)" \
	        		ADAFLAGS="$(BOOT_ADAFLAGS)"\
				LDFLAGS="$(BOOT_LDFLAGS)" \
				GNATBIND=../stage2/gnatbind 
	$(MAKE) gnatcompare;

gnatcompare: force
	for file in ada/*.o; do \
	  tail +16c $$file > tmp-foo1; \
	  tail +16c stage2/$$file > tmp-foo2 2>/dev/null \
	    && (cmp tmp-foo1 tmp-foo2 || echo $$file differs); \
	done
	-rm -f tmp-foo* 

gnatstage1: force
	-mkdir stage1
	-mkdir stage1/ada
	-mv -f gnat1 gnatbind stage1
	-mv -f ada/*.o ada/*.ali ada/mkttypef stage1/ada
	-mv ada/stamp-* stage1/ada

gnatstage2: force
	-mkdir stage2
	-mkdir stage2/ada
	-mv -f gnat1 gnatbind stage2
	-mv -f ada/*.o ada/*.ali ada/mkttypef stage2/ada
	-mv ada/stamp-* stage2/ada
