<?xml version="1.0" encoding="UTF-8"?>
<project name="LinkBench" default="dist" basedir=".">
  <description>LinkBench ANT script</description>

  <!-- set global properties for this build -->
  <property name="base" location="${basedir}/benchmark"/>
  <property name="dist-addon" location="${basedir}/dist-addon"/>
  
  <property name="src-cli" location="${base}/cli"/>
  <property name="src-common" location="${base}/common"/>
    
  <property name="build" location="${basedir}/build"/>
  <property name="dist"  location="${basedir}/../javabin"/>
  <property name="lib" location="${dist}/lib"/>
  
  <property name="java-package" value="benchmark.common"/>

  <property environment="env"/>
  <property name="jar-tinyos" location="${env.TOSROOT}/support/sdk/java/tinyos.jar"/>

  <path id="classpath">
    <pathelement location="${build}"/>
    <fileset dir="${lib}" includes="**/*.jar"/>
    <fileset file="${jar-tinyos}"/>
  </path>

  <target name="-check-init-build-done">
    <available property="init.build.done" file="${build}"/>
  </target>
  
  <target name="-check-init-dist-done">
    <available property="init.dist.done" file="${dist}"/>
  </target>

  <target name="-check-compile-common">
    <available property="compile.common" file="${build}/benchmark/common"/>
  </target>

  <target name="-init-build" depends="-check-init-build-done" unless="init.build.done">
    <echo level="info" message="Creating build environment"/>
    <mkdir dir="${build}"/>
  </target>

  <target name="-init-dist" depends="-check-init-dist-done" unless="init.dist.done">
    <echo level="info" message="Creating dist environment"/>
    <mkdir dir="${dist}"/>
    <copy todir="${dist}" >
      <fileset dir="${dist-addon}" />
    </copy>
  </target>
  
  <target name="-init-mig">
		<echo message="Generating TinyOS message classes (Mig)" />
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/StatT.java" />
			<arg value="-java-classname=${java-package}.StatT" />
			<arg value="java" />
			<arg path="${basedir}/../BenchmarkCore.h" />
			<arg value="stat_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/ProfileT.java" />
			<arg value="-java-classname=${java-package}.ProfileT" />
			<arg value="java" />
			<arg path="${basedir}/../BenchmarkCore.h" />
			<arg value="profile_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/SetupT.java" />
			<arg value="-java-classname=${java-package}.SetupT" />
			<arg value="java" />
			<arg path="${basedir}/../BenchmarkCore.h" />
			<arg value="setup_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/CtrlMsgT.java" />
			<arg value="-java-classname=${java-package}.CtrlMsgT" />
			<arg value="java" />
			<arg path="${basedir}/../Messages.h" />
			<arg value="ctrlmsg_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/SyncMsgT.java" />
			<arg value="-java-classname=${java-package}.SyncMsgT" />
			<arg value="java" />
			<arg path="${basedir}/../Messages.h" />
			<arg value="syncmsg_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/SetupMsgT.java" />
			<arg value="-java-classname=${java-package}.SetupMsgT" />
			<arg value="java" />
			<arg path="${basedir}/../Messages.h" />
			<arg value="setupmsg_t" />
		</exec>
		
		<exec executable="mig" failonerror="true">
			<arg value="-target=iris" />
			<arg value="-o" />
			<arg path="${src-common}/DataMsgT.java" />
			<arg value="-java-classname=${java-package}.DataMsgT" />
			<arg value="java" />
			<arg path="${basedir}/../Messages.h" />
			<arg value="datamsg_t" />
		</exec>

		<exec executable="ncg" failonerror="true">
			<arg line="-target=iris -o ${src-common}/BenchmarkStatic.java -java-classname=${java-package}.BenchmarkStatic java ${basedir}/../Messages.h MAX_TIMER_COUNT SETUP_BASE CTRL_SETUP_SYN SYNC_SETUP_ACK CTRL_START CTRL_RESET CTRL_STAT_REQ DATA_STAT_OK CTRL_PROFILE_REQ DATA_PROFILE_OK GLOBAL_USE_ACK GLOBAL_USE_BCAST GLOBAL_USE_MAC_LPL GLOBAL_USE_MAC_PLINK LPL_WAKEUP_OFFSET PLINK_RETRIES_OFFSET PLINK_DELAY_OFFSET MAC_SETUP_LENGTH" />
		</exec>
		
	</target>

  <target name="init" depends="-init-build,-init-dist,-init-mig" />

  <target name="-clean-build">
    <delete dir="${build}"/>
  </target>
  
  <target name="-clean-mig">
  	<delete>
    	<fileset dir="${src-common}" includes="**/*T.java"/>
  	</delete>
    <delete file="${src-common}/BenchmarkStatic.java"/>
  </target>
  
  <target name="-clean-dist">
    <input
      message="All data is going to be deleted from ${dist}. It is possible that results live there. Continue (y/n)?"
      validargs="y,n"
      addproperty="do.delete"
    />
    <condition property="do.abort">
      <equals arg1="n" arg2="${do.delete}"/>
    </condition>
    <fail if="do.abort">Clean task aborted by user.</fail>
    <delete dir="${dist}"/>
  </target>

  <target name="clean" depends="-clean-build,-clean-dist,-clean-mig" description="clean up" />
    

  <target name="compile-common" depends="-check-compile-common,init" unless="compile.common" description="compile the Common library code">
    <javac srcdir="${src-common}" destdir="${build}" classpathref="classpath"/>
  </target>
  
  <target name="compile-cli" depends="init,compile-common" description="compile the CLI code">
    <javac srcdir="${src-cli}" destdir="${build}" classpathref="classpath"/>
  </target>

  <target name="jar-common" depends="compile-common" description="make the Common jar">
    <jar destfile="${lib}/benchmark-common.jar" basedir="${build}" includes="**/common/**">
      <manifest>
        <attribute name="Class-Path" value="${jar-tinyos} snakeyaml-1.7.jar"/>
      </manifest>
    </jar>
  </target>

  <target name="jar-cli" depends="compile-cli" description="make the CLI jar">
    <jar destfile="${dist}/linkbench.jar" basedir="${build}" includes="**/cli/**">
      <manifest>
        <attribute name="Main-Class" value="benchmark.cli.BenchmarkCli"/>
        <attribute name="Class-Path" value="lib/benchmark-common.jar lib/commons-cli-1.2.jar"/>
      </manifest>
    </jar>
  </target>

  <target name="dist" depends="jar-common,jar-cli,-clean-build" description="generate the distribution" >
  </target>


</project>
