# Top level makefile fragment for GNU C++.
#   Copyright (C) 1995 Free Software Foundation, Inc.

#This file is part of GNU Fortran.

#GNU Fortran 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 Fortran 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 Fortran; 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.realclean,
# 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: f771)
# - define the names for selecting the language in LANGUAGES.

# Extra flags to pass to recursive makes (and to sub-configure).
# Use different quoting rules compared with FLAGS_TO_PASS so we can use
# this to set environment variables as well
# Note that GCC_FOR_TARGET, GCC_FLAGS aren't in here -- treated separately.
F77_FLAGS_TO_PASS = \
	CROSS="$(CROSS)" \
	AR_FLAGS="$(AR_FLAGS)" \
	AR_FOR_TARGET="$(AR_FOR_TARGET)" \
	BISON="$(BISON)" \
	BISONFLAGS="$(BISONFLAGS)" \
	CC="$(CC)" \
	CFLAGS="$(CFLAGS)" \
	LDFLAGS="$(LDFLAGS)" \
	LEX="$(LEX)" \
	LEXFLAGS="$(LEXFLAGS)" \
	MAKEINFO="$(MAKEINFO)" \
	MAKEINFOFLAGS="$(MAKEINFOFLAGS)" \
	RANLIB_FOR_TARGET="$(RANLIB_FOR_TARGET)" \
	RANLIB_TEST_FOR_TARGET="$(RANLIB_TEST_FOR_TARGET)" \
	SHELL="$(SHELL)" \
	exec_prefix="$(exec_prefix)" \
	prefix="$(prefix)" \
	tooldir="$(tooldir)" \
	bindir="$(bindir)" \
	libsubdir="$(libsubdir)"
#	"F77_FOR_BUILD=$(F77_FOR_BUILD)" \
#	"F77FLAGS=$(F77FLAGS)" \
#	"F77_FOR_TARGET=$(F77_FOR_TARGET)"

# Actual names to use when installing a native compiler.
F77_INSTALL_NAME = f77
G77_INSTALL_NAME = g77

# Override the normal gcc install target.
INSTALL_TARGET=f77.install-normal f77.install-common

# Actual names to use when installing a cross-compiler.
F77_CROSS_NAME = $(target)-f77
G77_CROSS_NAME = $(target)-g77


# Define the names for selecting f77 in LANGUAGES.
# Note that it would be nice to move the dependency on g77
# into the F77 rule, but that needs a little bit of work
# to do the right thing within all.cross.
F77 f77: f771 f77-runtime

# Create the compiler driver for g77.
g77: $(srcdir)/f/g77.c $(CONFIG_H) $(LIBDEPS)
	$(CC) $(ALL_CFLAGS) $(INCLUDES) $(LDFLAGS) -o g77 $(srcdir)/f/g77.c $(LIBS)

# Create a version of the g77 driver which calls the cross-compiler.
g77-cross: $(srcdir)/f/g77.c
	$(CC) $(ALL_CFLAGS) $(INCLUDES) $(LDFLAGS) -o g77-cross \
	   -DGCC_NAME=\"$(GCC_CROSS_NAME)\" $(srcdir)/f/g77.c version.o $(LIBS)

