<project name="Trove4j" default="jar" basedir=".">
    <description>
        Builds the Trove4j library
    </description>
    
    <property name="Name" value="GNU Trove for Java"/>
    <property name="name" value="trove"/>
    <property name="version" value="2.1.0"/>
    <property name="year" value="2001-2009"/>
    
    <property name="debug" value="on"/>
    <property name="deprecation" value="on"/>

    
    <property name="jar_name" value="trove.jar"/>
    
    <property name="generator_subtree" value="gnu/trove/generate"/>
    
    <property name="output.dir" value="output"/>
    <property name="output.dest" value="${output.dir}/classes"/>
    <property name="output.tests" value="${output.dir}/test_classes"/>
    <property name="output.generator_classes" value="${output.dir}/generator_classes"/>
    <property name="output.generated" value="${output.dir}/gen_src"/>
    <property name="output.lib" value="${output.dir}/lib"/>
    <property name="output.tests" value="${output.dir}/tests"/>
    <property name="output.javadocs" value="${output.dir}/javadocs"/>
    <property name="output.tmp" value="${output.dir}/tmp"/>

    <property name="dist.root" value="${output.dir}/dist"/>
    <property name="dist.dir" value="${dist.root}/${name}-${version}"/>
    
    <property name="src" value="src"/>
    <property name="test_src" value="test"/>
    <property name="lib" value="lib"/>
    
    
    <!-- =============================================================== -->
    <!-- The main target for building a release. This will perform a     -->
    <!-- "clean" followed by a "dist".                                   -->
    <!-- =============================================================== -->
    <target name="release"
        description="Do everything for a release: clean, build everything and test">
        <antcall target="clean"/>
        <antcall target="dist"/>
        <antcall target="test"/>
        
        <tar tarfile="${name}-${version}.tar" basedir="${dist.root}"/>
        <gzip src="${name}-${version}.tar" 
            zipfile="${dist.root}/${name}-${version}.tar.gz" />
        <delete file="${name}-${version}.tar" />
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Clean all output                                                -->
    <!-- =============================================================== -->
    <target name="clean" description="Remove all generated/compiled files">
        <delete dir="${output.dir}"/>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Generate classes based on templates.                            -->
    <!-- =============================================================== -->
    <target name="generate" depends="-build_generator"
        description="Generate classes from templates">
        <java classname="gnu.trove.generate.Generate">
            <arg value="${src}/${generator_subtree}"/>
            <arg value="${output.generated}/gnu/trove/"/>
            <classpath>
                <pathelement path="${output.generator_classes}"/>
            </classpath>
        </java>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Compile the source directory.                                   -->
    <!-- =============================================================== -->
    <target name="compile" depends="generate">
        <javac destdir="${output.dest}"
            classpath="${lib}/junit.jar"
            debug="${debug}" target="1.5">
            <src path="${src}"/>
            <src path="${output.generated}"/>
        </javac>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Build the main jar.                                             -->
    <!-- =============================================================== -->
    <target name="jar" depends="compile">
        <jar destfile="${output.lib}/${name}-${version}.jar">
            <fileset dir="${output.dest}">
                <exclude name="gnu/trove/benchmark/**"/>
                <exclude name="gnu/trove/test/**"/>
                <exclude name="gnu/trove/generate/**"/>
            </fileset>
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Implementation-Title" value="${Name}"/>
                <attribute name="Implementation-Version" value="${version}"/>
                <attribute name="Implementation-URL" value="http://trove4j.sourceforge.net/"/>
                <attribute name="Sealed" value="false"/>
            </manifest>
        </jar>
    </target>


    <!-- =============================================================== -->
    <!-- Build the source jar.                                           -->
    <!-- =============================================================== -->
    <target name="jarsrc" depends="compile">
        <jar destfile="${output.lib}/${name}-src-${version}.jar">
            <fileset dir="${src}">
                <exclude name="gnu/trove/generate/**"/>
                <exclude name="gnu/trove/benchmark/**"/>
            </fileset>
            <fileset dir="${output.generated}"/>
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Implementation-Title" value="${Name}"/>
                <attribute name="Implementation-Version" value="${version}"/>
                <attribute name="Implementation-URL" value="http://trove4j.sourceforge.net/"/>
                <attribute name="Sealed" value="false"/>
            </manifest>
        </jar>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Places files to prepare for a release.                          -->
    <!-- This build javadocs and copies misc. files.                     -->
    <!-- =============================================================== -->
    <target name="dist" depends="jar,jarsrc,javadoc"
        description="Build all files for distribution">

        <mkdir dir="${dist.root}"/>
        <mkdir dir="${dist.dir}"/>
        <mkdir dir="${dist.dir}/src"/>
        
        <!-- Copy source -->
        <copy todir="${dist.dir}/src">
            <fileset dir="${src}"/>
        </copy>
        <copy todir="${dist.dir}/test">
            <fileset dir="${test_src}"/>
        </copy>
        
        <!-- Copy javadocs & libs -->
        <copy todir="${dist.dir}">
            <fileset dir="${output.dir}" includes="lib/**,javadocs/**" />
        </copy>
        <copy todir="${dist.dir}/lib" file="${lib}/junit.jar"/>
        
        <!-- Misc. Files -->
        <copy todir="${dist.dir}" file="AUTHORS"/>
        <copy todir="${dist.dir}" file="ChangeLog"/>
        <copy todir="${dist.dir}" file="LICENSE.txt"/>
        <copy todir="${dist.dir}" file="README-license.txt"/>
        <copy todir="${dist.dir}" file="README.txt"/>
        <copy todir="${dist.dir}" file="build.xml"/>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Create the javadocs.                                            -->
    <!-- =============================================================== -->
    <target name="javadoc" depends="compile">
        <mkdir dir="${output.javadocs}"/>
    
        <copy file="${src}/gnu/trove/decorator/package.html"
            tofile="${output.generated}/gnu/trove/decorator/package.html"/>
    
        <javadoc destdir="${output.javadocs}"
            packagenames="gnu.trove.*"
            author="true"
            version="true"
            use="true"
            splitindex="true"
            noindex="false"
            windowtitle="${Name} API v${version}"
            doctitle="${Name} API v${version}"
            header="&lt;a href=&quot;http://trove4j.sourceforge.net/&quot;>GNU Trove&lt;/a>"
            bottom="GNU Trove is copyright &#169; ${year} Eric D. Friedman. All Rights Reserved.">
            <fileset dir="${src}">
                <exclude name="**/*.html"/>
                <exclude name="gnu/trove/benchmark/**"/>
                <exclude name="gnu/trove/generate/**"/>
                <exclude name="gnu/trove/test/**"/>
            </fileset>
            <fileset dir="${output.generated}">
                <exclude name="**/*.html"/>
                <exclude name="gnu/trove/benchmark/**"/>
                <exclude name="gnu/trove/test/**"/>
            </fileset>
        </javadoc>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Compile and run the unit tests.                                 -->
    <!-- =============================================================== -->
    <target name="test" depends="compile"
        description="Compile unit tests and run them using JUnit.">
        
        <echo message="output.dest = ${output.dest}"/>
            
        <javac destdir="${output.tests}" debug="${debug}" target="1.5">
            <src path="${test_src}"/>
            <classpath>
                <pathelement location="${lib}/junit.jar"/>
                <pathelement location="${output.dest}"/>
            </classpath>
        </javac>

        <copy todir="${output.tests}">
            <fileset dir="${test_src}">
                <include name="**/*.obj"/>
            </fileset>
        </copy>
        
        <!-- Verify that JUnit is available on the classpath -->
        <available classname="junit.framework.TestCase" property="junit.present"/>
        <fail unless="junit.present" message="To run this task, install junit.jar and optional.jar into ${ant.home}/lib/ JUnit is available free at http://www.junit.org"/>
        <!-- Execute JUnit tests -->
        <junit printsummary="true" showoutput="false" fork="on"
            errorProperty="test.failed" failureProperty="test.failed">
            <assertions>
                <enable/>
            </assertions>
            <classpath path="${output.tests}:${output.dest}:${lib}/junit.jar"/>
            <formatter type="brief" usefile="false"/>
            <batchtest fork="no" todir="${output.dir}">
                <fileset dir="${output.tests}/" includes="**/**Test.class"/>
            </batchtest>
        </junit>
        <fail message="Tests failed. Check test output." if="test.failed"/>
    </target>
    
    
    <!-- =============================================================== -->
    <!-- Build generator classes.                                        -->
    <!-- =============================================================== -->
    <target name="-build_generator" depends="-init">
        <javac srcdir="${src}/${generator_subtree}"
            destdir="${output.generator_classes}"
            defaultexcludes="yes"
            debug="${debug}"
            source="1.5" target="1.5"/>
        
        <copy todir="${output.generator_classes}">
            <fileset dir="${src}">
                <include name="${generator_subtree}/*.template"/>
            </fileset>
        </copy>
    </target>
    
    <!-- =================================================================== -->
    <!-- Initialization target                                               -->
    <!-- =================================================================== -->
    <target name="-init">
        <tstamp/>
    
        <mkdir dir="${output.dir}"/>
        <mkdir dir="${output.generator_classes}"/>
        <mkdir dir="${output.dest}"/>
        <mkdir dir="${output.tests}"/>
        <mkdir dir="${output.generated}"/>
        <mkdir dir="${output.generated}/gnu"/>
        <mkdir dir="${output.generated}/gnu/trove"/>
        <mkdir dir="${output.lib}"/>
        <mkdir dir="${dist.root}"/>

        <echo message="----------- ${Name} ${version} [${year}] ------------"/>
    </target>
</project>