#-----------------------------------------------------------------------------
# GNU makefile for building WCSLIB 3.4.
#
# 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.
#
# $Id: Makefile,v 3.4 2004/02/11 00:11:52 mcalabre Exp $
#-----------------------------------------------------------------------------
# C compiler and options.
  CC := gcc
  CFLAGS := -O

# 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) which 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 which
# plot test grids.
  PGPLOTLIB := -L/usr/local/lib -lcpgplot -lpgplot -lX11

# 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 := lin.o \
           prj.o \
           sph.o \
           cel.o \
           spx.o \
           spc.o \
           wcs.o \
           cylfix.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
  TWCS2   := twcs2
endif

%.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

.PHONY : all clean cleaner cleanest lib realclean test test_s

all : show_all lib

lib : $(WCSLIB)

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

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

realclean cleaner cleanest : clean
	- $(RM) tlin tlin_s tprj1 tprj1_s tprj2 tprj2_s tsph tsph_s tcel1 \
                tcel2 tspx tspc twcsprt twcs1 twcs2 $(WCSLIB)

test : show_all tlin tprj1 $(TPRJ2) tsph $(TCEL) tspx twcsprt tspc twcs1 \
       $(TWCS2)
	-@ 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
	-@ tspc < /dev/null
	-@ echo ""
	-@ twcsprt
	-@ echo ""
	-@ twcs1
        ifdef TWCS2
	   -@ echo ""
	   -@ twcs2 < /dev/null
        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 twcsprt twcs1 : % : 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

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

$(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)(wcstrig.o) : wcsmath.h wcstrig.h

tlin    : lin.h
tlin_s  : lin.h
tcel1   : cel.h prj.h
tcel2   : cel.h prj.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
twcsprt : cel.h lin.h prj.h spc.h spx.h wcs.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
