<project name="compiler-design" default="test" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">

	<!-- A very simple ant file that primarily exists to run cup and lex;
	it also includes targets to build the compiler and run tests for those
	who would rather not use Eclipse -->

	<!-- Set project properties. -->
	<property name="src.dir" value="${basedir}/src"/>
	<property name="test.dir" value="${basedir}/test"/>
	<property name="javali_tests.dir" value="${basedir}/javali_tests"/>
	<property name="benchmarks.dir" value="${basedir}/benchmarks"/>
	<property name="parser.dir" value="${src.dir}/cd/frontend/parser"/>
	<property name="parser.jar" value="${basedir}/lib/javaliParserObf.jar"/>
	<property name="build.dir" value="${basedir}/bin"/>
	<property name="junit.jar" value="${basedir}/lib/junit-4.12.jar"/>
	<property name="hamcrest.jar" value="${basedir}/lib/hamcrest-core-1.3.jar"/>
	<property name="antlr.jar" value="${basedir}/lib/antlr-4.7.1-complete.jar"/>
	<property name="antlr.profile" value="false"/>
	<property name="antlr.report" value="false"/>
	<property name="jacocoant.jar" value="${basedir}/lib/jacocoant.jar"/>
	<property name="coverage.file" location="${build.dir}/jacoco.exec"/>
	<property name="min.coverage" value="0.5"/>
	<property name="coverage.check" value="cd.frontend.*:cd.backend.*"/>

	<!-- Cleans generated code, but NOT the parser source! -->
	<target name="clean">
		<delete dir="${build.dir}"/>
	</target>

    <!-- Builds the compiler framework for HW > HW1. -->
    <target name="compile">
        <mkdir dir="${build.dir}"/>

        <javac  debug="true" destdir="${build.dir}" includeantruntime="false">
            <src path="${src.dir}"/>
            <src path="${test.dir}"/>
            <classpath>
                <pathelement location="${antlr.jar}"/>
                <pathelement location="${junit.jar}"/>
                <pathelement location="${hamcrest.jar}"/>
            	<pathelement location="${parser.jar}"/>
            </classpath>
        </javac>
    </target>

	<!-- Deletes all byproducts of running the tests -->
	<target name="clean-test">
		<delete>
			<fileset dir="${javali_tests.dir}" includes="**/*.err"/>
			<fileset dir="${javali_tests.dir}" includes="**/*.s"/>
			<fileset dir="${javali_tests.dir}" includes="**/*.bin"/>
			<fileset dir="${javali_tests.dir}" includes="**/*.dot"/>
			<fileset dir="${javali_tests.dir}" includes="**/*.exe"/>
            <fileset dir="${javali_tests.dir}" includes="**/*.ref"/>
			
			<fileset dir="${benchmarks.dir}" includes="**/*.err"/>
			<fileset dir="${benchmarks.dir}" includes="**/*.s"/>
			<fileset dir="${benchmarks.dir}" includes="**/*.bin"/>
			<fileset dir="${benchmarks.dir}" includes="**/*.dot"/>
			<fileset dir="${benchmarks.dir}" includes="**/*.exe"/>
            <fileset dir="${benchmarks.dir}" includes="**/*.ref"/>
		</delete>
	</target>

	<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
		<classpath path="${jacocoant.jar}"/>
	</taskdef>

	<!-- Runs the tests.  Use the compile target first! -->
	<target name="test" depends="compile">
		<jacoco:coverage destfile="${coverage.file}">
            <junit fork="true" forkmode="once" failureproperty="tests-failed" outputtoformatters="false">
            	<formatter type="brief" usefile="false"/>
                <batchtest skipNonTests="true">
                    <fileset dir="bin" includes="**/*.class" />
                </batchtest>
                <assertions enablesystemassertions="true" />
                <sysproperty key="cd.meta_hidden.Version" value="BENCH" />
                <classpath>
                    <pathelement location="${build.dir}"/>
                    <pathelement location="${junit.jar}"/>
                    <pathelement location="${hamcrest.jar}"/>
                    <pathelement location="${antlr.jar}"/>
                    <pathelement location="${parser.jar}"/>
                </classpath>
            </junit>
		</jacoco:coverage>
        <fail if="tests-failed" />
	</target>

	<target name="bench" depends="compile">
		<java classname="cd.BenchmarksRunner" fork="yes" error="bench-log.txt">
			<classpath>
				<pathelement location="${build.dir}"/>
                <pathelement location="${antlr.jar}"/>
                <pathelement location="${junit.jar}"/>
                <pathelement location="${hamcrest.jar}"/>
                <pathelement location="${parser.jar}"/>
            </classpath>
		</java>	
	</target>
	
</project>
