# jmyers, 11/24/08
# new makefile, built for PanSTARRS

CXX=g++
OPTS=-Wall -O2 -fPIC 
#-fPIC is needed so we can build SWIG interfaces.

#########################################################################
#  EDIT THESE
#########################################################################

# gsl is used for linear fitting, which requires a header file as well as two
# libraries (libgsl, libgslcblas.)  PanSTARRS uses GSL, so please update the following 
# to use the same GSL as that installed on your system.
GSL_DIR=./gsl/1.8

# Of course you'll need SWIG if you'd like to build the SWIG interfaces.
SWIG=$(HOME)/Linux64/external/swig/1.3.34/bin/swig

# Python.h is required to build the SWIG interfaces.  Set this to the
# proper location of Python.h on your system.
PYTHON_INCLUDE_DIR=$(HOME)/Linux64/external/python/2.5.2/include/python2.5/


# boost_dir is used EXCLUSIVELY for unit_test.hpp.  There are no
# libraries, so there can be no platform compatibility issues.
# However, if you wish, change BOOST_DIR to use the boost installed on
# your system.
BOOST_DIR=./boost/



GSL_LIBDIR=$(GSL_DIR)/lib
GSL_SOS=-lgsl -lgslcblas
BOOST_INCLUDES=-I$(BOOST_DIR)/include
GSL_INCLUDES=-I$(GSL_DIR)/include
GSL_LIBS=-L$(GSL_LIBDIR) $(GSL_SOS)

LIBS=$(GSL_LIBS)
INCLUDES=$(BOOST_INCLUDES) $(GSL_INCLUDES) 

PointAndValue=PointAndValue.cc PointAndValue.h
KDTree=KDTree.h KDTreeNode.h $(PointAndValue)

tools: CollapseTracklets removeSubsets purifyTracklets

tests: unitTests

all: tools tests swigInterfaces




#  SWIG INTERFACES

swigInterfaces: TrackletSWIG DetectionSWIG CollapseTrackletsSWIG RemoveSubsetsSWIG PurifyTrackletsSWIG

TrackletSWIG: Tracklet.py _Tracklet.so

Tracklet.py: Tracklet.i Tracklet.h
	$(SWIG) -c++ -python Tracklet.i

_Tracklet.so: Tracklet.py Tracklet.i Tracklet.h
	$(CXX) $(OPTS) -c Tracklet_wrap.cxx -I$(PYTHON_INCLUDE_DIR)
	$(CXX) -shared Tracklet_wrap.o -o _Tracklet.so

DetectionSWIG: Detection.py _Detection.so

Detection.py: Detection.i Detection.h
	$(SWIG) -c++ -python Detection.i

_Detection.so: Detection.py Detection.i Detection.h Detection.o
	$(CXX) $(OPTS) -c Detection_wrap.cxx -I$(PYTHON_INCLUDE_DIR)
	$(CXX) $(OPTS) -shared Detection_wrap.o Detection.o -o _Detection.so

CollapseTrackletsSWIG: CollapseTracklets.py _CollapseTracklets.so

CollapseTracklets.py: CollapseTracklets.i collapseTracklets.h
	$(SWIG) -c++ -python CollapseTracklets.i

_CollapseTracklets.so: CollapseTracklets.py CollapseTracklets.i collapseTracklets.h CollapseTracklets.o removeSubsets.o rmsLineFit.o Detection.o common.o
	$(CXX) $(OPTS) -c CollapseTracklets_wrap.cxx -I$(PYTHON_INCLUDE_DIR)
	$(CXX) $(OPTS) -shared -Wl,$(GSL_LIBS) $(GSL_SOS) CollapseTracklets_wrap.o CollapseTracklets.o removeSubsets.o rmsLineFit.o Detection.o common.o  -o _CollapseTracklets.so


RemoveSubsetsSWIG: RemoveSubsets.py _RemoveSubsets.so

RemoveSubsets.py: removeSubsets.i removeSubsets.h
	$(SWIG) -c++ -python removeSubsets.i