F77_SRCS = \
 $(srcdir)/f/assert.j \
 $(srcdir)/f/bad.c \
 $(srcdir)/f/bad.def \
 $(srcdir)/f/bad.h \
 $(srcdir)/f/bit.c \
 $(srcdir)/f/bit.h \
 $(srcdir)/f/bld-op.def \
 $(srcdir)/f/bld.c \
 $(srcdir)/f/bld.h \
 $(srcdir)/f/com-rt.def \
 $(srcdir)/f/com.c \
 $(srcdir)/f/com.h \
 $(srcdir)/f/config.j \
 $(srcdir)/f/convert.j \
 $(srcdir)/f/data.c \
 $(srcdir)/f/data.h \
 $(srcdir)/f/equiv.c \
 $(srcdir)/f/equiv.h \
 $(srcdir)/f/expr.c \
 $(srcdir)/f/expr.h \
 $(srcdir)/f/fini.c \
 $(srcdir)/f/flags.j \
 $(srcdir)/f/glimits.j \
 $(srcdir)/f/global.c \
 $(srcdir)/f/global.h \
 $(srcdir)/f/hconfig.j \
 $(srcdir)/f/implic.c \
 $(srcdir)/f/implic.h \
 $(srcdir)/f/info-b.def \
 $(srcdir)/f/info-k.def \
 $(srcdir)/f/info-w.def \
 $(srcdir)/f/info.c \
 $(srcdir)/f/info.h \
 $(srcdir)/f/intrin.c \
 $(srcdir)/f/intrin.def \
 $(srcdir)/f/intrin.h \
 $(srcdir)/f/lab.c \
 $(srcdir)/f/lab.h \
 $(srcdir)/f/lex.c \
 $(srcdir)/f/lex.h \
 $(srcdir)/f/malloc.c \
 $(srcdir)/f/malloc.h \
 $(srcdir)/f/name.c \
 $(srcdir)/f/name.h \
 $(srcdir)/f/parse.c \
 $(srcdir)/f/proj.c \
 $(srcdir)/f/proj.h \
 $(srcdir)/f/rtl.j \
 $(srcdir)/f/src.c \
 $(srcdir)/f/src.h \
 $(srcdir)/f/st.c \
 $(srcdir)/f/st.h \
 $(srcdir)/f/sta.c \
 $(srcdir)/f/sta.h \
 $(srcdir)/f/stb.c \
 $(srcdir)/f/stb.h \
 $(srcdir)/f/stc.c \
 $(srcdir)/f/stc.h \
 $(srcdir)/f/std.c \
 $(srcdir)/f/std.h \
 $(srcdir)/f/ste.c \
 $(srcdir)/f/ste.h \
 $(srcdir)/f/storag.c \
 $(srcdir)/f/storag.h \
 $(srcdir)/f/stp.c \
 $(srcdir)/f/stp.h \
 $(srcdir)/f/str-1t.fin \
 $(srcdir)/f/str-2t.fin \
 $(srcdir)/f/str-fo.fin \
 $(srcdir)/f/str-io.fin \
 $(srcdir)/f/str-nq.fin \
 $(srcdir)/f/str-op.fin \
 $(srcdir)/f/str-ot.fin \
 $(srcdir)/f/str.c \
 $(srcdir)/f/str.h \
 $(srcdir)/f/sts.c \
 $(srcdir)/f/sts.h \
 $(srcdir)/f/stt.c \
 $(srcdir)/f/stt.h \
 $(srcdir)/f/stu.c \
 $(srcdir)/f/stu.h \
 $(srcdir)/f/stv.c \
 $(srcdir)/f/stv.h \
 $(srcdir)/f/stw.c \
 $(srcdir)/f/stw.h \
 $(srcdir)/f/symbol.c \
 $(srcdir)/f/symbol.def \
 $(srcdir)/f/symbol.h \
 $(srcdir)/f/target.c \
 $(srcdir)/f/target.h \
 $(srcdir)/f/tconfig.j \
 $(srcdir)/f/tm.j \
 $(srcdir)/f/top.c \
 $(srcdir)/f/top.h \
 $(srcdir)/f/tree.j \
 $(srcdir)/f/type.c \
 $(srcdir)/f/type.h \
 $(srcdir)/f/where.c \
 $(srcdir)/f/where.h \
 $(srcdir)/f/zzz.c \
 $(srcdir)/f/zzz.h

f771: $(P) $(F77_SRCS) $(LIBDEPS) stamp-objlist f/Makefile
	$(MAKE) -f f/Makefile $(FLAGS_TO_PASS) VPATH=$(srcdir) srcdir=$(srcdir)/f f771

f/Makefile: $(srcdir)/f/Makefile.in $(srcdir)/configure
	$(SHELL) config.status

