<?xml version="1.0" encoding="UTF-8"?>
<project name="platform"
         default="default"
         basedir="."
         xmlns:if="ant:if"
         xmlns:unless="ant:unless">
    <description>Builds and tests platform-specific code.</description>

    <import file="../../common.xml" />
    <property name="build" value="build"/>
    <property name="classes" location="${build}/classes"/>
    <property name="debug" value="true"/>
    <property name="javac.release" value="8"/>
    <property name="platform.name" value="jna-platform"/>
    <property name="jar" value="${platform.name}.jar"/>
    <property name="jar-base" value="${platform.name}-base.jar"/>
    <property name="jar-jpms" value="${platform.name}-jpms.jar"/>
    <property name="src" location="src"/>
    <property name="testjar" value="${platform.name}-test.jar"/>
    <property name="test.classes" location="${build}/test-classes"/>
    <property name="test.src" location="test"/>
    <property name="tests.exclude" value=""/>
    <property name="tests.exclude-patterns" value=""/>
    <property name="dist" value="dist" />
    <path id="src.path">
        <pathelement location="${src}"/>
    </path>
    <path id="compile.path">
        <fileset dir="../../build">
            <include name="jna.jar"/>
        </fileset>
    </path>
    <path id="compile-test.path">
        <path id="test.libs">
            <fileset dir="../../lib">
                <include name="junit.jar"/>
                <include name="hamcrest-core-1.3.jar"/>
            </fileset>
            <fileset dir="../../lib/test">
                <include name="bcpkix-jdk15on-161.jar"/>
                <include name="bcprov-jdk15on-161.jar"/>
                <include name="reflections-0.9.11.jar"/>
                <include name="guava-27.1-jre.jar"/>
                <include name="javassist-3.12.1.GA.jar"/>
            </fileset>
            <fileset dir="../../build">
                <include name="jna-test.jar"/>
                <include name="jna.jar"/>
            </fileset>
            <pathelement path="../../build/test-classes"/>
            <pathelement path="${classes}"/>
        </path>
    </path>
    <path id="test.runpath">
      <pathelement path="../../build/${native.subdir}"/>
      <pathelement path="${build}/${jar}"/>
      <pathelement path="${test.classes}"/>
      <pathelement path="${test.src}"/>
      <pathelement path="../../lib/clover.jar"/>
      <path refid="test.libs"/>
    </path>

    <!-- Prepare additional ant task -->
    <path id="maven-ant-tasks.classpath">
        <pathelement location="../../lib/maven-ant-tasks-2.1.3.jar" />
    </path>

    <target name="clean">
        <delete failOnError="false" includeEmptyDirs="true">
            <fileset dir="${build}"/>
        </delete>
    </target>

    <target name="compile" description="Compile all Java source">

        <mkdir dir="${classes}" />

        <javac release="${javac.release}"
               destdir="${classes}"
               includeantruntime="false"
               deprecation="on"
               debug="${debug}"
               encoding="UTF-8"
               classpathref="compile.path">
            <src refid="src.path"/>
        </javac>
    </target>

    <target name="compile-tests" depends="compile" description="Compile all test code">

        <mkdir dir="${test.classes}" />

        <javac classpathref="compile-test.path"
               release="${javac.release}"
               destdir="${test.classes}"
               includeantruntime="false"
               excludes="${tests.exclude-patterns}"
               deprecation="on"
               debug="${debug}"
               encoding="UTF-8"
        >
            <src path="${test.src}"/>
            <exclude name="${tests.exclude}"/>
        </javac>
        <!-- Create a jar for easy movement of tests, and jar load test -->
        <jar jarfile="${build}/${testjar}" createUnicodeExtraFields="never" encoding="UTF-8">
            <fileset dir="${test.classes}">
                <include name="**/*"/>
            </fileset>
            <manifest>
                <attribute name="permissions" value="all-permissions"/>
            </manifest>
        </jar>
    </target>

    <target name="jar" depends="-pre-jar,-build-jar,-post-jar" />

    <target name="-prepare-anttools">
        <subant antfile="build-ant-tools.xml" buildpath="${basedir}/../../"></subant>
        <taskdef name="BuildArmSoftFloatDetector" classname="com.sun.jna.BuildArmSoftFloatDetector" classpath="${basedir}/../../build/ant-tools:${basedir}/../../lib/asm-8.0.1.jar" />
        <taskdef name="CalcAndroidVersion" classname="com.sun.jna.ant.CalcAndroidVersion" classpath="${basedir}/../../build/ant-tools:${basedir}/../../lib/asm-8.0.1.jar" />
        <taskdef name="ModuleGenerator" classname="com.sun.jna.ant.ModuleGenerator" classpath="${basedir}/../../build/ant-tools:${basedir}/../../lib/asm-8.0.1.jar" />
    </target>

    <macrodef name="build-manifest">
        <attribute name="target" />
        <attribute name="module-info" default="false" />
        <sequential>
            <manifest file="@{target}" mode="replace">
                <attribute name="Manifest-Version" value="1.0"/>
                <attribute name="Implementation-Title" value="${impl.title}"/>
                <attribute name="Implementation-Vendor" value="${vendor}"/>
                <attribute name="Implementation-Version" value="${impl.version}"/>
                <attribute name="Specification-Title" value="${spec.title}"/>
                <attribute name="Specification-Vendor" value="${spec.vendor}"/>
                <attribute name="Specification-Version" value="${spec.version}"/>
                <!--
                  OSGi Bundle attributes
                  See http://www.osgi.org/Specifications/Reference
                -->
                <attribute name="Bundle-Category" value="jni"/>
                <attribute name="Bundle-ManifestVersion" value="2"/>
                <attribute name="Bundle-Name" value="jna-platform"/>
                <attribute name="Bundle-Description" value="JNA Platform Library"/>
                <attribute name="Bundle-SymbolicName" value="com.sun.jna.platform"/>
                <attribute name="Bundle-Version" value="${osgi.version}"/>
                <attribute name="Bundle-RequiredExecutionEnvironment" value="J2SE-1.4"/>
                <attribute name="Bundle-Vendor" value="${vendor}"/>
                <attribute name="Require-Bundle" value="com.sun.jna;bundle-version=&quot;${osgi.version}&quot;"/>
                <!--
                The full "Export-Package" statement can be generated by invoking
                create-export-package-metadata-pom.sh in the main folder. This is
                necessary if the exported packages change. Then the list in the
                shell script needs to be updated as well.
                -->
                <attribute name="Export-Package"
                           value="
