#-----------------------------------------------------------------------
# Makefile for sort subroutines               Steve Chappell
# Last edited 20th Mar 1998
#-----------------------------------------------------------------------

include version.mk

#Subroutine Libraries.

LIB = ../sunsort_lib/libsortsubs.a
LIBDBX = ../sunsort_lib/libdbx_sortsubs.a
LIBPG = ../sunsort_lib/libpg_sortsubs.a

LIB2 = ../sunsort_lib/libsunsortsubs.a
LIBDBX2 = ../sunsort_lib/libdbx_sunsortsubs.a
LIBPG2 = ../sunsort_lib/libpg_sunsortsubs.a

LIB.so = ../sunsort_lib/libsortsubs.so
LIBDBX.so = ../sunsort_lib/libdbx_sortsubs.so

LIB2.so = ../sunsort_lib/libsunsortsubs.so
LIBDBX2.so = ../sunsort_lib/libdbx_sunsortsubs.so

LIBS = $(LIB) $(LIB2) $(LIB.so) $(LIB2.so)
LIBDBXS = $(LIBDBX) $(LIBDBX2) $(LIBDBX.so) $(LIBDBX2.so)
LIBPGS = $(LIBPG) $(LIBPG2)

#Misc subroutine sources

# Next generation stuff for sort codes
OBJECTS1 = detector.o structure.o properties.o fortprop.o loadadcs.o \
	markhits.o nextgen.o event_dump.o
# Event-struct stuff which isn't sort code dependent and so may be used in
# simulations.
OBJECTS2 = rp.o nrp.o reac.o files.o crhit.o nrhit.o corr.o coord.o \
	missing_p.o punchthru.o vector.o
# Code which spans both of the above groups, or relies on routines in sunsort
# so can really only be used in sort codes.
OBJECTS3 = plot_spec.o detprocs.o

EXTRAOBJECTS = event-struct/ev.o event-struct/wrtev.o event-struct/ev-f.o \
	simulations/scanga.o simulations/scanga_inputs.o \
	simulations/scanga_energy_loss.o masses/getmass.o dedx/dedx.o
#simulations/scanga.o

# Objects which don't rely on sunsort_lib
OBJECTSSUBS = $(OBJECTS2) $(EXTRAOBJECTS)
# Objects which do rely on sunsort_lib
OBJECTSSUN  = $(OBJECTS1) $(OBJECTS3)
# Objects in this directory
OBJECTSHERE = $(OBJECTS1) $(OBJECTS2) $(OBJECTS3)

SOURCES = $(OBJECTSHERE:%.o=%.c)

# Compiler flags etc.

COPTFLAGS = $(OPTFLAG)
FOPTFLAGS = -O4
FFLAGS = $(FOPTFLAGS) -Kpic -e
CFLAGS = $(STDCFLAG) $(COPTFLAGS) $(PICFLAG) -I../sunsort_lib -I../sunsort_src

# Standard targets.

$(LIB) $(LIBDBX) $(LIBPG): $(OBJECTSSUBS)
	-rm -f $@
	ar crv $@ `lorder $(OBJECTSSUBS) | tsort`
	@echo "make $@ ... completed"

$(LIB2) $(LIBDBX2) $(LIBPG2): $(OBJECTSSUN)
	-rm -f $@
	ar crv $@ `lorder $(OBJECTSSUN) | tsort`
	@echo "make $@ ... completed"

$(LIB.so) $(LIBDBX.so) $(LIBPG.so): $(OBJECTSSUBS)
	-rm -f $@ $@.*
	$(LINK.c) -o $@.$(SSVERS) -G $(OBJECTSSUBS) -h $(@F).$(SSVERS)
	ln -s $@.$(SSVERS) $@
	@echo "make $@ ... completed"

$(LIB2.so) $(LIBDBX2.so) $(LIBPG2.so): $(OBJECTSSUN)
	-rm -f $@ $@.*
	$(LINK.c) -o $@.$(SSVERS) -G $(OBJECTSSUN) -h $(@F).$(SSVERS)
	ln -s $@.$(SSVERS) $@
	@echo "make $@ ... completed"

install:
	-rm -f $(OBJECTSHERE)
	-test -d optimised || mkdir optimised
	for FILE in $(OBJECTSHERE) ; do ln -s optimised/$$FILE ; done
	(cd event-struct; $(MAKE) install)
	(cd simulations; $(MAKE) install)
	(cd masses; $(MAKE) install)
	(cd dedx; $(MAKE) install)
	$(MAKE) $(LIBS)
	-rm -f $(OBJECTSHERE)	
	-test -d debug || mkdir debug
	for FILE in $(OBJECTSHERE) ; do ln -s debug/$$FILE ; done
	(cd event-struct; $(MAKE) dbx)
	(cd simulations; $(MAKE)  dbx)
	(cd masses; $(MAKE)  dbx)
	(cd dedx; $(MAKE)  dbx)
	$(MAKE) COPTFLAGS=-g FOPTFLAGS=-g $(LIBDBXS)
	@echo "subroutine libraries installed ..."
	(cd simulations; $(MAKE) program)
	(cd calib-routines; $(MAKE) install)
	(cd ridgeproj; $(MAKE) install)
	@echo "calibration routines installed ..."
	@echo "make sort-subs completed ..."

