#-----------------------------------------------------------------------------
# GNU makefile for building WCSLIB 4.6 and its test suite.
#
# Summary of the main targets
# ---------------------------
#   build:     Build the library.
#
#   clean:     Delete intermediate object files.
#
#   cleaner:   clean, and also delete the test executables.
#
#   cleanest (distclean or realclean): cleaner, and also delete the object
#              library the C source files generated by 'flex'.
#
#   check (or test): Compile and run the test programs.  By default they are
#              executed in batch mode; use
#
#                gmake MODE=interactive check
#
#              to run them interactively.
#
#   tests:     Compile the test programs (but don't run them).
#
# Notes:
#   1) If you need to make changes then preferably modify ../makedefs.in
#      instead and re-run configure.
#
# Author: Mark Calabretta, Australia Telescope National Facility
# http://www.atnf.csiro.au/~mcalabre/index.html
# $Id: GNUmakefile,v 4.6.1.3 2010/11/24 01:26:44 cal103 Exp cal103 $
#-----------------------------------------------------------------------------
# Get configure settings.
include ../makedefs

FLEXMODS := $(patsubst %.l,%.c,$(wildcard *.l))
MODULES  := $(sort \
              $(patsubst %.c,%.o, \
                $(filter-out getwcstab.c,$(wildcard *.c)) $(FLEXMODS)))

ifeq "$(WCSTRIG)" "MACRO"
  CPPFLAGS += -DWCSTRIG_MACRO
  MODULES  := $(filter-out wcstrig.o, $(MODULES))
else
  ifeq "$(WCSTRIG)" "NATIVE"
    MODULES := $(filter-out wcstrig.o, $(MODULES))
  endif
endif

# For building the sharable library.
PICLIB := libwcs-PIC.a

CPPFLAGS += -I. -I..

vpath %.c  test
vpath %.h  ..
vpath %.in ..


# For building and exercising the test suite
# ------------------------------------------
# Signals tfitshdr to use wcshdr().
ifneq "$(DO_WCSHDR)" ""
  CPPFLAGS += -DDO_WCSHDR
endif

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

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

# Test programs that aren't automatically exercised.
TEST_X := tsphdpa

TESTS  := $(TEST_N)
ifneq "$(PGPLOTINC)" ""
ifneq "$(PGPLOTLIB)" ""
  TESTS   += $(TEST_P) tpih2
endif
endif

# Test programs that require CFITSIO.
ifneq "$(CFITSIOINC)" ""
ifneq "$(CFITSIOLIB)" ""
  TESTS  += twcstab twcshdr
endif
endif

LIBPGSBOX := ../pgsbox/libpgsbox-$V.a

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

ifeq "$(FLEX)" "flex"
  %.c : %.l
	-@ echo ''
	-@ $(RM) $@
	   $(FLEX) $(FLFLAGS) -t $< | sed -e 's/^[	 ]*#/#/' > $@
else
  %.c : %.l
	-@ echo ''
	-@ $(RM) $@
	   cp flexed/$@ .
endif

$(LIBWCS)(%.o) : %.c
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFLAGS) -c $<
	   $(AR) r $(LIBWCS) $%
	-@ $(RM) $%

$(PICLIB)(%.o) : %.c
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFLAGS) $(SHRFLAGS) -c $<
	   $(AR) r $(PICLIB) $%
	-@ $(RM) $%

%.i : %.c
	-@ echo ''
	-@ $(RM) $@
	   $(CPP) $(CPPFLAGS) $(CFLAGS) $< > $@

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

# Use 'make FLAVOUR=Linux run_%' to have VALGRIND defined (from flavours).
run_% : %
	-@ echo ''
	-@ $(TIMER)
	 @ if [ '$(MODE)' = interactive ] ; then \
	     printf 'Press <CR> to run $<: ' ; \
	     read DUMMY ; \
	     if [ '$<' = tunits ] ; then \
	       $(VALGRIND) ./$< < test/units_test ; \
	     elif [ '$<' != twcshdr ] ; then \
	       $(VALGRIND) ./$< ; \
	     fi ; \
	   else \
	     if [ '$<' = tcel2 ] ; then \
	       echo N | $(VALGRIND) ./$< ; \
	     elif [ '$<' = tunits ] ; then \
	       $(VALGRIND) ./$< < test/units_test ; \
	     elif [ '$<' != twcshdr ] ; then \
	       $(VALGRIND) ./$< < /dev/null 2>&1 ; \
	     fi ; \
	   fi
	-@ echo ''

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