com.sun.jna.platform;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform.win32&quot;,
com.sun.jna.platform.dnd;version=&quot;${osgi.version}&quot;,
com.sun.jna.platform.linux;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform.unix&quot;,
com.sun.jna.platform.mac;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform,com.sun.jna.platform.unix,com.sun.jna.ptr&quot;,
com.sun.jna.platform.unix;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.ptr&quot;,
com.sun.jna.platform.unix.aix;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna&quot;,
com.sun.jna.platform.unix.solaris;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.ptr&quot;,
com.sun.jna.platform.win32;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform,com.sun.jna.platform.win32.COM,com.sun.jna.ptr,com.sun.jna.win32&quot;,
com.sun.jna.platform.win32.COM;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform.win32,com.sun.jna.platform.win32.COM.util,com.sun.jna.ptr,com.sun.jna.win32&quot;,
com.sun.jna.platform.win32.COM.tlb;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna.platform.win32.COM.tlb.imp&quot;,
com.sun.jna.platform.win32.COM.tlb.imp;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna.platform.win32,com.sun.jna.platform.win32.COM&quot;,
com.sun.jna.platform.win32.COM.util;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform.win32,com.sun.jna.platform.win32.COM,com.sun.jna.platform.win32.COM.util.annotation,com.sun.jna.ptr&quot;,
com.sun.jna.platform.win32.COM.util.annotation;version=&quot;${osgi.version}&quot;,
com.sun.jna.platform.wince;version=&quot;${osgi.version}&quot;;uses:=&quot;com.sun.jna,com.sun.jna.platform.win32&quot;
"/>
            </manifest>
            <manifest file="@{target}" mode="update" if:true="@{module-info}">
                <attribute name="Multi-Release" value="true"/>
            </manifest>
            <manifest file="@{target}" mode="update" unless:true="@{module-info}">
                <attribute name="Automatic-Module-Name" value="com.sun.jna.platform"/>
            </manifest>
        </sequential>
    </macrodef>

    <target name="-pre-jar" depends="-prepare-anttools,compile">
        <delete dir="${classes}/META-INF" />
        <mkdir dir="${classes}/META-INF"/>
        <copy todir="${classes}/META-INF">
            <fileset dir="../../" includes="LICENSE,AL2.0,LGPL2.1"/>
        </copy>
    </target>

    <target name="-build-jar" depends="-pre-jar">
        <jar jarfile="${build}/${jar-base}" duplicate="preserve" createUnicodeExtraFields="never" encoding="UTF-8">
            <fileset dir="${classes}" >
                <patternset>
                    <include name="**/*"/>
                </patternset>
            </fileset>
            <fileset dir="${src}" >
                <patternset>
                    <include name="**/*"/>
                    <exclude name="**/*.java"/>
                </patternset>
            </fileset>
        </jar>
    </target>

    <target name="-post-jar" depends="-prepare-anttools">
        <delete dir="${build}/manifest"/>
        <mkdir dir="${build}/manifest"/>
        <build-manifest target="${build}/manifest/module.mf" module-info="true"/>
        <build-manifest target="${build}/manifest/automatic.mf" module-info="false"/>
        <ModuleGenerator
            targetFile="${build}/manifest/module-info.class"
            name="com.sun.jna.platform"
            version="${osgi.version}"
            open="false"
        >
            <exports package="com.sun.jna.platform" />
            <exports package="com.sun.jna.platform.dnd" />
            <exports package="com.sun.jna.platform.linux" />
            <exports package="com.sun.jna.platform.mac" />
            <exports package="com.sun.jna.platform.unix" />
            <exports package="com.sun.jna.platform.unix.aix" />
            <exports package="com.sun.jna.platform.unix.solaris" />
            <exports package="com.sun.jna.platform.win32" />
            <exports package="com.sun.jna.platform.win32.COM" />
            <exports package="com.sun.jna.platform.win32.COM.tlb" />
            <exports package="com.sun.jna.platform.win32.COM.tlb.imp" />
            <exports package="com.sun.jna.platform.win32.COM.util" />
            <exports package="com.sun.jna.platform.win32.COM.util.annotation" />
            <exports package="com.sun.jna.platform.wince" />
            <requires module="java.logging" />
            <requires module="java.desktop" transitive="true" />
            <requires module="java.datatransfer" transitive="true" />
            <requires module="com.sun.jna" transitive="true" />
        </ModuleGenerator>
        <jar jarfile="${dist}/${jar}" duplicate="preserve" createUnicodeExtraFields="never" encoding="UTF-8" manifest="${build}/manifest/automatic.mf">
            <zipfileset src="${build}/${jar-base}" excludes="META-INF/MANIFEST.mf"/>
        </jar>
        <jar jarfile="${dist}/jna-platform-jpms.jar" duplicate="preserve" createUnicodeExtraFields="never" encoding="UTF-8" manifest="${build}/manifest/module.mf">
            <zipfileset src="${build}/${jar-base}" excludes="META-INF/MANIFEST.mf"/>
            <zipfileset dir="${build}/manifest/" includes="module-info.class" prefix="META-INF/versions/9"/>
        </jar>
    </target>

    <target name="test" depends="compile,compile-tests" description="Run platform unit tests.">
      <echo>Running platform tests: ${test.src}</echo>
      <condition property="test.jdwp" value="-Xrunjdwp:transport=dt_socket,address=${test.debugport},server=y,suspend=y">
        <isset property="test.debugport" />
      </condition>
      <property name="test.fork" value="yes"/>
      <property name="reports.junit" location="${build}/reports/junit/${os.prefix}"/>
      <property name="results.junit" location="${build}/junit-results/${os.prefix}"/>
      <mkdir dir="${results.junit}"/>
      <echo>Saving test results in ${results.junit}</echo>
      <condition property="tests.platform.mac" value="**/mac/**/*Test.java">
        <and>
          <os family="mac"/>
          <not>
            <isset property="tests.include" />
          </not>
        </and>
      </condition>
      <condition property="tests.platform.windows" value="**/win32/**/*Test.java">
        <and>
          <os family="windows"/>
          <not>
            <isset property="tests.include" />
          </not>
        </and>
      </condition>
      <condition property="tests.platform.linux" value="**/linux/**/*Test.java">
        <and>
          <os name="Linux"/>
          <not>
            <isset property="tests.include" />
          </not>
        </and>
      </condition>
      <condition property="tests.platform.unix" value="**/unix/**/*Test.java">
        <and>
          <os family="unix"/>
          <not>
            <os family="mac"/>
          </not>
          <not>
            <isset property="tests.include" />
          </not>
        </and>
      </condition>
      <property name="tests.include" value="com/sun/jna/platform/*Test.java"/>
      <property name="tests.platform.mac" value=""/>
      <property name="tests.platform.windows" value=""/>
      <property name="tests.platform.linux" value=""/>
      <property name="tests.platform.unix" value=""/>
      <property name="tests.exclude" value=""/>
      <property name="tests.exclude-patterns" value=""/>
      <condition property="java.awt.headless" value="true">
        <isset property="headless"/>
      </condition>
      <condition property="vmopt.arch" value="-d64" else="-Dignore">
        <and>
          <!-- Sun 64-bit VM for windows doesn't support the -d64 switch -->
          <not><os family="windows"/></not>
          <equals arg1="${vm.arch}" arg2="-d64" trim="true"/>
        </and>
      </condition>
      <propertyset id="headless">
        <propertyref prefix="java.awt.headless"/>
      </propertyset>
      <echo>tests.include=${tests.include}</echo>
      <echo>tests.platform.mac=${tests.platform.mac}</echo>
      <echo>tests.platform.windows=${tests.platform.windows}</echo>
      <echo>tests.platform.linux=${tests.platform.linux}</echo>
      <echo>tests.platform.unix=${tests.platform.unix}</echo>
      <propertyset id="native.api.windows">
        <propertyref name="w32.ascii"/>
      </propertyset>
      <junit fork="${test.fork}" failureproperty="testfailure" tempdir="${build}">
        <jvmarg if:set="test.jdwp" value="${test.jdwp}" />
        <!-- optionally run headless -->
        <syspropertyset refid="headless"/>
        <!-- avoid VM conflicts with JNA protected mode -->
        <env key="${ld.preload.name}" file="${libjsig}"/>
        <!-- Ignore any system install of JNA -->
        <sysproperty key="jna.builddir" file="${file.reference.jna.build}"/>
        <!-- Optionally force native API on Windows -->
        <syspropertyset refid="native.api.windows"/>
        <jvmarg value="${vmopt.arch}"/>
        <classpath><path refid="test.runpath"/></classpath>
        <formatter type="brief" usefile="false"/>
        <formatter type="xml"/>
        <batchtest todir="${results.junit}">
          <fileset dir="${test.src}" excludes="${tests.exclude-patterns}">
            <!-- Until StructureFieldOrderTest gets fixed up a little -->
            <include name="${tests.include}" />
            <include name="${tests.platform.mac}"/>
            <include name="${tests.platform.windows}"/>
            <include name="${tests.platform.linux}"/>
            <include name="${tests.platform.unix}"/>
            <exclude name="${tests.exclude}"/>
          </fileset>
        </batchtest>
      </junit>
      <junitreport todir="${results.junit}">
        <fileset dir="${results.junit}">
          <include name="TEST-*.xml"/>
        </fileset>
        <report todir="${reports.junit}"/>
      </junitreport>
      <echo message="View test report in file://${reports.junit}/index.html" />
      <fail if="testfailure">One or more tests failed</fail>
    </target>
</project>
