<!-- ========================================================================= -->
<!-- Copyright (c) 2003-2005 Contributors.                                     -->
<!-- All rights reserved.                                                      -->
<!-- This program and the accompanying materials are made available            -->
<!-- under the terms of the Eclipse Public License v1.0                        -->
<!-- which accompanies this distribution and is available at                   -->
<!-- http://www.eclipse.org/legal/epl-v10.html                                 -->
<!--                                                                           -->
<!-- Contributors:                                                             -->
<!--     Wes Isberg     initial implementation                                 -->
<!-- ========================================================================= -->

<project name="build" default="all" basedir="..">

    <property name="aspectj.modules.dir" location="${basedir}/.."/>
    
    <import file="${aspectj.modules.dir}/build/build-properties.xml"/>
    <import file="${aspectj.modules.dir}/build/release/release-tests.xml"/>

	<target name="info">
	    <echo message="see build.readme.txt and readme.txt"/>
	</target>
	
    <target name="init" depends="init-release-properties">
	</target>
    
	<target name="init-release-properties" depends="init-properties,init-directories">
		<property name="do.cvs" value="false"/>
	    <property name="run.${min.vm}" value="true"/>
	    <property name="run.14" value="false"/>
	    <property name="run.${max.vm}" value="true"/>
		
		<property name="aspectj.build.props" value=""/>
		<property name="ant.verbose" value=""/>
		<property name="failonerror" value="false"/>
		<property name="harness.jvmline" value="-Dignore=me"/>

		<property name="aspectj.ant.home"
			location="${aspectj.modules.lib.dir}/ant"/>
		<property name="aspectj.ant.lib.dir"
			location="${aspectj.ant.home}/lib"/>

		<property name="harness.jar"
			location="${aj.jar.dir}/testing-drivers-test-all.jar"/>
		
		<property name="java.home.file"
			location="${aj.build.dir}/javahome.props.tmp"/>

		<property name="java13.command"
			location="${java13.home}/bin/java"/>

		<property name="java14.command"
			location="${java14.home}/bin/java"/>
		
		<property name="java15.command"
			location="${java15.home}/bin/java"/>

        <mkdir dir="${aj.logs.dir}"/>
	</target>
	
	<target name="all" depends="init">
	    <build-aspectj vm="${min.vm}" target="clean"/>
	    <save-build-id id="min-vm-${min.vm}"/>
	    <build-aspectj vm="${min.vm}" target="echo-properties"/>
	    <build-aspectj vm="${min.vm}" target="all"/>
	    <!-- ANDY removed... build-aspectj vm="${min.vm}" target="test-each-module"/-->
		<build-aspectj vm="${max.vm}" target="junitreport"/>
		<build-aspectj vm="${max.vm}" target="save-result"/>
	    
	    <build-aspectj vm="${max.vm}" target="clean-keep-results"/>
	    <!-- can't use safety of build-aspectj since deleting output file -->
		<antcall target="clean-default-results"/>
		
	    <save-build-id id="max-vm-${max.vm}"/>
		<build-aspectj vm="${max.vm}" target="echo-properties"/>
	    <build-aspectj vm="${max.vm}" target="all"/>
	    <build-aspectj vm="${max.vm}" target="test-each-module"/>
		<build-aspectj vm="${max.vm}" target="junitreport"/>
		<antcall target="install"/>
		<antcall target="product-tests"/>
		<!-- TODO re-enable antcall target="harness-tests"/ --> 
		<!-- Temporary removal to see if build will work... -->
		<!--build-aspectj vm="13" target="compile-runtime-11"/-->
			
	    <build-aspectj vm="${max.vm}" target="save-result"/>
	    
		<antcall target="fail-if-allresults-allfailures"/>
	</target>
          
	<target name="install" depends="init-release-properties"
		description="install AspectJ distribution using Java min.vm=${min.vm}">
	    <antcall target="init-postBuild-properties"/>
		<require-available property="aspectj.jar"
			path="${aj.dist.dir}/aspectj-${build.version}.jar"/>
		<clean-dir dir="${aj.install.dir}"/>
		<setup-vm vm="${min.vm}"/>
		<property file="${java.home.file}"/>
		<fail unless="build.java.command" 
		     message="failed to read ${build.java.command} from ${java.home.file}"/>
	    <java 
		    fork="true" 
			jvm="${build.java.command}"
			jar="${aspectj.jar}" 
		    failonerror="true">
			<arg line="-to ${aj.install.dir}"/>
		</java>
	</target>
    
    <macrodef name="build-aspectj">
	    <attribute name="target"/>
	    <attribute name="vm"/>
	    <sequential>
	        <setup-vm vm="@{vm}"/>
	        <condition property="do.ant.props" value=" ">
	            <not><isset property="do.ant.props"/></not>
	        </condition>
	        	
			<antcall target="do-run-ant">
				<param name="verbose" value="true"/>
				<param name="ant.dir" location="${aspectj.modules.build.dir}"/>
				<param name="run.ant.file" value="build.xml"/>
				<param name="ant.target" value="@{target}"/>
				<param name="ant.output" 
					location="${aj.logs.dir}/build-@{target}"/>
				<param name="ant.properties" 
				    value="${do.ant.props} ${aspectj.build.props}"/>
			</antcall>
		</sequential>
    </macrodef>
    
	<target name="do-run-ant" depends="init-release-properties"
	 description="the ant we use to build">
		<!-- we could use normal ant, but we enforce using
		     our ant libraries and the specified java runtime
		 -->
		<fail unless="ant.target" message="define ant.target"/>
		<fail unless="run.ant.file" message="define run.ant.file"/>
		<!-- don't fail - set if not defined -->
		<property name="ant.properties" value=""/>

		<property file="${java.home.file}"/>
		<fail unless="build.java.home" 
		     message="failed to read ${build.java.home} from ${java.home.file}"/>
		<fail unless="build.java.version" 
		     message="failed to read ${build.java.version} from ${java.home.file}"/>
		<available property="aspectjrt.path" 
			value="${aj.install.dir}/lib/aspectjrt.jar"
			file="${aj.install.dir}/lib/aspectjrt.jar"/>
		<available property="aspectjrt.path"
			value="${lib.test.aspectjrt.jar}" 
			file="${lib.test.aspectjrt.jar}"/>
		<property name="ant.output.file" 
			location="${ant.output}-${build.java.version}.txt"/>
		<echo>
  do-run-ant dir ${ant.dir}
     file/target -f ${run.ant.file} ${ant.target}
         options ${ant.verbose} ${ant.properties}
          output ${ant.output.file}
     failonerror ${failonerror}
  aspectjrt.path ${aspectjrt.path}