.PHONY : build check clean cleaner cleanest distclean install lib realclean \
         run_% test tests

build : lib

lib : $(FLEXMODS) $(LIBWCS) $(SHRLIB)

$(LIBWCS) : $(MODULES:%=$(LIBWCS)(%))
	-@ echo ''
	   $(RANLIB) $@

$(SHRLIB) : $(PICLIB)
	-@ echo ''
	-@ $(RM) -r tmp
	   mkdir tmp && \
	     cd tmp && \
	     trap 'cd .. ; $(RM) -r tmp' 0 1 2 3 15 ; \
	     $(AR) x ../$(PICLIB) && \
	     $(SHRLD) -o $@ *.o && \
	     mv $@ ..

$(PICLIB) : $(MODULES:%.o=$(PICLIB)(%.o)) ;

install : build
	-  if [ ! -d "$(LIBDIR)" ] ; then \
	     $(INSTALL) -d -m 2775 $(LIBDIR) ; \
	   fi
	   $(INSTALL) -m 644 $(LIBWCS) $(LIBDIR)
	   $(RANLIB) $(LIBDIR)/$(LIBWCS)
	-  if [ -h "$(LIBDIR)/libwcs.a" ] ; then \
	     $(RM) $(LIBDIR)/libwcs.a ; \
	   fi
	-  $(LN_S) $(LIBWCS) $(LIBDIR)/libwcs.a
	-  if [ "$(SHRLIB)" != "" ] ; then \
	     $(INSTALL) -m 644 $(SHRLIB) $(LIBDIR) ; \
	     if [ "$(SHRLN)" != "" ] ; then \
	       if [ -h "$(LIBDIR)/$(SHRLN)" ] ; then \
	         $(RM) $(LIBDIR)/$(SHRLN) ; \
	       fi ; \
	       $(LN_S) $(SHRLIB) $(LIBDIR)/$(SHRLN) ; \
	     fi ; \
	   fi
	-  if [ ! -d "$(INCDIR)" ] ; then \
	     $(INSTALL) -d -m 2775 $(INCDIR) ; \
	   fi
	   $(INSTALL) -m 444 *.h $(INCDIR)
	   $(RM) $(INCLINK)
	   $(LN_S) $(WCSLIBV) $(INCLINK)

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

cleaner : clean
	-  $(RM) $(TEST_N) $(TEST_X)
	-  $(RM) $(TEST_P) tpih2 twcstab twcshdr
	-  $(RM) tofits bth.fits pih.fits wcstab.fits
	-  $(RM) t*_cfitsio

cleanest distclean realclean : cleaner
	-  $(RM) ../wcsconfig.h ../wcsconfig_tests.h
	-  $(RM) fitshdr.c wcsbth.c wcspih.c wcsulex.c wcsutrn.c
	-  $(RM) $(PICLIB) libwcs-*.a libwcs.so.* libwcs.*.dylib

check test : tests $(TESTS:%=run_%)

tests : $(TESTS)

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

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

tpih2 : test/tpih2.c $(LIBPGSBOX) $(LIBWCS)
	-@ echo ''
	   $(CC) $(CPPFLAGS) -I../pgsbox $(PGPLOTINC) $(CFLAGS) -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(LIBPGSBOX) $(PGPLOTLIB) $(LIBWCS) \
	     $(FLIBS) $(LIBS)
	-@ $(RM) $@.o

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

tpih2_cfitsio : test/tpih2.c $(LIBPGSBOX) $(LIBWCS)
	-@ echo ''
	   $(CC) -DDO_CFITSIO $(CPPFLAGS) -I../pgsbox $(PGPLOTINC) \
	     $(CFITSIOINC) $(CFLAGS) -c -o $@.o $<
	   $(LD) $(LDFLAGS) -o $@ $@.o $(LIBPGSBOX) $(PGPLOTLIB) \
	     $(CFITSIOLIB) $(LIBWCS) $(FLIBS) $(LIBS)
	-@ $(RM) $@.o

