MULTIDO    = $(MAKE)
MULTICLEAN = $(MAKE)
MKPROM  = sparc-elf-mkprom
CCOPT   = -g -O3 -DDP
CC      = sparc-elf-gcc $(CFLAGS) $(CCOPT)
CXX     = sparc-elf-gcc $(CFLAGS) $(CCOPT)
LD      = sparc-elf-gcc 
OBJDUMP = sparc-elf-objdump
PT_OBJS = pthread3 pthread4 pthread5 pthread6
LDFLAGS = #-qsvt

ifeq ($(DESTDIR),)
DESTDIR = .
endif

FLAGS_TO_PASS = \
	"CC=$(CC)" \
	"CFLAGS=$(CFLAGS)" \
	"AR=$(AR)" \
	"RANLIB=$(RANLIB)" \
	"AR_FLAGS=$(AR_FLAGS)" \
	"MAKEINFO=$(MAKEINFO)" \
	"AS=$(AS)" \
	"LD=$(LD)" \
	"CROSS_CFLAGS=$(CROSS_CFLAGS)" \
	"TARGET_CFLAGS=$(TARGET_CFLAGS)" \
	"exec_prefix=$(exec_prefix)" \
	"prefix=$(prefix)" \
	"tooldir=$(tooldir)" \
	"INSTALL=$(INSTALL)" \
	"INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \
	"INSTALL_DATA=$(INSTALL_DATA)" \
	"DESTDIR=$(DESTDIR)"


all:
	$(MULTIDO) $(FLAGS_TO_PASS) multi-do DO="clean build"
	make $(FLAGS_TO_PASS) DESTDIR="." clean build

test:
	$(MULTIDO) $(FLAGS_TO_PASS) multi-do DO="dotest"
	make $(FLAGS_TO_PASS) DESTDIR="." dotest

build:  dhry.exe whetstone.exe hello.exe singlemalloc.exe stanford.exe paranoia.exe prime.exe pthread3.exe pthread4.exe pthread5.exe pthread6.exe pthread7.exe pthread8.exe  

TESTS=hello.exe stanford.exe hello.prom stanford.prom
dotest:
	for i in $(TESTS); do \
	 cd $(CURDIR)/$(DESTDIR); $(CURDIR)/runtest.in -S $(CURDIR)/tsim-leon3 $$i; \
	done; \

#testpthread1.exe testpthread2.exe
testbuild: testpthread2.exe

LDFLAGS_whetstone.exe = -lm
LDFLAGS_paranoia.exe = -lm -DBATCHMODE
LDFLAGS_pthread3.exe = -lpthread
LDFLAGS_pthread4.exe = -lpthread
LDFLAGS_pthread5.exe = -lpthread
LDFLAGS_pthread6.exe = -lpthread
LDFLAGS_pthread7.exe = -lpthread
LDFLAGS_pthread8.exe = -lpthread
LDFLAGS_testpthread1.exe = -lpthread
LDFLAGS_testpthread2.exe = -lpthread
LDFLAGS_testpthread3.exe = -lpthread
LDFLAGS_testpthread4.exe = -lpthread
LDFLAGS_testpthread5.exe = -lpthread
LDFLAGS_testpthread6.exe = -lpthread
LDFLAGS_testpthread7.exe = -lpthread
LDFLAGS_testpthread8.exe = -lpthread
LDFLAGS_prime.exe = -lstdc++

%.exe: %.o
	mkinstalldirs $(DESTDIR)
	$(LD) $^ $(LDFLAGS) $(LDFLAGS_$(@F)) -o $(DESTDIR)/$(@F) 
#	$(OBJDUMP) -d -S $(DESTDIR)/$(@F) >$(DESTDIR)/$(@F).dis
#	$(MKPROM) $(DESTDIR)/$(@F) -o $(DESTDIR)/$(basename $(@F)).prom

clean:
	-rm -f *.o *.exe

distclean:
	find . | grep .exe$$ | xargs rm -f $$1
	find . | grep .dis$$ | xargs rm -f $$1
	find . | grep ~$$ | xargs rm -f $$1
	rm -f progs.tar.gz
	rm -rf progs

progs.tar.gz:
	./mkinstalldirs progs
	for i in `find . | grep .exe$$`; do \
	 dir=`echo $$i | sed -e 's/^.//' | sed -e 's/^\///'`; \
	 ./mkinstalldirs `dirname progs/$$dir`; \
	 install $$i progs/$$dir; \
	done; \
	tar cvf progs.tar progs
	rm -f progs.tar.gz
	gzip progs.tar
	rm -rf progs

multi-do:
	  compiler="$(CC)"; \
	  for i in `$${compiler} --print-multi-lib 2>/dev/null`; do \
	    dir=`echo $$i | sed -e 's/;.*$$//'`; \
	    if [ "$${dir}" = "." ]; then \
	      true; \
	    else \
	        flags=`echo $$i | sed -e 's/^[^;]*;//' -e 's/@/ -/g'`; \
		$(MAKE) $(FLAGS_TO_PASS) \
				CFLAGS="$(CFLAGS) $${flags}" \
				FCFLAGS="$(FCFLAGS) $${flags}" \
				FFLAGS="$(FFLAGS) $${flags}" \
				ADAFLAGS="$(ADAFLAGS) $${flags}" \
				prefix="$(prefix)" \
				exec_prefix="$(exec_prefix)" \
				GCJFLAGS="$(GCJFLAGS) $${flags}" \
				CXXFLAGS="$(CXXFLAGS) $${flags}" \
				LIBCFLAGS="$(LIBCFLAGS) $${flags}" \
				LIBCXXFLAGS="$(LIBCXXFLAGS) $${flags}" \
				LDFLAGS="$(LDFLAGS) $${flags}" \
				MULTIFLAGS="$${flags}" \
				DESTDIR="$${dir}" \
				INSTALL="$(INSTALL)" \
				INSTALL_DATA="$(INSTALL_DATA)" \
				INSTALL_PROGRAM="$(INSTALL_PROGRAM)" \
				INSTALL_SCRIPT="$(INSTALL_SCRIPT)" \
				$(DO);  \
	    fi; \
	  done; \



