# Makefile for the Contract4J examples and unit tests, from aspectprogramming.com
# http://www.aspectprogramming.com/contract4j
#
# Copyright (c) 2005 Dean Wampler. All rights reserved.
#   Licensed under the Eclipse Public License - v 1.0; you may not use this
#   software except in compliance with the License. You may obtain a copy of the 
#   License at
#
#       http://www.eclipse.org/legal/epl-v10.html
#
#   A copy is also included with this distribution. See the "LICENSE" file.
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

HOMES = \
	JAVA_HOME=${JAVA_HOME} \
	ASPECTJ_HOME=${ASPECTJ_HOME} \
	JUNIT_HOME=${JUNIT_HOME} \
	CONTRACT4J_HOME=${CONTRACT4J_HOME}

UTIL_HOME=${CONTRACT4J_HOME}/../util

ASPECTJ_JAR=${ASPECTJ_HOME}/lib/aspectjrt.jar
JUNIT_JAR=$(JUNIT_HOME)/junit.jar

PATH:=${JAVA_HOME}/bin:$(ASPECTJ_HOME)/bin:${PATH}

SRC_DIR=src/lotto
GENSRC_DIR=gensrc/lotto

JAVA_FILES=${wildcard $(SRC_DIR)/*.java}
CLASSES=${JAVA_FILES:%.java=%.class}
# CLASSES2 used for the clean target; picks up classes for nested types!
CLASSES2=${wildcard $(SRC_DIR)/*.class} 

CONTRACT4J_JAR=${CONTRACT4J_HOME}/contract4j/contract4j.jar
UTIL_JAR=${UTIL_HOME}/util.jar
EX_JAVA_JAR=${PWD}/examples_java.jar
EX_JAR=${PWD}/examples.jar

CLASSPATH1  = .:${JAVA_HOME}/lib/tools.jar:${UTIL_JAR}:${CONTRACT4J_JAR}:${ASPECTJ_JAR}:${JUNIT_JAR}
JAVA_FLAGS  = -classpath ${CLASSPATH1}:classes ${JAVA_FLAGS2}
JAVAC_FLAGS = -classpath ${CLASSPATH1}:src ${JAVA_FLAGS2} -Xlint ${JAVAC_FLAGS2}
AJC_FLAGS   = -classpath ${CLASSPATH1}:src:gensrc ${JAVA_FLAGS2} -1.5 -inpath $(EX_JAVA_JAR) -d classes

#CONTRACT4JFACTORY=-factorypath ${CONTRACT4JAPT_JAR}
CONTRACT4JFACTORY=-factory com.aspectprogramming.contract4j.ap.Contract4JAPF
#APT_XFLAGS=-XListAnnotationTypes -XListDeclarations -XPrintAptRounds -XPrintFactoryInfo
#APT_FLAGS2=-print
APT_FLAGS=${JAVAC_FLAGS} -nocompile -sourcepath src -s gensrc \
	${CONTRACT4JFACTORY} ${APT_FLAGS2} ${APT_XFLAGS} 
DBG=jdb

all: test

# Since APT can't drive AspectJ, we drive javac and ajc ourselves. However, it would
# probably work to have apt drive javac, then invoke ajc afterwards for the aspectj
# files.
compile: gensrc classes
	apt   ${APT_FLAGS} ${JAVA_FILES}
	javac ${JAVAC_FLAGS} ${JAVA_FILES}
	cd src; jar cf ${EX_JAVA_JAR} lotto
	ajc   ${AJC_FLAGS} ${GENSRC_DIR}/*.aj

# Rebuild and rerun with a different set of flags and property files.
test: 
	$(MAKE) N=1 APT_FLAGS2_TEST=             testrun
#	$(MAKE) N=2 APT_FLAGS2_TEST=-Anocontract testrun
#	$(MAKE) N=3 APT_FLAGS2_TEST=-Anopre      testrun
#	$(MAKE) N=4 APT_FLAGS2_TEST=-Anopost     testrun
#	$(MAKE) N=5 APT_FLAGS2_TEST=-Anoinvar    testrun

testrun: 
	$(MAKE) clean
	cp $(SRC_DIR)/test/test.properties.$N $(SRC_DIR)/test/test.properties
	$(MAKE) APT_FLAGS2=${APT_FLAGS2_TEST} compile
	$(MAKE) runtests
	rm -f $(SRC_DIR)/test/test.properties

runtests: Trommel

Trommel:
	java ${JAVA_FLAGS} lotto.$@ 

debug.Trommel:
	${DBG} ${JAVA_FLAGS} lotto.$@ 

debug.apt:
	${DBG} ${JAVAC_FLAGS} apt ${APT_FLAGS} ${JAVA_FILES}

%.class: %.java
	javac ${JAVAC_FLAGS} $<

gensrc classes:
	mkdir -p $@
	
doc:
	javadoc ${JAVAC_FLAGS} ${JAVA_FILES}

dump:
	@echo "JAVA_FILES=\"${JAVA_FILES}\""
	@echo "CLASSES=\"${CLASSES}\""

clean:
	-rm -f ${CLASSES} ${CLASSES2} *.jar
	-rm -rf gensrc classes