</echo>

		<java 
			fork="true"
			resultproperty="do-run-ant.result"
			failonerror="${failonerror}"
			dir="${ant.dir}"
			jvm="${build.java.command}"
			classname="org.apache.tools.ant.Main"
			maxmemory="350m"
			newenvironment="true"
			output="${ant.output.file}">
			<classpath>
			    <!-- TODO use lib/ant-launcher.jar only? use jdk.tools.jar (version) -->
				<fileset dir="${aspectj.ant.lib.dir}">
					<include name="*.jar"/>
				</fileset>
				<pathelement location="${junit.jar}"/>
				<pathelement location="${build.java.home}/lib/tools.jar"/> 
			</classpath>

			<!-- urk - all properties for any client -->
			<!-- for any build -->
			<sysproperty key="JAVA_HOME" path="${build.java.home}"/>
			<sysproperty key="ANT_HOME" 
				path="${aspectj.ant.home}"/>

			<!-- for ajde -->
			<sysproperty key="aspectjrt.path" file="${aspectjrt.path}"/>

			<!-- for tests/product/build-aspectj/build.xml -->
			<sysproperty key="ASPECTJ_MODULES"
				file="${aspectj.modules.dir}"/>
			<sysproperty key="ASPECTJ_HOME"
				file="${aj.install.dir}"/>
			<arg line="-f ${run.ant.file} ${ant.target} ${ant.verbose} ${ant.properties}"/>
		</java>
		<condition property="do-run-ant.failed" value="true">
			<not><equals arg1="0" arg2="${do-run-ant.result}"/></not>			
		</condition>
		<echo message="dra: do-run-ant.failed=${do-run-ant.failed} do-run-ant.result=${do-run-ant.result}"/>
		<report-if-failed property="do-run-ant.failed"
			text="Build failed: ${ant.output.file}"/>
	</target>
    
    <macrodef name="do-vm-targets">
        <attribute name="target"/>
        <sequential>            
			<do-vm-target target="@{target}" vm="min" />
			<do-vm-target target="@{target}" vm="14" />
	 		<do-vm-target target="@{target}" vm="max" />
	    </sequential>
	</macrodef>
    <macrodef name="do-vm-target">
	    <attribute name="vm"/>
    	<attribute name="target"/>
	    <sequential>
	        <antcall target="do-@{vm}-target">
    			<param name="do.target" value="@{target}"/>
            </antcall>
	    </sequential>
	</macrodef>
    
	<target name="do-13-target"	if="run.13">
		<setup-vm vm="13"/>
		<antcall target="${do.target}"/>
	</target>

	<target name="do-14-target"	if="run.14">
		<setup-vm vm="14"/>
		<antcall target="${do.target}"/>
	</target>

    <target name="do-15-target"	if="run.15">
		<setup-vm vm="15"/>
		<antcall target="${do.target}"/>
	</target>
	
	<target name="do-min-target"	if="run.${min.vm}">
		<setup-vm vm="${min.vm}"/>
		<antcall target="${do.target}"/>
	</target>

	<target name="do-max-target"	if="run.${max.vm}">
		<setup-vm vm="${max.vm}"/>
		<antcall target="${do.target}"/>
	</target>
    <macrodef name="setup-vm">
        <attribute name="vm"/>
        <sequential>            
			<property name="wjf@{vm}.bjh" location="${java@{vm}.home}"/>
			<property name="wjf@{vm}.cmd" location="${wjf@{vm}.bjh}/bin/java"/>
            <delete file="${java.home.file}"/>
			<propertyfile file="${java.home.file}">
				<entry key="build.java.version" value="@{vm}"/>
				<entry key="build.java.home" value="${wjf@{vm}.bjh}"/>
				<entry key="build.java.command" value="${wjf@{vm}.cmd}"/>
			</propertyfile>
        </sequential>
	</macrodef>

</project>