<!-- 
/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2010, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* ~~~~~~~~~
 * build.xml
 * ~~~~~~~~~
 * (C) Copyright 2003-2010, by Barak Naveh and Contributors.
 *
 * Original Author:  Barak Naveh
 * Contributor(s):   John V. Sichi
 *
 * $Id: build.xml 729 2010-11-28 05:27:24Z perfecthash $
 *
 * Changes
 * ~~~~~~~
 * 24-Jul-2003 : Initial revision (BN);
 * 15-Aug-2003 : Used wildcards instead of enumerating files(JVS);
 * 15-Aug-2003 : Added checkstyle task (JVS);
 * 15-Aug-2003 : Used JUnit batch test feature (JVS);
 * 15-Aug-2003 : Added jalopy task (JVS);
 * 03-Sep-2003 : Added etags support (JVS);
 * 24-Feb-2004 : Removed duplicates and cleaned code (BN);
 * 06-Mar-2004 : Added tasks for building a release by CVS tag (BN);
 * 13-Oct-2004 : Added task for building experimental package (MB);
 * 31-Jul-2005 : Added support for retroweaver (JVS);
 * 23-Apr-2006 : Renamed package to org.jgrapht (JVS);
 * 25-Jun-2006 : Fold experimental into main build (JVS);
 * 01-Jul-2006 : Remove checkstyle, switch from CVS to SVN (JVS);
 * 29-Sep-2007 : Dropped support for retroweaver in 0.7.2 release (JVS);
 * 28-Sep-2008 : Moved to JDK 1.6 for 0.8.0 release (JVS);
 *
 */
-->

<!--
/**
 * An ant build file for JGraphT (use Ant 1.6.0 and above).
 * Before running this script, make sure that the libraries used by JGraphT are
 * found under ./lib directory. This script will create directories ./build 
 * and ./testbuild for generated classfiles. These can be removed at any time 
 * via the 'clean' target.  A temporary ./distribution directory is created 
 * when building a distribution. A ./javadoc directory is created for the 
 * Javadoc HTML pages. 
 *
 * This script was originally based on a nice script written by David Gilbert.
 *
 * @author Barak Naveh
 * @author John V. Sichi
 *
 * @since 24-Jul-2003
 */
-->

