<project name="clojure" default="all">

  <description>
    Build with "ant" and then start the
    REPL with: "java -cp clojure.jar clojure.main".
  </description>

  <property name="src" location="src"/>
  <property name="test" location="test"/>
  <property name="jsrc" location="${src}/jvm"/>
  <property name="jtestsrc" location="${test}/java"/>
  <property name="cljsrc" location="${src}/clj"/>
  <property name="cljscript" location="${src}/script"/>
  <property name="test-script" location="${cljscript}/run_test.clj"/>
  <property name="test-generative-script" location="${cljscript}/run_test_generative.clj"/>
  <property name="compile-script" location="${cljscript}/bootstrap_compile.clj"/>
  <property name="target" location="target"/>
  <property name="build" location="${target}/classes"/>
  <property name="test-classes" location="${target}/test-classes"/>
  <property name="dist" location="dist"/>
  <property file="maven-classpath.properties"/>

  <!-- Get the version string out of the POM -->
  <xmlproperty file="pom.xml" prefix="pom"/>
  <property name="clojure.version.label" value="${pom.project.version}"/>
  <property name="version.properties" value="${build}/clojure/version.properties"/>

  <property name="clojure_jar" location="clojure-${clojure.version.label}.jar"/>
  <property name="clojure_noversion_jar" location="clojure.jar"/>

  <property name="directlinking" value="true"/>

  <target name="init" depends="clean">
    <tstamp/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build}/clojure"/>
    <echo file="${version.properties}">version=${clojure.version.label}</echo>
  </target>

  <target name="compile-java" depends="init"
          description="Compile Java sources.">
    <javac srcdir="${jsrc}" destdir="${build}" includeJavaRuntime="yes"
           includeAntRuntime="false"
           debug="true" source="1.8" target="1.8"/>
  </target>

  <target name="compile-clojure"
          description="Compile Clojure sources.">
    <java classname="clojure.lang.Compile"
          classpath="${maven.compile.classpath}:${build}:${cljsrc}"
          failonerror="true"
          fork="true">
      <sysproperty key="clojure.compile.path" value="${build}"/>
         <!--<sysproperty key="clojure.compiler.elide-meta" value="[:doc :file :line :added]"/>-->
         <!--<sysproperty key="clojure.compiler.disable-locals-clearing" value="true"/>-->
       <!--<sysproperty key="clojure.compile.warn-on-reflection" value="true"/>-->
        <sysproperty key="clojure.compiler.direct-linking" value="true"/>
      <sysproperty key="java.awt.headless" value="true"/>
      <arg value="clojure.core"/>
      <arg value="clojure.core.protocols"/>
      <arg value="clojure.core.server"/>
      <arg value="clojure.main"/>
      <arg value="clojure.set"/>
      <arg value="clojure.edn"/>
      <arg value="clojure.xml"/>
      <arg value="clojure.zip"/>
      <arg value="clojure.inspector"/>
      <arg value="clojure.walk"/>
      <arg value="clojure.stacktrace"/>
      <arg value="clojure.template"/>
      <arg value="clojure.test"/>
      <arg value="clojure.test.tap"/>
      <arg value="clojure.test.junit"/>
      <arg value="clojure.pprint"/>
      <arg value="clojure.java.io"/>
      <arg value="clojure.repl"/>
      <arg value="clojure.java.browse"/>
      <arg value="clojure.java.javadoc"/>
      <arg value="clojure.java.shell"/>
      <arg value="clojure.java.process"/>
      <arg value="clojure.java.browse-ui"/>
      <arg value="clojure.java.basis.impl"/>
      <arg value="clojure.java.basis"/>
      <arg value="clojure.string"/>
      <arg value="clojure.data"/>
      <arg value="clojure.reflect"/>
      <arg value="clojure.datafy"/>
      <arg value="clojure.instant"/>
      <arg value="clojure.uuid"/>
      <arg value="clojure.core.reducers"/>
      <arg value="clojure.math"/>
      <arg value="clojure.tools.deps.interop"/>
      <arg value="clojure.repl.deps"/>
    </java>
  </target>

  <target name="compile-tests" 
          description="Compile the subset of tests that require compilation."
          unless="maven.test.skip">
    <mkdir dir="${test-classes}"/>
    <javac srcdir="${jtestsrc}" destdir="${test-classes}" includeJavaRuntime="yes"
           debug="true" source="1.8" target="1.8" includeantruntime="no"/>
    <echo>Direct linking = ${directlinking}</echo>
    <java classname="clojure.lang.Compile"
          classpath="${test-classes}:${test}:${build}:${cljsrc}:${maven.test.classpath}"
          failonerror="true"
	  fork="true">
      <sysproperty key="clojure.compile.path" value="${test-classes}"/>
        <!--<sysproperty key="clojure.compiler.elide-meta" value="[:doc]"/>-->
        <!--<sysproperty key="clojure.compiler.disable-locals-clearing" value="true"/>-->
      <sysproperty key="clojure.compiler.direct-linking" value="${directlinking}"/>
      <arg value="clojure.test-clojure.protocols.examples"/>
      <arg value="clojure.test-clojure.proxy.examples"/>
      <arg value="clojure.test-clojure.genclass.examples"/>
      <arg value="clojure.test-clojure.compilation.load-ns"/>
      <arg value="clojure.test-clojure.annotations"/>
    </java>
  </target>

  <target name="test-example"
          description="Run clojure tests without recompiling clojure."
          depends="compile-tests"
          unless="maven.test.skip">
    <java classname="clojure.main" failonerror="true" fork="true">
      <sysproperty key="clojure.test-clojure.exclude-namespaces"
                   value="#{clojure.test-clojure.compilation.load-ns clojure.test-clojure.ns-libs-load-later}"/>
      <sysproperty key="clojure.compiler.direct-linking" value="${directlinking}"/>
      <classpath>
        <pathelement path="${maven.test.classpath}"/>
        <path location="${test-classes}"/>
        <path location="${test}"/>
        <path location="${build}"/>
	<path location="${cljsrc}"/>
      </classpath>
      <arg value="${test-script}"/>
    </java>
  </target>

  <target name="test-generative"
          description="Run test generative tests without recompiling clojure."
          depends="compile-tests"
          unless="maven.test.skip">
    <java classname="clojure.main" failonerror="true" fork="true">
      <sysproperty key="clojure.compiler.direct-linking" value="${directlinking}"/>
      <classpath>
        <pathelement path="${maven.test.classpath}"/>
        <path location="${test-classes}"/>
        <path location="${test}"/>
        <path location="${build}"/>
	<path location="${cljsrc}"/>
      </classpath>
      <arg value="${test-generative-script}"/>
    </java>
  </target>

  <target name="test"
          description="Run all the tests"
          depends="test-example,test-generative"/>

  <target name="build"
          description="Build Clojure (compilation only, no tests)."
          depends="compile-java, compile-clojure"/>

  <target name="jar" depends="build"
          description="Create clojure jar file.">
    <jar jarfile="${clojure_jar}" basedir="${build}">
      <fileset dir="${cljsrc}">
        <include name="**/*.clj"/>
      </fileset>
      <manifest>
        <attribute name="Main-Class" value="clojure.main"/>
        <attribute name="Class-Path" value="."/>
      </manifest>
    </jar>
    <copy file="${clojure_jar}" tofile="${clojure_noversion_jar}"/>
  </target>

  <target name="javadoc"
	  description="Creates javadoc for Clojure API.">
    <copy file="src/jvm/clojure/lang/IFn.java" tofile="target/tmpjd/IFn.java"/>
    <copy file="src/jvm/clojure/lang/package.html" tofile="target/tmpjd/package.html"/>
    <replaceregexp file="target/tmpjd/IFn.java" match="(static public interface .*})" replace="" byline="true"/>
    <javadoc destdir="target/javadoc"
	    nodeprecatedlist="true" nohelp="true" nonavbar="true" notree="true"
	    link="http://docs.oracle.com/javase/7/docs/api/"
	    windowtitle="Clojure API">
      <classpath>
        <path location="${build}"/>
      </classpath>
      <fileset dir="${basedir}">
	<include name="src/jvm/clojure/java/api/Clojure.java"/>
	<include name="target/tmpjd/IFn.java"/>
      </fileset>
    </javadoc>
  </target>

  <target name="all" depends="build,test,jar"/>

  <target name="clean"
          description="Remove autogenerated files and directories.">
    <delete dir="${target}"/>
    <delete verbose="true">
      <fileset dir="${basedir}" includes="*.jar"/>
      <fileset dir="${basedir}" includes="*.zip"/>
    </delete>
  </target>

  <target name="local">
    <exec executable="mvn">
      <arg value="-Plocal"/>
      <arg value="-Dmaven.test.skip=true"/>
      <arg value="package"/>
    </exec>
  </target>
</project>
