SHELL = /bin/sh
CC = gcc
CFLAGS += -g -std=c99 -I/home/mithrandir/price/psLib/include -D_GNU_SOURCE # -DCRFLUX -DTESTING
PSLIB += -L/home/mithrandir/price/psLib/lib/ -lpslib -lgsl -lgslcblas -lfftw3f -lsla -lcfitsio -lm -lxml2 -lmysqlclient
LDFLAGS += $(PSLIB)

STAC = stac.o stacConfig.o stacRead.o stacErrorImages.o stacTransform.o stacCheckMemory.o \
	stacCombine.o stacInvertMaps.o stacRejection.o stacAreaOfInterest.o stacSize.o stacScales.o \
	stacHelp.o

SHIFT = shift.o stacRead.o stacErrorImages.o stacTransform.o stacCheckMemory.o \
	stacCombine.o stacInvertMaps.o stacSize.o

SHIFTSIZE = shiftSize.o shiftSizeHelp.o stacWrite.o stacConfig.o stacRead.o stacCheckMemory.o stacSize.o

.PHONY: tags clean empty test profile optimise

.c.o:
		$(CC) -c $(CFLAGS) $(OPTFLAGS) $<

stac:		$(STAC)
		$(CC) $(CFLAGS) -o $@ $(STAC) $(LDFLAGS) $(OPTFLAGS)

shift:		$(SHIFT)
		$(CC) $(CFLAGS) -o $@ $(SHIFT) $(LDFLAGS) $(OPTFLAGS)

shiftSize:	$(SHIFTSIZE)
		$(CC) $(CFLAGS) -o $@ $(SHIFTSIZE) $(LDFLAGS) $(OPTFLAGS)

clean:
		-$(RM) *.o gmon.* profile.txt

empty:		clean
		-$(RM) $(TARGET) TAGS

test:		stac test_0.fits test_1.fits test_2.fits test_3.fits
		-$(RM) testout.fits
		-$(RM) testout.fits.pre
		-$(RM) chi2_*.fits
		-$(RM) test_[0-3].fits.err
		-$(RM) test_[0-3].fits.mask
		-$(RM) test_[0-3].fits.shift.*
		-$(RM) test_[0-3].fits.shiftrej
		-$(RM) test_[0-3].fits.shifterr.*
		-$(RM) test_[0-3].fits.grad
		-$(RM) test_[0-3].fits.region
		-$(RM) test_[0-3].fits.rejmap
		-$(RM) leaks.dat
		./stac -v testout.fits test_0.fits test_1.fits test_2.fits test_3.fits

# Run profiling.
profile:	CFLAGS += -pg
profile:	empty $(TARGET)
		$(RM) gmon.*
		for ((i = 0; i < 10; i++)) \
		do \
			$(MAKE) test; \
			mv -f gmon.out gmon.$$i; \
		done
		gprof -p -q -l $(TARGET) gmon.* > profile.txt

# Do gcc optimisation
optimise:	clean
		-$(RM) $(TARGET)
		export OPTFLAGS=-fprofile-generate ; $(MAKE) test
		-$(RM) $(TARGET)
		$(MAKE) clean
		export OPTFLAGS=-fbranch-use ; $(MAKE) $(TARGET)
		-$(RM) *.da

# Tags for emacs
tags:
		etags `find . -name \*.[ch] -print`

