#-----------------------------------------------------------------------------
#   GNU makefile for building WCSLIB 3.5.
#
#   Summary of the main targets
#   ---------------------------
#      all:       Build the library
#      clean:     Delete intermediate object files.
#      realclean: clean, and also delete executables and the object library.
#      test:      Compile and run the test programs.
#      test_s:    Compile and run the scalar 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.
#            SUN/Pure   SUN/GNU with "Purify" and "PureCoverage".
#            SUN/Quant  SUN/GNU with "Quantify".
#            Linux      Some version of Linux.
#            Linuxp     Some version of Linux, with profiling options.
#            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 04:41:01 mcalabre Exp $
#-----------------------------------------------------------------------------
# C compiler and options.
  CC := gcc
  CFLAGS := -O

# Flex; add -d to FLFLAGS for debugging.
  FLEX := flex
  FLFLAGS :=

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

# Linker options.
  LDFLAGS := -s

# Extra required libraries.
  LIBS := -lm

# There are three choices for trigd functions - cosd(), sind(), tand(),
# acosd(), asind(), atand(), and atan2d().  Uncomment one of the following
# choices (see also wcstrig.h).
#
#    1: Use the wrapper functions supplied with WCSLIB:
     WCSTRIG := WRAPPER
#
#    2: Use native trigd functions supplied in a mathematics library such as
#    libsunmath (also add the library to the LIBS variable):
   # WCSTRIG := NATIVE
#
#    3: Use C preprocessor macro implementations of the trigd functions (this
#    method is typically 20% faster but may lead to rounding errors near the
#    poles):
   # WCSTRIG := MACRO

# The PGPLOTLIB option is only required for compiling test programs (tprj2,
# tprj2_s tcel1, tcel2, tspc, and twcs2) that 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 that
# plot test grids.
  PGPLOTLIB := -L/usr/local/lib -lcpgplot -lpgplot -lX11

# CFITSIO definitions are only required for test program tpih2.
  CFITSIOINC := /usr/local/include/cfitsio
  CFITSIOLIB := -L/usr/local/lib -lcfitsio

# Use the FORTRAN compiler to link to PGPLOT.
  LD := f77

# Extra files to clean up.
  override EXTRA_CLEAN :=

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

ifeq "$(FLAVOUR)" "SUN/GNU"
  CC      := gcc -ansi
  CFLAGS  += -Wall -Wno-parentheses
  LD      := g77
  LIBS    := -L/opt/SUNWspro/lib -lm
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -L/usr/local/X11/lib -lX11
endif

ifeq "$(FLAVOUR)" "SUN/GNU3"
  CC      := gcc-3.1.1 -ansi
  CFLAGS  += -Wall -Wno-parentheses
  LD      := g77-3.1.1
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -L/usr/local/X11/lib -lX11
endif

ifeq "$(FLAVOUR)" "SUN/GNUp"
  CC      := gcc -ansi
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  LD      := g77
  LDFLAGS := -pg -a -g
  LIBS    := -L/opt/SUNWspro/SC5.0/lib/libp -lm
  PGPLOTLIB := -lcpgplot -lpgplot_g77 -lpng -lz -L/usr/local/X11/lib -lX11
  override EXTRA_CLEAN := gmon.out bb.out
endif

ifeq "$(FLAVOUR)" "SUN/ANSI"
  CC      := cc
  CFLAGS  += -I/usr/local/include
  WCSTRIG := NATIVE
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/Pure"
  CC      := purify purecov gcc
  CFLAGS  := -g
  WCSTRIG := NATIVE
  LDFLAGS :=
  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 :=
  LIBS    := -L/opt/SUNWspro/lib -lsunmath -lm
  override EXTRA_CLEAN := *_pure_q*.[ao] .pure
endif

ifeq "$(FLAVOUR)" "Linux"
  CC      := gcc -ansi
  CFLAGS  += -Wall -Wno-parentheses
  LD      := g77
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
endif

ifeq "$(FLAVOUR)" "Linuxp"
  CC      := gcc -ansi
  CFLAGS  := -pg -g -O -Wall -Wno-parentheses
  LD      := g77
  LDFLAGS := -pg -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
endif

ifeq "$(FLAVOUR)" "DEC/Alpha"
  CC      := cc
  CFLAGS  += -D"atan2d=atand2" -I/usr/local/include
  LD      := cc
  WCSTRIG := NATIVE
  PGPLOTLIB := -L/usr/local/lib -lcpgplot -lpgplot -lfor -lUfor -lX11
endif

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

WCSLIB  := libwcs.a
MODULES := cylfix.o \
           wcspih.o \
           wcshdr.o \
           wcs.o \
           spc.o \
           spx.o \
           cel.o \
           sph.o \
           prj.o \
           lin.o

ifeq "$(WCSTRIG)" "MACRO"
  CFLAGS += -DWCSTRIG_MACRO
