<?xml version="1.0" encoding="UTF-8"?>

<project name="GlueGenTest" basedir=".">

    <description>JUNIT Tests GlueGen</description>

    <property name="gluegen.root" value=".." />

    <import file="gluegen-cpptasks.xml" />
    <import file="jogamp-archivetasks.xml" />
    <import file="jogamp-env.xml" />

    <condition property="rootrel.build" value="build">
        <not>
          <isset property="rootrel.build"/>
        </not>
    </condition>

    <target name="init" depends="jogamp.env.init,gluegen.properties.load.user">

        <!-- Call the external config validator script to make sure the config is ok and consistent -->
        <ant antfile="validate-properties.xml" inheritall="true"/>

        <property name="make" value="." />

        <property name="test.base.dir"  value="${gluegen.root}/src/junit" />
        <property name="test.junit.rel" value="com/jogamp" />
        <property name="test.junit.generation.rel" value="${test.junit.rel}/gluegen/test/junit/generation" />
        <property name="test.junit.generation.dir" value="${test.base.dir}/${test.junit.generation.rel}" />

        <property name="test.dir"     value="${gluegen.root}/${rootrel.build}/test"/>
        <property name="results"      value="${test.dir}/results" />
        <property name="build_t"      value="${test.dir}/build" />
        <property name="build_t.gen"  value="${build_t}/gensrc"/>
        <property name="build_t.java" value="${build_t}/classes"/>
        <property name="build_t.obj"  value="${build_t}/obj"/>
        <property name="build_t.lib"  value="${build_t}/natives"/>

        <property name="batchtest.timeout"    value="1800000"/> <!-- 30 min -->

        <property name="build_t.gen.rootrel" value="${rootrel.build}/test/build/gensrc"/>

        <mkdir dir="${test.dir}"/>
        <mkdir dir="${results}"/>
        <mkdir dir="${build_t}"/>
        <mkdir dir="${build_t.gen}"/>
        <mkdir dir="${build_t.java}"/>
        <mkdir dir="${build_t.obj}"/>
        <mkdir dir="${build_t.lib}"/>

        <property name="gluegen.jar" location="${gluegen.root}/${rootrel.build}/gluegen.jar" />
        <property name="gluegen-rt.jar" location="${gluegen.root}/${rootrel.build}/gluegen-rt.jar" />
        <property name="gluegen.lib" value="${gluegen.root}/${rootrel.build}/obj" />

        <property name="gluegen-test.jar" location="${build_t}/gluegen-test.jar" />

        <path id="gluegen.classpath">
          <pathelement location="${gluegen.jar}" />
          <pathelement location="${antlr.jar}" />
        </path>

        <path id="gluegen_rt.classpath">
            <pathelement location="${gluegen-rt.jar}" />
        </path>

        <path id="junit.compile.classpath">
            <pathelement location="${junit.jar}" />
            <pathelement location="${gluegen.jar}" />
        </path>

        <path id="junit.run.classpath">
            <pathelement location="${junit.jar}" />
            <pathelement location="${gluegen.jar}" />
            <pathelement location="${gluegen-test.jar}" />
        </path>

        <property name="stub.includes.dir" value="stub_includes" /> <!-- NOTE:  this MUST be relative for FileSet -->

        <echo message="test.base.dir: ${test.base.dir} "/>
        <echo message="test.junit.generation.dir: ${test.junit.generation.dir} "/>
        <echo message="test.dir: ${test.dir} "/>
        <echo message="results: ${results} "/>
        <echo message="build_t: ${build_t} "/>
        <echo message="build_t.gen: ${build_t.gen} "/>
        <echo message="build_t.java: ${build_t.java} "/>
        <echo message="build_t.obj: ${build_t.obj} "/>
        <echo message="build_t.lib: ${build_t.lib} "/>

        <!-- Add the GlueGen tasks to ANT -->
        <taskdef name="gluegen" classname="com.jogamp.gluegen.ant.GlueGenTask"
                 classpathref="gluegen.classpath" />

        <property name="test.archive.name" value="${archive.name}-test-results-${build.node.name}"/>

        <property name="target.sourcelevel" value="1.5" />
        <property name="host.sourcelevel"   value="1.5" />
        <property name="javacdebug"       value="true" /> 
        <property name="javacdebuglevel"  value="source,lines" /> 
        <!--property name="javacdebuglevel"  value="source,lines,vars" /--> 
        <!-- property name="javac.memorymax"  value="128m" /-->  <!-- I ran out of memory with these .. linux x86_64 6u14 -->
        <property name="javac.memorymax"  value="512m" /> 

    </target>

    <target name="java.build" depends="java.generate">
        <echo message=" - - - compiling all java files - - - "/>
        <echo message=" test.base.dir ${test.base.dir}"/>
        <echo message=" build_t.gen     ${build_t.gen}"/>

        <javac destdir="${build_t.java}" 
               fork="yes"
               includeAntRuntime="false"
               memoryMaximumSize="${javac.memorymax}"
               source="${target.sourcelevel}" debug="${javacdebug}" debuglevel="${javacdebuglevel}">
            <classpath refid="junit.compile.classpath"/>
            <src path="${test.base.dir}"/>
            <src path="${build_t.gen}" />
        </javac>

        <jar destfile="${gluegen-test.jar}">
          <fileset dir="${build_t.java}">
            <include name="${test.junit.rel}/**/*.class"/>
          </fileset>
        </jar>
    </target>

    <target name="c.configure" depends="gluegen.cpptasks.detect.os,gluegen.cpptasks.setup.compiler">
        <property name="compiler.cfg.id" value="${compiler.cfg.id.base}" />
        <property name="linker.cfg.id"   value="${linker.cfg.id.base}" />
    </target>

    <!-- FIXME: this is a hack; the cpptask should have an option to change the
                suffix or at least understand the override from dylib to jnilib -->
    <target name="rename.dylib" if="isOSX">
        <move file="${src}" tofile="${dest}" />
    </target>

    <macrodef name="c.build">
      <attribute name="c.compiler.src.files" />
      <attribute name="compiler.cfg.id" />
      <attribute name="linker.cfg.id" />
      <attribute name="output.lib.name" />
      <sequential>
        <echo message="Compiling @{output.lib.name}" />
        <echo message="   c.compiler.src.files @{c.compiler.src.files}" />
        <echo message="   compiler.cfg.id @{compiler.cfg.id}" />
        <echo message="   linker.cfg.id @{linker.cfg.id}" />
        <echo message="   output.lib.name @{output.lib.name}" />

        <!-- NOTE: the value of the debug and optimise attributes will not be overridden if already set externally -->
        <property name="c.compiler.debug"     value="false" /> 
        <!-- Optimise flags one of { none, size, speed, minimal, full, aggressive, extreme, unsafe } --> 
        <property name="c.compiler.optimise"  value="none" /> 

        <condition property="c.compiler.use-debug"><istrue value="${c.compiler.debug}"/></condition>

        <cc outtype="shared" 
            objdir="${build_t.obj}" 
            outfile="${build_t.lib}/@{output.lib.name}"
            optimize="${c.compiler.optimise}" 
            debug="${c.compiler.debug}"
            multithreaded="true" 
            exceptions="false" 
            rtti="false">   
  
          <fileset dir="${gluegen.root}"><patternset refid="@{c.compiler.src.files}"/></fileset>
  
          <compiler extends="@{compiler.cfg.id}" >
            <sysincludepath path="${java.includes.dir}"/>
            <sysincludepath path="${java.includes.dir.platform}"/>
            <sysincludepath path="${stub.includes.dir}/platform"/>
            <includepath path="${test.junit.generation.dir}"/>
            <includepath path="${build_t.gen}/native"/>
          </compiler>

          <linker extends="@{linker.cfg.id}">
            <syslibset dir="${java.lib.dir.platform}" libs="jawt" if="@{output.lib.name}.useLibJAWT"/>
            <syslibset dir="${java.lib.dir.platform}/server" libs="jvm" if="@{output.lib.name}.useLibJVM"/>
          </linker>
        </cc>
      </sequential>
    </macrodef>  

    <target name="junit.compile.check" depends="init">
      <delete quiet="true">
        <fileset dir="${build}/test/results" includes="**"/>
        <fileset file="${build}/${test.archive.name}.7z"/>
      </delete>
      <mkdir dir="${build}/test/results"/>

      <uptodate property="junit.compile.skip">
        <srcfiles dir= "."                 includes="*.xml"/>
        <srcfiles dir= "${test.base.dir}"  includes="**"/>
        <srcfiles                          file="${gluegen.jar}" />
        <mapper type="merge" to="${gluegen-test.jar}"/>
      </uptodate>
    </target>

    <target name="junit.compile" depends="junit.compile.check" unless="junit.compile.skip">
      <mkdir dir="${build}/test/build/classes"/>
      <antcall target="java.generate"  inheritRefs="true"/>
      <antcall target="java.build"     inheritRefs="true"/>
      <antcall target="native.build"   inheritRefs="true"/>
    </target>

    <target name="junit.run" depends="init,gluegen.cpptasks.detect.os">
        <!-- Use absolute path -->
        <property name="gluegen.lib.abs" location="${gluegen.lib}" />
        <property name="build_t.lib.abs" location="${build_t.lib}" />

        <!-- Perform the junit tests-->
        <junit forkmode="once" showoutput="true" fork="true" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${gluegen.lib.abs}${path.separator}${build_t.lib.abs}"/>
            <jvmarg value="-Djava.library.path=${gluegen.lib.abs}${path.separator}${build_t.lib.abs}"/>
            <jvmarg value="-Drootrel.build=${rootrel.build}"/>
            <!--
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->
            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit.run.classpath"/>

            <batchtest todir="${results}">
              <fileset dir="${build_t.java}">
                  <include name="${test.junit.rel}/**/*Test*"/>
                  <exclude name="**/*$$*"/>
              </fileset>
              <formatter usefile="false" type="brief"/>
              <formatter usefile="true" type="xml"/>
            </batchtest>
        </junit>
        <antcall target="test-zip-archive" inheritRefs="true" />
    </target>

    <!-- 

        Hook all junit.test* ..

      -->

    <target name="java.generate" depends="junit.test1.java.generate"/>

    <target name="native.build" depends="c.configure, junit.test1.c.build" unless="build.javaonly">
      <antcall target="gluegen.cpptasks.striplibs" inheritRefs="true">
          <param name="libdir" value="${build_t.lib}"/>
      </antcall>
    </target>


    <!-- 

        junit.test1

      -->


    <target name="junit.test1.java.generate">
        <echo message=" - - - junit.test1.java.generate" />

        <dirset id="stub.includes.fileset.test" dir=".">
            <include name="${test.junit.generation.dir}/**"/>
            <include name="${stub.includes.dir}/gluegen" />
            <include name="${stub.includes.dir}/macosx" />
            <include name="${stub.includes.dir}/unix" />
            <include name="${stub.includes.dir}/windows" />
        </dirset>

        <gluegen src="${test.junit.generation.dir}/test1-gluegen.c"
                 outputRootDir="${build_t.gen}"
                 config="${test.junit.generation.dir}/test1-gluegen.cfg"
                 literalInclude="${test.junit.generation.dir}"
                 includeRefid="stub.includes.fileset.test"
                 emitter="com.jogamp.gluegen.JavaEmitter"
                 dumpCPP="false"
                 debug="false">
            <classpath refid="gluegen.classpath" />
        </gluegen>

        <gluegen src="${test.junit.generation.dir}/test1-gluegen.c"
                 outputRootDir="${build_t.gen}"
                 config="${test.junit.generation.dir}/test1p1-gluegen.cfg"
                 literalInclude="${test.junit.generation.dir}"
                 includeRefid="stub.includes.fileset.test"
                 emitter="com.jogamp.gluegen.JavaEmitter"
                 dumpCPP="false"
                 debug="false">
            <classpath refid="gluegen.classpath" />
        </gluegen>

        <gluegen src="${test.junit.generation.dir}/test1-gluegen.c"
                 outputRootDir="${build_t.gen}"
                 config="${test.junit.generation.dir}/test1p2-gluegen.cfg"
                 literalInclude="${test.junit.generation.dir}"
                 includeRefid="stub.includes.fileset.test"
                 emitter="com.jogamp.gluegen.procaddress.ProcAddressEmitter"
                 dumpCPP="false"
                 debug="false">
            <classpath refid="gluegen.classpath" />
        </gluegen>
    </target>

    <target name="junit.test1.c.build" depends="junit.test1i.c.build, junit.test1p1.c.build, junit.test1p2.c.build" unless="build.javaonly" />

    <!-- this is the test1 implementation -->
    <target name="junit.test1i.c.build">
        <patternset id="junit.test1i.c.src.files">
          <include name="src/junit/${test.junit.generation.rel}/test1.c"/>
        </patternset>

        <!-- Windows hacks ro make a proper DLL -->
        <linker id="linker.test1.dll.cfg.id" extends="${linker.cfg.id}">
            <linkerarg value="-Wl,-soname=test1.dll" if="isMingW"/>
            <linkerarg value="-Wl,--output=test1.dll" if="isMingW"/>
        </linker>

        <c.build c.compiler.src.files="junit.test1i.c.src.files"
                 output.lib.name="test1"
                 compiler.cfg.id="${compiler.cfg.id}"
                 linker.cfg.id="linker.test1.dll.cfg.id"/>
    </target>

    <!-- this is a fixed binding to the test1 implementation -->
    <target name="junit.test1p1.c.build">
        <linker id="linker.test1.fixed.cfg.id" extends="${linker.cfg.id}">
            <linkerarg value="-Wl,-soname=Bindingtest1p1.dll" if="isMingW"/>
            <linkerarg value="-Wl,--output=Bindingtest1p1.dll" if="isMingW"/>
            <syslibset dir="${build_t.lib}" libs="test1"/>
        </linker>

        <patternset id="junit.test1p1.c.src.files">
          <include name="${build_t.gen.rootrel}/native/Bindingtest1p1Impl_JNI.c"/>
        </patternset>

        <c.build c.compiler.src.files="junit.test1p1.c.src.files"
                 output.lib.name="Bindingtest1p1"
                 compiler.cfg.id="${compiler.cfg.id}"
                 linker.cfg.id="linker.test1.fixed.cfg.id"/>

        <antcall target="rename.dylib" inheritRefs="true">
            <param name="src"  value="${build_t.lib}/libBindingtest1p1.dylib" />
            <param name="dest" value="${build_t.lib}/libBindingtest1p1.jnilib" />
        </antcall>
    </target>

    <!-- this is a dynamic lookup binding to the test1 implementation -->
    <target name="junit.test1p2.c.build">
        <linker id="linker.test1.runtime.cfg.id" extends="${linker.cfg.id}">
            <linkerarg value="-Wl,-soname=Bindingtest1p2.dll" if="isMingW"/>
            <linkerarg value="-Wl,--output=Bindingtest1p2.dll" if="isMingW"/>
        </linker>

        <patternset id="junit.test1p2.c.src.files">
          <include name="${build_t.gen.rootrel}/native/Bindingtest1p2Impl_JNI.c"/>
        </patternset>

        <c.build c.compiler.src.files="junit.test1p2.c.src.files"
                 output.lib.name="Bindingtest1p2"
                 compiler.cfg.id="${compiler.cfg.id}"
                 linker.cfg.id="linker.test1.runtime.cfg.id"/>

        <antcall target="rename.dylib" inheritRefs="true">
            <param name="src"  value="${build_t.lib}/libBindingtest1p2.dylib" />
            <param name="dest" value="${build_t.lib}/libBindingtest1p2.jnilib" />
        </antcall>

    </target>

    <!-- 

        junit.test2

      -->

    <!-- updates / create the test results zip file -->
    <target name="test-zip-archive" depends="init">
        <archive.7z destfile="${build}/${test.archive.name}.7z"
                    basedir="${build}/test/results" 
                    includes="**" />
    </target>

</project>