#-----------------------------------------------------------------------------
# GNU makefile for building WCSLIB.
#
# Summary of the main targets
# ---------------------------
#    all:       Build the library
#    clean:     Delete intermediate object files.
#    realclean: clean, and also delete executables and the object library.
#    test:      Compile and run the test programs.
#    show_all:  Print the values of all variables used.
#
# Notes:
#    1) The makefile sets certain variables to default values and may then
#       change some of them depending on the value of the FLAVOUR variable.
#       The following FLAVOURs are available:
#
#          SUN        Some version of SunOS.
#          DEC/Ultrix DEC/Ultrix.
#          Convex     Convex/ConvexOS.
#          SGI/32     SGI/IRIX using the 32-bit FORTRAN compiler.
#          SGI/64     SGI/IRIX using the 64-bit FORTRAN compiler.
#          SGI/P32    SGI/IRIX using the 32-bit Power FORTRAN compiler.
#          SGI/P64    SGI/IRIX using the 64-bit Power FORTRAN compiler.
#
#       The FLAVOUR may conveniently be set as an environment variable or on
#       the GNU make command line, e.g.
#
#          gmake FLAVOUR='SGI/32'
#
#       If your requirements don't match any predefined FLAVOUR then you might
#       be able to modify an existing one, add a new one, or simply modify the
#       defaults.
#
# $Id: Makefile,v 2.2 1996/05/07 21:19:25 mcalabre Exp $
#-----------------------------------------------------------------------------
# FORTRAN compiler and options.
  FFLAGS  := -O

# Uncomment this if your FORTRAN compiler doesn't have COSD, SIND, TAND,
# ACOSD, ASIND, ATAND, and ATAN2D as intrinsic functions.
# WCSTRIG := wcstrig.o

# Reset this to ":" if ranlib is not needed.
  RANLIB  := ranlib

# Set or reset linker options here if needed.
  LDFLAGS := -s

# PGPLOT object library; used by tproj2, tcel, and twcs2 to plot test grids.
# If you don't have PGPLOT then set PGPLOTLIB to blank.  PGPLOT is a FORTRAN
# plotting library available from astro.caltech.edu.
  PGPLOTLIB := -lpgplot -lX11

# Extra files to clean up.
  override EXTRA_CLEAN :=

# Overrides for various combinations of
# architecture, operating system and compiler.
#---------------------------------------------

ifeq "$(FLAVOUR)" "SUN"
   FFLAGS  += -u
endif

ifeq "$(FLAVOUR)" "DEC/Ultrix"
   # Nothing.
endif

ifeq "$(FLAVOUR)" "Convex"
   FFLAGS := -O3
endif

ifeq "$(FLAVOUR)" "SGI/32"
   FFLAGS  += -32
   RANLIB  := @ :
   PGPLOTLIB := -L/usr/local/lib -lpgplot -lgl_s -lX11
endif

ifeq "$(FLAVOUR)" "SGI/64"
   FFLAGS  += -OPT:fold_arith_limit=2000
   RANLIB  := @ :
   PGPLOTLIB := -L/usr/local/lib64 -lpgplot -lX11
endif

ifeq "$(FLAVOUR)" "SGI/P32"
   FFLAGS  += -32 -pca keep
   RANLIB  := @ :
   PGPLOTLIB := -L/usr/local/lib -lpgplot -lgl_s -lX11
endif

ifeq "$(FLAVOUR)" "SGI/P64"
   FFLAGS  += -OPT:fold_arith_limit=2000 -pca keep
   RANLIB  := @ :
   PGPLOTLIB := -L/usr/local/lib64 -lpgplot -lX11
   override EXTRA_CLEAN := *.anl *.L *.m
endif

#-----------------------------------------------------------------------------
# You shouldn't need to change anything below here.

LIBRARY := libwcs_f.a
MODULES := $(WCSTRIG) \
           lin.o  \
           proj.o \
           sph.o  \
           cel.o  \
           wcs.o

ifneq "$(PGPLOTLIB)" ""
   TPROJ2 := tproj2
   TCEL   := tcel
   TWCS2  := twcs2
endif

%.o : %.f
	-@ echo ""
	$(FC) $(FFLAGS) -c $<

.PHONY : all clean lib realclean test

all : show_all lib

lib : $(LIBRARY)

$(LIBRARY) : $(MODULES:%=$(LIBRARY)(%))
	$(RANLIB) $(LIBRARY)

clean :
	- $(RM) *.o *.i a.out core $(EXTRA_CLEAN)

realclean : clean
	- $(RM) tlin tproj1 tproj2 tsph tcel twcs1 twcs2 $(LIBRARY)

test : show_all tlin tproj1 $(TPROJ2) tsph $(TCEL) twcs1 $(TWCS2)
	-@ echo ""
	-@ echo "Running WCSLIB test programs:"
	-@ echo ""
	-@ tlin
	-@ echo ""
	-@ tproj1
        ifdef TPROJ2
	   -@ echo ""
	   -@ tproj2 < /dev/null
        endif
	-@ echo ""
	-@ tsph
        ifdef TCEL
	   -@ echo ""
	   -@ tcel < /dev/null
        endif
	-@ echo ""
	-@ twcs1
        ifdef TWCS2
	   -@ echo ""
	   -@ twcs2 < /dev/null
        endif

tlin tproj1 tsph twcs1 : % : %.f $(LIBRARY)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(LIBRARY)
	   $(RM) $@.o

tproj2 tcel twcs2 : % : %.f $(LIBRARY)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(LIBRARY) $(PGPLOTLIB)
	   $(RM) $@.o

show_all :
	-@ echo ""
	-@ echo "FLAVOUR   := $(FLAVOUR)"
	-@ echo "FC        := $(FC)"
	-@ echo "FFLAGS    := $(FFLAGS)"
	-@ echo "WCSTRIG   := $(WCSTRIG)"
	-@ echo "RANLIB    := $(RANLIB)"
	-@ echo "LDFLAGS   := $(LDFLAGS)"
	-@ echo "PGPLOTLIB := $(PGPLOTLIB)"
	-@ echo "EXTRA_CLEAN := $(EXTRA_CLEAN)"
