#-----------------------------------------------------------------------------
#   GNU makefile for building PGSBOX 3.5.
#
#   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.5 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.5 2004/06/28 05:43:01 mcalabre Exp $
#-----------------------------------------------------------------------------
# FORTRAN compiler and options.
  FC := g77
  FFLAGS := -O

# C compiler and options.
  CC := gcc
  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.
  LIBS := -L../C -lwcs
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -lX11

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

ifeq "$(FLAVOUR)" "SUN/GNU"
  FFLAGS  += -fPIC -Wimplicit -Wuninitialized -Wno-globals -I.
  CC      := gcc -ansi
  CFLAGS  += -fPIC -Wall -Wno-parentheses
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -lX11
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_g77 -lpng -lz -lX11
endif

ifeq "$(FLAVOUR)" "SUN/GNUp"
  FFLAGS  := -fPIC -pg -O -Wimplicit -Wuninitialized -Wno-globals -I.
  CC      := gcc -ansi
  CFLAGS  := -fPIC -pg -O -Wall -Wno-parentheses
  LDFLAGS := -pg
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -lX11
  override EXTRA_CLEAN := gmon.out
endif

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

ifeq "$(FLAVOUR)" "Linux"
  CC      := gcc -ansi
  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  := -fPIC -pg -O -Wimplicit -Wuninitialized -Wno-globals -I.
  CC      := gcc -ansi
  CFLAGS  := -pg -g -O -fPIC -Wall -Wno-parentheses
  LD      := g77
  LDFLAGS := -pg -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
endif

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

WCSLIB := ../C/libwcs.a

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

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

%.o : %.c
	$(CC) $(CFLAGS) -I. -I../C -I/usr/local/include -c -o $@ $<

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

%.d : %.c
	-@ $(CC) $(CFLAGS) -I. -I../C -I/usr/local/include -E $< | \
	   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

cleaner : clean
	$(RM) pgtest cpgtest

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

test : wcslib 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) ::
	$(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 $@ $^ $(LIBS) $(PGPLOTLIB)

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

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 "LIBS      := $(LIBS)"
	-@ echo "PGPLOTLIB := $(PGPLOTLIB)"
	-@ 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