_RemoveSubsets.so: RemoveSubsets.py removeSubsets.i CollapseTracklets.o removeSubsets.o removeSubsets.o rmsLineFit.o Detection.o common.o
	$(CXX) $(OPTS) -c removeSubsets_wrap.cxx -I$(PYTHON_INCLUDE_DIR)
	$(CXX) $(OPTS) -shared -Wl,$(GSL_LIBS) $(GSL_SOS) removeSubsets_wrap.o CollapseTracklets.o removeSubsets.o rmsLineFit.o Detection.o common.o  -o _RemoveSubsets.so

PurifyTrackletsSWIG: PurifyTracklets.py _PurifyTracklets.so

PurifyTracklets.py: PurifyTracklets.i rmsLineFit.h
	$(SWIG) -c++ -python PurifyTracklets.i

_PurifyTracklets.so: PurifyTracklets.py PurifyTracklets.i CollapseTracklets.o rmsLineFit.o Detection.o common.o
	$(CXX) $(OPTS) -c PurifyTracklets_wrap.cxx -I$(PYTHON_INCLUDE_DIR)
	$(CXX) $(OPTS) -shared -Wl,$(GSL_LIBS) $(GSL_SOS) PurifyTracklets_wrap.o CollapseTracklets.o rmsLineFit.o Detection.o common.o  -o _PurifyTracklets.so





#  TOOLS

CollapseTracklets: collapseTrackletsMain.cc rmsLineFit.o CollapseTracklets.o collapseTracklets.h Detection.o common.o removeSubsets.o Tracklet.h
	$(CXX) $(OPTS) $(LIBS) rmsLineFit.o Detection.o common.o CollapseTracklets.o removeSubsets.o -o CollapseTracklets collapseTrackletsMain.cc

purifyTracklets: rmsLineFit.o rmsLineFit.o CollapseTracklets.o purifyTracklets.cc
	$(CXX) $(OPTS) $(LIBS) removeSubsets.o CollapseTracklets.o rmsLineFit.o common.o Detection.o -o purifyTracklets purifyTracklets.cc

removeSubsets:	removeSubsetsMain.cc removeSubsets.o CollapseTracklets.o rmsLineFit.o collapseTracklets.h Detection.o Tracklet.h $(KDTree)
	$(CXX) $(OPTS) $(LIBS) CollapseTracklets.o rmsLineFit.o common.o Detection.o removeSubsets.o -o removeSubsets removeSubsetsMain.cc



#  OBJECTS


rmsLineFit.o: rmsLineFit.cc rmsLineFit.h collapseTracklets.h Tracklet.h Detection.h
	$(CXX) $(OPTS) $(INCLUDES) -c rmsLineFit.cc

removeSubsets.o: removeSubsets.cc removeSubsets.h collapseTracklets.h Tracklet.h
	$(CXX) $(OPTS) $(INCLUDES) -o removeSubsets.o -c removeSubsets.cc

CollapseTracklets.o: CollapseTracklets.cc collapseTracklets.h Detection.h common.h  Tracklet.h rmsLineFit.h $(KDTree)
	$(CXX) $(OPTS) $(INCLUDES) -o CollapseTracklets.o -c CollapseTracklets.cc

Detection.o: Detection.cc Detection.h 
	$(CXX) $(OPTS) $(INCLUDES) -o Detection.o -c Detection.cc

common.o: common.cc common.h
	$(CXX) $(OPTS) $(INCLUDES) -c common.cc

unitTests: unitTests.cc $(KDTree) CollapseTracklets.o  rmsLineFit.o Detection.o common.o removeSubsets.o
	$(CXX) $(OPTS) $(LIBS) $(INCLUDES) -o unitTests CollapseTracklets.o rmsLineFit.o Detection.o common.o removeSubsets.o unitTests.cc

clean: 
	rm -f *.o
	rm -f *.so
	rm -f testPointAndValue
	rm -f CollapseTracklets
	rm -f purifyTracklets
	rm -f testCommon
	rm -f testKDTree
	rm -f removeSubsets
	rm -f Tracklet.py 
	rm -f Detection.py
	rm -f CollapseTracklets.py
	rm -f RemoveSubsets.py
	rm -f *_wrap.cc
	rm -f *_wrap.cxx
