#-----------------------------------------------------------------------------
#   GNU makefile for building the WCSLIB 4.1 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.1 2005/08/30 08:24:29 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

# CFITSIO definitions required for getwcstab_f.c and twcstab.
  CFITSIOINC := /usr/local/include/cfitsio
  CFITSIOLIB := -L/usr/local/lib -lcfitsio

# 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 -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -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 -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -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 -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -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
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/Pure"
  CC      := purify gcc
  CFLAGS  := -g
  FC      := purify gcc
  FFLAGS  := -g -Wimplicit -Wno-globals -I.
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lpgplot -lpng -lz -lX11 -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm -lg2c
  override EXTRA_CLEAN := *_pure_p*.[ao] *.pcv .pure ../C/*_pure_p*.[ao]
endif

ifeq "$(FLAVOUR)" "SUN/Quant"
  CC      := quantify gcc
  CFLAGS  := -g
  FC      := quantify gcc
  FFLAGS  := -g -Wimplicit -Wno-globals -I.
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lpgplot -lpng -lz -lX11 -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm -lg2c
  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.1

WCSLIB  := ../C/libwcs-$V.a
MODULES := cel_f.o \
           fitshdr_f.o \
           getwcstab_f.o \
           lin_f.o \
           log_f.o \
           prj_f.o \
           spc_f.o \
           sph_f.o \
           spx_f.o \
           tab_f.o \
           wcs_f.o \
           wcsfix_f.o \
           wcshdr_f.o \
           wcsunits_f.o

# Test programs that don't require PGPLOT.
TEST_N := tlin tlog tprj1 tsph tspx ttab1 twcs twcssub tpih1 tfitshdr tunits \
          twcsfix

# Test programs that do require PGPLOT.
TEST_P := tspc tprj2 tcel1 ttab2 ttab3 twcsmix

TESTS  := $(TEST_N)
ifneq "$(DO_PLOTS)" ""
  TESTS   += $(TEST_P) tpih2
endif

TESTS  += twcstab

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
	-@ echo ""
	-@ $(CC) $(CFLAGS) -E -I../C $< | \
	   sed -n -e 's|^# 1 "\([^/].*\.h\)".*|\1|p' | \
	   sort -u

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

run_% : %
	-@ echo ""
	 @ if [ "$<" = tunits ] ; then \
	     $< < ../C/test/units_test ; \
	   else \
	     $< < /dev/null ; \
	   fi
	-@ echo ""

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

$(WCSLIB)(getwcstab_f.o) : getwcstab_f.c
	-@ echo ""
	   $(CC) $(CFLAGS) -I../C -I$(CFITSIOINC) -c $<
	   $(AR) r $(WCSLIB) getwcstab_f.o
	-@ $(RM) getwcstab_f.o

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) $(TEST_N)
	- $(RM) $(TEST_P) tpih2 twcstab
	- $(RM) tofits test.fits

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

test : show_all $(TESTS) $(TESTS:%=run_%)

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

$(TEST_P) : % : 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

twcstab : test/twcstab.f ../C/wcstab.fits $(WCSLIB)
	-@ echo ""
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(WCSLIB) $(CFITSIOLIB) $(LIBS)
	   $(RM) $@.o

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

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

test.fits : ../C/test/wcs.cards tofits
	sed '/^BADCARD/q' $< | 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)(fitshdr_f.o)  : fitshdr.h wcsutil.h
$(WCSLIB)(getwcstab_f.o): getwcstab.h
$(WCSLIB)(lin_f.o)      : lin.h
$(WCSLIB)(log_f.o)      : log.h
$(WCSLIB)(prj_f.o)      : prj.h
$(WCSLIB)(spc_f.o)      : spc.h spx.h wcsutil.h
$(WCSLIB)(sph_f.o)      : sph.h
$(WCSLIB)(spx_f.o)      : spx.h
$(WCSLIB)(tab_f.o)      : tab.h
$(WCSLIB)(wcs_f.o)      : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcsutil.h
$(WCSLIB)(wcsfix_f.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcsfix.h
$(WCSLIB)(wcshdr_f.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h
$(WCSLIB)(wcsunits_f.o) : wcsunits.h wcsutil.h

tcel1   : cel.inc prj.inc
tfitshdr: fitshdr.inc test.fits
tlin    : lin.inc
tlog    : log.inc
tpih1   : wcs.inc wcshdr.inc test.fits
tpih2   : wcs.inc wcshdr.inc test.fits
tprj1   : prj.inc
tprj2   : prj.inc
tspc    : spc.inc spx.inc
tspx    : spx.inc
ttab1   : tab.inc
ttab2   : tab.inc
ttab3   : prj.inc tab.inc
tunits  : wcsunits.inc
twcs    : cel.inc prj.inc wcs.inc
twcsfix : wcs.inc
twcsmix : cel.inc lin.inc prj.inc wcs.inc
twcssub : cel.inc prj.inc wcs.inc
twcstab : getwcstab.inc wcs.inc wcshdr.inc
