<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build" name="Java Information Dynamics Toolkit" xmlns:if="ant:if">
	<description>
		Build file for the Java Information Dynamics Toolkit
	</description>
	
	<!-- set global properties for this build -->
	<property name="version" value="1.6.1"/>
	<property name="mainfilename" value="infodynamics"/>
	<property name="jarplainname" value="${mainfilename}.jar" />
	<property name="jarversiondistnamezip" value="${mainfilename}-jar-${version}.zip" />
	<property name="distname" value="${mainfilename}-dist-${version}" />
	<property name="distnamezip" value="${distname}.zip" />
	<property name="distnametargz" value="${distname}.tar.gz" />
	<property name="src" location="java/source"/>
	<property name="cudasrc" location="cuda"/>
	<property name="bin" location="bin"/>
	<property name="unittestsouttoplevel" location="unittests"/>
	<property name="unittestssrc" location="java/unittests"/>
	<property name="unittestsbin" location="${unittestsouttoplevel}/bin"/>
	<property name="reports.tests" location="${unittestsouttoplevel}/reports"/>
	<property name="javadocsdir" location="javadocs"/>
	<property name="versionfile" value="version-${version}.txt"/>
	<!-- To enable GPU code, set the following variable to true -->
	<property name="enablegpu" value="false"/>
	
	<path id="project.classpath">
		<pathelement location="bin"/>
	</path>

	<path id="apache-classpath">
		<pathelement path="./share/commons-math3-3.5.jar"/>
	</path>

	<!-- Make required directories -->
	<target name="init" description="Create the compiled code directories">
		<mkdir dir="${bin}"/>
		<mkdir dir="${bin}/cuda"/>
		<mkdir dir="${unittestsbin}"/>
		<mkdir dir="${unittestsbin}/cuda"/>
		<mkdir dir="${reports.tests}"/>
	</target>

	<!-- Compile the java toolkit -->
	<target name="compile" depends="init" description="compile the source">
		<!-- Compile to Java 7 to provide compatibility for users with older JREs.
		     Caveat: The flags here only check the language compatibility, but
		     may still use newer libraries which may cause issues for users with JDK 7.
		     Indeed, one gets the warning: "bootstrap class path not set in conjunction with -source 1.7"
		     To fix this, one would use the bootstrap classpath to point our JDK to an rt.jar
		     for Java 7. 
		     At this stage, I'm sure I'm not using new library calls from Java 8+, so we can
		     ignore the warning, and I don't want to bother installing newer Java just to compile
		     like this. I'll endeavour not to use JDK 8 libraries so as not to cause
		     any issues here ... -->
		<javac srcdir="${src}" destdir="${bin}" includeAntRuntime="false" target="1.7" source="1.7" encoding="UTF8">
			<classpath refid="apache-classpath"/>
		</javac>

		<!-- Compiling Cpp code -->
		<antcall target="gpu" if:true="${enablegpu}">
			<param name="DEBUG" value="0"/>
		</antcall>

	</target>

	<!-- Jar the toolkit -->
	<target name="jar" depends="compile" description="Create the jar for distribution">
		<!-- Put everything in ${bin} into the infodynamics-${version}.jar file -->
		<jar jarfile="${jarplainname}" basedir="${bin}" excludes="cuda/*.o,cuda/*.a,cuda/findComputeCapability">
			<manifest>
				<attribute name="Main-Class" value="infodynamics.demos.autoanalysis.AutoAnalyserLauncher"/>
			</manifest>
		</jar>
		<!-- Set the jar to be runnable: (only has effect on unix/linux)
		     so we can double click to run the AutoAnalyser -->
		<chmod file="${jarplainname}" perm="u+x"/>
	</target>

	<!-- Compile and run the JUnit tests -->
	<target name="junit" depends="compile" description="Run the junit tests and make sure they compile">

		<!-- Compile the junit tests first -->
		<javac destdir="${unittestsbin}" includeAntRuntime="true" debug="true">
			<!-- Need includeAntRuntime=true if you want to pick up junit.jar and ant-junit.jar in ANT_HOME/lib;
			     Otherwise you will need to set the classpath to include these here. -->
			<src path="${unittestssrc}"/>
			<classpath refid="project.classpath"/>
			<classpath refid="apache-classpath"/>
		</javac>

		<!-- Run the junit tests and make sure they complete ok -->
		<junit printsummary="yes" showoutput="yes" fork="true" forkmode="once" haltonfailure="yes" haltonerror="yes" includeantruntime="true">
			<classpath>
				<path refid="project.classpath"/>
				<pathelement path="${unittestsbin}"/>
				<pathelement location="${basedir}/clover.jar"/>
			</classpath>
			<formatter type="plain"/>
			<batchtest todir="${reports.tests}"> <!-- Writes full reports with stdout and stderr to ${reports.tests} -->
				<fileset dir="${unittestsbin}">
					<include name="**/*.class"/>
					<exclude name="**/*AbstractTester.class"/>
					<exclude name="**/ActiveInfoStorageCalculatorCorrelationIntegrals.class"/>
					<exclude name="**/ActiveInfoStorageCalculatorKernelDirect.class"/>
					<exclude name="**/*GPU*.class"/>
				</fileset>
			</batchtest>
		</junit>

		<!-- If applicable, run also GPU tests -->
		<antcall target="gputest" if:true="${enablegpu}"/>
	</target>

	<!-- Make javadocs, excluding the AutoAnalyser and classes we've derived from Apache Commons Math -->
	<target name="javadocs" depends="compile" description="Make the javadocs for the toolkit">
		<delete dir="${javadocsdir}"/>
		<javadoc destdir="${javadocsdir}">
			<packageset dir="${src}">
				<include name="**"/>
				<exclude name="infodynamics/demos/**"/>
				<exclude name="**/commonsmath3/*"/>
				<exclude name="**/commonsmath3/**"/>
			</packageset>
		</javadoc>
		<!-- Change some of the style in the javadocs css for our lists: -->
		<concat destfile="${javadocsdir}/stylesheet.css" append="true">
		.block ul li {list-style:disc; margin-left: 20px;}
		.block ol li {list-style:decimal; margin-left: 40px;}
		.block ol li ol li {list-style:lower-alpha; margin-left: 40px;}
		.block ol li ul li {list-style:disc; margin-left: 40px;}
		.block ol li ul li ol li {list-style:lower-alpha; margin-left: 40px;}
		.block ol li ol li ol li {list-style:lower-roman; margin-left: 40px;}
		.block ul li ol li {list-style:decimal; margin-left: 20px;}
		</concat>
	</target>

	<!-- Clean up -->
	<target name="clean">
		<!-- Delete the compiled code directories -->
		<delete dir="${bin}"/>
		<delete dir="${unittestsbin}"/>
		<delete dir="${reports.tests}"/>
		<delete dir="${unittestsouttoplevel}"/>
		<delete dir="${javadocsdir}"/>
		<delete dir="demos/clojure/target"/>
		<delete file="demos/clojure/pom.xml"/>
		<delete file="demos/clojure/pom.xml.asc"/>
		<delete file="demos/clojure/project.clj"/>
		<delete file="${jarversiondistnamezip}"/>
		<delete file="${distnamezip}"/>
		<delete file="${distnametargz}"/>
		<delete file="${jarplainname}"/>
		<delete>
			 <fileset dir="demos/AutoAnalyser" includes="GeneratedCalculator.*"/>
		</delete>
		<delete>
			 <fileset dir="demos/java/infodynamics/demos/autoanalysis" includes="GeneratedCalculator.*"/>
		</delete>
		<antcall target="gpuclean"/>
		<!-- Don't delete the readme and version files - the user may not have the template to recreate them from -->
	</target>
	
	<!-- Build the project jar for users -->
	<target name="build" depends="jar" description="user: build the project jar file">
		<echo message="${ant.project.name}: ${ant.file}"/>
	</target>

	<!--
	***********************************
	
	Developer builds below here
	
	***********************************
	-->

	<!-- Compile and jar the toolkit with debug symbols -->
	<target name="debug" depends="init" description="compile and jar with debug symbols">
		<echo message="Compiling for debug"/>
		<javac srcdir="${src}" destdir="${bin}" includeAntRuntime="false" target="1.7" source="1.7" debug="true">
			<classpath refid="apache-classpath"/>
		</javac>

		<!-- Compiling Cpp code -->
		<antcall target="gpu" if:true="${enablegpu}">
			<param name="DEBUG" value="1"/>
		</antcall>

		<jar jarfile="${jarplainname}" basedir="${bin}"/>
	</target>

	<!-- Compile GPU code -->
	<target name="gpu" depends="init" description="compile only C/C++ GPU code">
		<exec executable="make" dir="${cudasrc}" failonerror="true" resultproperty="return.code">
			<env key="DEBUG" value="${DEBUG}"/>
		</exec>
		<fail>
			<condition>
				<isfailure code="${return.code}"/>
			</condition>
		</fail>
	</target>

	<!-- Clean GPU code -->
	<target name="gpuclean" description="clean only C/C++ GPU code">
		<exec executable="make" dir="${cudasrc}" failonerror="true">
			<arg value="clean"/>
		</exec>
	</target>

	<!-- Test GPU code -->
	<target name="gputest" depends="gpu, compile" description="compile and run C unit tests for GPU code">

		<!-- Compile and run the C-only GPU unit tests -->
		<exec executable="make" dir="${cudasrc}" failonerror="true" resultproperty="return.code">
			<arg value="test"/>
		</exec>
		<fail>
			<condition>
				<isfailure code="${return.code}"/>
			</condition>
		</fail>

		<exec executable="./unittest" dir="${unittestsbin}/cuda/" failonerror="true" resultproperty="return.code"/>
		<fail>
			<condition>
				<isfailure code="${return.code}"/>
			</condition>
		</fail>

		<!-- Compile and run the Java+C GPU tests -->
		<javac destdir="${unittestsbin}" includeAntRuntime="true" debug="true">
			<src path="${unittestssrc}"/>
			<classpath refid="project.classpath"/>
			<classpath refid="apache-classpath"/>
		</javac>

		<junit printsummary="yes" showoutput="yes" fork="true" forkmode="once" haltonfailure="yes" haltonerror="yes" includeantruntime="true">
			<classpath>
				<path refid="project.classpath"/>
				<pathelement path="${unittestsbin}"/>
				<pathelement location="${basedir}/clover.jar"/>
			</classpath>
			<formatter type="plain"/>
			<batchtest todir="${reports.tests}"> <!-- Writes full reports with stdout and stderr to ${reports.tests} -->
				<fileset dir="${unittestsbin}">
					<include name="**/*GPU*.class"/>
				</fileset>
			</batchtest>
		</junit>

	</target>

	<!-- Developer build - creates readme and version files -->
	<target name="readmefiles" description="developer: create the readme and version files from templates">
		<tstamp>
			<format property="TODAY_DATE" pattern="dd/MM/yyyy" locale="en,UK"/>
			<format property="TODAY_YEAR" pattern="yyyy" locale="en,UK"/>
		</tstamp>
		<copy file="readme-template.txt" toFile="readme.txt" failonerror="false" overwrite="true">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="YEAR" value="${TODAY_YEAR}"/>
				<filter token="DATE" value="${TODAY_DATE}"/>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
		<copy file="version-template.txt" toFile="${versionfile}" failonerror="false">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="YEAR" value="${TODAY_YEAR}"/>
				<filter token="DATE" value="${TODAY_DATE}"/>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
		<copy file="demos/clojure/deploy/project-template.clj" toFile="demos/clojure/deploy/project.clj" failonerror="false" overwrite="true">
			<!-- The template file only exists in SVN - this won't crash ant 
			     if users try to run it though because failonerror="false" -->
			<filterset>
				<filter token="VERSION" value="${version}"/>
			</filterset>
		</copy>
	</target>

	<!-- Developer build - builds everything and makes the jar only distribution file -->
	<target name="jardist" depends="build,readmefiles" description="developer: generate the jar distribution">
		<zip destfile="${jarversiondistnamezip}">
			<fileset file="${jarplainname}"/>
			<fileset file="license-gplv3.txt"/>
			<fileset file="readme.txt"/>
			<fileset file="${versionfile}"/>
			<zipfileset dir="notices" includes="**/*.*,**/*" prefix="notices"/>
		</zip>
	</target>
	
	<!-- Developer build - builds everything and makes the full distribution file in zip and tar.gz -->
	<target name="dist" depends="jar,junit,javadocs,readmefiles" description="developer: generate the full distribution">
		<echo message="${ant.project.name}: ${ant.file}"/>
		<zip destfile="${distnamezip}">
			<fileset file="build.xml"/>
			<zipfileset file="${jarplainname}" filemode="755"/>
			<fileset file="license-gplv3.txt"/>
			<fileset file="readme.txt"/>
			<fileset file="InfoDynamicsToolkit.pdf"/>
			<fileset file="JIDT-logo.png" erroronmissingdir="false"/> <!-- This file is missing in full repository versions -->
			<fileset file="${versionfile}"/>
			<zipfileset dir="java" includes="**/*.java" prefix="java"/>
			<zipfileset dir="demos" includes="**/*.*,**/*" excludes="clojure/deploy,clojure/deploy/*.*,python/*.pyc,**/*.sh,**/*.bat" prefix="demos"/>
			<zipfileset dir="demos" includes="**/*.sh,**/*.bat" prefix="demos" filemode="755"/> <!-- Do these separately to get executable permissions -->
			<zipfileset dir="javadocs" includes="**/*.*,**/*" prefix="javadocs"/>
			<zipfileset dir="notices" includes="**/*.*,**/*" prefix="notices"/>
			<zipfileset dir="cuda" prefix="cuda" excludes="benchmark.sh"/>
			<zipfileset dir="cuda" prefix="cuda" includes="benchmark.sh" filemode="755"/> <!-- Do this separately to get executable permissions -->
			<zipfileset dir="course" prefix="course"/>
			<zipfileset dir="tutorial" prefix="tutorial"/> <!-- Get rid of this when tutorial is subsumed in course... -->
			<zipfileset dir="web" includes="JIDT-logo.png" prefix="" erroronmissingdir="false"/> <!-- This file is missing in zip dist versions -->
		</zip>
		<tar destfile="${distnametargz}" compression="gzip" longfile="posix"> <!-- for longfiles could also use "gnu" but apparently is slightly less widely supported -->
			<zipfileset src="${distnamezip}"/>
		</tar>
	</target>
</project>
