<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
     
         http://www.apache.org/licenses/LICENSE-2.0
     
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License. -->

<!-- ====================================================================== 
     Build source and lay out results in the required format
     ====================================================================== -->
<project name="build-contribution" default="build" basedir=".">
    <description>
    	Build contributed source and lay out results in the required format 
    </description>

    <target name="help">
        <echo>
Apache Harmony Ant Build

Usage:

  ant build
    Compiles the java and native code to produce a jdk in
    "deploy/jdk".

  ant clean
    Removes all the files generated by a build.

  ant rebuild
    Performs a full build - that is 'clean' then 'build'.

  ant test
    Runs all the tests.

  ant doc
    Generates the javadoc.

  ant snapshot
    Produces a archive (tar/zip) of a build.

  ant fetch-depends
    Fetches dependencies. Note: Some of Harmony's dependencies are
    licensed under terms other than the Apache License v2.

  ant properties
    Display the common properties that are use in the ant build files.


The 'build' and 'test' targets can be restricted to processing only
a single module by defining the 'build.module' variable on the ant
command line.  For example, to run only the luni tests:

  ant -Dbuild.module=luni test

        </echo>
    </target>

    <property file="${user.home}/.harmony-classlib.properties" />

    <property name="hy.hdk" location="deploy" />
    <property name="hy.jdk" location="${hy.hdk}/jdk" />

    <!-- import properties for snapshot targets -->
    <import file="${basedir}/make/properties.xml" />

    <!-- ================================
	 target : rebuild
	     
	 builds with a clean first
	 ================================= -->
    <target name="rebuild" depends="clean,build"
        description="Performs a full build - that is 'clean' then 'build'" />
	
    <target name="build" depends="build-java,build-native"
        description="Compiles the java and native code to produce a jdk">
        <ant antfile="make/build-test.xml" inheritall="false"
             target="support-jar"/>
    </target>

    <target name="clean" depends="clean-java,clean-native,-clean-hdk" 
            description="Removes all the files generated by a build" />

    <!-- ================================
	 target : rebuild-java
	     
	 builds java source with a clean first
	 ================================= -->
    <target name="rebuild-java" depends="clean-java,build-java"
            description="Like 'rebuild' but for java code only" />

    <!-- ================================= 
          target: build-java              

          runs the build target in build-java.xml
         ================================= -->
    <target name="build-java" depends="-init-hdk"
            description="Compiles the java code only" >
        <echo>
========================================
Building Java component archives...
========================================
        </echo>
        <ant antfile="make/build-java.xml" inheritall="false" target="build"/>
    </target>

    <!-- ================================= 
          target: clean-java              

          runs the clean target in build-java.xml
         ================================= -->
    <target name="clean-java" depends="-init-hdk-for-clean"
            description="Removes the compiled java code" >
        <ant antfile="make/build-java.xml" inheritall="false" target="clean" >
            <property name="keep.working" value="${keep.working}" />
        </ant>
    </target>

    <!-- ================================
	 target : rebuild-native
	     
	 builds native source with a clean first
	 ================================= -->
    <target name="rebuild-native" depends="clean-native,build-native"
            description="Like 'rebuild' but for native code only" />

    <!-- ================================= 
          target: build-native             

          runs the build target in build-native.xml
         ================================= -->
    <target name="build-native" depends="-init-hdk"
            description="Compiles the native code only" >
        <echo>
========================================
Building native libs and executables...
========================================
        </echo>
        <ant antfile="make/build-native.xml" inheritall="false" target="build"/>
    </target>

    <!-- ================================= 
          target: clean-native              

          runs the clean target in build-native.xml
         ================================= -->
    <target name="clean-native" depends="-init-hdk-for-clean"
        description="Removes the compiled native code" >

        <ant antfile="make/build-native.xml" inheritall="false" target="clean" />
    </target>

    <target name="test"
            description="Runs all tests">
        <echo>
========================================
Testing class libraries...
========================================
</echo>
        <ant antfile="make/build-test.xml" inheritall="false" target="test-all"/>
    </target>


    <!-- ================================= 
          target: doc             
         ================================= -->
    <target name="doc"
            description="Generates the javadoc">
        <echo>
