<?xml version="1.0" ?>

<!--

  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You 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 name="flex-compiler-oem" default="main" basedir=".">

    <!-- 

        PROPERTIES

    -->

    <!-- The 'compiler' property is the absolute path, with forward slashes, -->
    <!-- to the 'compiler' directory that contains this file. -->
    <!-- All input paths are expressed as absolute paths starting with ${compiler}. -->
    <pathconvert property="oem" dirsep="/">
        <path location="${basedir}"/>
    </pathconvert>
    <property name="compiler" value="${basedir}/../compiler" />
    
    <!-- The 'env' property contains all the environment variables -->
    <property environment="env"/>
    
    <!-- Properties can be overridden locally by loading a local.properties file -->
    <property file="${compiler}/local.properties"/>
    <property file="${compiler}/../build.properties"/>
    
    <!-- set FLEX_HOME from environment if not already set -->
    <property name="FLEX_HOME" value="${env.FLEX_HOME}"/>

    <!-- The 'sdk' property is the absolute path, with forward slashes, to the compiler directory -->
    <!-- where a Royale SDK is built -->
    <!-- All output paths are expressed as absolute paths starting with ${sdk} -->
    <property name="sdk" value="${compiler}"/>

    <property name="src.depend" value="true"/>

    <!-- Options for <javac> tasks -->
    <property name="javac.debug" value="true"/>
    <property name="javac.deprecation" value="false"/>
    <property name="javac.src" value="1.6"/>

    <!-- JAR manifest entries -->
    <property name="manifest.sealed" value="false"/>
    <property name="manifest.Implementation-Title" value="Apache Flex Compiler"/>
    <property name="manifest.Implementation-Version" value="${release.version}"/>
    <property name="manifest.Implementation-Vendor" value="Apache Software Foundation"/>
	
    <property name="compiler.ant.binaries" value="org/apache/flex/compiler/ant/**/*.class"/>
    <property name="compiler.font.binaries" value="org/apache/flex/fonts/**"/>
            
    <!-- label is set by CruiseControl script based on P4 label incrementer -->
    <condition property="build.number" value="${label}">
        <isset property="label"/>
    </condition>
    
    <!--

        CLASSPATHS

    -->

    <path id="classpath">
        <fileset dir="${sdk}/lib" includes="**/*.jar"/>
        <fileset dir="../compiler-jx/lib" includes="jsc.jar"/>
    </path>


    <!--

        BUILDING

    -->

    <target name="src.depend" if="src.depend">
        <depend srcdir="${oem}/src/main/java"
                destdir="${oem}/target/classes" cache="${oem}/target/classes"/>
    </target>

    <target name="compile" depends="src.depend" description="compile">
        <javac debug="${javac.debug}" deprecation="${javac.deprecation}"
               source="${javac.src}" target="${javac.src}"
        	   includes="**/*.java" destdir="${oem}/target/classes" classpathref="classpath" includeAntRuntime="true">
            <src path="${oem}/src/main/java"/>
        </javac>
        <!--
        <copy todir="${compiler}/generated/classes">
            <fileset dir="${compiler}/src" includes="**/*.properties"/>
        </copy>
         -->
    </target>


    <target name="set.compc.jar.uptodate">
        <uptodate property="compc.jar.uptodate"
                  targetfile="${sdk}/lib/compc.jar">
            <srcfiles dir="${compiler}/target/classes">
                <include name="**/*.class"/>
                <include name="**/*.properties"/>
            </srcfiles>
        </uptodate>
    </target>

    <target name="compc.jar" depends="oem.jar,set.compc.jar.uptodate" unless="compc.jar.uptodate">
        <!-- do we need this
        <mkdir dir="${sdk}/lib"/>
        <jar file="${sdk}/lib/compc.jar" basedir="${compiler}/target/classes" whenmanifestonly="create">
            <include name="META-INF/LICENSE"/>
            <include name="META-INF/NOTICE"/>
            <manifest>
                <attribute name="Sealed" value="${manifest.sealed}"/>
                <attribute name="Implementation-Title" value="${manifest.Implementation-Title} - COMPC Command Line Compiler"/>
                <attribute name="Implementation-Version" value="${manifest.Implementation-Version}.${build.number}"/>
                <attribute name="Implementation-Vendor" value="${manifest.Implementation-Vendor}"/>
                <attribute name="Main-Class" value="flex2.tools.Compc"/>
                <attribute name="Class-Path" value="flex-compiler-oem.jar swfutils.jar"/>
            </manifest>
        </jar>
         -->
    </target>
		
	<target name="set.oem.jar.uptodate">
		<uptodate property="oem.jar.uptodate"
		          targetfile="${sdk}/lib/flex-compiler-oem.jar">
			<srcfiles dir="${oem}/target/classes">
				<include name="**/*.class"/>
				<include name="**/*.properties"/>
			</srcfiles>
		</uptodate>
	</target>
	
	<target name="oem.jar" depends="compile, set.oem.jar.uptodate" unless="oem.jar.uptodate"
		    description="Builds flex-oem-compiler.jar">
        <mkdir dir="${sdk}/lib"/>
        <mkdir dir="${oem}/target/classes/META-INF"/>
        <copy file="${basedir}/../LICENSE.base" tofile="${oem}/target/classes/META-INF/LICENSE"/>
        <copy file="${basedir}/../NOTICE.oem" tofile="${oem}/target/classes/META-INF/NOTICE"/>
        <jar file="${sdk}/lib/flex-compiler-oem.jar" basedir="${oem}/target/classes" includes="**/*.properties,**/*">
            <include name="META-INF/LICENSE"/>
            <include name="META-INF/NOTICE"/>
            <manifest>
                <attribute name="Sealed" value="${manifest.sealed}"/>
                <attribute name="Implementation-Title" value="${manifest.Implementation-Title}"/>
                <attribute name="Implementation-Version" value="${manifest.Implementation-Version}.${build.number}"/>
                <attribute name="Implementation-Vendor" value="${manifest.Implementation-Vendor}"/>
                <attribute name="Main-Class" value="flex2.tools.Mxmlc"/>
                <attribute name="Class-Path" value="compiler.jar swfutils.jar ../js/lib/jsc.jar"/>
            </manifest>
        </jar>
	</target>
	
    <target name="jar" depends="oem.jar, compc.jar"
    	    description="Creates JAR files"/>
	    	
	<target name="sdk" depends="jar" description="Builds a Royale SDK"/>

    <target name="main" depends="sdk" description="Default target - Builds a Royale SDK and builds Royale Javadoc">
        <tstamp>
            <format property="build.datetime" pattern="MM/dd/yyyy hh:mm:ss aa"/>
        </tstamp>
        <echo>flex-compiler-oem main completed on ${build.datetime}</echo>
    </target>

    <!--

        CLEANUP

    -->

    <target name="clean" description="clean">
        <delete dir="${oem}/target/classes"/>
    </target>

    <target name="wipe" depends="clean" >
        <delete dir="${oem}/target"/>
    </target>

    <target name="fail-if-not-found">
        <fail message="${destDir}/${destFile} could not be downloaded or found in cache">
            <condition>
                <not>
                    <available file="${destDir}/${destFile}" />
                </not>
            </condition>
        </fail>
    </target>

    <target name="double-check-file" >
        <echo>${env.FLEX_DOWNLOAD_CACHE}</echo>
        <condition property="still-no-file" value="true">
            <and>
                <not>
                    <available file="${destDir}/${destFile}" />
                </not>
                <isset property="env.FLEX_DOWNLOAD_CACHE" />
            </and>
        </condition>
        <echo>Need file: ${still_no_file}</echo>
    </target>
    <target name="get-from-cache-if-needed" depends="double-check-file" if="still-no-file">
        <copy file="${env.FLEX_DOWNLOAD_CACHE}/${srcFile}" tofile="${destDir}/${destFile}" />
    </target>

</project>
