Ant, Cpptasks, & Multi-Platform C/C++ Projects 
by Mirko Raner 

Listing One
<?xml version="1.0"?>
<project name="Hello" default="hello" basedir=".">
 <taskdef resource="cpptasks.tasks"/>
 <typedef resource="cpptasks.types"/>
 <target name="init">
  <condition property="cc" value="msvc">
   <os family="windows"/>
  </condition>
  <condition property="cc" value="gcc">
   <os family="unix"/>
  </condition>
 </target>
 <target name="hello" depends="init">
  <cc name="${cc}" outfile="hello">
   <fileset dir="." includes="hello.c"/>
  </cc>
 </target>
</project>


Listing Two
<?xml version="1.0"?>
<project name="JNI Demo" default="jnidemo" basedir=".">
 <taskdef resource="cpptasks.tasks"/>
 <typedef resource="cpptasks.types"/>
 <property environment="getenv"/>
 <compiler id="cc">
  <defineset define="_POSIX_SOURCE"/>
 </compiler>
 <compiler name="gcc" id="gcc-compiler" extends="cc">
  <compilerarg value="-ansi"/>
 </compiler>
 <compiler name="msvc" id="msvc-compiler" extends="cc">
  <compilerarg value="/Za"/>
  <compilerarg value="/G5"/>
 </compiler>
 <linker name="gcc" id="gcc-linker"/>
 <linker name="msvc" id="msvc-linker">
  <linkerarg value="/libpath:${getenv.MSDEVDIR}\lib"/>
 </linker>
 <target name="init">
  <condition property="cc" value="msvc">
   <os family="windows"/>
  </condition>
  <condition property="cc" value="gcc">
   <os family="unix"/>
  </condition>
  <condition property="msvc">
   <equals arg1="${cc}" arg2="msvc"/>
  </condition>
  <condition property="gcc">
   <equals arg1="${cc}" arg2="gcc"/>
  </condition>
  <condition property="lib" value="">
   <isset property="msvc"/>
  </condition>
  <condition property="lib" value="lib">
   <isset property="gcc"/>
  </condition>
  <condition property="static" value=".lib">
   <isset property="msvc"/>
  </condition>
  <condition property="static" value=".a">
   <isset property="gcc"/>
  </condition>
  <condition property="shared" value=".dll">
   <isset property="msvc"/>
  </condition>
  <condition property="shared" value=".so">
   <isset property="gcc"/>
  </condition>
  <condition property="obj" value=".obj">
   <isset property="msvc"/>
  </condition>
  <condition property="obj" value=".o">
   <isset property="gcc"/>
  </condition>
  <condition property="exe" value=".exe">
   <isset property="msvc"/>
  </condition>
  <condition property="exe" value="">
   <isset property="gcc"/>
  </condition>
  <condition property="platform" value="linux">
   <os name="Linux"/>
  </condition>
  <condition property="platform" value="win32">
   <os family="windows"/>
  </condition>
  <condition property="platform" value="solaris">
   <os name="SunOS"/>
  </condition>
 </target>
 <target name="jnilib" depends="init">
  <cc name="${cc}" outfile="jnilib" outtype="static">
   <compiler refid="${cc}-compiler"/>
   <linker refid="${cc}-linker"/>
   <fileset dir="." includes="jnilib1.c jnilib2.c"/>
  </cc>
 </target>
 <target name="jnidemo" depends="jnilib">
  <cc name="${cc}" outfile="jnidemo" outtype="shared">
   <compiler extends="${cc}-compiler">
    <includepath location="${java.home}/../include"/>
    <includepath location="${java.home}/../include/${platform}"/>
    <compilerarg value="-O0" if="gcc"/>
    <compilerarg value="/Od" if="msvc"/>
   </compiler>
   <linker refid="${cc}-linker"/>
   <fileset dir="." includes="jnidemo.c"/>
   <libset dir="." libs="jnilib"/>
  </cc>
 </target>
 <target name="jnilib-clean" depends="init">
  <delete>
   <fileset dir="." includes="*${obj}"/>
   <fileset dir="." includes="${lib}jnilib${static}"/>
  </delete>
 </target>
 <target name="jnidemo-clean" depends="init">
  <condition property="additionalfiles" value="">
   <isset property="gcc"/>
  </condition>
  <condition property="additionalfiles"
   value="jnidemo.lib jnidemo.exp">
   <isset property="msvc"/>
  </condition>
  <delete>
   <fileset dir="."
    includes="jnidemo${obj} ${lib}jnidemo${shared} ${additionalfiles}" />
  </delete>
 </target>
 <target name="clean" depends="jnidemo-clean, jnilib-clean"/>
</project>





3


