<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    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.
-->

<project name="drlvm" default="build">
    <description>
    	Build contributed source and lay out results in the required format 
    </description>
    
    <property name="hy.target" location="target" />
    <property name="hy.component" value="drlvm" />
    <property name="hy.module" value="toplevel" />

    <fail message="Property 'common.resources' is not defined">
        <condition>
            <not><isset property="common.resources" /></not>
        </condition>
    </fail>

    <mkdir dir="${hy.target}/hdk/build/ant" />
    <copy todir="${hy.target}/hdk/build/ant" preservelastmodified="true">
        <fileset dir="${common.resources}/make" includes="*.xml"/>
    </copy>

    <!-- import properties for snapshot targets -->
    <import file="${hy.target}/hdk/build/ant/properties.xml" />
    <init-hdk/>

    <import file="make/properties.xml"/>

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

Usage:

  ant build
    Compiles the java and native code to produce a jre in
    "deploy/jdk/jre".
    It is possible to build an individual component via 
    corresponding targets.

  ant clean
    Removes all the files generated by a build.

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

  ant test
    Runs smoke and unit tests.
            
  ant test2
    Runs all pre-commit tests (in parallel on multi-way machines).
    A single suite can be run as:
  ant cunit.test      - runs cunit tests
  ant ehwa.test       - runs automated Eclispe HWA scenario
  ant kernel.test     - runs tests for kernel.classes
  ant smoke.test      - runs smoke tests
  ant jvmti.test      - runs JVMTI tests
  ant reg.test        - runs regression tests
    Also, it is possible to run a single testcase from specific suite 
    and customize JRE configuration, e.g.:
  ant smoke.test -Dtest.case=exception.NPE -Dtest.mode=jit -Dtest.vmargs=-XX:+vm.crash_handler
            
  ant doc
    Generates the javadoc (TBD).

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

  ant drlvm-echo echo debug-echo
    Display the common properties that are used in the ant build files.

        </echo>
    </target>

    <target name="fetch-depends">
        <ant antfile="make/depends.xml" target="fetch"/>
    </target>
    
    <target name="check-depends">
        <ant antfile="make/depends.xml" target="check"/>
    </target>

    <target name="rebuild" depends="clean,build"
        description="Performs a full build - that is 'clean' then 'build'" />

    <target name="build" 
        depends="drlvm-echo, -auto-fetch,
        setup,
        interpreter,
        kernel,
        em,
        vmi,
        jitrino,
        gc_gen,
        deploy,
        deploy-relocate,
        deploy-canonical"
        description="Compiles the java and native code to produce a jdk">
    </target>
    
    <target name="-auto-fetch" if="auto.fetch">
        <antcall target="fetch-depends"/>
    </target>

    <target name="clean" depends="" 
            description="Removes all the files generated by a build">
        <delete quiet="true" dir="${drlvm.build.dir}"/>
    </target>
    
    <!-- ================================================================
            build individual components
         ================================================================-->

    <target name="build-extra" depends=""
        description="Compiles external dependencies">
        <ant antfile="make/extra/apr.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="encoder">
        <ant antfile="make/vm/encoder.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="port" depends="build-extra">
        <ant antfile="make/vm/port.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="port_ch" depends="port">
        <ant antfile="make/vm/port_ch.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="hythr" depends="port">
        <ant antfile="make/vm/hythr.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="verifier">
        <ant antfile="make/vm/verifier.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="verifier-ext"
            description="Build a verifier extentions library">
        <ant antfile="make/vm/verifier-ext.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="clean-verifier-ext"
            description="Clean a verifier extentions library">
        <ant antfile="make/vm/verifier-ext.xml" target="clean"
             inheritall="false">
            <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="vmcore" depends="encoder, port, port_ch, hythr, verifier">
        <ant antfile="make/vm/vmcore.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="em" depends="vmcore">
        <ant antfile="make/vm/em.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="interpreter" depends="vmcore">
        <ant antfile="make/vm/interpreter.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="kernel">
        <ant antfile="make/vm/kernel.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="vmi" depends="vmcore">
        <ant antfile="make/vm/vmi.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="jitrino" depends="vmcore">
        <ant antfile="make/vm/jitrino.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="gc_gen" depends="vmcore,kernel">
        <ant antfile="make/vm/gc_gen.xml" inheritall="false">
             <propertyset refid="module.props" />
        </ant>
    </target>

    <target name="-init-hdk">
        <copy todir="${hy.hdk}/jdk/include" preservelastmodified="true">
            <fileset dir="vm/include">
                <include name="jvmti.h" />
                <include name="jvmti_types.h" />
                <include name="jni.h" />
                <include name="jni_types.h" />
            </fileset>
        </copy>
    </target>

    <!-- ================================================================
            import the deploy stuff from hdk / classlib
         ================================================================-->
    <target name="setup">
        <mkdir dir="${drlvm.deploy.dir}/lib" />
        <mkdir dir="${drlvm.deploy.dir}/jdk/jre" />
        <mkdir dir="${drlvm.deploy.dir}/include" />        
        <copy todir="${drlvm.deploy.dir}">
            <fileset dir="${hy.hdk}">
                <include name="**" unless="skip.import.hdk"/>
                <include name="jdk/jre/**" if="skip.import.hdk"/>
                <exclude name="jdk/jre/bin/default/**" if="skip.import.hdk"/>
                <exclude name="jdk/jre/bin/*hythr*" if="skip.import.hdk"/>
            </fileset>
        </copy>
    </target>
        
    <!-- ================================================================
            deploy the rest of product
         ================================================================-->
    <target name="deploy">
        <copy todir="${drlvm.deploy.dir}/jdk/include"
              preservelastmodified="true">
            <fileset dir="${vm.home}/include">
                <include name="jni_types.h" />
                <include name="jvmti_types.h" />
                <include name="ncai_types.h" />
                <include name="jni.h" />
                <include name="jvmti.h" />
                <include name="ncai.h" />
            </fileset>
        </copy>

        <!-- a trick to drop duplicated dependencies -->
        <delete quiet="yes">
            <fileset dir="${drlvm.bin.dir}" includes="*" excludes="*hythr*">
                <not>
                    <different targetdir="${drlvm.deploy.dir}/jdk/jre/bin"/>
                </not>
            </fileset>
        </delete>
        
        <!-- launcher should use our hythr -->
        <copy todir="${drlvm.deploy.dir}/jdk/jre/bin"
              preservelastmodified="true" overwrite="yes">
            <fileset dir="${drlvm.bin.dir}">
                <include name="*hythr*"/>
            </fileset>
        </copy>
        
        <copy file="${drlvm.base.dir}/make/resources/harmonyvm.properties" 
            todir="${drlvm.bin.dir}"/>

        <copy file="${drlvm.base.dir}/make/resources/readme.txt" 
              tofile="${drlvm.deploy.dir}/jdk/jre/README"/>
        <replace file="${drlvm.deploy.dir}/jdk/jre/README" >
            <replacefilter token="@{hy.os}" value="${hy.os}"/>
            <replacefilter token="@{hy.arch}" value="${hy.arch}"/>
        </replace>
        <fixcrlf srcDir="${drlvm.deploy.dir}/jdk/jre" includes="README" />
        
        <chmod file="${drlvm.deploy.dir}/jdk/jre/bin/java*" perm="ugo+x" />
    </target>
    

    <!-- ==================================================================
      place things into canonical 'deploy' directory
      so irrespective of the platform, compiler or release/debug
      there's a predicable place to find the output 
      ================================================================== -->
    <target name="deploy-canonical" unless="skip.deploy.canonical">
        <property name="canonical.deploy.dir" location="${drlvm.base.dir}/deploy" />
        <delete dir="${canonical.deploy.dir}" failonerror="false" />
        <mkdir dir="${canonical.deploy.dir}" />
        <copy todir="${canonical.deploy.dir}">
            <fileset dir="${drlvm.deploy.dir}"/>
        </copy>
        <chmod file="${canonical.deploy.dir}/jdk/jre/bin/java*" perm="ugo+x" />
    </target>

    <!-- ==================================================================
        relocate build for Windows
      ================================================================== -->
    <target name="deploy-relocate" if="is.windows">
        <fileset dir="${drlvm.deploy.dir}/jdk/jre/bin/" id="relocate.files">
            <include name="**/*.dll" />
            <!-- Ignore ICUInterface34 stub from classlib -->
            <exclude name="ICUInterface34.dll" />
        </fileset>

        <pathconvert pathsep=" " property="relocate.files.all" refid="relocate.files" />

        <exec executable="editbin" outputproperty="null" logerror="yes" failonerror="true">
            <arg value="/LARGEADDRESSAWARE" />
            <arg value="/BIND" />
            <arg value="${drlvm.deploy.dir}/jdk/jre/bin/java.exe" />
        </exec>

        <exec executable="editbin" outputproperty="null" logerror="yes" failonerror="true">
            <arg value="/LARGEADDRESSAWARE" />
            <arg value="/BIND" />
            <arg value="/rebase:base=0x500000" />
            <arg line="${relocate.files.all}" />
        </exec>
    </target>
        
    <!-- Main target for generating javadoc & deploying guildes -->
    <target name="doc">
        <echo message="Not yet completed"/>
    </target>

    <!-- Main target for tests run: call unit.test and smoke.test -->
    <target name="test" depends="drlvm-echo,
        jvmti.test, cunit.test, smoke.test, kernel.test">
        <fail if="test.failed" message="Some tests failed"/>
    </target>
    
    <target name="hut.test">
        <property name="hut.module" value="*"/>
        <property name="test.vmargs" value=""/>
        <ant antfile="${external.dep.CLASSLIB}/build.xml" inheritAll="no">
            <target name="properties"/>
            <target name="test"/>
            <property name="hy.test.vm.name" value="drl"/>
            <property name="test.jre.home" value="${drlvm.deploy.dir}/jdk/jre"/>
            <property name="short.report" value="true"/>
            <property name="hy.test.vmargs" value="${test.vmargs}"/>
            <property name="build.module" value="${hut.module}"/>
        </ant>
    </target>
    
    <target name="test2" depends="drlvm-echo">
        <property file="test.properties"/>
        <!--property name="run.all.tests" value="true"/-->
        <property name="short.report" value="true"/>
        <mkdir dir="${base.test.dir}"/>
        <parallel threadsPerProcessor="1" failonany="no">
            <ant target="hut.test"      output="${base.test.dir}/hut.log"/>
            <ant target="kernel.test"   output="${base.test.dir}/kernel.log"/>
            <ant target="ehwa.test"     output="${base.test.dir}/ehwa.log"/>
            <ant target="jvmti.test"    output="${base.test.dir}/jvmti.log"/>
            <ant target="smoke.test"    output="${base.test.dir}/smoke.log"/>
            <ant target="reg.test"      output="${base.test.dir}/reg.log"/>
            <ant target="cunit.test"    output="${base.test.dir}/cunit.log"/>
        </parallel>
        <fail if="test.failed" message="Some tests failed"/>
    </target>

    <!-- Run cunit tests only -->
    <target name="cunit.test">
        <ant antfile="make/tests/cunit-test.xml" target="cunit-test"/>
        <available property="test.failed" file="${base.test.dir}/cunit.test.failed" />
    </target>

    <!-- Main target to run smoke tests -->
    <target name="smoke.test">
        <ant antfile="make/tests/smoke-test.xml" target="smoke-test"/>
        <available property="test.failed" file="${base.test.dir}/smoke.test.failed" />
    </target>

    <!-- Main target to run kernel tests -->
    <target name="kernel.test">
        <ant antfile="make/tests/kernel-test.xml" target="kernel-test"/>
        <available property="test.failed" file="${base.test.dir}/kernel.test.failed" />
    </target>

    <!-- Main target to run Eclipse HelloWorld test scenario -->
    <target name="ehwa.test">
        <ant antfile="make/tests/ehwa-test.xml" target="ehwa"/>
        <available property="test.failed" file="${base.test.dir}/ehwa.test.failed" />
    </target>

    <!-- Main target to run jvmti tests /-->
    <target name="jvmti.test">
        <ant antfile="make/tests/jvmti-test.xml" target="jvmti-test"/>
        <available property="test.failed" file="${base.test.dir}/jvmti.test.failed" />
    </target>

    <!-- Main target to run regression test /-->
    <target name="reg.test">
        <ant antfile="make/tests/reg-test.xml" target="reg-test"/>
        <available property="test.failed" file="${base.test.dir}/reg.test.failed" />
    </target>

    <target name="lazy.test">
        <ant antfile="make/tests/lazy-test.xml" target="lazy-test"/>
        <available property="test.failed" file="${base.test.dir}/lazy.test.failed" />
    </target>

</project>
