<?xml version="1.0"?>
<project name="Processing PDE" default="build">

  <property name="generated" 
	    value="${basedir}/generated/processing/mode/java/preproc" />
  <mkdir dir="${generated}" />

  <property name="grammars" 
	    value="${basedir}/src/processing/mode/java/preproc" />

  <property name="antlrjar" 
	    value="${basedir}/lib/antlr.jar" />

  <classloader taskname="antlr">
    <classpath path="lib/antlr.jar" />
  </classloader>

  <target name="clean" description="Clean the build directories">
    <delete dir="bin" />
    <delete file="pde.jar" />
    <delete>
      <fileset dir="${generated}">
	<include name="*.java" />
	<include name="*.tokens" />
	<include name="*.txt" />
	<include name="*.g" />
	<include name="*.smap" />
      </fileset>
    </delete>
  </target>

<!--<target name="preproc" depends="clean" description="Compile ANTLR grammar">-->
  <target name="preproc" description="Compile ANTLR grammar">
    <antlr target="${grammars}/java15.g" outputdirectory="${generated}">
      <classpath path="${antlrjar}" />
    </antlr>
    <antlr target="${grammars}/pde.g" 
	   outputdirectory="${generated}" 
	   glib="${grammars}/java15.g">
      <classpath path="${antlrjar}" />
    </antlr>
  </target>

  <target name="compile" depends="preproc" description="Compile sources">
    <condition property="core-built">
      <available file="../core/library/core.jar" />
    </condition>
    <fail unless="core-built" 
	  message="Please build the core library first and make sure it sits in ../core/library/core.jar" />

    <mkdir dir="bin" />
    <!-- in some cases, pde.jar was not getting built
	 https://github.com/processing/processing/issues/1792 -->
    <delete file="pde.jar" />

    <!-- env used to set classpath below -->
    <property environment="env" />

    <!-- moved to the main build script -->
    <!-- ant seems to nuke ${java.home} for some reason, pointing at the JRE
	 subfolder instead of the actual JDK found at JAVA_HOME. 
	 To avoid this, we grab the actual JAVA_HOME environment variable 
	 and use that to specify the location of tools.jar. -->
    <!-- if someone is better with ant please help clean this up -->
    <!--
    <property name="java_home" value="${env.JAVA_HOME}" />
    <available file="${env.JAVA_HOME}/lib/tools.jar"
	       property="java_tools_found" />

    <condition property="linux">
      <and>
	<os family="unix" />
	<not>
	  <os family="mac" />
	</not>
      </and>
    </condition>
    <fail if="linux" unless="java_tools_found" 
	  message="The JAVA_HOME variable must be set to the location of a full JDK. For instance, on Ubuntu Linux, this might be /usr/lib/jvm/java-6-sun." />

    <condition property="windows">
      <os family="windows" />
    </condition>
    <fail if="windows" unless="java_tools_found" 
	  message="The JAVA_HOME variable must be set to the location of a full JDK. For instance, on Windows, this might be c:\jdk1.6.0_19." />
    -->

    <javac source="1.6" 
	   target="1.6" 
	   destdir="bin" 
	   excludes="**/tools/format/**" 
	   encoding="UTF-8" 
	   includeAntRuntime="false" 
	   classpath="../core/library/core.jar; lib/ant.jar; lib/ant-launcher.jar; lib/antlr.jar; lib/apple.jar; lib/jdt-core.jar; lib/jna.jar; lib/org-netbeans-swing-outline.jar;lib/com.ibm.icu_4.4.2.v20110823.jar;lib/jdi.jar;lib/jdimodel.jar;lib/org.eclipse.osgi_3.8.1.v20120830-144521.jar"
	   debug="on"
	   nowarn="true"
	   compiler="org.eclipse.jdt.core.JDTCompilerAdapter">
      <src path="src" />
      <src path="generated" />
      <compilerclasspath path="../java/mode/ecj.jar" />
    </javac>
  </target>

  <target name="build" depends="compile" description="Build PDE">
    <jar basedir="bin" destfile="pde.jar" />
  </target>
</project>
