<!DOCTYPE project
[
<!ENTITY quote "&#034;">
 ]>

<project name="My Project" default="build-lib" basedir=".">

	<property name="compile-lib" value="-DBUILDING_LIBRARY -Wall -g2 -gdwarf-2 -fpack-struct -fshort-enums -std=gnu99 -funsigned-char -funsigned-bitfields"/>
	<property name="compile-examples" value="-Wall -g2 -gdwarf-2 -fpack-struct -fshort-enums -std=gnu99 -funsigned-char -funsigned-bitfields -IC:/eclipse/workspace/WebbotCLibrary"/>
	<property name="version" value="1.5"/>

	<target name="clean-lib" description="Clean the library">
		<delete dir="./ant"/>
	  	<delete>
	    	<fileset dir="." includes="*.a, *.zip, *.lss"/>
	  	</delete>
	</target>

	<target name="clean-examples" description="Clean the examples">
	  	<delete>
	    	<fileset dir="examples" excludes="**/*.h, **/*.c"/>
	  	</delete>
	  	<delete>
	    	<fileset dir="." includes="examples-*.zip"/>
	  	</delete>
	</target>

	<target name="clean" description="clean everything" depends="clean-lib, clean-examples">
	</target>
	
	<target name="make-example" description="internal use only">
		<echo message="Making examples for ${system}${suffix}" />
		<!--  Compile the c files into o file -->
		<apply executable="avr-gcc" dest="examples" parallel="false" failonerror="true">
		  <arg line="${compile-examples}"/>
		  <arg value="-Os"/>
		  <arg value="-mmcu=${mcu}"/>
		  <arg value="-DF_CPU=${mhz}"/>
		  <arg value="-c"/>
		  <arg value="-o"/> <targetfile/> <srcfile/>
		  <fileset dir="examples" includes="**/${system}.c" />
		  <mapper type="glob" from="*.c" to="*${suffix}.o"/>
		</apply>
		
		<!-- Link to elf -->
		<apply executable="avr-gcc" dest="examples" parallel="false" failonerror="true">
		  <arg line="-Wl"/>
		  <arg value="-L&quote;.&quote;"/>
		  <arg value="-mmcu=${mcu}"/>
		  <arg value="-o"/> <targetfile/> <srcfile/>
		  <arg value="-lm"/>
		  <arg value="-l${lib}"/>
		  <fileset dir="examples" includes="**/${system}${suffix}.o" />
		  <mapper type="glob" from="*.o" to="*.elf"/>
		</apply>
		
		<!--  create extended listing -->
		<apply executable="avr-objdump" dest="examples" parallel="false" failonerror="true">
			<arg line ="-h"/>
		    <arg value="-S"/> <srcfile/>
			<fileset dir="." includes="examples/**/${system}${suffix}.elf"  />
	  		<mapper type="glob" from="*.elf" to="*.lss"/>
			<redirector>
	  		  	<outputmapper type="glob" from="*.elf" to="*.lss"/>
			</redirector>
		</apply>

		<!--  Show size of file -->
		<apply executable="avr-size" parallel="false" failonerror="true">
		  <arg line="--format=avr"/>
		  <arg line="--mcu=${mcu}"/> <srcfile/>
		  <fileset dir="examples" includes="**/${system}${suffix}.elf" />
		</apply>
		
		<!--  create hex file from elf -->
		<apply executable="avr-objcopy" dest="examples" parallel="false" failonerror="true">
		  <arg value="-R"/>
		  <arg value=".eeprom"/>
		  <arg value="-O"/>
		  <arg value="ihex"/>
		  <srcfile/> <targetfile/>
		  <fileset dir="examples" includes="**/${system}${suffix}.elf" />
		  <mapper type="glob" from="*.elf" to="*.hex"/>
		</apply>
		
		
		<!--  Zip the source code (*.c and *.h) and the .hex -->
		<zip destfile="./examples-${system}${suffix}.zip" basedir="examples"
			includes="**/${system}.c, **/*.h, **/${system}${suffix}.hex"
		/>
		
		
	</target>
	
	<target name="examples" description="Examples" depends="build-lib">
		<antcall target="make-example">
			<param name="system" value="Axon" />
			<param name="mcu" value="atmega640" />
			<param name="mhz" value="16000000" />
			<param name="suffix" value="" />
			<param name="lib" value="Webbot-ATMega640" />
		</antcall>
		<antcall target="make-example">
			<param name="system" value="ATMega168" />
			<param name="mcu" value="atmega168" />
			<param name="mhz" value="8000000" />
			<param name="suffix" value="-8MHz" />
			<param name="lib" value="Webbot-ATMega168" />
		</antcall>
		<antcall target="make-example">
			<param name="system" value="BabyOrangutan168" />
			<param name="mcu" value="atmega168" />
			<param name="mhz" value="20000000" />
			<param name="suffix" value="" />
			<param name="lib" value="Webbot-ATMega168" />
		</antcall>
	</target>
	
	<target name="release" description="Create release" depends="clean,build-lib,examples" >
		
		<!--  Zip the source code (*.c and *.h) and the libraries (*.a) -->
		<zip destfile="./webbotavrclib-${version}.zip" basedir="."
			includes="**/*.c, **/*.h, *.a"
			excludes="examples/**" 
		/>
	</target>
	
	<!--  Pass argument of 'mcu = processor' -->
	<target name="makeone" description="internal use only">
		<echo message="Compiling for target = ${mcu} to libWebbot-${lib}.a" />
		
		<!--  create the directories that are needed -->
		<mkdir dir="./ant/${mcu}"/>
		<mkdir dir="./ant/${mcu}/Sensors"/>
		<mkdir dir="./ant/${mcu}/Sensors/Voltage"/>
		<mkdir dir="./ant/${mcu}/Sensors/Temperature"/>
		<mkdir dir="./ant/${mcu}/Sensors/Light"/>
		<mkdir dir="./ant/${mcu}/Sensors/Humidity"/>
		<mkdir dir="./ant/${mcu}/Sensors/Distance"/>
		<mkdir dir="./ant/${mcu}/Sensors/Compass"/>
		<mkdir dir="./ant/${mcu}/Sensors/Current"/>
		<mkdir dir="./ant/${mcu}/Sensors/Encoder"/>
		<mkdir dir="./ant/${mcu}/Sensors/Acceleration"/>
		<mkdir dir="./ant/${mcu}/Motors"/>
		<mkdir dir="./ant/${mcu}/Motors/Dimension"/>
		<mkdir dir="./ant/${mcu}/Motors/Pololu"/>
		<mkdir dir="./ant/${mcu}/Cameras/Surveyor"/>

		<!--  Compile the c files into o files -->
		<apply executable="avr-gcc" dest="./ant/${mcu}" parallel="false" failonerror="true">
		  <arg line="${compile-lib}"/>
		  <arg value="-Os"/>
		  <arg value="-mmcu=${mcu}"/>
		  <arg value="-c"/>
		  <arg value="-o"/> <targetfile/> <srcfile/>
		  <fileset dir="." includes="**/*.c" excludes="examples/**/*.c"/>
		  <mapper type="glob" from="*.c" to="*.o"/>
		</apply>
		
		<!--  Now add them all to a library -->
		<apply executable="avr-ar"  parallel="true" failonerror="true">
			<arg line ="-r ./libWebbot-${lib}.a"         />
			<fileset dir="./ant/${mcu}" includes="**/*.o"  />
			<mapper type="merge" to="./libWebbot-${lib}.a"/>
		</apply>
		
		<!--  create extended listing -->
		<apply executable="avr-objdump"  parallel="false" failonerror="true">
			<arg line ="-h"/>
		    <arg value="-S"/> <srcfile/>
			<fileset dir="." includes="libWebbot-${lib}.a"  />
			<mapper type="merge" to="./libWebbot-${lib}.lss"/>
			<redirector>
				<outputmapper type="merge" to="./libWebbot-${lib}.lss"/>
			</redirector>
		</apply>
		
	</target>
	
	
	<target name="ATMega8" description="ATMega8 library">
		<antcall target="makeone">
			<param name="mcu" value="atmega8" />
			<param name="lib" value="ATMega8" />
		</antcall>
	</target>

	<target name="ATMega168" description="ATMega168 library">
		<antcall target="makeone">
			<param name="mcu" value="atmega168" />
			<param name="lib" value="ATMega168" />
		</antcall>
	</target>
	
	<target name="ATMega32" description="ATMega32 library">
		<antcall target="makeone">
			<param name="mcu" value="atmega32" />
			<param name="lib" value="ATMega32" />
		</antcall>
	</target>
	
	<target name="ATMega640" description="ATMega640 library">
		<antcall target="makeone">
			<param name="mcu" value="atmega640" />
			<param name="lib" value="ATMega640" />
		</antcall>
	</target>
	
	<target name="ATMega2560" description="ATMega2560 library">
		<antcall target="makeone">
			<param name="mcu" value="atmega2560" />
			<param name="lib" value="ATMega2560" />
		</antcall>
	</target>
	
	
	<target name="build-lib" description="Make all libraries" depends="ATMega8,ATMega168,ATMega32,ATMega640,ATMega2560">
	</target>
	
</project>