<!--
This buildfile is for compiling applications that use the Standard Widget
Toolkit (SWT) from the Eclipse project (http://www.eclipse.org).

For a listing of options and further description, see the 'help' target.
Alternately, type 'ant help' at the command-line.
-->
<project name="Build" default="native">

	<!--
	Setup a custom task. This task calls an Ant target for each file
	in a fileset. Make sure that customtasks.jar is in the same directory
	as this buildfile.
	-->
	<taskdef name="applyantcall"
	         classname="org.apache.tools.ant.taskdefs.optional.CallTargetOn"
	         classpath="customtasks.jar"/>

	<!--
	The following properties can be overridden at the command-line
	(e.g. e.g. ant -Dfoo=bar).
	-->
	<property name="library_dir" value="../Library"/>
	<property name="link" value="static"/>

	<!--
	These properties are used internally by the buildfile and shouldn't need
	to be overriden.
	-->
	<property name="classname" value="AddressBook"/>
	<property name="main_class_package" value="org.eclipse.swt.examples.addressbook"/>
	<property name="classname_fully_qualified" value="${main_class_package}.${classname}"/>
	
	<target name="help">
		<echo>
This buildfile is for compiling applications that use the
Standard Widget Toolkit (SWT) from the Eclipse project
(http://www.eclipse.org).

This buildfile can compile applications in the following
ways:

1. Compiling to native using GCJ linking to SWT library
   statically.

2. Compiling to native using GCJ linking to SWT dynamically
   (i.e. shared library).
   Note that this option is not currently supported on
   Windows hosts (as of GCJ 3.2).
   
3. Compiling to bytecode (i.e. .class) using the default
   system Java compiler. Note that if you would like to 
   force the script to use GCJ for compilation to bytecode
   (instead of javac), supply the -Dbuild.compiler=gcj at the
   command line.
   
This buildfile has been tested on Windows 2000, Windows XP,
Red Hat Linux 6.2/Motif, Red Hat Linux 8.0/Motif,
and Red Hat Linux 8.0/GNOME. This buildfile should
work (but hasn't been tested) on the other UNIX systems
supported by Eclipse (AIX, HP/UX, Solaris) on Motif. It may
also work on QNX/Photon.

This buildfile has the following parameters. You can either
accept the default or provide your own value at the
command-line (e.g. ant -Dfoo=bar).

	library_dir
		The location where your static or shared library
		resides.
		default: ${library_dir}
		
	link
		The linking method: static or dynamic.
		default: ${link}
		values: static, dynamic

	swt.jar
		The location of your SWT JAR file. This is an optional
		setting that is useful when running the build from
		Eclipse. When compiling, you either need to have the
		Eclipse SWT source code in your library_dir or set this
		property to point to a valid swt.jar. If you have the
		Eclipse SWT source code in your library_dir, you do
		not need to set this value. If not, set this value to
		point to a valid Eclipse swt.jar. Note that the file
		should be in a path that does not contain spaces.
		defaults: ${swt.jar}
		values: A swt.jar file
		e.g. /usr/local/eclipse/plugins/org.eclipse.swt.gtk_2.0.
		     1\ws\win32\swt.jar
		

This buildfile also has the following targets that you might
want to call:

	jar
		Calling this task will compile your application to
		bytecode and package it in a JAR. This task is useful
		if you'd like to run your application in a virtual
		machine. Note that if you would like to force the script
		to use GCJ for compilation to bytecode (instead of
		javac), supply the -Dbuild.compiler=gcj at the command-
		line.

	clean
		Deletes compiled class files, object code (i.e. .o), and
		library files; thus allowing you to do a clean build.
		
Examples:
	
	Build using a shared library:
		ant -Dlink=dynamic


	Build using a shared library with an alternate library
	directory:
		ant -Dlibrary_dir=/usr/lib -Dlink=dynamic


	Compile your application natively from within Eclipse:
		ant -Dswt.jar=..\..\plugins\org.eclipse.swt.win32_2.0.
		    1\ws\win32\swt.jar 
	
	Compile your application to a JAR file:
		ant jar
		</echo>
	</target>

	<target name="init">
		<!--
		Set the executable name. On Windows systems, it will have a .exe
		suffix. On Unix systems, it will have no suffix.
		-->
		<condition property="executable" value="${classname}.exe">
			<os family="windows"/>
		</condition>

		<condition property="executable" value="${classname}">
			<not>
				<isset property="executable"/>
			</not>
		</condition>
	</target>

	<target name="compile">
		<javac srcdir="." listfiles="true"
		       classpath=".${path.separator}${library_dir}${path.separator}${swt.jar}">
			<compilerarg line="-d ."/>
		</javac>
	</target>
	
	<target name="jar" depends="compile">
		<jar basedir="." destfile="${classname}.jar"
		     includes="**/*.class,**/*.properties,**/*.bmp,**/*.gif">
			<manifest>
				<attribute name="Class-Path" value="."/>
				<attribute name="Main-Class" value="${classname_fully_qualified}"/>
			</manifest>
		</jar>
	</target>
	
	<target name="resource-compile">
		<applyantcall target="resource-compile-helper">
			<param name="resource_file">
				<fileset dir="." includes="**/*.properties"/>
				<fileset dir="." includes="**/*.bmp"/>
				<fileset dir="." includes="**/*.gif"/>
			</param>
		</applyantcall>
	</target>

	<target name="native-compile" depends="resource-compile">
		<!--
		The relative="true" is necessary if your source path has spaces in it.
		(e.g. ~/My Project/java). In this case, without relative="true" jc1 will
		complain.
		-->
		<apply executable="gcj" dest="." parallel="false" relative="true"
		       failonerror="true">
			<arg value="-fjni"/>
			<arg value="-g0"/>
			<arg line="--classpath .${path.separator}${library_dir}${path.separator}${swt.jar}"/>
			<arg value="-c"/>
			<arg value="-o"/>
			<targetfile/>
			<srcfile/>
			<fileset dir="." includes="**/*.java"/>
			<mapper type="glob" from="*.java" to="*.o"/>
		</apply>
	</target>
	
	<target name="native" depends="native-compile, init, objects-up-to-date"
	        unless="objects_up_to_date">
		<dependset>
			 <srcfileset dir="." includes="**/*.o"/>
			 <targetfilelist dir="." files="${executable}"/>
		</dependset>	

		<antcall target="${link}-link"/>
		
		<apply executable="strip">
			<fileset dir="." includes="${executable}"/>
		</apply>
	</target>
	
	<target name="static-link">
		<apply executable="gcj" dest="." parallel="true"
		       skipemptyfilesets="true">
			<arg line="--main=${classname_fully_qualified}"/>
			<arg value="-mwindows"/>
			<arg value="-o"/>
			<targetfile/>
			<srcfile/>
			<fileset dir="." includes="**/*.o"/>
			<fileset dir="${library_dir}" includes="**/*.a"/>
			<mapper type="merge" to="${executable}"/>
		</apply>
	</target>
		
	<target name="dynamic-link">
		<apply executable="gcj" dest="." parallel="true"
		       skipemptyfilesets="true">
			<arg line="--main=${classname_fully_qualified}"/>
			<arg value="-mwindows"/>
			<arg value="-o"/>
			<targetfile/>
			<srcfile/>
			<fileset dir="." includes="**/*.o"/>
			<mapper type="merge" to="${executable}"/>
			<arg line="-L${library_dir}"/>
			<arg line="-l-org-eclipse-swt"/>
		</apply>
	</target>
	
	<target name="library" depends="native-compile, init, objects-up-to-date"
	        unless="objects_up_to_date">
		<dependset>
			 <srcfileset dir="." includes="**/*.o"/>
			 <targetfilelist dir="." files="${library}"/>
		</dependset>	

		<apply executable="ar" dest="." parallel="true"
		       skipemptyfilesets="true">
			<arg value="rcs"/>
			<targetfile/>
			<srcfile/>
			<fileset dir="." includes="**/*.o"/>
			<mapper type="merge" to="${library}"/>
		</apply>
		
		<apply executable="ranlib">
			<fileset dir="." includes="${library}"/>
		</apply>
	</target>
	
	<target name="resource-compile-helper" depends="resource-up-to-date"
	        unless="resource_up_to_date">
    	<pathconvert dirsep="/" property="resource_name">
    		<path>
	    		<pathelement location="${resource_file}"/>
	    	</path>
    		<map from="${basedir}\" to=""/>
    	</pathconvert>

		<echo message="Compiling the resource: ${resource_name}"/>
		<exec executable="gcj" dir=".">
			<arg value="-c"/>
			<arg line="--resource=${resource_name}"/>
			<arg line="-o ${resource_file}.o"/>
			<arg value="${resource_file}"/>
		</exec>
	</target>

	<target name="resource-up-to-date">
		<uptodate property="resource_up_to_date" srcfile="${resource_file}"
		          targetfile="${resource_file}.o"/>
	</target>

	<target name="objects-up-to-date">
		<uptodate property="objects_up_to_date" targetfile="${executable}">
			<srcfiles dir= "." includes="**/*.o"/>
		</uptodate>
	</target>
	
	<target name="clean" depends="init">
		<delete>
			<fileset dir="." includes="**/*.class, **/*.o, ${classname}.jar,
			                           ${classname}, ${executable}, **/*.dll,
			                           **/*.so"/>
		</delete>
	</target>

</project>