twcstab : test/twcstab.c $(GETWCSTAB) $(LIBWCS)
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFITSIOINC) $(CFLAGS) $(LDFLAGS) -o $@ $< \
	     $(GETWCSTAB) $(CFITSIOLIB) $(LIBWCS) $(LIBS)
	-@ $(RM) $@.o

twcshdr : test/twcshdr.c $(GETWCSTAB) $(LIBWCS)
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFITSIOINC) $(CFLAGS) $(LDFLAGS) -o $@ $< \
	     $(GETWCSTAB) $(CFITSIOLIB) $(LIBWCS) $(LIBS)
	-@ $(RM) $@.o

getwcstab.o : getwcstab.c getwcstab.h
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFLAGS) $(CFITSIOINC) -c $<

$(LIBPGSBOX) :
	-@ echo ''
	   $(MAKE) -C ../pgsbox lib

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

pih.fits : test/pih.keyrec tofits
	   ./tofits < $< > $@

bth.fits : test/bth.keyrec tofits
	   ./tofits < $< > $@

GNUmakefile : ../makedefs ;

../makedefs ../wcsconfig.h ../wcsconfig_tests.h : makedefs.in wcsconfig.h.in \
    wcsconfig_tests.h.in ../config.status
	-@ $(RM) ../wcsconfig.h ../wcsconfig_tests.h
	   cd .. && ./config.status

show ::
	-@ -@ echo '  FLEXMODS    := $(FLEXMODS)'
	-@ -@ echo '  MODULES     := $(MODULES)'

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

$(LIBWCS)(cel.o)      : cel.h prj.h sph.h wcsmath.h wcstrig.h
$(LIBWCS)(fitshdr.o)  : wcsconfig.h fitshdr.h
$(LIBWCS)(lin.o)      : lin.h
$(LIBWCS)(log.o)      : log.h
$(LIBWCS)(prj.o)      : prj.h wcsmath.h wcstrig.h
$(LIBWCS)(spc.o)      : spc.h spx.h wcsmath.h wcstrig.h
$(LIBWCS)(sph.o)      : sph.h wcstrig.h
$(LIBWCS)(spx.o)      : spx.h
$(LIBWCS)(tab.o)      : tab.h wcsmath.h
$(LIBWCS)(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
$(LIBWCS)(wcsbth.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                        wcsmath.h
$(LIBWCS)(wcsfix.o)   : cel.h lin.h prj.h spc.h sph.h spx.h tab.h wcs.h \
                        wcsfix.h wcsmath.h wcsunits.h
$(LIBWCS)(wcshdr.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                        wcsutil.h
$(LIBWCS)(wcspih.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h \
                        wcsmath.h
$(LIBWCS)(wcstrig.o)  : wcsmath.h wcstrig.h
$(LIBWCS)(wcsulex.o)  : wcsmath.h wcsunits.h
$(LIBWCS)(wcsunits.o) : wcsunits.h
$(LIBWCS)(wcsutil.o)  : wcsutil.h
$(LIBWCS)(wcsutrn.o)  : wcsunits.h

tbth1 tbth1_cfitsio : cel.h wcsconfig.h wcsconfig_tests.h lin.h prj.h spc.h \
                      spx.h tab.h wcs.h wcsfix.h wcshdr.h
tcel1   : cel.h prj.h
tcel2   : cel.h prj.h
tfitshdr tfitshdr_cfitsio : wcsconfig.h wcsconfig_tests.h fitshdr.h
tlin    : lin.h
tlog    : log.h
tpih1 tpih1_cfitsio : cel.h wcsconfig.h wcsconfig_tests.h lin.h prj.h spc.h \
                      spx.h tab.h wcs.h wcsfix.h wcshdr.h
tpih2 tpih2_cfitsio : cel.h wcsconfig.h wcsconfig_tests.h lin.h prj.h spc.h \
                      spx.h tab.h wcs.h wcshdr.h
tprj1   : prj.h wcstrig.h
tprj2   : prj.h
tspc    : spc.h spx.h wcstrig.h
tsph    : sph.h wcstrig.h
tsphdpa : 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
twcshdr : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.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 lin.h prj.h spc.h spx.h tab.h wcs.h wcshdr.h

run_tbth1 run_tbth1_cfitsio : bth.fits
run_tfitshdr run_tfitshdr_cfitsio : pih.fits
run_tpih1 run_tpih1_cfitsio : pih.fits
run_tpih2 run_tpih2_cfitsio : pih.fits
