<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed 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 xmlns:j="jelly:core" xmlns:util="jelly:util" xmlns:ant="jelly:ant">

    <!--==================================================================-->
    <!-- Compile all source code                                      -->
    <!--==================================================================-->
    
    <goal name="native" description="Native compiler" prereqs="native:compile"/>

    <!--==================================================================-->
    <!-- Compile all source code                                          -->
    <!--==================================================================-->

    <goal name="native:compile" description="Compile the native compiler" prereqs="native:jniheader">

       <taskdef name="cc" classname="net.sf.antcontrib.cpptasks.CCTask"/>
       <typedef name="linker" classname="net.sf.antcontrib.cpptasks.LinkerDef"/>
       <typedef name="compiler" classname="net.sf.antcontrib.cpptasks.CompilerDef"/>

       <ant:echo> Real Java Home = '${java.home}/..' 
                  artifactId = '${pom.artifactId}' </ant:echo>
       
       <mkdir dir="${maven.build.dir}/native"/>
                
       <ant:available property="jniFilesPresent" file="${maven.build.dir}/native/jni"/>
        
       <!-- for some reason '.' aren't allow as parameter for outfile-->
       <cc outfile="${maven.build.dir}/native/_" link="${maven.native.link}" debug="${maven.native.debug}">
         <compiler name="${maven.native.compiler.name}">
           <compilerparam name="target" value="${maven.native.compiler.target}"/>
          </compiler>
         <linker name="${maven.native.linker.name}">     
           <linkerparam name="target" value="${maven.native.linker.target}"/>
         </linker>
         <fileset dir="${maven.native.src}" />
         <j:if test="${jniFilesPresent == 'true'}">
            <includepath location="${java.home}/../include"/>
            <includepath location="${java.home}/../include/${maven.native.include.os}"/>
            <includepath location="${maven.build.dir}/native/jni"/>
         </j:if>   
       </cc>

       <!-- just copy the file don't move because we don't want the compiler to compile if for no reason -->
       <ant:available property="outputPresent" file="${maven.build.dir}/native/lib_.so"/>
       
       <j:if test="${outputPresent == 'true'}">
          <ant:copy file="${maven.build.dir}/native/lib_.so" 
            tofile="${maven.build.dir}/native/lib${pom.artifactId}-${pom.currentVersion}.so" overwrite="true"/>
       </j:if> 
    </goal>

    <!-- ================================================================== -->
    <!-- I N S T A L L   L I B                                              -->
    <!-- ================================================================== -->
    
    <goal name="native:install" prereqs="native:compile" description="Install the lib in the local repository">
      <ant:echo>
                id = '${pom.artifactId}'
                groupId = '${pom.groupId}'
                artifactDirectory = '${pom.artifactDirectory}'
      </ant:echo>
      
      <ant:property name="libdir__" value="${maven.repo.local}/${pom.artifactDirectory}/so"/>
      <ant:mkdir dir="${libdir__}"/>
      <ant:copy file="${maven.build.dir}/native/lib${pom.artifactId}-${pom.currentVersion}.so" tofile="${libdir__}/lib${pom.artifactId}-${pom.currentVersion}.so" overwrite="true"/>
    </goal>
    
    <!-- ================================================================== -->
    <!-- R U N   J A V A H                                                  -->
    <!-- ================================================================== -->
    
    <goal name="native:jniheader" description="make jniheaders for classes" prereqs="java:compile">
       
      <fileScanner var="classFiles">
         <fileset dir="${maven.build.dest}">
           <patternset>
              <include name="**/*.class"/>
           </patternset>
         </fileset>
       </fileScanner>

      <!--do we need to run javah -->
      <j:set var="compileJNI" value="false"/>
      <j:useBean var="javaSourceTool" class="org.apache.maven.natives.util.JavaSourceTool"/>

      <j:forEach var="file" items="${classFiles.iterator()}">
        <j:set var="clazz" value="${javaSourceTool.getBcelClass(file.toString())}"/>
        <j:forEach var="method" items="${clazz.getMethods()}">
          <j:if test="${method.isNative() == 'true'}">
            <j:set var="compileJNI" value="true"/>
          </j:if>
        </j:forEach>
      </j:forEach>

      <j:if test="${compileJNI == 'true'}">
        <ant:echo>Running Javah on java classes</ant:echo>
        <mkdir dir="${maven.build.dir}/native/jni"/>

<!--  need ANT 1.6

        <javah destdir="${maven.build.dir}/native/jni">
          <classpath>
            <pathelement location="${maven.build.dest}"/>
          </classpath>
          <j:forEach var="file" items="${classFiles.iterator()}">
            <j:set var="isNative" value="false"/>
            <j:set var="clazz" value="${javaSourceTool.getBcelClass(file.toString())}"/>
            <j:forEach var="method" items="${clazz.getMethods()}">
              <j:if test="${method.isNative() == 'true'}">
                <j:set var="isNative" value="true"/>
              </j:if>
            </j:forEach>
            <j:if test="${isNative == 'true'}">
              <class name="${clazz.className}"/>
            </j:if>
          </j:forEach>
        </javah> -->

        <!--  workaround -->
        <property environment="env"/>
        <exec executable="${env.JAVA_HOME}/bin/javah">
          <arg line="-d ${maven.build.dir}/native/jni"/>
          <arg line="-classpath ${maven.build.dest}"/>
      
          <j:forEach var="file" items="${classFiles.iterator()}">
            <j:set var="isNative" value="false"/>
            <j:set var="clazz" value="${javaSourceTool.getBcelClass(file.toString())}"/>
            <j:forEach var="method" items="${clazz.getMethods()}">
              <j:if test="${method.isNative() == 'true'}">
                <j:set var="isNative" value="true"/>
              </j:if>
            </j:forEach>
            <j:if test="${isNative == 'true'}">
              <arg value="${clazz.className}"/>
            </j:if>
          </j:forEach>
        </exec>

      </j:if>
    </goal>
</project>
