<?xml version="1.0" encoding="UTF-8"?>
<project name="compile-base" default="compile-base">
    <description>
        Holds common imported definitions for ANT build.xml file(s)
        that build/generate artifacts
    </description>

    <dirname property="module.root.folder" file="${ant.file}"/>
    <property name="jna.parent.folder" value="${module.root.folder}"/>
    <import file="${jna.parent.folder}${file.separator}build-base.xml"/>

    <tstamp>
        <format property="year" pattern="yyyy" locale="en,US"/>
    </tstamp>
    <property name="copyright" value="Copyright &amp;copy; 2007-${year} Timothy Wall. All Rights Reserved."/>
    <buildnumber/>

    <!-- JNA library release version - NOTE: this MUST match Maven -->
    <property name="jna.major" value="4"/>
    <property name="jna.minor" value="3"/>
    <property name="jna.revision" value="0"/>
    <property name="jna.build" value="0"/> <!--${build.number}-->
    <condition property="version.suffix" value="" else="-SNAPSHOT">
        <or>
            <isset property="release"/>
            <isset property="maven-release"/>
        </or>
    </condition>
    <property name="jna.version" value="${jna.major}.${jna.minor}.${jna.revision}${version.suffix}"/>

    <property name="spec.title" value="Java Native Access (JNA)"/>
    <property name="spec.vendor" value="JNA Development Team"/>
    <property name="spec.version" value="${jna.major}"/>
    <property name="impl.title" value="com.sun.jna"/>
    <property name="impl.vendor" value="${spec.vendor}"/>
    <property name="impl.version" value="${jna.version} (b${jna.build})"/>

    <!-- +++++++++++++++ Supported platforms - Add other supported platforms here ++++++++++++++++ -->
    <condition property="jre.arch" value="x86">
        <matches pattern="(i[3456]86|pentium)" string="${os.arch}"/>
    </condition>
    <condition property="jre.arch" value="x86-64">
        <matches pattern="(x86_64|amd64|em64t)" string="${os.arch}"/>
    </condition>
    <condition property="jre.arch" value="ppc">
        <matches pattern="(powerpc|power)" string="${os.arch}"/>
    </condition>
    <condition property="jre.arch" value="ppc64le">
        <or>
            <matches pattern="(powerpc64le|power64le|ppc64le)" string="${os.arch}"/>
            <and>
                <matches pattern="(powerpc64|power64|ppc64)" string="${os.arch}"/>
                <matches pattern="little" string="${sun.cpu.endian}"/>
            </and>
        </or>
    </condition>
    <condition property="jre.arch" value="ppc64">
        <matches pattern="(powerpc64|power64)" string="${os.arch}"/>
    </condition>
    <property name="jre.arch" value="${os.arch}"/>

    <!-- ++++++++++++ Maven related properties definitions +++++++++++++ -->
    <property name="maven.base.name" value="maven"/>
    <property name="maven.archiver.sub.folder.name" value="${maven.base.name}-archiver"/>
    <property name="maven.repository.root.folder.name" value=".m2"/>
    <property name="maven.repository.sub.folder.name" value="repository"/>
    <property name="maven.settings.file.name" value="settings.xml"/>
    <property name="maven.pom.file.name" value="pom.xml"/>
    <property name="maven.sources.classifier" value="sources"/>
    <property name="maven.install.location" value="${env.M2_HOME}"/>
    <condition property="mvn.exe.script.name" value="mvn.cmd" else="mvn">
        <istrue value="${win32.local.shell}"/>
    </condition>

    <property name="mvn.executable"
              value="${maven.install.location}${file.separator}bin${file.separator}${mvn.exe.script.name}"/>
    <property name="maven.settings.file.path" value="${jna.parent.folder}${file.separator}${maven.settings.file.name}"/>
    <property name="maven.opts" value="-Xmx512m"/>
    <condition property="maven.local.repository.folder"
               value="${env.M2_REPO}"
               else="${user.home}${file.separator}${maven.repository.root.folder.name}${file.separator}${maven.repository.sub.folder.name}">
        <isset property="env.M2_REPO"/>
    </condition>

    <!-- Procedure to execute the Maven script - SYNPOSIS:

            <antcall target='run-mvn'>
                <param name='mvn.run.dir' value='wwww' />
                <param name='mvn.run.opts' value='xxxxxx'/>
                <param name='mvn.arg.line' value='yyyyy'/>
            </antcall>
            
            Where:
            
                mvn.run.dir - folder to be used as CWD when running the script
                mvn.run.opts - options to be set to MAVEN_OPTS (e.g., -Xmx128)
                mvn.arg.line - line to be passed as argument to the invocation
        -->
    <target name="run-mvn">
        <fail message="Cannot find ${mvn.executable} - check that M2_HOME environment variable set">
            <condition>
                <not>
                    <available file="${mvn.executable}" type="file"/>
                </not>
            </condition>
        </fail>

        <echo message="mvn ${mvn.arg.line}" level="info"/>
        <exec executable="${mvn.executable}" failonerror="true" dir="${mvn.run.dir}">
            <env key="MAVEN_OPTS" value="${mvn.run.opts}"/>
            <!-- arg line="-s ${maven.settings.file.path}" / -->
            <arg line="${mvn.arg.line}"/>
        </exec>
    </target>

    <property name="maven.module.options" value="-Xmx728m"/>
    <property name="maven.full.test.options"
              value="-Dmaven.test.haltafterfailure=true -Dmaven.test.failure.ignore=false -Dmaven.test.error.ignore=false"/>

    <property name="maven.install.options" value="${maven.full.test.options} install"/>
    <target name="run-module-mvn">
        <antcall target='run-mvn'>
            <param name='mvn.run.dir' value='${module.root.folder}'/>
            <param name='mvn.run.opts' value='${maven.module.options}'/>
            <param name='mvn.arg.line' value='${maven.install.options}'/>
        </antcall>
    </target>

    <property name="maven.clean.options" value="build-helper:remove-project-artifact -DremoveAll=true clean"/>
    <target name="clean-module-mvn">
        <antcall target='run-mvn'>
            <param name='mvn.run.dir' value='${module.root.folder}'/>
            <param name='mvn.run.opts' value='${maven.module.options}'/>
            <param name='mvn.arg.line' value='${maven.clean.options}'/>
        </antcall>
    </target>

    <target name="compile-base" depends="workspace-base"/>
    <target name="compile" depends="run-module-mvn"
            description="Runs the mvn install command - including all the tests"/>
    <target name="uncompile" depends="clean-module-mvn"
            description="Runs the mvn clean command - including un-install the artifact from Maven local repository"/>
    <target name="recompile" depends="uncompile,compile" description="Re-compiles the module"/>
</project>