profile:
	-rm -f $(OBJECTSHERE)	
	-test -d profiled || mkdir profiled
	for FILE in $(OBJECTSHERE) ; do ln -s profiled/$$FILE ; done
	(cd event-struct; $(MAKE) profile)
	(cd simulations; $(MAKE) profile)
	(cd masses; $(MAKE) profile)
	(cd dedx; $(MAKE) profile)
	$(MAKE) FOPTFLAGS="-xO4 -xcg92 -xpg -xinline=" $(LIBPGS)

clean :
	-rm -f *.o optimised/*.o debug/*.o profiled/*.o core *~ *%
	-rmdir -s optimised debug profiled
	(cd event-struct; $(MAKE) clean)
	(cd simulations; $(MAKE) clean)
	(cd masses; $(MAKE) clean)
	(cd dedx; $(MAKE) clean)
	(cd calib-routines; $(MAKE) clean)
	(cd ridgeproj; $(MAKE) clean)

lint:
	lint -DSVR4 -I../sunsort_src -I../sunsort_lib -m -u $(SOURCES) \
		-L../sunsort_lib -lsunsort

coord.o: coord.c evsubs.h subs1.h version.h vector.h files.h err.h
coord.o: event-struct/ev.h reac.h simulations/sim.h coord.h

corr.o: corr.c evsubs.h subs1.h version.h vector.h files.h err.h
corr.o: event-struct/ev.h reac.h simulations/sim.h coord.h

crhit.o: crhit.c evsubs.h subs1.h version.h vector.h files.h err.h
crhit.o: event-struct/ev.h reac.h simulations/sim.h coord.h

detector.o: detector.c structure.h detector.h

detprocs.o: detprocs.c evsubs.h subs1.h version.h vector.h files.h err.h
detprocs.o: event-struct/ev.h reac.h simulations/sim.h coord.h hitsubs.h
detprocs.o: structure.h detprocs.h detector.h markhits.h

event_dump.o: event_dump.c hitsubs.h subs1.h version.h vector.h files.h err.h
event_dump.o: structure.h detprocs.h detector.h sunsort_initadc.h
event_dump.o: ../sunsort_src/sort_def.h ../sunsort_lib/rdtape.h
event_dump.o: ../sunsort_lib/formats.h ../sunsort_lib/spectra.h

files.o: files.c files.h

fortprop.o: fortprop.c structure.h

loadadcs.o: loadadcs.c structure.h loadadcs.h fortprop.h

markhits.o: markhits.c fortprop.h hitsubs.h subs1.h version.h vector.h files.h
markhits.o: err.h structure.h detprocs.h detector.h

missing_p.o: missing_p.c evsubs.h subs1.h version.h vector.h files.h err.h
missing_p.o: event-struct/ev.h reac.h simulations/sim.h coord.h

nextgen.o: nextgen.c hitsubs.h subs1.h version.h vector.h files.h err.h
nextgen.o: structure.h detprocs.h detector.h markhits.h loadadcs.h
nextgen.o: sunsort_initadc.h ../sunsort_src/sort_def.h ../sunsort_lib/rdtape.h
nextgen.o: ../sunsort_lib/formats.h ../sunsort_lib/spectra.h

nrhit.o: nrhit.c evsubs.h subs1.h version.h vector.h files.h err.h
nrhit.o: event-struct/ev.h reac.h simulations/sim.h coord.h

nrp.o: nrp.c evsubs.h subs1.h version.h vector.h files.h err.h
nrp.o: event-struct/ev.h reac.h simulations/sim.h coord.h

plot_spec.o: plot_spec.c evsubs.h subs1.h version.h vector.h files.h err.h
plot_spec.o: event-struct/ev.h reac.h simulations/sim.h coord.h hitsubs.h
plot_spec.o: structure.h detprocs.h detector.h sunsort_initadc.h
plot_spec.o: ../sunsort_src/sort_def.h ../sunsort_lib/rdtape.h
plot_spec.o: ../sunsort_lib/formats.h ../sunsort_lib/spectra.h

properties.o: properties.c structure.h fortprop.h

punchthru.o: punchthru.c evsubs.h subs1.h version.h vector.h files.h err.h
punchthru.o: event-struct/ev.h reac.h simulations/sim.h coord.h

reac.o: reac.c evsubs.h subs1.h version.h vector.h files.h err.h
reac.o: event-struct/ev.h reac.h simulations/sim.h coord.h

rp.o: rp.c evsubs.h subs1.h version.h vector.h files.h err.h
rp.o: event-struct/ev.h reac.h simulations/sim.h coord.h

structure.o: structure.c structure.h properties.h

vector.o: vector.c vector.h
