#CFLAGS = -O4 -g3 -tune ev5 -arch ev5 # -DINTERNAL_OUT  -DSIMULATOR  # -DUSE_SPECIAL_CONFIGULATION 
#CC = ccc 
#LD = ccc 
CPPFLAGS =  -DX86 -DLINUX  -DDMA # -DINTERNAL_OUT  # -DUSE_SPECIAL_CONFIGULATION # -DINTERNAL_OUT  -DSIMULATOR
#CFLAGS =   -O1   $(CPPFLAGS)
CFLAGS =  $(CPPFLAGS)   -O4   -funroll-loops -ffast-math -funroll-loops 
#CC = gcc -DGCC
CC = /usr/local/gcc-3.2/bin/gcc -DGCC
LDFLAGS = -L ../grape6-x86 -lg6lxsim2 -lm
#LDFLAGS = -non_shared -L ../grape6 -lg6sim2 -lm
LDNEMO =  -L $(NEMOLIB) -lnemo -lm
#CC = cc
BINS = echoback g6libtest g6_test
LIBOBJS = memmap.o grape6util.o grape6utilsub.o
DBGLIBOBJS = memmap.o grape6util_dbg.o grape6utilsub.o
SIMONLYLIBOBJS = memmap-simonly.o grape6util-simonly.o grape6utilsub.o
SIMLIBOBJS = memmap.o grape6util-sim.o grape6utilsub.o
DOCDIR = /usr2/makino/WWW/softwares/GRAPE6
all: g6hibtest g6libtest g6ptest g6cbtest libg6lx.a libg6lxsim.a
libs: libg6lx.a
	cp -pi libg6lx.a /usr2/makino/disk3src/harplibs
echoback:	echoback.o memmap.o g6util.o
	$(LD) $(CFLAGS) -o echoback echoback.o memmap.o g6util.o

g6nbtest:	nbtest.c libg6lxtest.a
	$(CC) $(CFLAGS) -o g6nbtest nbtest.c -L .  -lg6lxtest  $(LDFLAGS)
g6nbtimetest:	nbtest.c libg6lxtest.a
	$(CC) -DTIMETEST $(CFLAGS) -o g6nbtimetest nbtest.c -L .  -lg6lxtest  $(LDFLAGS)
mpitest: mpitest.c  ../grape6-x86/libg6lxsim2.a libg6lxsim.a
	$(CC) $(CFLAGS)  -DSIMULATOR  -o mpitest mpitest.c -L . -lg6lxsim $(LDFLAGS)


g6hibtest:	g6hibtest.c libg6lxtest.a
	$(CC) $(CFLAGS) -o g6hibtest g6hibtest.c -L .  -lg6lxtest  $(LDFLAGS) $(LDNEMO)


g6libtest: grape6utilsub.o memmap.o grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a libg6lx.a
	$(CC) $(CFLAGS)  -DSIMULATOR -DTESTMAIN -o g6libtest grape6util.c grape6utilsub.o memmap.o $(LDFLAGS)
g6libtestsimonly: grape6utilsub.c memmap-sim.c grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a Makefile
	$(CC) $(CFLAGS)  -DSIMONLY -DSIMULATOR -DTESTMAIN -o g6libtestsimonly grape6util.c grape6utilsub.c memmap-sim.c $(LDFLAGS)
g6mctest: grape6utilsub.o memmap.o grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a libg6lx.a
	$(CC) $(CFLAGS)  -DSIMULATOR -DMCTESTMAIN -o g6mctest grape6util.c grape6utilsub.o memmap.o $(LDFLAGS)
g6ptest: grape6utilsub.o memmap.o grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a
	$(CC) $(CFLAGS) -DSIMULATOR -DJPIPTEST  -o g6ptest grape6util.c grape6utilsub.o memmap.o $(LDFLAGS)
g6cbtest: grape6utilsub.o memmap.o grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a
	$(CC) -pg $(CFLAGS)  -DCBTESTMAIN  -o g6cbtest grape6util.c grape6utilsub.o memmap.o $(LDFLAGS)
