#-----------------------------------------------------------------------------
#   GNU makefile for building WCSLIB 4.1.
#
#   Summary of the main targets
#   ---------------------------
#      all:       Build the library
#      clean:     Delete intermediate object files.
#      cleaner:   clean, and also delete the test executables.
#      cleanest:  cleaner, and also delete 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.
#
#         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 4.1 2005/08/30 08:23:54 cal103 Exp $
#-----------------------------------------------------------------------------
  CPPFLAGS := -I.

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

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

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

# Use the C compiler to link executables.
  LD  = $(CC)
  LDFLAGS := -s

# Define the native 64-bit integer data type if your compiler supports it.
# WCS_INT64 := long long int

# 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

# PGPLOT is only required for the test programs that plot test grids (tprj2,
# tprj2_s, tcel1, tcel2, tspc, ttab2 ttab3, twcsmix, and tpih2).  You can
# circumvent this by setting DO_PLOTS to blank.  PGPLOT is Tim Pearson's
# FORTRAN plotting library with separate C interface available from
# astro.caltech.edu.
  DO_PLOTS  := 1
  PGPLOTLIB := -L/usr/local/lib -lcpgplot -lpgplot -lX11

# CFITSIO definitions are optionally required for test programs tfitshdr,
# tpih1, tpih2, and non-optionally for getwcstab and twcstab.
  CFITSIOINC := /usr/local/include/cfitsio
  CFITSIOLIB := -L/usr/local/lib -lcfitsio

# Extra required libraries.
  LIBS := -lm

# Installation utilities.
  LN := ln
  INSTALL := install

# Extra files to clean up.
  override EXTRA_CLEAN :=

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

ifeq "$(FLAVOUR)" "SUN/GNU"
  CFLAGS  += -Wall -Wno-parentheses
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11 \
               -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/GNU3"
  CC      := gcc-3.1.1 -ansi
  CFLAGS  += -Wall -Wno-parentheses
  LD      := gcc-3.1.1
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11 \
               -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/GNUp"
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  LDFLAGS := -pg -a -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11 \
               -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm
  override EXTRA_CLEAN := gmon.out bb.out
endif

ifeq "$(FLAVOUR)" "SUN/ANSI"
  CC      := cc
  CFLAGS  += -I/usr/local/include
  WCSTRIG := NATIVE
  LD      := f77
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm
endif

ifeq "$(FLAVOUR)" "SUN/Pure"
  CC      := purify gcc
  CFLAGS  := -g
  FC      := purify gcc
  FFLAGS  := -g -Wimplicit -Wno-globals -I.
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11 \
               -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm -lg2c
  override EXTRA_CLEAN := *_pure_p*.[ao] *.pcv .pure
endif

ifeq "$(FLAVOUR)" "SUN/Quant"
  CC      := quantify gcc
  CFLAGS  := -g
  FC      := quantify gcc
  FFLAGS  := -g -Wimplicit -Wno-globals -I.
  WCSTRIG := NATIVE
  LDFLAGS :=
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -lz -lg2c -lX11 \
               -L/opt/SUNWspro/lib -lF77 -lM77
  CFITSIOLIB += -lsocket
  LIBS    := -lsunmath -lm -lg2c
  override EXTRA_CLEAN := *_pure_q*.[ao] .pure
endif

ifeq "$(FLAVOUR)" "Linux"
  CFLAGS  += -Wall -Wno-parentheses
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
  LIBS    := -lg2c -lm
endif

ifeq "$(FLAVOUR)" "Linuxp"
  CFLAGS  := -pg -a -g -O -Wall -Wno-parentheses
  LDFLAGS := -pg -a -g
  PGPLOTLIB := -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11
  LIBS    := -lg2c -lm
  override EXTRA_CLEAN := gmon.out bb.out
endif

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

# Version.
V := 4.1

VPATH := .:./test

WCSLIB  := libwcs-$V.a
MODULES := cel.o \
           fitshdr.o \
           getwcstab.o \
           lin.o \
           log.o \
           prj.o \
           spc.o \
           sph.o \
           spx.o \
           tab.o \
           wcs.o \
           wcsfix.o \
           wcshdr.o \
           wcspih.o \
           wcsulex.o  \
           wcsunits.o \
           wcsutil.o \
           wcsutrn.o

ifneq "$(WCS_INT64)" ""
  CFLAGS += -DWCS_INT64='$(WCS_INT64)'
endif

ifneq "$(DO_WCSHDR)" ""
  CFLAGS += -DDO_WCSHDR
endif

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

# Test programs that don't require PGPLOT.
TEST_N := tlin tlog tprj1 tsph tspx ttab1 twcs twcssub tpih1 tfitshdr tunits \
          twcsfix

# Test programs that do require PGPLOT.
TEST_P := tspc tprj2 tcel1 tcel2 ttab2 ttab3 twcsmix

TESTS  := $(TEST_N)
ifneq "$(DO_PLOTS)" ""
  TESTS   += $(TEST_P) tpih2
  TPRJ2_S := run_tprj2_s
endif

TESTS  += twcstab


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

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

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

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

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

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

run_% : %
	-@ echo ""
	 @ if [ "$<" = tcel2 ] ; then \
	     echo N | $< ; \
	   elif [ "$<" = tunits ] ; then \
	     $< < test/units_test ; \
	   else \
	     $< < /dev/null ; \
	   fi
	-@ echo ""

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

