<?xml version="1.0"?>
<!-- 
============================================================================ 
Name        : macros.xml 
Part of     : Helium AntLib

Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
This component and the accompanying materials are made available
under the terms of the License "Eclipse Public License v1.0"
which accompanies this distribution, and is available
at the URL "http://www.eclipse.org/legal/epl-v10.html".

Initial Contributors:
Nokia Corporation - initial contribution.

Contributors:

Description:

============================================================================
-->
<project name="build-macros" xmlns:au="org.apache.ant.antunit">
    <description>Helium Antlib build macro.</description>
    
    <dirname property="helium.antlib.root.dir" file="${ant.file.build-macros}" />
    <property name="build.temp.dir" location="${basedir}/../build" />
    <property name="bin.dir" location="${basedir}/../bin" />

    <path id="helium.antlib.classpath">
        <fileset dir="${helium.antlib.root.dir}/lib" includes="*.jar" />
        <fileset dir="${helium.antlib.root.dir}/antlibs" includes="*.jar" />
    </path>
    
    <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="helium.antlib.classpath"/>
    <taskdef resource="org/apache/ant/antunit/antlib.xml" uri="org.apache.ant.antunit" classpathref="helium.antlib.classpath"/>
    
    
    <presetdef name="subantPreset">
        <subant failonerror="true">
            <resources refid="subant.build.files"/>
        </subant>
    </presetdef>
    
    
    <macrodef name="compileModule">
        <attribute name="name" />
        <attribute name="dir" default="${helium.antlib.root.dir}" />
        <sequential>
            <mkdir dir="${build.temp.dir}/@{name}/classes" />
            <javac srcdir="${basedir}/src" destdir="${build.temp.dir}/@{name}/classes"
                   debug="true" source="1.6" target="1.6">
                <classpath refid="helium.antlib.classpath" />
                <classpath refid="helium.@{name}.classpath" />
                <include name="**/*.java" />
                <compilerarg value="-Xlint:unchecked" />
                <compilerarg value="-Xlint:deprecation" />
            </javac>
        </sequential>
    </macrodef>

    
    <macrodef name="jarModule">
        <attribute name="name" />
        <attribute name="dir" default="${helium.antlib.root.dir}" />
        <attribute name="bindir" default="${bin.dir}" />
        <sequential>
            <echo>Packaging module @{name}</echo>
            <mkdir dir="@{bindir}" />
            <jar destfile="@{bindir}/helium-@{name}.jar">
                <fileset dir="${build.temp.dir}/@{name}/classes" />
                <fileset dir="${basedir}/src" includes="**/*.xml" />
                <fileset dir="${basedir}/src" includes="**/*.*ftl" />

                <manifest>
                    <section name="common">
                        <attribute name="Implementation-Title" value="@{name}" />
                        <attribute name="Implementation-Vendor" value="Nokia" />
                    </section>
                </manifest>
            </jar>
        </sequential>
    </macrodef>
    

    <macrodef name="testModule">
        <attribute name="name" />
        <attribute name="dir" default="${helium.antlib.root.dir}" />
        <sequential>
            <if>
                <available type="dir" file="${basedir}/tests"/>
                <then>
                    <exec osfamily="windows" executable="cmd" dir="${basedir}/tests">
                        <env key="ANT_LIB_HOME" path="${helium.antlib.root.dir}"/>
                        <arg line="/c build.bat unittest" />
                    </exec>
                    <exec osfamily="unix" executable="./bld.sh" dir="${basedir}/tests">
                        <env key="ANT_LIB_HOME" path="${helium.antlib.root.dir}"/>
                        <arg line="unittest" />
                    </exec>
                </then>
            </if>
        </sequential>
    </macrodef>
    
    
    <path id="emma.classpath">
        <fileset dir="${helium.antlib.root.dir}\antlibs">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <taskdef classpathref="emma.classpath" resource="emma_ant.properties" />
 
    
    <macrodef name="junitModule">
        <attribute name="name" />
        <attribute name="dir" default="${helium.antlib.root.dir}" />
        <sequential>
            <if>
                <available type="dir" file="${basedir}/tests/src"/>
                <then>
                    <echo>JUnit test module @{name}</echo>
                    <delete dir="${build.temp.dir}/@{name}/junit_classes"/>
                    <mkdir dir="${build.temp.dir}/@{name}/junit_classes" />
                    <javac srcdir="${basedir}/tests/src" destdir="${build.temp.dir}/@{name}/junit_classes" debug="true" source="1.5" target="1.5">
                        <classpath refid="helium.antlib.classpath" />
                        <classpath refid="helium.@{name}.classpath" />
                        <include name="**/*.java" />
                    </javac>
                    
                    <path id="build.classpath">
                        <pathelement location="${build.temp.dir}/@{name}/classes" />
                        <pathelement location="${build.temp.dir}/@{name}/junit_classes" />
                    </path>
                    
                    <emma enabled="true">
                        <instr instrpathref="build.classpath"  
                        destdir="${build.temp.dir}/@{name}/emma/instrumented"  
                        metadatafile="${build.temp.dir}/@{name}/metadata.emma"
                        merge="true">
                            <filter excludes="org.apache.log4j.*" />
                        </instr>
                    </emma>
                    
                    <junit fork="yes" printsummary="yes" haltonfailure="yes" showoutput="yes">
                        <formatter type="brief" usefile="false"/>
                        <jvmarg value="-Demma.coverage.out.file=${build.temp.dir}/@{name}/coverage.emma" />
                        <jvmarg value="-Demma.coverage.out.merge=false" />
                        <jvmarg value="-Dtestdir=${basedir}" />
                        <classpath refid="helium.antlib.classpath" />
                        <classpath refid="helium.@{name}.classpath" />
                        <classpath>
                            <pathelement path="${build.temp.dir}/@{name}/emma/instrumented"/>
                            <pathelement path="${helium.antlib.root.dir}/antlibs/*.jar"/>
                        </classpath>
                        <batchtest fork="yes" todir="${build.temp.dir}/@{name}/junit_test">
                            <fileset dir="${basedir}/tests/src">
                                <include name="**/*Test*.java"/>
                            </fileset>
                        </batchtest>
                    </junit>
                    
                    <emma enabled="true" >
                        <report>
                            <infileset dir="${build.temp.dir}/@{name}" includes="*.emma" />
                            <txt outfile="${build.temp.dir}/@{name}/coverage.txt" />
                            <html outfile="${build.temp.dir}/@{name}/coverage.html" />
                        </report>
                    </emma>
                    <loadfile property="message" srcFile="${build.temp.dir}/@{name}/coverage.txt"/>
                    <echo message="${message}"/>
                </then>
            </if>
        </sequential>
    </macrodef>
    
    
    <target name="compile">
        <echo>Compiling ${ant.project.name}</echo>
        <compileModule name="${name}"/>
    </target>
    
    
    <target name="jar">
        <echo>Creating jar for ${ant.project.name}</echo>
        <jarModule name="${name}"/>
    </target>
    
    
    <target name="build" depends="compile,jar">
        <echo>${ant.project.name} is built.</echo>
    </target>
    
    
    <target name="clean">
        <echo>Cleaning ${ant.project.name}</echo>
        <delete dir="${build.temp.dir}/${name}/classes"/>
        <delete file="${bin.dir}/helium-${name}.jar"/>
    </target>
    
    
    <target name="junit">
        <junitModule name="${name}"/>
    </target>
    
    
    <target name="unittest">
        <testModule name="${name}"/>
    </target>
    
    
    <target name="test" depends="junit,unittest">
        <echo>${ant.project.name} tested.</echo>
    </target>
</project>