else
  ifneq "$(WCSTRIG)" "NATIVE"
    MODULES += wcstrig.o
  endif
endif

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

PGSBOX := $(addprefix ../pgsbox/, cpgsbox.o pgsbox.o)

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

%.c : %.l
	-@ echo ""
	$(FLEX) $(FLFLAGS) -P$* $<
	$(RM) $@
	mv -f lex.$*.c $@

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

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

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

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

.PHONY : all clean cleaner cleanest lib realclean test test_s

all : show_all wcspih.c lib

lib : $(WCSLIB)

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

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

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

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 test programs:"
	-@ echo ""
	-@ tlin
	-@ echo ""
	-@ tprj1
        ifdef TPRJ2
	   -@ echo ""
	   -@ tprj2 < /dev/null
        endif
	-@ echo ""
	-@ tsph
        ifdef TCEL
	   -@ echo ""
	   -@ tcel1 < /dev/null
	   -@ echo ""
	   -@ echo N | tcel2
        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

test_s : show_all tlin_s tprj1_s $(TPRJ2_S) tsph_s
	-@ echo ""
	-@ echo "Running WCSLIB scalar test programs:"
	-@ echo ""
	-@ tlin_s
	-@ echo ""
	-@ tprj1_s
        ifdef TPRJ2_S
	   -@ echo ""
	   -@ tprj2_s < /dev/null
        endif
	-@ echo ""
	-@ tsph_s

tlin tprj1 tsph tspx twcs1 tpih1 : % : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. $(LDFLAGS) -o $@ $< $(WCSLIB) $(LIBS)
	   $(RM) $@.o

tprj2 tcel1 tcel2 tspc twcs2 : % : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(WCSLIB) $(PGPLOTLIB) $(LIBS)
	   $(RM) $@.o

tpih2 : test/tpih2.c $(WCSLIB) $(PGSBOX) $(WCSLIB)(pgwcsl.o)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. -I../pgsbox -I$(CFITSIOINC) -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(WCSLIB) $(PGSBOX) $(CFITSIOLIB) \
	      $(PGPLOTLIB) $(LIBS)
	   $(RM) $@.o

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

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

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

tlin_s tprj1_s tsph_s : % : test_s/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. $(LDFLAGS) -o $@ $< $(WCSLIB) $(LIBS)
	   $(RM) $@.o

tprj2_s : % : test_s/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(WCSLIB) $(PGPLOTLIB) $(LIBS)
	   $(RM) $@.o

show_all :
	-@ echo ""
	-@ echo "FLAVOUR     := $(FLAVOUR)"
	-@ echo "CC          := $(CC)"
	-@ echo "CFLAGS      := $(CFLAGS)"
	-@ echo "FLEX        := $(FLEX)"
	-@ echo "FLFLAGS     := $(FLFLAGS)"
	-@ echo "RANLIB      := $(RANLIB)"
	-@ echo "LDFLAGS     := $(LDFLAGS)"
	-@ echo "LIBS        := $(LIBS)"
	-@ echo "WCSTRIG     := $(WCSTRIG)"
	-@ echo "CFITSIOINC  := $(CFITSIOINC)"
	-@ echo "CFITSIOLIB  := $(CFITSIOLIB)"
	-@ echo "PGPLOTLIB   := $(PGPLOTLIB)"
	-@ echo "EXTRA_CLEAN := $(EXTRA_CLEAN)"
	-@ echo ""

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

$(WCSLIB)(cel.o)     : cel.h prj.h sph.h wcstrig.h
$(WCSLIB)(cylfix.o)  : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h
$(WCSLIB)(lin.o)     : lin.h
$(WCSLIB)(prj.o)     : prj.h wcsmath.h wcstrig.h
$(WCSLIB)(spc.o)     : spc.h spx.h wcstrig.h
$(WCSLIB)(sph.o)     : sph.h wcstrig.h
$(WCSLIB)(spx.o)     : spx.h
$(WCSLIB)(wcs.o)     : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h wcsmath.h \
                       wcstrig.h
$(WCSLIB)(wcshdr.o)  : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h wcshdr.h
$(WCSLIB)(wcspih.o)  : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h wcshdr.h
$(WCSLIB)(wcstrig.o) : wcsmath.h wcstrig.h

tcel1   : cel.h prj.h
tcel2   : cel.h prj.h
tlin    : lin.h
tlin_s  : lin.h
tpih1   : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h wcshdr.h
tpih2   : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h wcshdr.h
tprj1   : prj.h wcstrig.h
tprj1_s : prj.h wcstrig.h
tprj2   : prj.h
tprj2_s : prj.h
tspc    : spc.h spx.h wcstrig.h
tsph    : sph.h wcstrig.h
tsph_s  : sph.h wcstrig.h
tspx    : spx.h
twcs1   : cel.h lin.h prj.h spc.h spx.h wcs.h
twcs2   : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h
