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

OBJECTS = stac.o stacConfig.o stacRead.o stacErrorImages.o stacTransform.o stacCheckMemory.o \
	stacCombine.o stacInvertMaps.o stacRejection.o psPlaneTransform.o

TARGET = stac

.PHONY: tags clean empty test profile optimise

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

$(TARGET):	$(OBJECTS)
		$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LDFLAGS) $(OPTFLAGS)

calcGrad:	calcGrad.o
		$(CC) $(CFLAGS) -o $@ calcGrad.o $(LDFLAGS) $(OPTFLAGS)

median:		median.o
		$(CC) $(CFLAGS) -o $@ median.o $(LDFLAGS) $(OPTFLAGS)

getPixels:	getPixels.o
		$(CC) $(CFLAGS) -o $@ getPixels.o $(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) leaks.dat
		./stac -v -k 2.5 -f 0.5 -G 0.96 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`

