#-----------------------------------------------------------------------------
# GNU makefile for building the WCSLIB FORTRAN wrappers.
#
# 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/GNU    GNU  C compiler running under some version of SunOS.
#          SUN/GNUp   As above with profiling options.
#          SUN/ANSI   ANSI C compiler running under some version of SunOS.
#          Linux      Some version of Linux.
#          DEC/Alpha  DEC/Alpha running Digital Unix (OSF/1).
#
#       Note that K&R C is no longer supported.
#
#       The FLAVOUR may conveniently be set as an environment variable or on
#       the GNU make command line, e.g.
#
#          gmake FLAVOUR=SUN/GNU
#
#       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 3.0 2003/04/01 05:32:40 mcalabre Exp $
#-----------------------------------------------------------------------------
# C compiler and options.
  CC := gcc
  CFLAGS := -O

# FORTRAN compiler and options.
  FC := g77
  FFLAGS := -O

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

# Linker options.
  LDFLAGS := -s

# Extra required libraries.
  LIBS := -lm

# The PGPLOTLIB option is only required for compiling test programs (tprj2,
# tcel, tspc, and twcs2) which use PGPLOT to plot test grids.  You can
# circumvent this by setting PGPLOTLIB to blank.  PGPLOT is a FORTRAN plotting
# library with separate C interface available from astro.caltech.edu.

# PGPLOT link list; unset this to defeat compilation of test programs which
# plot test grids.
  PGPLOTLIB := -lpgplot -lpng -lX11

# Extra files to clean up.
  override EXTRA_CLEAN :=

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

ifeq "$(FLAVOUR)" "SUN/GNU"
  CC      := gcc
  CFLAGS  += -Wall -Wno-parentheses
  FFLAGS  += -Wimplicit -Wuninitialized -Wno-globals -I.
  PGPLOTLIB := -lpgplot_g77 -lpng -lX11
  LIBS    := -L/opt/SUNWspro/lib -lsunmath
endif

ifeq "$(FLAVOUR)" "SUN/GNU3"
  CC      := gcc-3.1.1
  CFLAGS  += -Wall -Wno-parentheses
  FC      := g77-3.1.1
  FFLAGS  += -Wimplicit -Wuninitialized -Wno-globals -I.
  PGPLOTLIB := -lpgplot_g77 -lpng -lX11
  LIBS    := -L/opt/SUNWspro/lib -lsunmath
endif

ifeq "$(FLAVOUR)" "SUN/GNUp"
  CC      := gcc
  CFLAGS  := -pg -O -Wall -Wno-parentheses
  FFLAGS  := -pg -O -Wimplicit -Wuninitialized -Wno-globals -I.
  LDFLAGS := -pg
  PGPLOTLIB := -lpgplot_g77 -lpng -lX11
  LIBS    := -L/opt/SUNWspro/lib -lsunmath
  override EXTRA_CLEAN := gmon.out
endif

ifeq "$(FLAVOUR)" "SUN/ANSI"
  CC      := cc
  FC      := f77 -erroff=WDECL_LOCAL_NOTUSED
  LIBS    := -lsunmath -lm
  CFLAGS  += -I/usr/local/include
endif

ifeq "$(FLAVOUR)" "Linux"
  CC      := gcc
  CFLAGS  += -Wall -Wno-parentheses
  FFLAGS  += -Wimplicit -Wuninitialized -Wno-globals -I.
  PGPLOTLIB := -lpgplot -lg2c -L/usr/local/X11/lib -lpng -lX11
endif

ifeq "$(FLAVOUR)" "DEC/Alpha"
  CFLAGS  += -D"atan2d=atand2"
endif

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

WCSLIB  := ../C/libwcs.a
MODULES := lin_f.o  \
           prj_f.o  \
           sph_f.o  \
           cel_f.o  \
           spx_f.o  \
           spc_f.o  \
           wcs_f.o

ifneq "$(PGPLOTLIB)" ""
  TPRJ2 := tprj2
  TCEL  := tcel
  TSPC  := tspc
  TWCS2 := twcs2
endif

vpath %.h ../C

%.o : %.c
	-@ echo ""
	$(CC) $(CFLAGS) -I../C -c $<

%.i : %.c
	-@ echo ""
	$(CC) -E $(CFLAGS) -I../C -c $< > $@

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

.PHONY : all clean cleaner cleanest lib realclean test

all : show_all lib

lib : $(WCSLIB)

$(WCSLIB) ::
	$(MAKE) -C ../C lib

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

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

realclean cleaner cleanest : clean
	- $(RM) tlin tprj1 tprj2 tsph tcel tspx tspc twcs1 twcs2 twcsprt \
                $(WCSLIB)

test : show_all tlin tprj1 $(TPRJ2) tsph $(TCEL) tspx $(TSPC) twcs1 $(TWCS2) \
       twcsprt
	-@ echo ""
	-@ echo "Running WCSLIB test programs:"
	-@ echo ""
	-@ tlin
	-@ echo ""
	-@ tprj1
        ifdef TPRJ2
	   -@ echo ""
	   -@ tprj2 < /dev/null
        endif
	-@ echo ""
	-@ tsph
        ifdef TCEL
	   -@ echo ""
	   -@ tcel
        endif
	-@ echo ""
	-@ tspx
        ifdef TSPC
	   -@ echo ""
	   -@ tspc < /dev/null
        endif
	-@ echo ""
	-@ twcs1
        ifdef TWCS2
	   -@ echo ""
	   -@ twcs2 < /dev/null
        endif
	-@ echo ""
	-@ twcsprt

tlin tprj1 tsph tspx twcs1 twcsprt : % : test/%.f $(WCSLIB)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< -L../C -lwcs $(LIBS)
	   $(RM) $@.o

tprj2 tcel tspc twcs2 : % : test/%.f $(WCSLIB)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(PGPLOTLIB) -L../C -lwcs $(LIBS)
	   $(RM) $@.o

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

$(WCSLIB)(lin_f.o) : lin.h
$(WCSLIB)(prj_f.o) : prj.h
$(WCSLIB)(sph_f.o) : sph.h
$(WCSLIB)(cel_f.o) : cel.h prj.h
$(WCSLIB)(spx_f.o) : spx.h
$(WCSLIB)(spc_f.o) : spc.h spx.h
$(WCSLIB)(wcs_f.o) : wcs.h lin.h cel.h prj.h spc.h spx.h

tlin    : lin.inc
tprj1   : prj.inc
tprj2   : prj.inc
tcel    : cel.inc
tspx    : spx.inc
tspc    : spc.inc spx.inc
twcs1   : wcs.inc lin.inc cel.inc prj.inc spc.inc spx.inc
twcsprt : wcs.inc lin.inc cel.inc prj.inc spc.inc spx.inc
