<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->


<!--
    Build file for Rhino using Ant (see http://jakarta.apache.org/ant/index.html)
    Requires Ant version 1.2 or later

    Compilation currently requires JDK 1.5 or later. Can cross-compile to
    support JDK 1.4.
-->

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

  <target name="properties">
    <!-- Allow user to override default settings from build.properties -->
    <property file="build.local.properties" />
    <tstamp>
      <!-- Specify date part of Context#getImplementationVersion() -->
      <format property="implementation.date" pattern="yyyy MM dd"/>
    </tstamp>
    <property file="build.properties"/>

    <property name="dist.file" value="rhino${version}.zip"/>
    <property name="dist.source-only-zip" value="rhino${version}-sources.zip"/>

    <property file="apiClasses.properties"/>
    <property name="xmlimplsrc-build-file"
              location="xmlimplsrc/build.xml"/>

    <available property="xmlimplsrc-present?"
               file="${xmlimplsrc-build-file}" />

  </target>

  <target name="init" depends="properties">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${classes}"/>
    <mkdir dir="${dist.dir}"/>
  </target>

  <target name="compile" depends="init">
    <ant antfile="src/build.xml" target="compile"/>
    <ant antfile="toolsrc/build.xml" target="compile"/>
    <antcall target="xmlimplsrc-compile" />
  </target>

  <target name="compile-all" depends="compile"/>

  <target name="graph" depends="init">
    <ant antfile="src/build.xml" target="graph"/>
  </target> 

  <target name="shell" depends="compile">
    <ant antfile="src/build.xml" target="shell"/>
  </target> 

  <target name="copy-source" depends="init">
    <ant antfile="src/build.xml" target="copy-source"/>
    <ant antfile="toolsrc/build.xml" target="copy-source"/>
    <ant antfile="testsrc/build.xml" target="copy-source"/>
    <antcall target="xmlimplsrc-copy-source" />
    <copy todir="${dist.dir}" file="build.xml"/>
    <copy todir="${dist.dir}" file="build.properties"/>
    <copy todir="${dist.dir}" file="apiClasses.properties"/>
    <copy todir="${dist.dir}" file="LICENSE.txt"/>
  </target>

  <target name="xmlimplsrc-compile" if="xmlimplsrc-present?">
    <echo>Calling ${xmlimplsrc-build-file}</echo>
    <!-- Ignore compilation errors under JDK less then 1.4 -->
    <property name="xmlimpl.compile.failonerror" value="no"/>
    <ant antfile="${xmlimplsrc-build-file}" target="compile"/>
  </target>

  <target name="xmlimplsrc-copy-source" if="xmlimplsrc-present?">
    <echo>Calling ${xmlimplsrc-build-file}</echo>
    <ant antfile="${xmlimplsrc-build-file}" target="copy-source"/>
  </target>

  <target name="jar" depends="compile-all">
    <copy todir="${classes}" file="LICENSE.txt"/>
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <jar jarfile="${jarfile}"
         basedir="${classes}"
         manifest="src/manifest"
         compress="${jar-compression}"
     />
  </target>

  <target name="console" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}"
          fork="true">
      <arg line="-version 170"/>
    </java>
  </target>

  <target name="smalljar" depends="compile">
    <property name="smalljarfile" location="${dist.dir}/${small-rhino.jar}"/>
    <jar basedir="${classes}" destfile="${smalljarfile}"
         compress="${jar-compression}">
      <include name="org/mozilla/javascript/*.class"/>

      <include name="org/mozilla/javascript/debug/*.class"/>
      <include name="org/mozilla/javascript/resources/*.properties"/>
      <include name="org/mozilla/javascript/xml/*.class"/>
      <include name="org/mozilla/javascript/continuations/*.class"/>
      <include name="org/mozilla/javascript/jdk13/*.class"/>
      <include name="org/mozilla/javascript/ast/*.class"/>
      <include name="org/mozilla/javascript/json/*.class"/>
      <include name="org/mozilla/javascript/annotations/*.class"/>
      <include name="org/mozilla/javascript/v8dtoa/*.class"/>

      <!-- exclude classes that uses class generation library -->
      <exclude name="org/mozilla/javascript/JavaAdapter*.class"/>

      <include name="org/mozilla/javascript/regexp/*.class"
               unless="no-regexp"/>
    </jar>
  </target>

  <target name="copy-examples" depends="init">
    <mkdir dir="${dist.dir}/examples"/>
    <copy todir="${dist.dir}/examples">
      <fileset dir="examples" includes="**/*.java,**/*.js,**/*.html" />
    </copy>
  </target>

  <target name="copy-misc" depends="init">
    <filter token="datestamp" value="${TODAY}"/>
    <copy todir="${dist.dir}" filtering="yes">
      <fileset dir=".">
        <patternset>
          <include name="build-date"/>
        </patternset>
      </fileset>
    </copy>
  </target>

  <target name="copy-all" depends="copy-source,copy-examples,copy-misc">
  </target>

  <target name="javadoc" depends="init">
    <mkdir dir="${dist.dir}/javadoc"/>
    <javadoc sourcefiles="${apiClasses}"
             sourcepath="src"
             destdir="${dist.dir}/javadoc"
             version="true"
             author="true"
             windowtitle="${Name}" />
    <zip destfile="${dist.dir}/javadoc.zip" basedir="${dist.dir}/javadoc"/>
  </target>

  <target name="dev-javadoc" depends="init">
    <mkdir dir="${dist.dir}/javadoc"/>
    <javadoc sourcepath="src"
             destdir="${dist.dir}/javadoc"
             version="true"
             package="true"
             author="true"
             windowtitle="${Name}">
       <fileset
         dir="."
         includes="**/*.java"
         excludes="**/testsrc/**/*.java"
       />
    </javadoc>
  </target>

  <!--
    Compiles and tests all sources and then creates the distribution file
  -->
  <target name="all" depends="deepclean,compile-all,junit-all,dist">
  </target>

  <target name="dist" depends="deepclean,jar,copy-all,javadoc">
    <delete file="${dist.file}" />
    <zip destfile="${dist.file}">
      <fileset dir="${build.dir}" includes="${dist.name}/**"/>
    </zip>
  </target>

  <target name="source-zip" depends="copy-source,copy-examples,javadoc">
    <delete file="${dist.source-only-zip}" />
    <zip destfile="${dist.source-only-zip}">
      <zipfileset prefix="${dist.name}" dir="${dist.dir}">
        <include name="*src/**"/>
        <include name="build.xml"/>
        <include name="*.properties"/>
        <include name="examples/**"/>
      </zipfileset>
    </zip>
  </target>

  <target name="compile-debugger">
    <ant antfile="toolsrc/build.xml" target="compile-debugger"/>
  </target>

  <target name="clean" depends="properties">
    <delete quiet="true" file="${dist.dir}/${rhino.jar}"/>
    <delete quiet="true" file="${dist.dir}/${small-rhino.jar}"/>
    <delete quiet="true" dir="${build.dir}"/>
  </target>

  <target name="deepclean" depends="properties">
    <delete quiet="true" dir="${build.dir}"/>
    <delete quiet="true" file="${dist.file}"/>
    <delete quiet="true" file="${dist.source-only-zip}"/>
  </target>

  <!--
    The next two targets run the JavaScript Test Library tests.  Note that these tests are quite extensive and take a long time
    to run.  They are not documented in the 'help' target for now.
  -->

  <!--
    Run the tests using JUnit.  Beware that if you are using Ant from the command-line, there are some difficulties you may
    encounter setting this up correctly; see http://ant.apache.org/faq.html#delegating-classloader

    IDEs that use Ant as the build system probably handle this fine.
  -->
  <target name="junit-all" depends="compile">
    <ant antfile="testsrc/build.xml" target="junit-coveragereport"/>
  </target>
  <target name="junit" depends="compile">
    <ant antfile="testsrc/build.xml" target="junit"/>
  </target>

   <target name="junit-benchmarks" depends="compile">
    <ant antfile="testsrc/build.xml" target="junit-benchmarks"/>
  </target>

  <!--
  Run the tests using the Java port of jsdriver.pl.  Note that running this port
  from the command-line may be more useful running this Ant target, as running
  from the command line allows configuration options, such as running with a
  non-default optimization level, or running only a subset of the tests.
  -->
  <target name="jsdriver-run" depends="compile">
      <ant antfile="testsrc/build.xml" target="jsdriver" />
  </target>

  <!--
    Compile the JsDriver test driver.
  -->
  <target name="jsdriver" depends="compile">
      <ant antfile="testsrc/build.xml" target="clean" />
      <ant antfile="testsrc/build.xml" target="compile" />
  </target>

  <target name="benchmark-v8-opt-1" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/v8-benchmarks-v5" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt -1 run.js"/>
    </java>
  </target>

  <target name="benchmark-v8-opt0" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/v8-benchmarks-v5" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt 0 run.js"/>
    </java>
  </target>

  <target name="benchmark-v8-opt9" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/v8-benchmarks-v5" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt 9 run.js"/>
    </java>
  </target>

  <target name="benchmark-sunspider-opt-1" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/sunspider-0.9.1" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt -1 run.js"/>
    </java>
  </target>

  <target name="benchmark-sunspider-opt0" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/sunspider-0.9.1" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt 0 run.js"/>
    </java>
  </target>

  <target name="benchmark-sunspider-opt9" depends="jar">
    <property name="jarfile" location="${dist.dir}/${rhino.jar}"/>
    <java jar="${jarfile}" dir="testsrc/benchmarks/sunspider-0.9.1" fork="true">
      <jvmarg value="-server"/>
      <arg line="-opt 9 run.js"/>
    </java>
  </target>

  <target name="help" depends="properties">
<echo>The following targets are available with this build file:

 clean       remove all compiled classes and copied property files

 compile     compile classes and copy all property files
             into ${classes} directory
             excluding deprecated code

 compile-all compile all classes and copy all property files
             into ${classes} directory
             including deprecated code

 deepclean   remove all generated files and directories

 dist        create ${dist.file} with full Rhino distribution

 help        print this help

 jar         create ${rhino.jar} in ${dist.dir}

 smalljar    create ${small-rhino.jar} in ${dist.dir} with
             minimalist set of Rhino classes. See footprint.html
             from the doc directory for details.

 javadoc     generate Rhino API documentation
             in ${dist.dir}/javadoc

 source-zip  create ${dist.source-only-zip} with all Rhino
             source files necessary to recreate ${dist.file}
</echo>
  </target>

</project>
