<project name="TraceAgent" basedir="." default="help">

  <!-- directory that contains emma.jar and emma_ant.jar: -->
  <path id="emma.lib" >
    <pathelement location="./lib/emma.jar" />
    <pathelement location="./lib/emma_ant.jar" />
  </path>

  <path id="findbugs.lib" >
    <fileset dir="./lib/findbugs" includes="*.jar" />
  </path>

  <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="./lib/ant-contrib-1.0b3.jar" />
  <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
  <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
    <classpath refid="findbugs.lib"/>
  </taskdef>
  <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
           classpath="./lib/jarjar-1.0.jar"/>


  <property name="findbugs.home" value="./lib/findbugs" />


  <target name="help">
    <java classname="org.apache.tools.ant.Main">
      <arg value="-projecthelp" />
    </java>
  </target>



  <target name="clean" description="Comile classes">
    <delete dir="./build" />
    <delete dir="./reports" />
  </target>



  <target name="build" description="Comile classes">
    <mkdir dir="./build" />
    <mkdir dir="./build/classes" />

    <javac destdir="./build/classes" debug="true" source="1.6" target="1.6">
      <src path="./src"/>
      <classpath>
        <fileset dir="./lib" includes="*.jar" />
      </classpath>
    </javac>
  </target>



  <target name="getgitdetails" >
    <exec executable="git" outputproperty="git.tagstring">
      <arg value="describe"/>
    </exec>
    <exec executable="git" outputproperty="git.revision">
      <arg value="rev-parse"/>
      <arg value="HEAD"/>
    </exec>
    <if>
      <contains string="${git.tagstring}" substring="cannot"/>
      <then>
        <property name="git.tag" value="none"/>
      </then>
      <else>
        <property name="git.tag" value="${git.tagstring}"/>
      </else>
    </if>
  </target>



  <target name="jar" description="Create Jars" depends="build,getgitdetails" >

    <mkdir dir="./build/jars/" />

    <!-- Trace Agent -->
    <jarjar jarfile="./build/jars/traceagent.jar" compress="false">
      <manifest>
        <attribute name="Premain-Class" value="org.intrace.agent.Agent" />
        <attribute name="Agent-Class" value="org.intrace.agent.Agent" />
        <attribute name="Can-Redefine-Classes" value="true" />
        <attribute name="Git-Revision" value="${git.revision}" />
        <attribute name="Implementation-Version" value="${git.tag}" />
      </manifest>      
      <fileset dir="./build/classes" includes="**/agent/**/*.class" />
      <fileset dir="./build/classes" includes="**/output/**/*.class" />
      <fileset dir="./build/classes" includes="**/shared/*.class" />
      <zipfileset src="./lib/asm-debug-all-3.3.jar"/>
      <rule pattern="org.objectweb.asm.**" result="org.intrace.internal.objectweb.asm.@1"/>
    </jarjar>

    <!-- Test Trace Agent -->
    <jar destfile="./build/jars/traceagent_test.jar" compress="false">
      <manifest>
        <attribute name="Premain-Class" value="org.intrace.agent.Agent" />
        <attribute name="Agent-Class" value="org.intrace.agent.Agent" />
        <attribute name="Can-Redefine-Classes" value="true" />
        <attribute name="Git-Revision" value="${git.revision}" />
        <attribute name="Implementation-Version" value="${git.tag}" />
      </manifest>     
    </jar>

    <!-- CUI Trace Client -->
    <jar destfile="./build/jars/cuitraceclient.jar" compress="false">
      <manifest>
        <attribute name="Main-Class" value="org.intrace.client.cui.TraceClient" />
        <attribute name="Class-Path" value="." />
        <attribute name="Git-Revision" value="${git.revision}" />
        <attribute name="Implementation-Version" value="${git.tag}" />
      </manifest>
      <fileset dir="./build/classes" includes="**/shared/*.class" />
      <fileset dir="./build/classes" includes="**/client/cui/*.class" />
    </jar>

    <!-- GUI Trace Client - Windows -->
    <jar destfile="./build/jars/guitraceclient-win.jar" filesetmanifest="mergewithoutmain">
      <manifest>
        <attribute name="Main-Class" value="org.intrace.client.gui.TraceClient" />
        <attribute name="Class-Path" value="." />
        <attribute name="Git-Revision" value="${git.revision}" />
        <attribute name="Implementation-Version" value="${git.tag}" />
      </manifest>
      <fileset dir="./build/classes" includes="**/shared/*.class" />
      <fileset dir="./build/classes" includes="**/client/gui/**/*.class" />
      <zipfileset excludes="META-INF/*.MF" src="lib/swt-windows-3.5.2.jar" />
      <zipfileset excludes="META-INF/*.MF" src="lib/miglayout-3.7-swt.jar" />
    </jar>

    <!-- GUI Trace Client - Linux -->
    <jar destfile="./build/jars/guitraceclient-linux.jar" filesetmanifest="mergewithoutmain">
      <manifest>
        <attribute name="Main-Class" value="org.intrace.client.gui.TraceClient" />
        <attribute name="Class-Path" value="." />
        <attribute name="Git-Revision" value="${git.revision}" />
        <attribute name="Implementation-Version" value="${git.tag}" />
      </manifest>
      <fileset dir="./build/classes" includes="**/shared/*.class" />
      <fileset dir="./build/classes" includes="**/client/gui/**/*.class" />
      <zipfileset excludes="META-INF/*.MF" src="lib/swt-linux-3.5.2.jar" />
      <zipfileset excludes="META-INF/*.MF" src="lib/miglayout-3.7-swt.jar" />
    </jar>

  </target>

  <target name="test" description="Run Tests" depends="build" >
    <property name="emma.enabled" value="true" />

    <mkdir dir="./reports" />
    <mkdir dir="./reports/junit" />
    <mkdir dir="./reports/junit/raw" />
    <mkdir dir="./reports/emma" />
    <mkdir dir="./reports/emma/metadata" />

    <mkdir dir="./build/test" />
    <mkdir dir="./build/classes_instr" />

    <javac destdir="./build/test" debug="true" source="1.6" target="1.6">
      <src path="./testsrc"/>
      <classpath>
        <pathelement path="./build/classes" />
        <fileset dir="./lib" includes="*.jar" />
      </classpath>
    </javac>

    <copy todir="./build/classes_instr">
      <fileset dir="./build/classes"/>
    </copy>

    <emma enabled="${emma.enabled}" >
      <instr instrpath="./build/classes_instr"
             metadatafile="./reports/emma/metadata/metadata.emma"
             merge="true"
             mode="overwrite"
      />
    </emma>

    <junit printsummary="yes" haltonfailure="yes">
      <classpath>
        <pathelement location="./build/classes_instr"/>
        <pathelement location="./build/test"/>
        <fileset dir="./lib" includes="*.jar" />
        <path refid="emma.lib" />
      </classpath>
      <jvmarg value="-Demma.coverage.out.file=./reports/emma/metadata/coverage.emma" />
      <jvmarg value="-Demma.coverage.out.merge=true" />
      <jvmarg value="-javaagent:lib/traceagent_test.jar"/>

      <formatter type="xml"/>
      <formatter type="plain"/>

      <batchtest fork="yes" todir="./reports/junit/raw">
      <fileset dir="./testsrc">
        <include name="**/*Test.java"/>
      </fileset>
      </batchtest>
    </junit>

    <junitreport todir="./reports/junit">
      <fileset dir="./reports/junit/raw">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="./reports/junit/"/>
    </junitreport>

    <emma enabled="${emma.enabled}" >
      <report sourcepath="./src" >
        <fileset dir="./reports/emma/metadata/" >
          <include name="*.emma" />
        </fileset>

        <txt outfile="./reports/emma/coverage.txt" />
        <html outfile="./reports/emma/coverage.html" />
      </report>
    </emma>
  </target>


  <target name="findbugs" depends="build">

    <mkdir dir="./reports/findbugs" />

    <!-- Build Findbugs Trace Agent (Don't include ASM classes) -->
    <jar destfile="./reports/findbugs/traceagent_findbugs.jar" compress="false">
      <fileset dir="./build/classes" includes="**/agent/**/*.class" />
      <fileset dir="./build/classes" includes="**/output/**/*.class" />
      <fileset dir="./build/classes" includes="**/shared/*.class" />
    </jar>

    <findbugs home="${findbugs.home}"
              output="xml"
              outputFile="./reports/findbugs/traceagent-fb.xml"
        excludeFilter="./conf/findbugs_exclude.xml"
        jvmargs="-Xmx256m">
      <auxClasspath>
        <fileset dir="./lib" includes="*.jar" />
      </auxClasspath>
      <sourcePath path="./src" />
      <class location="./reports/findbugs/traceagent_findbugs.jar" />
    </findbugs>

  </target>

  <target name="release" description="Build jars and run all automated tests" depends="jar, test, findbugs" >
  </target>

  <target name="example_build">
    <mkdir dir="./build/test" />
    <javac destdir="./build/test" debug="true" source="1.6" target="1.6">
      <src path="./testsrc"/>
      <classpath>
        <pathelement path="./build/classes" />
        <fileset dir="./lib" includes="*.jar" />
      </classpath>
    </javac>  
  </target>

  <target name="example" depends="jar,example_build">
    <java classname="example.TraceExample" fork="true">
      <arg value="5000" />
      <classpath>
        <pathelement path="./build/test" />
      </classpath>
      <jvmarg value="-javaagent:build/jars/traceagent.jar=[instru-true" />
    </java>
  </target>

  <target name="fastexample" depends="jar,example_build">  
    <java classname="example.TraceExample" fork="true">
      <arg value="0" />
      <classpath>
        <pathelement path="./build/test" />
      </classpath>
      <jvmarg value="-javaagent:build/jars/traceagent.jar=[instru-true" />
    </java>
  </target>

</project>
