#
#   Copyright 2008 by Sun Microsystems, Inc.
#
#   This file is part of the CAVA system.
#
#   CAVA is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published
#   by the Free Software Foundation; either version 2, or (at your 
#   option) any later version.
#
#   CAVA is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with CAVA; see the file COPYING.  If not, write to
#   the Free Software Foundation, 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.
#
#                           ----    ----
#
#   Read the ISA desciption files and build the GCC files needed to build
#   a cava compiler for that ISA.
#
#   The ISA description files are:
#       logic.in
#       semantics.in
#       syntax.in
#
#   Four "template" files are also used as input:
#       fini.html
#       init.html
#       ../binutils/include/opcode_cava_h.in
#       ../binutils/bfd/elf64-cava_c.in
#
#   This "makefile" has eight "external" targets.
#       doit ......... Default target, run the cavanator.
#       cavacheck .... Make sure the environment is correct for cava.
#       clean ........ Remove any ancilary files.
#       clean_output . Remove the cavanator's output files.
#       unit_tests ... Test each module of the cavanator separatly.
#       whole_tests .. Test the cavanator as a whole.
#       check ........ The traditional GNU target to do testing.
#       gold_files ... Turn the cavanator's current output files into gold.
#

SHELL=/bin/bash

isa_dependent_flags = --more_args=$(ARGSFILE)

doit:	cavacheck FORCE
	cavanator.py $(isa_dependent_flags)

cavacheck:	FORCE
	@rc=0 ;                                             \
	if [ "x${CAVA}" = "x" ] ; then                      \
	    echo CAVA not set ;                             \
	    rc=$$(( $$rc + 1 )) ;                           \
	fi ;                                                \
	if ( echo ${PATH} | grep -vq ${CAVA}/bin) ; then    \
	    echo ${CAVA}/bin is not in the PATH ;           \
	    rc=$$(( $$rc + 2 )) ;                           \
	fi ;                                                \
	if ( echo ${PATH} | grep -vq ${CAVA}/tools ) ; then \
	    echo ${CAVA}/tools is not in the PATH ;         \
	    rc=$$(( $$rc + 4 )) ;                           \
	fi ; exit $$rc

whole_test_flags = \
	--lang=both \
	--syntax_d=syntax_data.%s \
	--semantics_d=semantics_data.%s \
	--logic_d=logic_data.%s

gold_files_list = $(shell cavanator.py $(isa_dependent_flags) $(whole_test_flags) --input_names=gold_files --output_names=gold_files)

ut_gold_files_list = $(shell cavanator.py $(isa_dependent_flags) $(whole_test_flags) --input_names=ut_gold_files --output_names=ut_gold_files)

input_files_list = $(shell cavanator.py $(isa_dependent_flags) $(whole_test_flags) --input_names)

echo_input_list:
	@echo $(input_files_list)

output_files_list = $(shell cavanator.py $(isa_dependent_flags) $(whole_test_flags) --output_names)

modules=syntax.py        semantics.py      logic.py       \
	gen-analysis.py  gen-binutils.py   gen-caveat.py  \
	gen-docs.py      gen-equations.py  gen-gcc.py

check:	cavacheck unit_tests whole_test

clean:	FORCE
	rm -rf ut_gold_files gold_files ut_output check.log

clean_output:	cavacheck FORCE
	rm -f $(output_files_list)

gold_files: cavacheck FORCE
	cavanator.py $(isa_dependent_flags) $(whole_test_flags) --quite --set=all
	if [ -e gold_files.tbz2 ] ; then mv -f gold_files.tbz2 gold_files_tbz2.sav; fi
	rm -rf ut_gold_files gold_files
	mkdir gold_files
	mkdir gold_files/html-dir
	-cp $(output_files_list) $(input_files_list) gold_files
	rm gold_files/action.h
	cp ../micro/action.h gold_files/micro_action.h
	cp ../caveat/action.h gold_files/caveat_action.h
	mv gold_files/*.html gold_files/html-dir
	mv gold_files/html-dir/init.html gold_files
	mv gold_files/html-dir/fini.html gold_files
	tar cjf gold_files.tbz2 gold_files

FORCE:

unit_tests: cavacheck $(ut_gold_files_list) ut_output FORCE
	@echo '#######################################################' >> check.log; \
	echo 'cavanator check - unit tests:' | tee -a check.log; \
	date; echo '        ' `date` >> check.log; \
	echo '#######################################################' >> check.log; \
	failed=0; \
	for mod in $(modules); \
	do \
	    $${mod} --unknown_ok $(isa_dependent_flags) 2>&1 | tee -a check.log; \
	    $${mod} --unknown_ok $(isa_dependent_flags) >/dev/null 2>&1; \
	    if [ $$? -ne 0 ]; \
	    then \
	        failed=$$((failed + 1)); \
	    fi; \
	done ; \
	if [ $${failed} -eq 0 ]; \
	then \
	    echo All the unit tests succeeded. | tee -a check.log; \
	else \
	    echo $${failed} unit test\(s\) failed. | tee -a check.log; \
	fi

ut_output:
	mkdir ut_output

whole_test: cavacheck $(gold_files_list) $(input_files_list)  FORCE
	cavanator.py $(isa_dependent_flags) $(whole_test_flags) --quite
	@echo '#######################################################' >> check.log; \
	echo 'cavanator check - comparing gold files to output files:' | tee -a check.log; \
	date; echo '        ' `date` >> check.log; \
	echo '#######################################################' >> check.log; \
	gold=(`cavanator.py $(isa_dependent_flags) --output_names=gold_files`); \
	new1s=(`cavanator.py $(isa_dependent_flags) --output_names`); \
	failed=0; \
	for ((i=0;i<$${#gold[*]};++i)); \
	do \
	    diff -s $${gold[$$i]} $${new1s[$$i]} >>check.log 2>&1; \
	    if [ $$? -ne 0 ]; \
	    then \
	        failed=$$((failed + 1)); \
	        echo failed: $${gold[$$i]} $${new1s[$$i]} 2>&1 | \
                                                            tee -a check.log; \
	    fi; \
	done; \
	if [ $${failed} -eq 0 ]; \
	then \
	    echo Everything worked! | tee -a check.log; \
	    echo ------------------ >> check.log; \
	else \
	    echo Number failed: $${failed} | tee -a check.log; \
	    echo ----------------- >> check.log; \
	fi

$(gold_files_list):
	tar xjf gold_files.tbz2 $@

$(ut_gold_files_list):
	mkdir -p ut_gold_files/html-dir
	$(MAKE) -f $(MAKEFILE_LIST) $(subst ut_,,$@)
	ln $(subst ut_,,$@) $@

ut_gold_files/html-dir:
	mkdir -p ut_gold_files/html-dir

$(input_files_list):
	mkdir -p ../binutils/bfd
	mkdir -p ../binutils/include
	cp gold_files/`basename $@` $@