g6timetest: grape6utilsub.o memmap.o grape6util.c grape6util.h grape6util-local.h ../grape6-x86/libg6lxsim2.a
	$(CC) $(CFLAGS)  -DTIMETEST  -o g6timetest grape6util.c grape6utilsub.o memmap.o $(LDFLAGS)
grape6util.o :  grape6util.c grape6util.h grape6util-local.h 
	$(CC) -c $(CFLAGS) grape6util.c 
grape6util_dbg.o :  grape6util.c grape6util.h grape6util-local.h 
	$(CC) -c $(CFLAGS) -DINTERNAL_OUT=1 grape6util.c
	mv grape6util.o grape6util_dbg.o
grape6util-sim.o :  grape6util.c grape6util.h grape6util-local.h 
	$(CC) -c $(CFLAGS) -DSIMULATOR grape6util.c
	mv grape6util.o  grape6util-sim.o 
grape6util-simonly.o :  grape6util.c grape6util.h grape6util-local.h 
	$(CC) -c $(CFLAGS) -DSIMULATOR -DSIMONLY grape6util.c
	mv grape6util.o  grape6util-simonly.o 
grape6utilsub.o :  grape6utilsub.c grape6util.h grape6util-local.h 
	$(CC) $(CPPFLAGS) -c -O2 grape6utilsub.c 
gt:	gt.o memmap.o g6util.o
	$(LD) $(CFLAGS) -o gt gt.o memmap.o g6util.o
libg6lx.a : $(LIBOBJS)
	ar rvu libg6lx.a  $(LIBOBJS)
libg6lxdbg.a : $(DBGLIBOBJS)
	ar rvu libg6lxdbg.a  $(DBGLIBOBJS)
libg6lxtest.a : $(LIBOBJS)
	ar rvu libg6lxtest.a  $(LIBOBJS)
libg6lxsim.a : $(SIMLIBOBJS)
	ar rvu libg6lxsim.a  $(SIMLIBOBJS)
libg6lxsimonly.a : $(SIMONLYLIBOBJS)
	ar rvu libg6lxsimonly.a  $(SIMONLYLIBOBJS)
mpiftest: mpitest.f
	mpif77 -fno-second-underscore -o mpiftest mpitest.f -L .  -lg6lx  $(LDFLAGS)


g6_write_test:	g6_write_test.o memmap.o g6util.o
	$(LD) $(CFLAGS) -o g6_write_test g6_write_test.o memmap.o g6util.o
g6_test:	g6_test.o memmap.o g6util.o
	$(LD) $(CFLAGS) -o g6_test g6_test.o memmap.o g6util.o

gt.o:	gt.c gt.h memmap.h g6util.h
	$(CC) $(CFLAGS) -c -o gt.o gt.c
g6_test.o:	g6_test.c gt.h memmap.h g6util.h
	$(CC) $(CFLAGS) -c -o g6_test.o g6_test.c

g6_write_test.o:	g6_write_test.c gt.h memmap.h g6util.h
	$(CC) $(CFLAGS) -c -o g6_write_test.o g6_write_test.c

echoback.o:	echoback.c gt.h memmap.h g6util.h
	$(CC) $(CFLAGS) -c -o echoback.o echoback.c

g6util.o:	g6util.c g6util.h
	$(CC) $(CFLAGS) -c -o g6util.o g6util.c

memmap.o:	memmap.c memmap.h
	$(CC) $(CFLAGS) -c -o memmap.o memmap.c
memmap-simonly.o:	memmap-sim.c memmap.h
	$(CC) $(CFLAGS) -c -o memmap-simonly.o memmap-sim.c

docs: grape6user.tex
	latex grape6user
	dvi2ps grape6user > $(DOCDIR)/grape6user.ps
	latex2web.csh  grape6user  $(DOCDIR)
clean:
	rm -f *.o core *~ $(BINS)

bak:
	tar cvf bak.tar ./*.[ch] ./Makefile ./*.tex
	gzip bak.tar
