#-----------------------------------------------------------------------------
#   GNU makefile for building the WCSLIB 4.0 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/Pure   SUN/GNU with "Purify" and "PureCoverage".
#            SUN/Quant  SUN/GNU with "Quantify".
#            SUN/ANSI   ANSI C compiler running under some version of SunOS.
#            Linux      Some version of Linux.
#            Linuxp     Some version of Linux, with profiling options.
#
#         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 4.0 2005/02/04 12:32:15 cal103 Exp $
#-----------------------------------------------------------------------------
# C compiler and options.
  CC := gcc -ansi
  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

# PGPLOT is only required for the test programs that plot test grids (tprj2,
# tcel1, tspc, twcsmix, and tpih2).  You can circumvent this by setting
# DO_PLOTS to blank.  PGPLOT is Tim Pearson's FORTRAN plotting library with
# separate C interface available from astro.caltech.edu.
  DO_PLOTS  := 1
  PGPLOTLIB := -lpgplot -lpng -lz -lX11

# Installation utilities.
  INSTALL := install

# Extra files to clean up.
  override EXTRA_CLEAN :=

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

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

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

ifeq "$(FLAVOUR)" "SUN/GNUp"
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  FFLAGS  := -pg -a -g -O -Wimplicit -Wuninitialized -Wno-globals -I.
  LDFLAGS := -pg -a -g
  PGPLOTLIB := -lpgplot -lpng -lz -lX11
  LIBS    := -L/opt/SUNWspro/lib -lF77 -lM77 -lsunmath -lm
  override EXTRA_CLEAN := gmon.out bb.out
endif

ifeq "$(FLAVOUR)" "SUN/ANSI"
  CC      := cc
  CFLAGS  += -I/usr/local/include
  FC      := f77 -erroff=WDECL_LOCAL_NOTUSED
  PGPLOTLIB := -lpgplot -lpng -lz -L/usr/local/X11/lib -lX11
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/Pure"
  CC      := purify purecov gcc
  CFLAGS  := -g
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -L/usr/local/X11/lib -lX11
  LIBS    := -L/opt/SUNWspro/lib -lF77 -lM77 -lsunmath -lm
  override EXTRA_CLEAN := *_pure_p*.[ao] *.pcv .pure
endif

ifeq "$(FLAVOUR)" "SUN/Quant"
  CC      := quantify gcc
  CFLAGS  := -g
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -L/usr/local/X11/lib -lX11
  LIBS    := -L/opt/SUNWspro/lib -lF77 -lM77 -lsunmath -lm
  override EXTRA_CLEAN := *_pure_q*.[ao] .pure
endif

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

ifeq "$(FLAVOUR)" "Linuxp"
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  FFLAGS  := -pg -a -g -O -Wimplicit -Wuninitialized -Wno-globals -I.
  LDFLAGS := -pg -a -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
  override EXTRA_CLEAN := gmon.out bb.out
endif

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

# Version.
V := 4.0

WCSLIB  := ../C/libwcs-$V.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 "$(DO_PLOTS)" ""
  TPRJ2   := tprj2
  TCEL    := tcel1
  TSPC    := tspc
  TWCSMIX := twcsmix
  TPIH2   := tpih2
endif

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

vpath %.h ../C

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

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

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

%.d : %.c
	-@ echo ""
	-@ $(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 install lib realclean test

all : show_all lib

lib : $(WCSLIB)

$(WCSLIB) ::
	-@ echo ""
	   $(MAKE) -C ../C lib

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

install : lib
	$(MAKE) -C ../C install
	$(INSTALL) -m 444 CHANGES /usr/local/include/wcslib/CHANGES_FORTRAN
	$(INSTALL) -m 444 README  /usr/local/include/wcslib/README_FORTRAN
	$(INSTALL) -m 444 *.inc /usr/local/include/wcslib

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

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

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

test : show_all tlin tprj1 $(TPRJ2) tsph $(TCEL) tspx $(TSPC) \
       twcs twcssub $(TWCSMIX) 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 ""
	-@ twcs
	-@ echo ""
	-@ twcssub
        ifdef TWCSMIX
	   -@ echo ""
	   -@ twcsmix < /dev/null
        endif
	-@ echo ""
	-@ tpih1
        ifdef TPIH2
	   -@ echo ""
	   -@ tpih2 < /dev/null
	   -@ echo ""
        endif

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

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

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

$(PGSBOX) $(WCSLIB)(pgwcsl.o) :
	-@ echo ""
	   $(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 "DO_PLOTS  := $(DO_PLOTS)"
	-@ echo "PGPLOTLIB := $(PGPLOTLIB)"
	-@ echo "LIBS      := $(LIBS)"
	-@ echo "INSTALL   := $(INSTALL)"
	-@ 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
twcs    : cel.inc prj.inc wcs.inc
twcssub : cel.inc prj.inc wcs.inc
twcsmix : cel.inc lin.inc prj.inc wcs.inc
