# -*- makefile -*- mode for emacs
# @(#) $Id: Makefile,v 1.24 2006/07/16 20:35:14 wdietl Exp $

# Copyright (C) 1998-2004 Iowa State University

# This file is part of mjc, a MultiJava Compiler

# mjc 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.

# mjc 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 mjc; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

#
# Developer's Makefile for testing mjc
#

## ----------------------------------------------------------------- 
## At the bottom of this file we import $(TOPDIR)/Make.ProjDefs.  See the
## that file for substantial documentation.
## ----------------------------------------------------------------- 

TOPDIR =	../../../../..

PACKAGE =	org/multijava/mjc/testcase/codegen

SUBDIRS = protHelpers

## -------------------------------------------------------------
## GOALS

default:	buildtests runtests

clean-this:	simple-clean local-clean

local-clean:
	$(RM) *.*-ckd *.class core file-list.junk *-expected.old
	$(RM) `cat .cvsignore`

## -------------------------------------------------------------
## Local targets and vars

MJC = $(JAVA) org.multijava.mjc.Main
MJCFLAGS = $(USERMJCFLAGS)
# user can override this on the command line to pass extra flags to mjc
USERMJCFLAGS = 

DIS = $(JAVA) org.multijava.dis.Main
DISFLAGS = --stdout --sorted

SED = sed
RM = rm -f
DIFF = diff -w
MV = mv -f

.SUFFIXES: .java .java-ckd .java-expected .class .class-ckd .class-expected

.java.java-expected:
	-mv $@ $@.old
	$(MJC) $(MJCFLAGS) $< 2>&1 | $(PATHTOFILE) | tee $@

.java.java-ckd:
	$(MJC) $(MJCFLAGS) $< 2>&1 | $(PATHTOFILE) | tee $@

.class.class-expected:
	-mv $@ $@.old
	$(DIS) $(DISFLAGS) $< 2>&1 | $(PATHTOFILE) | tee $@

.class.class-ckd:
	$(DIS) $(DISFLAGS) $< 2>&1 | $(PATHTOFILE) | tee $@

all: check

check: runtests-this-junit

runtests-this-junit: check-1.4 junittests checklists 

runtests-this-nojunit: check-1.4 compileall checkall

TESTFILES =	BytecodeAttr1TestCase BytecodeAttr2TestCase \
		BytecodeAttr3TestCase BytecodeAttr5TestCase \
		TestsWarningsTestSuite \
		TestsCompilesTestSuite TestsDisassemblesTestSuite \
		CharAttributeATestCase IntAttributeATestCase StrAttributeATestCase

junittests-before:
	@echo 'COMPILING&DISASSEMBLING' `pwd`
	@touch skip
	@ls .java *.java | grep -v -f skip -f warning-tests > listTestsCompiles
	@ls *.class-expected | sed -e s/-expected// | grep -v -f skip > listTestsDisassembles
	@$(JAVAC) *TestCase.java

# We cannot generate listTestsDisassembles by listing all the .class
# files because they get created by the compile tests and may not be
# present when junittests-before is run.  So we generate the list
# initially from *.class-expected .  But then after the tests are run
# we check that we get the same list from *.class.  This will catch
# any cases of a new .java test that does not have a corresponding
# .class-expected test.

# Secondly we check that there is a *TestCase.java file for each *.sh file
# FIXME - we should also check that each of the *TestCase.java files is in
# the TESTFILES list
checklists:
	@ls *.class | grep -v -f skip | diff - listTestsDisassembles
	@ls *.sh | sed -e 's/\.sh/TestCase.java/' > x
	@ls *TestCase.java | diff - x
	@rm x