.PHONY : all clean cleaner cleanest install lib realclean test test_s

all : show_all fitshdr.c wcspih.c wcsulex.c wcsutrn.c lib

lib : $(WCSLIB)

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

$(WCSLIB)(getwcstab.o) : getwcstab.c
	-@ echo ""
	   $(CC) $(CPPFLAGS) $(CFLAGS) -I$(CFITSIOINC) -c $<
	   $(AR) r $(WCSLIB) getwcstab.o
	-@ $(RM) getwcstab.o

install : lib
	$(INSTALL) -m 644 $(WCSLIB) /usr/local/lib
	$(RM) /usr/local/lib/libwcs.a
	$(LN) -s /usr/local/lib/$(WCSLIB) /usr/local/lib/libwcs.a
	$(INSTALL) -d -m 2755 /usr/local/include/wcslib-$V
	$(INSTALL) -m 444 ../COPYING /usr/local/include/wcslib-$V/COPYING
	$(INSTALL) -m 444 CHANGES /usr/local/include/wcslib-$V/CHANGES_C
	$(INSTALL) -m 444 README  /usr/local/include/wcslib-$V/README_C
	$(INSTALL) -m 444 *.h /usr/local/include/wcslib-$V
	$(RM) /usr/local/include/wcslib
	$(LN) -s /usr/local/include/wcslib-$V /usr/local/include/wcslib

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

cleaner : clean
	- $(RM) $(TEST_N)
	- $(RM) $(TEST_P) tpih2 twcstab
	- $(RM) tofits test.fits wcstab.fits
	- $(RM) t*_cfitsio
	- $(RM) tlin_s tprj1_s tprj2_s tsph_s

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

test : show_all $(TESTS) $(TESTS:%=run_%)

$(TEST_N) : % : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(WCSLIB) $(LIBS)
	   $(RM) $@.o

$(TEST_P) : % : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(WCSLIB) $(PGPLOTLIB) $(LIBS)
	   $(RM) $@.o

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

tfitshdr_cfitsio tpih1_cfitsio : %_cfitsio : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) -DDO_CFITSIO $(CPPFLAGS) $(CFLAGS) -I$(CFITSIOINC) \
	      $(LDFLAGS) -o $@ $< $(WCSLIB) $(CFITSIOLIB) $(LIBS)
	   $(RM) $@.o

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

twcstab : test/twcstab.c test/wcstab.cards $(WCSLIB)
	-@ echo ""
	   $(CC) -DDO_CFITSIO $(CPPFLAGS) $(CFLAGS) -I$(CFITSIOINC) \
	      $(LDFLAGS) -o $@ $< $(WCSLIB) $(CFITSIOLIB) $(LIBS)
	   $(RM) $@.o

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

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

test.fits : test/wcs.cards tofits
	sed '/^BADCARD/q' $< | tofits > $@

test_s : show_all run_tlin_s run_tprj1_s $(TPRJ2_S) run_tsph_s ;

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

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

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

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

$(WCSLIB)(cel.o)      : cel.h prj.h sph.h wcsmath.h wcstrig.h
$(WCSLIB)(fitshdr.o)  : fitshdr.h
$(WCSLIB)(getwcstab.o): getwcstab.h
$(WCSLIB)(lin.o)      : lin.h
$(WCSLIB)(log.o)      : log.h
$(WCSLIB)(prj.o)      : prj.h wcsmath.h wcstrig.h
$(WCSLIB)(spc.o)      : spc.h spx.h wcsmath.h wcstrig.h
$(WCSLIB)(sph.o)      : sph.h wcstrig.h
$(WCSLIB)(spx.o)      : spx.h
$(WCSLIB)(tab.o)      : tab.h wcsmath.h
$(WCSLIB)(wcs.o)      : cel.h lin.h log.h prj.h spc.h sph.h spx.h tab.h \
                        wcs.h wcsmath.h wcstrig.h wcsunits.h wcsutil.h
$(WCSLIB)(wcsfix.o)   : cel.h lin.h prj.h spc.h sph.h spx.h tab.h wcs.h \
                        wcsfix.h wcsmath.h wcsunits.h
$(WCSLIB)(wcshdr.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                        wcsutil.h
$(WCSLIB)(wcspih.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                        wcsmath.h
$(WCSLIB)(wcstrig.o)  : wcsmath.h wcstrig.h
$(WCSLIB)(wcsulex.o)  : wcsmath.h wcsunits.h
$(WCSLIB)(wcsunits.o) : wcsunits.h
$(WCSLIB)(wcsutil.o)  : wcsutil.h
$(WCSLIB)(wcsutrn.o)  : wcsunits.h

tcel1   : cel.h prj.h
tcel2   : cel.h prj.h
tfitshdr tfitshdr_cfitsio : fitshdr.h test.fits
tlin    : lin.h
tlin_s  : lin.h
tlog    : log.h
tpih1 tpih1_cfitsio : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcsfix.h \
                      wcshdr.h test.fits
tpih2 tpih2_cfitsio : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                      test.fits
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
ttab1   : tab.h
ttab2   : tab.h
ttab3   : prj.h tab.h
tunits  : wcsunits.h
twcs    : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h
twcsfix : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcsfix.h wcsunits.h
twcsmix : cel.h lin.h prj.h spc.h sph.h spx.h tab.h wcs.h
twcssub : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h
twcstab : cel.h getwcstab.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h