# Note that the runtime is built in the top-level directory rather
# than in f/runtime a la the Cygnus CHILL example; then xgcc -B./ will
# find it.  Use an absolute name for GCC_FOR_TARGET (so we don't have
# to keep stage? links around everywhere) unless this value has been
# overridden from the default "./xgcc -B./", hence the case statement.
# We depend on GCC_PASSES through f/runtime/Makefile.
stmp-int-hdrs = stmp-int-hdrs	# to be overrideable in unsafe version
f77-runtime:  f/runtime/Makefile include/f2c.h $(stmp-int-headers)
	case "$(LANGUAGES)" in \
	*f77*) top=`pwd`; \
	  cd f/runtime && $(MAKE) \
            GCC_FOR_TARGET="`case '$(GCC_FOR_TARGET)' in \
             './xgcc  -B./') echo $${top}/xgcc -B$${top}/;; \
             *) echo '$(GCC_FOR_TARGET)';; esac`" \
            GCC_FLAGS="$(GCC_FLAGS)" $(F77_FLAGS_TO_PASS) \
            all ;; \
        esac

# This one doesn't depend on cc1 etc. but f2c.h may not be found, 
# in particular, at present...
f77-runtime-unsafe:
	$(MAKE) stmp-int-hdrs= GCC_PARTS= f77-runtime

# The configuration of the runtime system relies on an autoconf-type
# configure, not a Cygnus-type one.  It needs to be run *after* the
# appropriate (cross-)compiler has been built, thus depend on GCC_PARTS.
# NB, sh uses the *first* value of $a from `a=fred a=joe prog'.
include/f2c.h f/runtime/Makefile: $(srcdir)/f/runtime/f2c.h.in \
             $(srcdir)/f/com.h f/proj.h \
            $(srcdir)/f/runtime/Makefile.in $(GCC_PARTS) \
            $(srcdir)/config/$(xmake_file) $(srcdir)/config/$(tmake_file)
# The make "stage?" in compiler spec. is fully qualified as above
	top=`pwd`; \
	src=`cd $(srcdir); pwd`; \
          cd f/runtime; \
          CC="`case '$(GCC_FOR_TARGET)' in \
             './xgcc  -B./') echo $${top}/xgcc -B$${top}/;; \
             *) echo '$(GCC_FOR_TARGET)';; esac`" \
          $(F77_FLAGS_TO_PASS) CONFIG_SITE=/dev/null $(SHELL) \
          $${src}/f/runtime/configure --srcdir=$${src}/f/runtime

#For now, omit f2c stuff.  -- burley
#f2c: stmp-int-hdrs f/f2c/Makefile
#	cd f/f2c; $(MAKE) all
#
#f/f2c/Makefile: $(srcdir)/f/f2c/Makefile.in $(GCC_PARTS) \
#            $(srcdir)/config/$(xmake_file) $(srcdir)/config/$(tmake_file)
#	top=`pwd`; cd f/f2c; \
#          $${top}/f/f2c/configure --srcdir=$${top}/f/f2c

# Build hooks:

# I'm not sure there's a way of getting f2c into here conditionally on
# the --enable-f2c flag detected by config-lang.in so kluge it with the
# maybe-f2c target by looking at STAGESTUFF.
f77.all.build: g77 maybe-f2c
f77.all.cross: g77-cross maybe-f2c
f77.start.encap: g77 maybe-f2c
f77.rest.encap:

f77.info:
f77.dvi:

maybe-f2c:
#For now, omit f2c stuff.  -- burley
#	case "$(STAGESTUFF)" in *f2c*) $(MAKE) f2c;; esac

# Install hooks:
# f771 is installed here so we don't clobber an existing gcc
# installation

f77.install-normal: install-libf77