<project name="jgrapht" default="all" basedir=".">

  <!-- import local property overrides; these take precedence over
  later definitions in this file -->
  <property file="etc/build.properties"/>

  <property name="latest.version.name"  
    value="0.8.2" />
  
  <!-- SVN tags and their respective version names -->
  <property name="ver_0_8_2.name"       
    value="0.8.2" />
  <property name="ver_0_8_1.name"       
    value="0.8.1" />
  <property name="ver_0_8_0.name"       
    value="0.8.0" />
  <property name="ver_0_7_3.name"       
    value="0.7.3" />
  <property name="ver_0_7_2.name"       
    value="0.7.2" />
  <property name="ver_0_7_1.name"       
    value="0.7.1" />
  <property name="ver_0_7_0.name"       
    value="0.7.0" />
  <property name="ver_0_6_0.name"       
    value="0.6.0" />
  <property name="ver_0_5_3.name"       
    value="0.5.3" />
  <property name="ver_0_5_2.name"       
    value="0.5.2" />

  <!-- Initialization. -->
  <target name="init.local" unless="svntag">
    <property name="checkout.dir"     value="checkout" />
    <property name="project.version"  value="${latest.version.name}-local" />
  </target>
  
  <target name="init.svn" if="svntag">
    <property name="checkout.dir"     
      value="${svntag}" />
    <!-- A dereferencing hack to set project.version=${${svntag}.name} -->
    <!-- To be simplified if future Ant versions will support it directly -->
    <echo file="project.version.tmp">project.version=$${${svntag}.name}</echo>
    <property file="project.version.tmp" />
    <delete file="project.version.tmp" />
    <!-- project.version now has the dereferenced value of ${${svntag}.name} -->
  </target>

  <target name="init" depends="init.local,init.svn">
    <echo level="info">JAVA_HOME=${java.home}</echo>
    <property name="project.name"     
      value="jgrapht" />
    <property name="project.prefix"   
      value="${project.name}-${project.version}" />

    <!-- Directories -->
    <property name="build.dir"        
      value="${basedir}/build" />
    <property name="testbuild.dir"    
      value="${basedir}/testbuild" />
    <property name="distrib.dir"      
      value="${basedir}/distribution"/>
    <property name="src.dir"          
      value="${basedir}/src"/>
    <property name="testsrc.dir"      
      value="${basedir}/testsrc"/>
    <property name="lib.dir"          
      value="${basedir}/lib"/>
    <property name="etc.dir"          
      value="${basedir}/etc"/>
    <property name="javadoc.dir"      
      value="${basedir}/javadoc"/>

    <!-- File names -->
    <property name="project.jar.name"    
      value="${project.name}-jdk1.6.jar" />
    <property name="project.tar.name"    
      value="${project.prefix}.tar" />
    <property name="project.tar.gz.name" 
      value="${project.prefix}.tar.gz" />
    <property name="project.zip.name"    
      value="${project.prefix}.zip" />
    <property name="build.file.name"     
      value="build.xml" />
    <property name="license.file.name"   
      value="license-LGPL.txt" />
    <property name="junit.jar.name"      
      value="junit.jar" />
    <property name="xmlunit.jar.name"      
      value="xmlunit1.0.jar" />
    <property name="jgraph.jar.name"     
      value="jgraph.jar" />
    <property name="touchgraph.jar.name" 
      value="TGGraphLayout.jar" />

    <!-- Files -->
    <property name="project.jar"      
      value="${basedir}/${project.jar.name}" />
    <property name="project.tar"      
      value="${basedir}/${project.tar.name}" />
    <property name="project.tar.gz"   
      value="${basedir}/${project.tar.gz.name}" />
    <property name="project.zip"      
      value="${basedir}/${project.zip.name}" />
    <property name="junit.jar"        
      value="${lib.dir}/${junit.jar.name}"/>
    <property name="xmlunit.jar"        
      value="${lib.dir}/${xmlunit.jar.name}"/>
    <property name="jgraph.jar"       
      value="${lib.dir}/${jgraph.jar.name}"/>
    <property name="touchgraph.jar"   
      value="${lib.dir}/${touchgraph.jar.name}"/>
    <property name="build.file"       
      value="${basedir}/${build.file.name}"/>
    <property name="jalopy.settings"  
      value="${etc.dir}/triemax-jalopy-settings.xml"/>

  <!-- NOTE jvs 18-Mar-2007:  junit.jar and xmlunit.jar should really be on
       testbuild.classpath only, but this way makes Eclipse setup easier -->

    <!-- Classpath for building functional sources. -->
    <path id="build.classpath">
      <pathelement location="${junit.jar}"/>
      <pathelement location="${xmlunit.jar}"/>
      <pathelement location="${jgraph.jar}"/>
      <pathelement location="${touchgraph.jar}"/>
    </path>
    <property name="build.classpath" refid="build.classpath"/>

    <!-- Classpath for building tests. -->
    <path id="testbuild.classpath">
      <pathelement path="${build.classpath}"/>
      <pathelement location="${build.dir}"/>
    </path>
    <property name="testbuild.classpath" refid="testbuild.classpath"/>

    <!-- Classpath for running svn task. -->
    <path id="svn.classpath">
      <pathelement location="${lib.dir}/svnant.jar"/>
      <pathelement location="${lib.dir}/svnClientAdapter.jar"/>
    </path>
    <property name="svn.classpath" refid="svn.classpath"/>

    <!-- Classpath for running tests. -->
    <path id="test.classpath">
      <pathelement path="${testbuild.classpath}"/>
      <pathelement location="${testbuild.dir}"/>
    </path>
    <property name="test.classpath" refid="test.classpath"/>
  </target>


  <!-- NOTE jvs 18-Mar-2007:  If you change the javac's below, update
       the Eclipse setup instructions in 
       http://jgrapht.wikispaces.com/ContributorGuidelines -->

  <!-- Compile the classes -->
  <target name="compile" depends="init"
    description="Compiles the JGraphT source code.">

    <!-- create a build directory -->
    <mkdir dir="${build.dir}" />

    <!-- compile the source -->
    <javac srcdir="${src.dir}" destdir="${build.dir}" source="1.6">
      <classpath refid="build.classpath" />
      <include name="org/jgrapht/util/TypeUtil.java"/>
    </javac>
    <javac srcdir="${src.dir}" destdir="${build.dir}" source="1.6">
      <compilerarg line="-Xlint:all" />
      <classpath refid="build.classpath" />
      <include name="**/*.java"/>
      <exclude name="org/jgrapht/experimental/**/*.java"/>
      <exclude name="org/jgrapht/util/TypeUtil.java"/>
      <exclude name="org/jgrapht/ext/JGraphModelAdapter.java"/>
      <exclude name="org/jgrapht/demo/JGraphAdapterDemo.java"/>
    </javac>
    <javac srcdir="${src.dir}" destdir="${build.dir}" source="1.6">
      <classpath refid="build.classpath" />
      <include name="**/*.java"/>
    </javac>
  </target>

  <!-- Run the experimental touchgraph demo -->
  <target name="touchgraph.demo" depends="compile"
    description="Runs the experimental Touchgraph demo.">
    <java 
      classname="org.jgrapht.experimental.touchgraph.SimpleTouchgraphApplet">
      <classpath refid="test.classpath" />
    </java>
  </target>


  <!-- Generate the runtime jar -->
  <target name="jar" depends="compile">
    <jar jarfile="${project.jar}" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" 
          value="org.jgrapht.demo.JGraphAdapterDemo"/>
        <attribute name="Class-Path" 
          value="lib/${jgraph.jar.name}"/>
        <attribute name="Specification-Title" 
          value="${project.name}"/>
        <attribute name="Specification-Version" 
          value="${project.version}"/>
        <attribute name="Implementation-Version" 
          value="${project.version}-jdk1.6"/>
      </manifest> 
    </jar>
  </target>

  <!-- Build all JUnit tests -->
  <target name="test.compile" depends="compile"
    description="Builds all JUnit tests.">
    <!-- create a test build directory -->
    <mkdir dir="${testbuild.dir}" />

    <javac srcdir="${testsrc.dir}" destdir="${testbuild.dir}" source="1.6"
      debug="on">
      <classpath refid="testbuild.classpath" />
      <include name="org/jgrapht/**"/>
    </javac>
  </target>

  <!-- Macro for running all JUnit tests -->
  <macrodef name="runTests">
    <attribute name="jvm" default="java"/>
    <attribute name="classpath.refid" default="test.classpath"/>
    <sequential>
      <echo level="info">
        If you get a junit task lookup failure, you need to add the junit.jar to
        the classpath, possibly by copying it into the ANT_HOME/lib folder.
        See http://ant.apache.org/manual/OptionalTasks/junit.html for more.
      </echo>
      <junit printsummary="no" fork="yes" jvm="@{jvm}"
        haltonerror="true" haltonfailure="true">
        <classpath refid="@{classpath.refid}"/>
        <jvmarg line="-ea -esa" />
        <formatter type="brief" usefile="no" />
        <batchtest>
          <fileset dir="${testsrc.dir}">
            <include name="**/*Test.java"/>
            <exclude name="**/Abstract*Test.java"/>
            <exclude name="**/DefaultEdgeTest.java"/>
          </fileset>
        </batchtest>
      </junit>
    </sequential>
  </macrodef>

  <!-- Run all JUnit tests -->
  <target name="test" depends="test.compile"
    description="Builds and runs all JUnit tests.">
    <runTests/>
  </target>

  <!-- Run jalopy on source code                                         -->
  <!-- NOTE:  requires manual installation of .jars in lib/jalopy        -->
  <target name="jalopy" depends="test.compile"
    description="Runs jalopy on all source code.">
    <taskdef name="jalopy" 
      classname="com.triemax.JalopyTask">
      <classpath>
        <fileset dir="${basedir}/lib/jalopy">
          <include name="*.jar"/>
        </fileset>
      </classpath>
    </taskdef>
    <jalopy force="true" backup="true" convention="${jalopy.settings}"
      classpathref="test.classpath" history="CRC32" threads="2">
      <fileset dir="${basedir}">
        <include name="src/**/*.java"/>
        <include name="testsrc/**/*.java"/>
      </fileset>
    </jalopy>
  </target>

  <!-- Generate the JGraphT JavaDoc HTML pages -->
  <target name="javadoc" depends="init" description="Creates the Javadocs.">
    <delete dir="${javadoc.dir}" />
    <mkdir dir="${javadoc.dir}" />
    <javadoc destdir="${javadoc.dir}" source="1.6" failonerror="true"
      access="protected" 
      windowtitle="JGraphT : a free Java graph library"
      overview="${src.dir}/overview.html">
      <classpath refid="build.classpath" />
      <packageset dir="${src.dir}" defaultexcludes="yes">
        <include name="**"/>
        <exclude name="org/jgrapht/experimental/**"/>
      </packageset>
    </javadoc>
  </target>


  <!-- Prepares a distribution directory of JGraphT -->
  <target name="prepare.distribution" depends="jar,javadoc">
    
    <!-- make a temporary distribution directory -->
    <mkdir dir="${distrib.dir}" />

    <!-- add README -->
    <copy file="${basedir}/README.html" tofile="${distrib.dir}/README.html" />

    <!-- add license -->
    <copy file="${basedir}/${license.file.name}" 
      tofile="${distrib.dir}/${license.file.name}" />

    <!-- add runtime jar file -->
    <copy file="${project.jar}" tofile="${distrib.dir}/${project.jar.name}" />
    
    <!-- add source files -->
    <copy todir="${distrib.dir}/src">
      <fileset dir="${src.dir}">
        <include name="overview.html"/>
        <include name="**/*.java"/>
        <include name="**/package.html"/>
      </fileset>
    </copy>

    <!-- add source files of unit tests -->
    <copy todir="${distrib.dir}/testsrc">
      <fileset dir="${testsrc.dir}">
        <include name="**"/>
      </fileset>
    </copy>

    <!-- add lib files -->
    <copy todir="${distrib.dir}/lib">
      <fileset dir="${lib.dir}">
        <include name="*.jar"/>
        <include name="lib-readme.txt"/>
      </fileset>
    </copy>

    <!-- add ant build file (this file) -->
    <copy file="${build.file}" tofile="${distrib.dir}/${build.file.name}" />

    <!-- add etc files -->
    <copy todir="${distrib.dir}/etc">
      <fileset dir="${etc.dir}">
        <include name="jalopy-settings.xml"/>
        <include name="build.properties.template"/>
      </fileset>
    </copy>

    <!-- add etc/build.properties -->
    <copy file="${etc.dir}/build.properties.template" 
      tofile="${distrib.dir}/etc/build.properties">
    </copy>

    <!-- add Javadoc files -->
    <copy todir="${distrib.dir}/javadoc">
      <fileset dir="${javadoc.dir}"/>
    </copy>
  </target>
  

  <!-- Packages a .zip distribution for JGraphT -->
  <target name="zip" depends="prepare.distribution">

    <!-- convert end-of-line characters in text files to PC style -->
    <fixcrlf srcdir="${distrib.dir}/src"     eol="crlf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}/testsrc" eol="crlf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}/etc"     eol="crlf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}"         eol="crlf" eof="remove" 
      includes="${build.file.name},${license.file.name}"/>

    <!-- make the zip file -->
    <zip zipfile="${project.zip}">
      <zipfileset dir="${distrib.dir}" prefix="${project.prefix}" />
    </zip>
  </target>


  <!-- Packages a .tar.gz distribution for JGraphT -->
  <target name="targz" depends="prepare.distribution">

    <!-- convert end-of-line characters in text files to Unix style -->
    <fixcrlf srcdir="${distrib.dir}/src"     eol="lf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}/testsrc" eol="lf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}/etc"     eol="lf" eof="remove" />
    <fixcrlf srcdir="${distrib.dir}"         eol="lf" eof="remove" 
      includes="${build.file.name},${license.file.name}"/>

    <!-- make the tar.gz file -->
    <tar tarfile="${project.tar}" longfile="gnu">
      <tarfileset dir="${distrib.dir}" prefix="${project.prefix}" />
    </tar>
    <gzip zipfile="${project.tar.gz}" src="${project.tar}" />
    <delete file="${project.tar}" />
  </target>
  

  <!-- Clean outputs from previous build -->    
  <target name="clean" depends="init"
    description="Cleans all outputs from previous builds">
    <delete dir="${distrib.dir}" />
    <delete dir="${build.dir}" />
    <delete dir="${testbuild.dir}" />
    <delete dir="${javadoc.dir}" />
    <delete file="${project.jar}" />
    <delete file="${project.zip}" />
    <delete file="${project.tar.gz}" />
  </target>


  <!-- ALL -->
  <target name="all" depends="clean,compile,test,zip,targz"
    description="Cleans, compiles, tests and creates distribution files (.zip and .tar.gz).">
    
    <!-- Clean unnecessary outputs from this build -->    
    <delete dir="${distrib.dir}" />
    <delete dir="${javadoc.dir}" />
    <delete file="${project.jar}" />
  </target>


  <!-- generate Emacs tags -->
  <fileset dir="${basedir}" id="fileset.taggable">
    <include name="src/**/*.java"/>
    <include name="testsrc/**/*.java"/>
  </fileset>
  <target name="etags" 
    depends="init" 
    description="Generates TAGS file for Emacs">
    <property name="file.taggable.tmp" 
      location="${basedir}/taggedfiles"/>
    <antcall target="etags.find_taggable_files"/>
    <exec dir="${basedir}" executable="ctags">
      <arg line="-e"/>
      <arg line="-L taggedfiles"/>
    </exec>
    <delete file="${file.taggable.tmp}" quiet="true"/>
  </target>

  <!-- Internal helper for etags target -->
  <target name="etags.find_taggable_files">
    <concat destfile="${file.taggable.tmp}">replaceme</concat>
    <pathconvert property="files.taggable" 
      pathsep="${line.separator}"
      dirsep="/" refid="fileset.taggable" >
      <map from="${basedir}" to="." />
    </pathconvert>
    <replace file="${file.taggable.tmp}" 
      token="replaceme" value="${files.taggable}" />
    <fixcrlf srcDir="${basedir}" includes="taggedfiles"
      eol="lf" eof="remove" />
  </target>


  <!-- Creates a release according to a user-defined SVN tag -->
  <target name="release" depends="init,svntag.missing" if="svntag"
    description="Checks out and builds a release according to a SVN tag">
    
    <echo>Starting release build at ${checkout.dir}/${build.file.name}</echo>

    <antcall target="checkout"/>
    <ant dir="${checkout.dir}" antfile="${build.file.name}" 
      target="all" inheritall="false" inheritrefs="false">
      <property name="svntag" value="${svntag}"/>
    </ant>
  </target>


  <!-- Checks out a snapshot according to a user-defined SVN tag -->
  <target name="checkout" depends="init,svntag.missing" if="svntag"
    description="Checks out a snapshot from SVN according to a tag">
    <taskdef resource="svntask.properties" classpathref="svn.classpath"/>
    <echo level="info">Checking out files of SVN tag: ${svntag}</echo>
    
    <property name="project.svnroot" 
      value="https://jgrapht.svn.sourceforge.net/svnroot/jgrapht" />
    <delete dir="${checkout.dir}" />
    <mkdir  dir="${checkout.dir}" />
    <svn username="guest" password="" javahl="false">
      <checkout url="${project.svnroot}/tags/${svntag}" 
        destPath="${checkout.dir}"/>
    </svn>
  </target>


  <!-- Outputs an error message if SVN tag is unspecified -->
  <target name="svntag.missing" unless="svntag">
    <echo level="error">Variable $${svntag} must be specified for selected target.</echo>
  </target>

  <target name="maven.pom" depends="init">
    <copy file="etc/pom.xml.template" tofile="pom.xml" overwrite="true">
      <filterchain>
        <tokenfilter>
          <replacestring from="@VERSION@" to="${latest.version.name}"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

</project>
