<!-- An ANT build file for the Pentaho-Reporting-Projects. -->
<!--
        This file defines some general macros. It is used in most jfree.org
        projects to simplify the module handling (which can become a real
        pain).

        This is no standalone file and is intended to be included by other
        build files.

        Changes:

        2007-10-15: Added modular retro-builds;
                    Allowed to exclude files for the monolithic builds
                    Standarized Maven-POM file name
                    Bug-Fix in ClassPath setting of isClassAvailable
        2007-10-16: Added separate retro-build-target as newer JDKs are no
                    longer able to generate JDK 1.2.2 binary class files
        2007-12-15: Added support for Manifest-Version Information
                    Manifest is created if it does not exist beforehand
        2008-03-31: Integration build support added.
        2008-04-30: MANIFEST.INF must be called MANIFEST.MF instead
-->
<project name="module-support" basedir="..">

  <!--
     This target tests whether a given class is available.
   -->
  <macrodef name="isClassAvailable">
    <attribute name="property-name"
               description="The property that will receive the result."/>
    <attribute name="class-name" description="The class to test for"/>
    <element name="test-classpath" optional="true"
             description="The classpath used for the testing"/>
    <sequential>
      <condition property="@{property-name}">
        <available classname="@{class-name}">
          <classpath>
            <test-classpath/>
          </classpath>
        </available>
      </condition>
    </sequential>
  </macrodef>

  <!--
     This target tests whether a given class and its library file is available.
   -->
  <macrodef name="isClassAndFileAvailable">
    <attribute name="property-name"
               description="The property that will receive the result."/>
    <attribute name="class-name" description="The class to test for"/>
    <attribute name="file-name" description="The file which we test for"/>
    <element name="test-classpath" optional="true"
             description="The classpath used for the testing"/>
    <sequential>
      <condition property="@{property-name}">
        <and>
          <available file="@{file-name}"/>
          <available classname="@{class-name}">
            <classpath>
              <pathelement location="@{file-name}"/>
              <test-classpath/>
            </classpath>
          </available>
        </and>
      </condition>
    </sequential>
  </macrodef>

  <!--
     This target tests whether a given class is available, and includes other
     tests as well, which also have to evaluate to true to pass this test.
   -->
  <macrodef name="isClassAvailableEx">
    <attribute name="property-name"
               description="The property that will receive the result."/>
    <attribute name="class-name" description="The class to test for"/>
    <element name="additional-tests" optional="true"
             description="Additional tests, which have to evaluate to true."/>
    <element name="test-classpath" optional="true"
             description="The classpath used for the testing"/>
    <sequential>
      <condition property="@{property-name}">
        <and>
          <additional-tests/>
          <available classname="@{class-name}">
            <classpath>
              <test-classpath/>
            </classpath>
          </available>
        </and>
      </condition>
    </sequential>
  </macrodef>


  <!--
     This target builds a single module. The module is specified by its path
     inside the source directory.

   -->
  <macrodef name="build-retro-module">
    <attribute name="jdk-dir"
               description="The directory where you installed JDK 1.2.2 (or whatever you are testing against)"/>
    <attribute name="module-path"
               description="The package name of the module"/>
    <attribute name="source-dir" description="The source directory"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="module-classpath"
             description="The classpath for building this module."/>
    <element name="module-resources"
             description="Additional resources to be copied."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src/@{module-path}/">
        <fileset dir="@{source-dir}/@{module-path}">
          <exclude name="**/junit/**"/>
          <include name="*.java"/>
        </fileset>
      </copy>

      <!-- compile the source -->
      <javac fork="true"
             memoryInitialSize="160m"
             memoryMaximumSize="160m"
             srcdir="${builddir}/src"
             destdir="${builddir}/bin"
             compiler="jikes"
             nowarn="on"
             includeAntRuntime="false"
             includeJavaRuntime="false"
             deprecation="${build.deprecation}"
             debug="${build.debug}"
             optimize="${build.optimize}"
             target="${build.retro.target}">
        <classpath>
          <module-classpath/>
        </classpath>
      </javac>

      <!-- copy across required resource files -->
      <copy todir="@{builddir}/bin/@{module-path}">
        <fileset dir="@{source-dir}/@{module-path}">
          <module-resources/>
        </fileset>
      </copy>

      <!-- make the jar -->
      <copy file="@{manifest-location}" tofile="@{builddir}/manifest.tmp" failonerror="false"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>


  <!--
     This target builds a single module. The module is specified by its path
     inside the source directory.

   -->
  <macrodef name="build-module">
    <attribute name="module-path"
               description="The package name of the module"/>
    <attribute name="source-dir" description="The source directory"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="module-classpath"
             description="The classpath for building this module."/>
    <element name="module-resources"
             description="Additional resources to be copied."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src/@{module-path}/">
        <fileset dir="@{source-dir}/@{module-path}">
          <exclude name="**/junit/**"/>
          <include name="*.java"/>
        </fileset>
      </copy>

      <!-- compile the source -->
      <javac
          srcdir="@{builddir}/src"
          destdir="@{builddir}/bin"

          deprecation="${build.deprecation}"
          debug="${build.debug}"
          optimize="${build.optimize}"
          target="${build.target}"
          source="${build.source}">
        <classpath>
          <module-classpath/>
        </classpath>
      </javac>

      <!-- copy across required resource files -->
      <copy todir="@{builddir}/bin/@{module-path}">
        <fileset dir="@{source-dir}/@{module-path}">
          <module-resources/>
        </fileset>
      </copy>

      <copy file="@{manifest-location}" tofile="@{builddir}/manifest.tmp" failonerror="false"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>


  <!--
      This target builds the core classes. All classes outside the modules
      directory are considered core classes.
   -->
  <macrodef name="build-retro-core">
    <attribute name="jdk-dir"
               description="The directory where you installed JDK 1.2.2 (or whatever you are testing against)"/>
    <attribute name="module-path"
               description="The package name of the modules"/>
    <attribute name="source-dir"
               description="The source directory"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="core-classpath"
             description="The classpath for the build process"/>
    <element name="core-resources"
             description="The resources to be included in the file jar file."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src">
        <fileset dir="@{source-dir}">
          <exclude name="**/junit/**"/>
          <exclude name="@{module-path}/**"/>
          <include name="**/*.java"/>
        </fileset>
      </copy>

      <javac fork="true"
             memoryInitialSize="160m"
             memoryMaximumSize="160m"
             srcdir="${builddir}/src"
             destdir="${builddir}/bin"
             compiler="jikes"
             nowarn="on"
             includeAntRuntime="false"
             includeJavaRuntime="false"
             deprecation="${build.deprecation}"
             debug="${build.debug}"
             optimize="${build.optimize}"
             target="${build.retro.target}">
        <classpath>
          <core-classpath/>
        </classpath>
      </javac>


      <copy todir="@{builddir}/bin">
        <fileset dir="@{source-dir}">
          <exclude name="@{module-path}/**"/>
          <core-resources/>
        </fileset>
      </copy>

      <copy file="@{manifest-location}" tofile="@{builddir}/manifest.tmp" failonerror="false"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>

  <!--
      This target builds the core classes. All classes outside the modules
      directory are considered core classes.
   -->
  <macrodef name="build-core">
    <attribute name="module-path"
               description="The package name of the modules"/>
    <attribute name="source-dir"
               description="The source directory"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="core-classpath"
             description="The classpath for the build process"/>
    <element name="core-resources"
             description="The resources to be included in the file jar file."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src">
        <fileset dir="@{source-dir}">
          <exclude name="**/junit/**"/>
          <exclude name="@{module-path}/**"/>
          <include name="**/*.java"/>
        </fileset>
      </copy>

      <javac
          srcdir="@{builddir}/src"
          destdir="@{builddir}/bin"

          deprecation="${build.deprecation}"
          debug="${build.debug}"
          optimize="${build.optimize}"
          target="${build.target}"
          source="${build.source}">
        <classpath>
          <core-classpath/>
        </classpath>
      </javac>


      <copy todir="@{builddir}/bin">
        <fileset dir="@{source-dir}">
          <exclude name="@{module-path}/**"/>
          <core-resources/>
        </fileset>
      </copy>

      <copyfile src="@{manifest-location}" dest="@{builddir}/manifest.tmp"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>


  <!--
      This target builds the complete project.
   -->
  <macrodef name="build-retro">
    <attribute name="source-dir" description="The source directory"/>
    <attribute name="jdk-dir"
               description="The directory where you installed JDK 1.2.2 (or whatever you are testing against)"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="all-classpath"
             description="The classpath for the build process"/>
    <element name="all-resources"
             description="The resources to be included in the file jar file."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <element name="all-sources" optional="true"
             description="Optional include or exclude definitions for the source-fileset"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src">
        <fileset dir="@{source-dir}">
          <exclude name="**/junit/**"/>
          <include name="**/*.java"/>
          <all-sources/>
        </fileset>
      </copy>

      <javac fork="true"
             memoryInitialSize="160m"
             memoryMaximumSize="160m"
             srcdir="${builddir}/src"
             destdir="${builddir}/bin"
             compiler="jikes"
             nowarn="on"
             includeAntRuntime="false"
             includeJavaRuntime="false"
             deprecation="${build.deprecation}"
             debug="${build.debug}"
             optimize="${build.optimize}"
             target="${build.retro.target}">
        <bootclasspath>
          <pathelement location="@{jdk-dir}/jre/lib/rt.jar"/>
        </bootclasspath>
        <classpath>
          <all-classpath/>
        </classpath>
      </javac>


      <copy todir="${builddir}/bin">
        <fileset dir="@{source-dir}">
          <all-resources/>
        </fileset>
      </copy>

      <copy file="@{manifest-location}" tofile="@{builddir}/manifest.tmp" failonerror="false"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>


  <!--
      This target builds the complete project.
   -->
  <macrodef name="build-all">
    <attribute name="source-dir" description="The source directory"/>
    <attribute name="target-jar"
               description="The name of the resulting jar-file"/>
    <attribute name="builddir" default="build"
               description="The temporary build directory"/>
    <attribute name="manifest-location"
               description="The (optional) name of the manifest file used as base."
               default="devresource/META-INF/MANIFEST.MF"/>
    <element name="all-classpath"
             description="The classpath for the build process"/>
    <element name="all-resources"
             description="The resources to be included in the file jar file."/>
    <element name="jar-args" optional="true"
             description="Parameters for the 'jar' task"/>
    <element name="manifest-args" optional="true"
             description="An optiona 'manifest' creation task"/>
    <element name="all-sources" optional="true"
             description="Optional include or exclude definitions for the source-fileset"/>
    <sequential>
      <!-- prune possibly existing entries from temp build directory -->
      <delete dir="@{builddir}"/>

      <!-- create a temp build directory -->
      <mkdir dir="@{builddir}"/>
      <mkdir dir="@{builddir}/src"/>
      <mkdir dir="@{builddir}/bin"/>

      <!-- import all java files for compilation -->
      <copy todir="@{builddir}/src">
        <fileset dir="@{source-dir}">
          <exclude name="**/junit/**"/>
          <include name="**/*.java"/>
          <all-sources/>
        </fileset>
      </copy>

      <javac
          srcdir="@{builddir}/src"
          destdir="@{builddir}/bin"

          deprecation="${build.deprecation}"
          debug="${build.debug}"
          optimize="${build.optimize}"
          target="${build.target}"
          source="${build.source}">
        <classpath>
          <all-classpath/>
        </classpath>
      </javac>


      <copy todir="@{builddir}/bin">
        <fileset dir="@{source-dir}">
          <all-resources/>
        </fileset>
      </copy>


      <copy file="@{manifest-location}" tofile="@{builddir}/manifest.tmp" failonerror="false"/>
      <manifest file="@{builddir}/manifest.tmp" mode="update">
        <manifest-args/>
      </manifest>

      <jar jarfile="@{target-jar}" basedir="@{builddir}/bin" manifest="@{builddir}/manifest.tmp">
        <jar-args/>
      </jar>

      <!-- delete the temp directory -->
      <delete dir="@{builddir}"/>
    </sequential>
  </macrodef>

  <!--
    This target builds the distribution-zip file. It depends on the existence
    of a 'fill-dist-dir' target.
    -->
  <macrodef name="make-zip">
    <attribute name="lib.version"
               default="The version of the resulting zip file."/>
    <attribute name="lib.name"
               default="The name prefix of the resulting zip file."/>
    <attribute name="dist-dir" default="distribution"
               description="The name of the distribution (temporary build) directory. "/>
    <attribute name="target-dir" default=""
               description="The directory where to write the tgz file"/>
    <sequential>
      <!-- make a temporary distribution directory -->
      <mkdir dir="@{dist-dir}"/>

      <antcall target="fill-dist-dir">
        <param name="dist-dir" value="@{dist-dir}"/>
      </antcall>

      <!-- convert end-of-line characters in text files -->
      <fixcrlf srcdir="@{dist-dir}/source"
               eol="crlf" eof="remove">
        <include name="**/*.java"/>
        <include name="**/*.properties"/>
        <include name="**/*.html"/>
        <include name="**/*.xml"/>
        <include name="**/*.txt"/>
        <include name="**/*.css"/>
      </fixcrlf>

      <!-- make the zip file -->
      <zip zipfile="@{target-dir}@{lib.name}-@{lib.version}.zip">
        <zipfileset dir="@{dist-dir}"
                    prefix="@{lib.name}-@{lib.version}"/>
      </zip>

      <!-- delete the temporary distribution directory -->
      <delete dir="@{dist-dir}"/>
    </sequential>

  </macrodef>

  <!--
    This target builds the distribution-tar-gz file. It depends on the existence
    of a 'fill-dist-dir' target.
    -->
  <macrodef name="make-targz">
    <attribute name="lib.version"
               default="The version of the resulting zip file."/>
    <attribute name="lib.name"
               default="The name prefix of the resulting zip file."/>
    <attribute name="dist-dir" default="distribution"
               description="The name of the distribution (temporary build) directory. "/>
    <attribute name="target-dir" default=""
               description="The directory where to write the tgz file"/>
    <sequential>
      <!-- Make .tar.gz distribution for libfonts -->
      <!-- make a temporary distribution directory -->
      <mkdir dir="@{dist-dir}"/>

      <antcall target="fill-dist-dir">
        <param name="dist-dir" value="@{dist-dir}"/>
      </antcall>

      <!-- convert end-of-line characters in text files -->
      <fixcrlf srcdir="@{dist-dir}/source"
               eol="lf" eof="remove">
        <include name="**/*.java"/>
        <include name="**/*.properties"/>
        <include name="**/*.html"/>
        <include name="**/*.xml"/>
        <include name="**/*.txt"/>
        <include name="**/*.css"/>
      </fixcrlf>

      <!-- make the tar.gz file -->
      <tar tarfile="@{target-dir}@{lib.name}-@{lib.version}.tar">
        <tarfileset dir="@{dist-dir}"
                    prefix="@{lib.name}-@{lib.version}"/>
      </tar>
      <gzip zipfile="@{target-dir}@{lib.name}-@{lib.version}.tar.gz"
            src="@{target-dir}@{lib.name}-@{lib.version}.tar"/>
      <delete file="@{target-dir}@{lib.name}-@{lib.version}.tar"/>

      <!-- delete the temporary distribution directory -->
      <delete dir="@{dist-dir}"/>
    </sequential>

  </macrodef>

  <macrodef name="make-maven-bundle">
    <attribute name="lib.version"
               default="The version of the resulting zip file."/>
    <attribute name="lib.name"
               default="The name prefix of the resulting zip file."/>
    <attribute name="dist-dir" default="distribution"
               description="The name of the distribution (temporary build) directory. "/>
    <attribute name="lib.file"
               description="The name of the binary jar to be included in the bundle. "/>
    <attribute name="target" description="The name of the resulting jar file"/>
    <attribute name="licence-file" default="licence-LGPL.txt"/>
    <sequential>
      <!-- make a temporary distribution directory -->
      <mkdir dir="@{dist-dir}"/>
      <!-- copy across LICENCE -->
      <copy file="@{licence-file}" tofile="distribution/LICENSE.txt"/>

      <filterchain id="version.filters">
        <replacetokens>
          <token key="VERSION" value="@{lib.version}"/>
        </replacetokens>
      </filterchain>
      <copy file="maven-project.xml" tofile="distribution/project.xml">
        <filterchain refid="version.filters"/>
      </copy>

      <!-- copy across runtime jar file -->
      <copy file="@{lib.file}" tofile="@{dist-dir}/@{lib.file}"/>

      <!-- make the jar -->
      <jar jarfile="@{target}" basedir="@{dist-dir}"/>

      <!-- delete the temporary distribution directory -->
      <delete dir="@{dist-dir}"/>

    </sequential>

  </macrodef>

</project>
