<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<project basedir="." default="jar" name="pigudf">

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="../../../ivy/ant-contrib-1.0b3.jar"/>
        </classpath>
    </taskdef>

    <property file="../../../build.properties" />
    <!-- javac properties -->
    <property name="javac.debug" value="on" />
    <property name="javac.level" value="source,lines,vars"/>
    <property name="javac.optimize" value="on" />
    <property name="javac.deprecation" value="off" />
    <property name="javac.version" value="1.8" />
    <property name="javac.args" value="" />
    <!-- TODO we should use warning...   <property name="javac.args.warnings" value="-Xlint:unchecked" /> -->
    <property name="javac.args.warnings" value="" />
    <property name="build.encoding" value="UTF8" />

    <!-- build properties -->
    <property name="build.dir" value="${basedir}/build" />
    <property name="build.classes" value="${build.dir}/classes" />
    <property name="build.docs" value="${build.dir}/docs" />
    <property name="build.javadoc" value="${build.docs}/api" />
    <property name="pigtest" value="../../../build/test/classes" />
    <property name="udfjar" value="piggybank.jar" />
    <property name="src.dir" value="src/main/java/org/apache/pig/piggybank" />
    <property name="hsqldb.jar" value="../../../build/ivy/lib/Pig/hsqldb-1.8.0.10.jar"/>

    <!--
      Hadoop master version
      (Value 23 is translated for backward compatibility in old build scripts)
    -->
    <if>
        <equals arg1="${hadoopversion}" arg2="23"/>
        <then>
            <echo>Property setting hadoopversion=23 is deprecated. Overwriting to hadoopversion=2</echo>
            <var name="hadoopversion" unset="true"/>
            <property name="hadoopversion" value="2" />
        </then>
    </if>
    <property name="hadoopversion" value="2" />

    <condition property="hadoopsuffix" value="2" else="">
        <equals arg1="${hadoopversion}" arg2="2"/>
    </condition>

    <!-- jar properties -->
    <property name=".javadoc" value="${build.docs}/api" />
    
    <!-- test properties -->
    <property name="test.build.dir" value="${build.dir}/test" />
    <property name="test.classes" value="${test.build.dir}/classes" />
    <property name="test.logs" value="${test.build.dir}/logs" />
    <property name="test.timeout" value="900000" />
    <property name="test.junit.output.format" value="plain" />
    <property name="test.src.dir" value="src/test/java" />

    <path id="pigudf.classpath">
        <pathelement location="../../../build/classes"/>
        <pathelement location="${build.classes}"/>
        <fileset dir="../../..">
            <include name="pig-*-core-h${hadoopsuffix}.jar"/>
        </fileset>
        <pathelement location="${pigtest}"/>
        <fileset dir="../../../build/ivy/lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="test.classpath">
        <pathelement location="${udfjar}"/>
        <pathelement location="${build.classes}"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.src.dir}"/>
        <path refid="pigudf.classpath"/>
    </path>

    <target name="init">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${test.build.dir}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${build.javadoc}"/>
    </target>
    <target name="clean">
        <delete dir="build"/>
        <delete file="${udfjar}"/>
    </target>
    <target depends="init" name="compile" description="compile all of the class files">
        <echo> *** Compiling Pig UDFs ***</echo>
        <javac srcdir="${src.dir}" debug="${javac.debug}" debuglevel="${javac.level}" destdir="${build.classes}" source="${javac.version}"
        target="${javac.version}" optimize="${javac.optimize}" deprecation="${javac.deprecation}" excludes="${build.classes.excludes}" encoding="${build.encoding}">
            <compilerarg line="${javac.args} ${javac.args.warnings}" />
            <classpath refid="pigudf.classpath"/>
        </javac>
    </target>
    <target depends="init,compile" name="jar" description="create the jar files">
        <echo> *** Creating pigudf.jar ***</echo>
      <jar destfile="${udfjar}">
        <fileset dir="build/classes"/>
      </jar>
    </target>
    <target depends="compile" name="compile-test">
        <echo> *** Compiling UDF tests ***</echo>
        <javac srcdir="${test.src.dir}" debug="${javac.debug}" debuglevel="${javac.level}" destdir="${test.classes}" source="${javac.version}"
        	target="${javac.version}" excludes="${test.classes.excludes}">
            <compilerarg line="${javac.args} ${javac.args.warnings}"/>
            <classpath refid="pigudf.classpath"/>
        </javac>
    </target>
    <target depends="compile-test,jar" name="test">
        <echo> *** Running UDF tests ***</echo>
        <delete dir="${test.logs}"/>
        <mkdir dir="${test.logs}"/>
        <tempfile property="junit.tmp.dir" prefix="piggybank_junit_tmp" destDir="${user.dir}/build/test" />
        <mkdir dir="${junit.tmp.dir}/"/>
        <junit printsummary="yes" haltonfailure="no" fork="yes" maxmemory="512m" dir="${basedir}" timeout="${test.timeout}" errorProperty="tests.failed" failureProperty="tests.failed">        
            <sysproperty key="hadoop.log.dir" value="${test.logs}"/>
            <sysproperty key="java.io.tmpdir" value="${junit.tmp.dir}" />
            <env key="MALLOC_ARENA_MAX" value="4"/>
            <classpath refid="test.classpath"/>
            <formatter type="${test.junit.output.format}" />
            <batchtest fork="yes" todir="${test.logs}" unless="testcase">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java" />
                    <exclude name="${test.classes.excludes}" />
                </fileset>
            </batchtest>
            <batchtest fork="yes" todir="${test.logs}" if="testcase">
                <fileset dir="${test.src.dir}" includes="**/${testcase}.java"/>
            </batchtest>
        </junit>
        <delete dir="${junit.tmp.dir}/"/>    	
        <fail if="tests.failed">Tests failed!</fail>
    </target>
    <target depends="init" name="javadoc"
            description="build javadoc for all of the packages">
        <echo> *** Creating Javadocs ***</echo>
      <javadoc destdir="build/javadoc"
               author="true">
        <fileset dir="${src.dir}/evaluation" includes="**/*.java"/>
        <fileset dir="${src.dir}/storage" includes="**/*.java"/>
        <classpath refid="pigudf.classpath"/>
      </javadoc>
    </target>
    
</project>