# Install the F77 run time library.
install-libf77: f77-runtime
# Check for the presence of other versions of the library and includes.
# Test libf2c.* in case of a shared version, for instance.
	@if test "`echo $(libdir)/libf2c.*`" != "$(libdir)/libf2c.*" && \
          test -z "$(F2CLIBOK)" ; then \
   echo ; \
   echo 'You already have a version of libf2c installed as' $(libdir)/libf2c.*; \
   echo 'To use g77 this must be consistent with the one that will be built.'; \
   echo 'You should probably delete it and/or install ./libf2c.a in its place.'; \
   echo 'Resume the "make install" after removing the existing library or'; \
   echo 'define the make variable F2CLIBOK to avoid this test.'; \
   echo 'Check also for' $(includedir)/f2c.h 'per INSTALL instructions.'; \
   echo '(Note that a quick and easy way to resume "make -k install" is to'; \
   echo 'use "make install-libf77".)'; \
   exit 1; else true; fi
	if [ -f libf2c.a ] ; then \
	  $(INSTALL_DATA) libf2c.a $(libsubdir)/libf2c.a; \
	  if $(RANLIB_TEST) ; then \
	    (cd $(libsubdir); $(RANLIB) libf2c.a); else true; fi; \
	  chmod a-x $(libsubdir)/libf2c.a; \
	else true; fi
	if [ -f include/f2c.h ] ; then \
	  $(INSTALL_DATA) include/f2c.h $(libsubdir)/include/f2c.h; \
	else true; fi
	for file in f771; do \
	  if [ -f $$file ] ; then \
	    rm -f $(libsubdir)/$$file; \
	    $(INSTALL_PROGRAM) $$file $(libsubdir)/$$file; \
	  else true; \
	  fi; \
	done

# Install the driver program as $(target)-g77
# and also as either g77 (if native) or $(tooldir)/bin/g77.
f77.install-common:
	-if [ -f f771 ] ; then \
	  if [ -f g77-cross ] ; then \
	    rm -f $(bindir)/$(G77_CROSS_NAME); \
	    $(INSTALL_PROGRAM) g77-cross $(bindir)/$(G77_CROSS_NAME); \
	    chmod a+x $(bindir)/$(G77_CROSS_NAME); \
	    rm -f $(bindir)/$(F77_CROSS_NAME); \
	    ln $(bindir)/$(G77_CROSS_NAME) $(bindir)/$(F77_CROSS_NAME); \
	  else \
	    rm -f $(bindir)/$(G77_INSTALL_NAME); \
	    $(INSTALL_PROGRAM) g77 $(bindir)/$(G77_INSTALL_NAME); \
	    chmod a+x $(bindir)/$(G77_INSTALL_NAME); \
	    rm -f $(bindir)/$(F77_INSTALL_NAME); \
	    $(INSTALL_PROGRAM) g77 $(bindir)/$(F77_INSTALL_NAME); \
	    chmod a+x $(bindir)/$(F77_INSTALL_NAME); \
	  fi ; \
	else true; fi

f77.install-info:

f77.install-man: $(srcdir)/f/g77.1
	-if [ -f f771 ] ; then \
	  $(INSTALL_DATA) $(srcdir)/f/g77.1 $(mandir)/g77$(manext) ; \
	  chmod a-x $(mandir)/g77$(manext) ; \
	else true; fi

f77.uninstall:
	-rm -rf $(bindir)/$(F77_CROSS_NAME)
	-rm -rf $(bindir)/$(G77_INSTALL_NAME)
	-rm -rf $(bindir)/$(G77_CROSS_NAME)
	-rm -rf $(mandir)/g77$(manext)
	-rm -rf $(libsubdir)/libf2c.a

# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

f77.mostlyclean:
	-rm -f f/*.o
	-cd f/runtime; $(MAKE) mostlyclean
f77.clean: f77.mostlyclean
f77.distclean: f77.clean
	-rm -f f/Makefile
	-rm -f f/fini f/f771 f/stamp-str f/str-*.h f/str-*.j
	-cd f/runtime; $(MAKE) distclean
f77.extraclean f77.realclean: f77.distclean

# Stage hooks:
# The main makefile has already created stage?/f.

G77STAGESTUFF = f/*.o f/fini f/stamp-str f/str-*.h f/str-*.j

f77.stage1:
	-mv $(G77STAGESTUFF) stage1/f
f77.stage2:
	-mv $(G77STAGESTUFF) stage2/f
f77.stage3:
	-mv $(G77STAGESTUFF) stage3/f
f77.stage4:
	-mv $(G77STAGESTUFF) stage4/f

# 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.
f77.distdir:
	mkdir tmp/f
#	cd f ; $(MAKE) $(FLAGS_TO_PASS) $(F77_FLAGS_TO_PASS) ?no files?
	cd f; \
	for file in *[0-9a-zA-Z+]; do \
	  ln $$file ../tmp/f >/dev/null 2>&1 || cp $$file ../tmp/f; \
	done
