#-----------------------------------------------------------------------------
#   GNU makefile for building PGSBOX 3.6.
#
#   Summary of the main targets
#   ---------------------------
#      all:       Compile pgsbox.f and cpgsbox.c to produce object modules.
#      wcslib:    Compile pgwcsl.c and insert it into libwcs.a.
#      clean:     Delete intermediate object files.
#      cleaner:   clean, and also delete the test executables.
#      cleanest:  cleaner, and also delete the WCSLIB object library.
#      test:      Compile and run the test programs.
#      show_all:  Print the values of all variables used.
#
#   Notes:
#      1) In compiling the test programs, the makefile assumes that the
#         WCSLIB 3.6 sources reside in ../C and ../FORTRAN (as in the
#         distribution kit).
#
#      2) Refer to ../C/Makefile for an explanation of the FLAVOUR variable.
#
#   Author: Mark Calabretta, Australia Telescope National Facility
#   http://www.atnf.csiro.au/~mcalabre/index.html
#   $Id: Makefile,v 3.6 2004/08/24 09:05:36 cal103 Exp $
#-----------------------------------------------------------------------------
# FORTRAN compiler and options.
  FC := g77
  FFLAGS := -O

# C compiler and options.
  CC := gcc -ansi
  CFLAGS := -O

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

# Linker options.
  LD := g77
  LDFLAGS := -s

# Libraries required for building the test programs.
  WCSLIB := -L../C -lwcs
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lX11

# Extra required libraries.
  LIBS := -lm

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

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

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

ifeq "$(FLAVOUR)" "SUN/GNUp"
  FFLAGS  := -pg -a -g -O -Wimplicit -Wuninitialized -Wno-globals -I.
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  LDFLAGS := -pg -a -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lX11
  LIBS    := -L/opt/SUNWspro/lib -lF77 -lM77 -lsunmath -lm
  override EXTRA_CLEAN := gmon.out bb.out
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 -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 -lsunmath -lm
  override EXTRA_CLEAN := *_pure_q*.[ao] .pure
endif

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

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

ifeq "$(FLAVOUR)" "Linuxp"
  FFLAGS  := -pg -a -g -O -Wimplicit -Wuninitialized -Wno-globals -I.
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  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.

WCSLIB := ../C/libwcs.a

vpath %.h   ../C
vpath %.inc ../FORTRAN

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

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

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

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

%.o : %.F
	$(FC) $(FFLAGS) -I../FORTRAN -c -o $@ $<

%.o : %.f
	$(FC) $(FFLAGS) -I../FORTRAN -c -o $@ $<

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

.PHONY : all clean cleaner cleanest realclean test wcslib

all : show_all pgsbox.o cpgsbox.o

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

cleaner : clean
	- $(RM) pgtest cpgtest

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

test : wcslib show_all pgtest cpgtest
	-@ echo ""
	-@ echo "Running the PGSBOX test program, PGTEST:"
	-@ echo ""
	-@ pgtest < /dev/null
	-@ echo ""
	-@ echo "Running the cpgsbox() test program, cpgtest:"
	-@ echo ""
	-@ cpgtest < /dev/null

wcslib : $(WCSLIB)

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

$(WCSLIB) :: $(WCSLIB)(pgwcsl.o)
	$(RANLIB) $(WCSLIB)

pgtest : pgtest.o pgsbox.o pgcrfn.o lngvel.o fscan.o $(WCSLIB)
	-@ rm -f $@
	$(LD) $(LDFLAGS) -o $@ $^ $(WCSLIB) $(PGPLOTLIB) $(LIBS)

cpgtest : cpgtest.o cpgsbox.o pgsbox.o pgcrfn.o lngvel.o fscan.o $(WCSLIB)
	-@ rm -f $@
	$(LD) $(LDFLAGS) -o $@ $^ $(WCSLIB) $(PGPLOTLIB) $(LIBS)

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

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

cpgsbox.o : cpgsbox.h

$(WCSLIB)(pgwcsl.o) : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h

pgtest.o  : wcs.inc
cpgtest.o : cel.h cpgsbox.h cylfix.h lin.h prj.h spc.h spx.h wcs.h