# compile all test cases.
compileall:
	@echo COMPILING `pwd`
	@touch skip;
	@-for f in `ls .java *.java | grep -v -f skip` ; \
	do \
	  if [ ! "$(QUIET)" ]; then echo Compiling $$f ... ; else printf "." ; fi; \
	  $(MJC) $(MJCFLAGS) $$f 2>&1 \
	     | $(PATHTOFILE) > $${f}-ckd ; \
	  $(DIFF) $${f}-ckd $${f}-expected && \
		(if [ ! "$(QUIET)" ]; then echo 'Compared as expected!'; fi && $(RM) $${f}-ckd; ); \
	done 2>&1 | tee checkall.out-ckd
	@echo; echo Completed compiling

# check generated code for all test cases.
checkall: checkjava checkscripts

checkjava:
	@echo DISASSEMBLING `pwd`
	@touch skip;
	@-for f in `ls *.class | grep -v -f skip` ; \
	do \
	  if [ ! "$(QUIET)" ]; then echo Checking generated code for $$f ... ; else printf "." ; fi; \
	  $(DIS) $(DISFLAGS) $$f 2>&1 \
	     | $(PATHTOFILE) > $${f}-ckd ; \
	  $(DIFF) $${f}-expected $${f}-ckd && \
             ( if [ ! "$(QUIET)" ]; then echo 'Compared as expected!'; fi && $(RM) $${f}-ckd; ); \
	done 2>&1 | tee checkall.out-ckd
	@echo; echo Completed disassembling

checkscripts:
	@echo CHECKING SCRIPTS `pwd`
	@-for f in `ls *.sh | grep -v -f skip` ; \
	do \
	  $(MAKE) $${f}-test ; \
	done 2>&1 | tee -a checkall.out-ckd
	@echo; echo Completed `pwd`

#check test cases that rely on java 1.4, if we are equipped to do so
check-1.4:
	@$(JAVA) -version 2>&1 \
	  | $(PATHTOFILE) > .javaVersion ; \
	grep -q 'java version \"1.3' .javaVersion \
	    && echo SKIPPING JAVA 1.4 TESTS ON THIS PLATFORM \
	|| $(MAKE) -C java1_4 check ; \

%.sh-test:
	@if  [ ! "$(QUIET)" ]  ; then echo Checking $(@F:%.sh-test=%.sh) ... ; else printf "." ; fi
	@chmod +x $(@F:%.sh-test=%.sh)
	@USERMJCFLAGS=$(USERMJCFLAGS) ./$(@F:%.sh-test=%.sh) 2>&1 \
	     | $(PATHTOFILE) > $(@F:%.sh-test=%.sh-ckd) 
	@$(DIFF) $(@F:%.sh-test=%.sh-expected) $(@F:%.sh-test=%.sh-ckd) && \
	    ( if [ ! "$(QUIET)" ]; then echo 'Compared as expected!' ; fi; ) && \
	    $(RM) $(@F:%.sh-test=%.sh-ckd)

create expected:
	-for f in `ls .java *.java | grep -v -f skip` ; \
	do \
	  echo Compiling $$f ... ; \
	  $(MV) $${f}-expected $${f}-expected.old ; \
	  $(MJC) $(MJCFLAGS) $$f 2>&1 \
	     | $(PATHTOFILE) > $${f}-expected ; \
	  $(DIFF) $${f}-expected.old $${f}-expected && echo 'Unchanged!' && $(RM) $${f}-expected.old;\
	done 2>&1 | tee create-compile.out-ckd
	-for f in `ls *.class` ; \
	do \
	  echo Disassembling $$f ... ; \
	  $(MV) $${f}-expected $${f}-expected.old ; \
	  $(DIS) $(DISFLAGS) $$f 2>&1 \
	     | $(PATHTOFILE) > $${f}-expected ; \
	  $(DIFF) $${f}-expected.old $${f}-expected && echo 'Unchanged!' && $(RM) $${f}-expected.old;\
	done 2>&1 | tee create-disasm.out-ckd

## ----------------------------------------------------------------------
## GLOBAL DEFINITIONS

include $(TOPDIR)/Make.ProjDefs