========================================
Generating HTML using Doxygen...
========================================
</echo>
        <ant antfile="doc/build.xml" inheritall="false" />
    </target>

    <target name="clean-doc" if="${really.clean.doc}">
        <!-- We really shouldn't do this, since svn diff complains
             with svn: Working copy 'doc/kernel_doc' is missing or not
             locked if we run it.  We shouldn't really clean things
             that are in the repository. -->
        <ant antfile="doc/build.xml" inheritall="false" target="clean" />
    </target>

    <!-- ================================= 
          target: -properties
         ================================= -->
    <target name="-properties" depends="svn-prop">
        <property name="deploy.file.prefix"
                  value="harmony-classlib-r${svn.revision}" />
        <property name="deploy.tar"
                  value="${deploy.file.prefix}-${hy.os}-${hy.arch}-snapshot.tar.gz" />
        <property name="deploy.zip"
                  value="${deploy.file.prefix}-${hy.os}-${hy.arch}-snapshot.zip" />
    </target>

    <!-- ================================= 
          target: snapshot
         ================================= -->
    <target name="snapshot" depends="-snapshot-tar,-snapshot-zip"
            description="Produce a classlib snapshot tar/zip" />

    <!-- ================================= 
          target: snapshot-tar
         ================================= -->
    <target name="-snapshot-tar" depends="build, -properties" if="is.unix">
        <echo>
========================================
Generating snapshot tar
========================================
</echo>
        <tar destfile="${deploy.tar}" compression="gzip">
            <tarfileset dir="${basedir}" prefix="Harmony">
                <include name="COPYRIGHT" />
                <include name="LICENSE" />
                <include name="NOTICE" />
                <include name="THIRD_PARTY_NOTICES.txt" />
                <include name="deploy/build/**" />
                <include name="deploy/include/**" />
                <include name="deploy/jdk/**" />
                <exclude name="deploy/jdk/jre/bin/default/**" /> 
                <exclude name="deploy/jdk/jre/bin/java" /> 
                <include name="deploy/lib/**" />
            </tarfileset>
            <tarfileset dir="${basedir}" prefix="Harmony" mode="755">
                <include name="deploy/jdk/jre/bin/java" />
            </tarfileset>
            <tarfileset dir="${basedir}/make/resources"
                includes="README-snapshot.txt"
                fullpath="Harmony/README"/>
        </tar>
        <chksum file="${deploy.tar}" />
        <chksum file="${deploy.tar}" type="sha" />
    </target>

    <!-- ================================= 
          target: snapshot-zip
         ================================= -->
    <target name="-snapshot-zip" depends="build,-properties" if="is.windows">
        <echo>
========================================
Generating snapshot zip
========================================
</echo>
        <zip destfile="${deploy.zip}">
            <zipfileset dir="${basedir}" prefix="Harmony">
                <include name="COPYRIGHT" />
                <include name="LICENSE" />
                <include name="NOTICE" />
                <include name="THIRD_PARTY_NOTICES.txt" />
                <include name="deploy/build/**" />
                <include name="deploy/include/**" />
                <include name="deploy/jdk/**" />
                <exclude name="deploy/jdk/jre/bin/default/**" /> 
                <exclude name="deploy/jdk/jre/bin/java" /> 
                <include name="deploy/lib/**" />
            </zipfileset>
            <zipfileset dir="${basedir}/make/resources"
                includes="README-snapshot.txt"
                fullpath="Harmony/README.txt"/>
        </zip>
        <chksum file="${deploy.zip}" />
        <chksum file="${deploy.zip}" type="sha" />
    </target>

    <target name="check-depends"
            description="Report on whether the dependencies are met">
        <ant antfile="make/depends.xml" inheritall="false" target="check" />
    </target>

    <target name="fetch-depends"
        description="Fetches dependencies. Note: Some of Harmony's dependencies are licensed under terms other than the Apache License v2.">
        <ant antfile="make/depends.xml" inheritall="false" target="download" />
    </target>

    <target name="properties" depends="echo"
        description="Display the properties that are use in the ant build files"
            />

    <target name="-init-hdk">
        <mkdir dir="${hy.hdk}/build/ant" />
        <copy file="make/properties.xml"
              tofile="${hy.hdk}/build/ant/properties.xml">
            <filterchain>
                <tokenfilter>
                    <replacestring from="Top-Level property definitions"
                                   to="Common property definitions" />
                </tokenfilter>
            </filterchain>
        </copy>
    </target>

    <target name="-init-hdk-for-clean">
        <!-- This is currently the same as "-init-hdk" but has
             a different name since otherwise it would only
             get called once - before the clean - during a rebuild
             and not called a second time - before the build.
          -->
        <mkdir dir="${hy.hdk}/build/ant" />
        <copy file="make/properties.xml"
              tofile="${hy.hdk}/build/ant/properties.xml">
            <filterchain>
                <tokenfilter>
                    <replacestring from="Top-Level property definitions"
                                   to="Common property definitions" />
                </tokenfilter>
            </filterchain>
        </copy>
    </target>

    <target name="-clean-hdk">
        <delete dir="${hy.hdk}/build/ant" />
    </target>
    
    <macrodef name="chksum">
        <attribute name="file" />
        <attribute name="type" default="md5" />
        <sequential>
            <checksum property="@{file}.@{type}" file="@{file}"
                      algorithm="@{type}" />
            <echo file="@{file}.@{type}"
                  message="${@{file}.@{type}}  @{file}${line.separator}" />
        </sequential>
    </macrodef>

</project>

