#-----------------------------------------------------------------------------
#   GNU makefile for building the WCSLIB 3.5 FORTRAN wrappers.
#
#   Summary of the main targets
#   ---------------------------
#      all:       Build the library
#      clean:     Delete intermediate object files.
#        cleaner:   clean, and also delete the test executables.
#      cleanest:  cleaner, and also delete 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.
#
#   Author: Mark Calabretta, Australia Telescope National Facility
#   http://www.atnf.csiro.au/~mcalabre/index.html
#   $Id: Makefile,v 3.5 2004/06/28 05:20:01 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,
# tcel1, 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 -lz -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 -lz -lX11
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 -lz -lX11
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 -lz -lX11
  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 -lpng -lz -L/usr/X11R6/lib -lX11
endif

ifeq "$(FLAVOUR)" "DEC/Alpha"
  CC      := cc
  CFLAGS  += -D"atan2d=atand2"
  FC      := f77 -I.
endif

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

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

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

PGSBOX := ../pgsbox/pgsbox.o
PGPLOTLIB := $(PGPLOTLIB)

vpath %.h ../C

# Pattern rules
#--------------

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

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

%.d : %.c
	-@ $(CC) $(CFLAGS) -E -I../C $< | \
	   sed -n -e 's|^# 1 "\([^/].*\.h\)".*|\1|p' | \
	   sort -u

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

# Static and static pattern rules
#--------------------------------

.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)

cleaner : clean
	- $(RM) tlin tprj1 tprj2 tsph tcel1 tspx tspc
	- $(RM) twcs1 twcs2
	- $(RM) tofits wcs.fits tpih1 tpih2

cleanest realclean : cleaner
	- $(RM) $(WCSLIB)

test : show_all tlin tprj1 $(TPRJ2) tsph $(TCEL) tspx $(TSPC) \
       twcs1 $(TWCS2) wcs.fits tpih1 $(TPIH2)
	-@ echo ""
	-@ echo "Running WCSLIB FORTRAN test programs:"
	-@ echo ""
	-@ tlin
	-@ echo ""
	-@ tprj1
        ifdef TPRJ2
	   -@ echo ""
	   -@ tprj2 < /dev/null
        endif
	-@ echo ""
	-@ tsph
        ifdef TCEL
	   -@ echo ""
	   -@ tcel1
        endif
	-@ echo ""
	-@ tspx
        ifdef TSPC
	   -@ echo ""
	   -@ tspc < /dev/null
        endif
	-@ echo ""
	-@ twcs1
        ifdef TWCS2
	   -@ echo ""
	   -@ twcs2 < /dev/null
        endif
	-@ echo ""
	-@ tpih1
        ifdef TPIH2
	   -@ echo ""
	   -@ tpih2 < /dev/null
	   -@ echo ""
        endif

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

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

tpih2 : % : test/%.f $(WCSLIB) $(PGSBOX) $(WCSLIB)(pgwcsl.o)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(PGSBOX) -L../C -lwcs $(PGPLOTLIB) $(LIBS)
	   $(RM) $@.o

$(PGSBOX) $(WCSLIB)(pgwcsl.o) :
	$(MAKE) -C ../pgsbox show_all pgsbox.o wcslib

tofits : ../C/test/tofits.c
	$(CC) $(CFLAGS) -o $@ $<

wcs.fits : ../C/test/wcs.cards tofits
	tofits < $< > $@

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)"
	-@ echo ""

# Dependencies
#-------------

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

tcel1   : cel.inc prj.inc
tlin    : lin.inc
tpih1   : wcs.inc wcshdr.inc
tpih2   : wcs.inc wcshdr.inc
tprj1   : prj.inc
tprj2   : prj.inc
tspc    : spc.inc spx.inc
tspx    : spx.inc
twcs1   : cel.inc prj.inc wcs.inc
twcs2   : cel.inc lin.inc prj.inc wcs.inc
