#-----------------------------------------------------------------------------
#   GNU makefile for building WCSLIB 3.6.
#
#   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.
#
#         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.6 2004/08/25 05:26:20 cal103 Exp $
#   A. Pecontal 2004/10/04  ligher makefile version to be included in Euro3D IO libs
#-----------------------------------------------------------------------------
# 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

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

# Extra files to clean up.
  override EXTRA_CLEAN :=

# Overrides for various combinations of
# architecture, operating system and compiler.
#---------------------------------------------
# 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
  TWCSMIX := twcsmix
  TPIH2   := tpih2
endif

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

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

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

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

%.d : %.c
	-@ echo ""
	-@ $(CC) $(CFLAGS) -E -I. $< | \
	   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) twcs twcssub twcsmix
	- $(RM) tofits wcs.fits tpih1 tpih2
	- $(RM) tlin_s tprj1_s tprj2_s tsph_s

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

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 twcs twcssub tpih1 : % : test/%.c $(WCSLIB)
	-@ echo ""
	   $(CC) $(CFLAGS) -I. $(LDFLAGS) -o $@ $< $(WCSLIB) $(LIBS)
	   $(RM) $@.o

tprj2 tcel1 tcel2 tspc twcsmix : % : 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

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 "CC          := $(CC)"
	-@ echo "CFLAGS      := $(CFLAGS)"
	-@ echo "FLEX        := $(LEX)"
	-@ echo "FLFLAGS     := $(FLFLAGS)"
	-@ echo "RANLIB      := $(RANLIB)"
	-@ echo "LDFLAGS     := $(LDFLAGS)"
	-@ echo "WCSTRIG     := $(WCSTRIG)"
	-@ echo "CFITSIOINC  := $(CFITSIOINC)"
	-@ echo "CFITSIOLIB  := $(CFITSIOLIB)"
	-@ echo "LIBS        := $(LIBS)"
	-@ 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
twcs    : cel.h lin.h prj.h spc.h spx.h wcs.h
twcssub : cel.h lin.h prj.h spc.h spx.h wcs.h
twcsmix : cel.h lin.h prj.h spc.h sph.h spx.h wcs.h

# making ditribution
#-------------

distdir :
	@for file in `ls -t`; do \
	if test -d $$file ; then continue; fi; \
	if file $$file | grep -i executable >> /dev/null; then \
	        continue; \
	fi; \
	if file $$file | grep -i object >> /dev/null; then \
	        continue; \
	fi; \
	if file $$file | grep -i relocatable >> /dev/null; then \
	        continue; \
	fi; \
	cp $$file $$distdir/$$file;  \
	done


