#-----------------------------------------------------------------------------
# GNU makefile for building WCSLIB.
#
# 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.
#    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/ANSI   ANSI C compiler running under some version of SunOS.
#          SUN/Pure   SUN/ANSI with "Purify" and "PureCoverage".
#          SUN/Quant  SUN/ANSI with "Quantify".
#          SUN/K&R    K&R  C compiler running under some version of SunOS.
#          Linux      Some version of Linux.
#          DEC/Alpha  DEC/Alpha running Digital Unix (OSF/1).
#
#       The FLAVOUR may conveniently be set as an environment variable or on
#       the GNU make command line, e.g.
#
#          gmake FLAVOUR='SUN/K&R'
#
#       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.
#
#    2) Preprocessor macro replacements for the IEEE copysign() and signbit()
#       functions are available by defining the COPYSIGN and/or SIGNBIT
#       preprocessor macros.
#
# $Id: Makefile,v 2.12 2001/11/15 03:10:17 mcalabre Exp $
#-----------------------------------------------------------------------------
# C compiler and options.
  CC := cc
  CFLAGS := -O

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

# Linker options.
  LDFLAGS := -s

# Extra required libraries.
  LIBS := -lm

# If your mathematics library has cosd(), sind(), tand(), acosd(), asind(),
# atand(), and atan2d() and you want to use them then declare the required
# header file here.
  TRIGD :=

# The PGPLOTLIB option is only required for compiling test programs (tproj2,
# tcel, 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 = -lcpgplot -lpgplot -lF77 -lM77 -lX11

# Extra files to clean up.
  override EXTRA_CLEAN :=

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

ifeq "$(FLAVOUR)" "SUN/GNU"
   CC      := gcc
   LIBS    := -L/opt/SUNWspro/lib -lsunmath -lm
endif

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

ifeq "$(FLAVOUR)" "SUN/K&R"
   CC      := /usr/ucb/cc
   CFLAGS  += -DSIGNBIT
endif

ifeq "$(FLAVOUR)" "SUN/Pure"
   CC      := purify purecov cc
   CFLAGS  := -g
   LIBS    := -lsunmath -lm
   TRIGD   := sunmath.h
   LDFLAGS :=
   override EXTRA_CLEAN := *_pure_p*.[ao] *.pcv .pure
endif

ifeq "$(FLAVOUR)" "SUN/Quant"
   CC      := quantify cc
   CFLAGS  := -g
   LIBS    := -lsunmath -lm
   TRIGD   := sunmath.h
   LDFLAGS :=
   override EXTRA_CLEAN := *_pure_q*.[ao] .pure
endif

ifeq "$(FLAVOUR)" "Linux"
   CC      := gcc
   CFLAGS  += -DSIGNBIT -I/usr/local/include
   CFLAGS  += -Wall -Wno-uninitialized -Wno-parentheses
   PGPLOTLIB := -lcpgplot -lpgplot -lg2c -L/usr/local/X11/lib -lX11
endif

ifeq "$(FLAVOUR)" "DEC/Alpha"
   CFLAGS  += -DSIGNBIT -D"atan2d=atand2"
   TRIGD   := math.h
endif

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

ifeq "$(TRIGD)" ""
   WCSTRIG := wcstrig.o
endif

LIBRARY := libwcs_c.a
MODULES := $(WCSTRIG) \
           lin.o  \
           proj.o \
           sph.o  \
           cel.o  \
           wcs.o

ifneq "$(PGPLOTLIB)" ""
   TPROJ2 := tproj2
   TCEL   := tcel
   TWCS2  := twcs2
endif

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

.PHONY : all clean lib realclean test

all : show_all lib

lib : $(LIBRARY)

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

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

realclean : clean
	- $(RM) tlin tproj1 tproj2 tsph tcel twcs1 twcs2 $(LIBRARY)

test : show_all tlin tproj1 $(TPROJ2) tsph $(TCEL) twcs1 $(TWCS2)
	-@ echo ""
	-@ echo "Running WCSLIB test programs:"
	-@ echo ""
	-@ tlin
	-@ echo ""
	-@ tproj1
        ifdef TPROJ2
	   -@ echo ""
	   -@ tproj2 < /dev/null
        endif
	-@ echo ""
	-@ tsph
        ifdef TCEL
	   -@ echo ""
	   -@ tcel < /dev/null
        endif
	-@ echo ""
	-@ twcs1
        ifdef TWCS2
	   -@ echo ""
	   -@ twcs2 < /dev/null
        endif

tlin tproj1 tsph twcs1 : % : %.c $(LIBRARY)
	-@ echo ""
	   $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBRARY) $(LIBS)
	   $(RM) $@.o

tproj2 tcel twcs2 : % : %.o $(LIBRARY)
	-@ echo ""
	   $(CC) $(LDFLAGS) -o $@ $< $(LIBRARY) $(LIBS) $(PGPLOTLIB)

show_all :
	-@ echo ""
	-@ echo "FLAVOUR   := $(FLAVOUR)"
	-@ echo "CC        := $(CC)"
	-@ echo "CFLAGS    := $(CFLAGS)"
	-@ echo "RANLIB    := $(RANLIB)"
	-@ echo "LDFLAGS   := $(LDFLAGS)"
	-@ echo "LIBS      := $(LIBS)"
	-@ echo "TRIGD     := $(TRIGD)"
	-@ echo "WCSTRIG   := $(WCSTRIG)"
	-@ echo "PGPLOTLIB := $(PGPLOTLIB)"
	-@ echo "EXTRA_CLEAN := $(EXTRA_CLEAN)"

$(LIBRARY)(wcstrig.o) : wcstrig.h
$(LIBRARY)(lin.o)  : lin.h
$(LIBRARY)(proj.o) : wcsmath.h proj.h
$(LIBRARY)(sph.o)  : wcstrig.h
$(LIBRARY)(cel.o)  : cel.h
$(LIBRARY)(wcs.o)  : wcsmath.h wcs.h

tlin   : lin.h
tproj1 : proj.h
tproj2 : proj.h
tsph   : wcstrig.h
tcel   : cel.h
twcs1  : wcs.h
twcs2  : wcs.h

proj.h : wcstrig.h
cel.h  : proj.h
wcs.h  : cel.h lin.h
