<project name="BlueCove GPL" default="default" basedir=".">
    <description>
        Open source implementation of JSR-82 Java Bluetooth API. Additional GPL licensed module to support BlueCove runtime on Linux BlueZ
    </description>

    <!--
        You may use to build the project
        ant -Dbuild.compiler=jikes
     -->

    <property name="product_version" value="2.0.3"/>

    <property name="bluecove_main_dist_dir" location="../bluecove/target"/>
    <property name="bluecove_main_jar" location="${bluecove_main_dist_dir}/bluecove-${product_version}.jar"/>

    <!-- set global properties for this build -->
    <property name="src" location="src/main/java"/>
    <property name="src_c" location="src/main/c"/>
    <property name="resources" location="src/main/resources"/>
    <property name="target" location="target"/>
    <property name="build_classes" location="${target}/classes"/>
    <property name="build_native" location="${target}/native"/>
    <property name="dist" location="${target}"/>

    <property name="CC" value="gcc"/>
    <property name="CC_compiler_options" value="-fPIC -fno-stack-protector"/>

    <target name="default" depends="compile, jni-headers, native-lib"/>

    <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build_classes}"/>
        <mkdir dir="${build_native}"/>
        <available property="bluecove-main.exists" file="${bluecove_main_jar}" type="file"/>
        <antcall target="verify-bluecove-main-exists"/>
    </target>

    <target name="verify-bluecove-main-exists" unless="bluecove-main.exists">
        <fail>
             The path "${bluecove_main_jar}" does not exist.
             Download or build main BlueCove jar.
        </fail>
    </target>

    <target name="compile" depends="init"
        description="Compile the source">

        <echo message="compiling on java ${java.version}"/>
        <javac source="1.3" target="1.1" debug="true"
               srcdir="${src}" destdir="${build_classes}">
            <classpath>
                <pathelement path="${bluecove_main_jar}"/>
            </classpath>
        </javac>

    </target>

    <target name="jni-headers" depends="init" unless="jni_headers_skip"
        description="Create JNI headers">

        <echo message="create JNI headers using java.home ${java.home}"/>
        <javah destdir="src/main/c" classpath="${build}">
            <classpath>
                <pathelement path="${build_classes}"/>
                <pathelement path="${bluecove_main_jar}"/>
            </classpath>
            <class name="com.intel.bluetooth.BluetoothStackBlueZ"/>
            <class name="com.intel.bluetooth.BluetoothStackBlueZConsts"/>
            <class name="com.intel.bluetooth.BluetoothStackBlueZNativeTests"/>
        </javah>

    </target>

    <target name="native-lib" depends="jni-headers"
        description="Create Native shared library">

        <echo message="compiling  .c -> .o"/>
        <apply executable="${CC}" dest="${build_native}" parallel="false">
             <arg line="${CC_compiler_options}"/>
             <arg value="-I${java.home}/../include" />
             <arg value="-I${java.home}/../include/linux" />
             <arg value="-c"/>
             <srcfile/>
             <arg value="-o"/>
             <targetfile/>
             <fileset dir="${src_c}"/>
             <mapper type='glob' from='*.c' to='*.o'/>
        </apply>

        <!-- i386 version does not have any sufix, See NativeLibLoader.java -->
        <condition property="library_sufix" value="">
            <os arch="i386"/>
        </condition>
        <condition property="library_sufix" value="_x64">
            <os arch="amd64"/>
        </condition>
        <condition property="library_sufix" value="_${os.arch}">
            <not>
                <isset property="library_sufix" />
            </not>
        </condition>

        <property name="library_name" value="libbluecove${library_sufix}.so"/>

        <echo message="linking    .o -> ${library_name}"/>
        <apply failonerror="true" executable="${CC}" parallel="true">
             <fileset dir='${build_native}' includes="*.o"/>
             <arg value="-shared"/>
             <arg value="-lbluetooth"/>
             <arg value="-nodefaultlibs"/>
             <arg value="-o"/>
             <arg value="${target}/${library_name}"/>
             <srcfile/>
        </apply>

        <copy file="${target}/${library_name}" todir="${resources}"/>

    </target>


    <target name="jar" depends="compile, native-lib"
            description="Create the distribution jar">

        <!-- Create the distribution directory -->
        <mkdir dir="${dist}"/>

        <tstamp>
            <format property="today" pattern="yyyy-MM-dd hh:mm:ss" />
        </tstamp>

        <!-- Put everything in ${build} into the BlueCove-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/bluecove-gpl-${product_version}.jar">
            <manifest>
                <attribute name="Description" value="BlueCove JSR-82 implementation, runtime module for Linux BlueZ"/>
                <attribute name="License" value="GNU General Public License (GPL)"/>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Implementation-Version" value="${product_version}"/>
                <attribute name="Build-Date" value="${today}"/>
                <attribute name="Built-Env"   value="${os.name} ${os.version} ${os.arch}, Java ${java.version}"/>
            </manifest>
            <fileset dir="${build_classes}">
                <include name="**/*.class"/>
                <exclude name="**/*.log"/>
                <exclude name="**/.*"/>
                <exclude name="**/Thumbs.db"/>
            </fileset>
            <fileset dir="${resources}">
                <include name="*.so"/>
            </fileset>
            <fileset dir=".">
                <include name="LICENSE.txt"/>
            </fileset>
        </jar>
    </target>

    <target name="clean"
            description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>

    <target name="all" depends="clean, jar"/>
</project>

