<?xml version="1.0" encoding="utf-8" ?> 
<!--
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-->
<project default="all" basedir="." name="C_Code">
<!-- Copyright (c) 2006, Intel Corporation -->
<!-- Filename: Tools/Source/build.xml -->

  <taskdef resource="net/sf/antcontrib/antlib.xml"/>

  <property name="ReallyVerbose" value="false"/>
  <property environment="env" />

  <property name="WORKSPACE" value="${env.WORKSPACE}" />
  <property name="WORKSPACE_DIR" value="${WORKSPACE}" />
  <property name="PACKAGE" value="Tools" />
  <property name="PACKAGE_DIR" value="${WORKSPACE}/Tools/CCode/Source" />
  <property name="LIB_DIR" value="${PACKAGE_DIR}/Library" />
  <property name="BIN_DIR" value="${WORKSPACE}/Tools/bin" />
  <property name="BUILD_MODE" value="PACKAGE" />
  <property name="Libs" 
    value="Common/build.xml CustomizedCompress/build.xml PeCoffLoader/build.xml String/build.xml"/>

  <import file="${WORKSPACE_DIR}/Tools/Conf/BuildMacro.xml" />

  <path id="classpath">
    <fileset dir="${WORKSPACE}/Tools/Jars" includes="*.jar"/>
    <fileset dir="${env.XMLBEANS_HOME}/lib" includes="*.jar"/>
  </path>

  <taskdef classpathref="classpath" resource="net/sf/antcontrib/antlib.xml" />


  <target name="all" depends="initArch, init, Tools">
    <echo message="The EDK II C Tools build complete!"/>
  </target>
  
  <target name="init">
    <echo message="EDK C Code Tools, build initialization"/>
    <taskdef classpathref="classpath" resource="GenBuild.tasks" />
    <taskdef classpathref="classpath" resource="cpptasks.tasks"/>
    <typedef classpathref="classpath" resource="cpptasks.types"/>
    <mkdir dir="${BIN_DIR}" />
    <mkdir dir="${LIB_DIR}" />

  </target>

  <target name="initArch">
    <condition property="HostArch" value="X64">
      <os arch="amd64"/>
    </condition>
    <condition property="HostArch" value="Ia32">
      <or>
        <os arch="x86"/>
        <os arch="i386"/>
      </or>
    </condition>
    <condition property="HostArch" value="Ia32">
      <os arch="i386"/>
    </condition> 

    <if>
      <os family="unix" />
      <then>
        <echo message="OS Family UNIX, ${HostArch}" />
      </then>
      <elseif>
        <os family="dos" />
        <then>
          <echo message="OS Family DOS, ${HostArch}" />
        </then>
      </elseif>
      <elseif>
        <os family="mac" />
        <then>
          <echo message="OS Family OS X, ${HostArch}" />
        </then>
      </elseif>
      <else>
        <fail message="OS Family Unsupported, ${HostArch}" />
      </else>
    </if>

    <if>
      <not>
        <isset property="ToolChain" />
      </not>
      <then>
        <if>
          <isset property="env.TOOL_CHAIN" />
          <then>
            <property name="ToolChain" value="${env.TOOL_CHAIN}"/>
          </then>
          <else>
            <!-- Default Tool Chain is Microsoft Visual Studio -->
            <property name="ToolChain" value="msvc"/>
          </else>
        </if>
      </then>
    </if>
    
    <if>
      <equals arg1="${ToolChain}" arg2="gcc" />
      <then>
        <exec executable="gcc" outputproperty="host.gcc.ver">
          <arg line="-E" />
          <arg line="-P" />
          <arg line="-x c" />
          <arg line="gcc.ver" />
        </exec>
        <if>
          <contains string="${host.gcc.ver}" substring="4" />
          <then>
            <property name="ExtraArgus" value="-Wno-pointer-sign" />
          </then>
        </if>
      </then>
    </if>

    <condition property="linux" value="true">
      <os name="Linux"/>
    </condition>
    
    <condition property="intel_win">
      <and>
        <os family="dos"/>
        <equals arg1="${ToolChain}" arg2="intel"/>
      </and>
    </condition>
    
    <condition property="intel_linux">
      <and>
        <os name="Linux"/>
        <equals arg1="${ToolChain}" arg2="intel"/>
      </and>
    </condition>
    
    <condition property="intel_mac">
      <and>
        <os family="mac"/>
        <equals arg1="${ToolChain}" arg2="intel"/>
      </and>
    </condition>
    
    <condition property="gcc">
      <and>
        <equals arg1="${ToolChain}" arg2="gcc"/>
      </and>
    </condition>

    <condition property="cygwin">
      <and>
        <os family="dos"/>
        <equals arg1="${ToolChain}" arg2="gcc"/>
      </and>
    </condition>
    
    <condition property="x86_64_linux">
      <and>
        <os name="Linux"/>
        <equals arg1="${HostArch}" arg2="X64"/>
      </and>
    </condition>
    
    <condition property="windows" value="true">
      <os family="Windows"/>
    </condition>
    
    <condition property="OSX" value="true">
      <os family="Mac"/>
    </condition>

    <condition property="cyglinux">
      <or>
        <istrue value="${linux}"/>
        <istrue value="${cygwin}"/>
      </or>
    </condition>

    <!-- msft is a family, used by both Microsoft and Intel Windows compiler tool chains -->
    <condition property="msft">
      <isfalse value="${gcc}"/>
    </condition>

    <if>
      <istrue value="${ReallyVerbose}"/>
      <then>
        <echo message="Test property msvc:         ${msvc}"/> 
        <echo message="Test property gcc:          ${gcc}"/> 
        <echo message="Test property intel_win:    ${intel_win}"/> 
        <echo message="Test property intel_linux:  ${intel_linux}"/> 
        <echo message="Test property intel_mac:    ${intel_mac}"/> 
        <echo message="Test property msft:         ${msft}"/> 
        <echo message="Test property cygwin:       ${cygwin}"/> 
        <echo message="Test property cyglinux:     ${cyglinux}"/> 
        <echo message="Test property windows:      ${windows}"/> 
        <echo message="Test property linux:        ${linux}"/> 
        <echo message="Test property OSX:          ${OSX}"/> 
        <echo message="Test property x86_64_linux: ${x86_64_linux}"/> 
       </then>
    </if>

    <property name="haveLibtool" value="false"/>
    <if>
      <and>
        <not>
          <isset property="env.CYGWIN_HOME"/>
        </not>
          <isset property="cygwin"/>
      </and>
      <then>
        <fail message="You must set the environment variable: CYGWIN_HOME"/>
      </then>
    </if>

    <if>
        <istrue value="${gcc}"/>
      <then>
        <property name="ext_static" value=".a"/>
        <property name="ext_dynamic" value=".so"/>
        <if>
          <istrue value="${cygwin}"/>
          <then>
            <property name="ext_exe" value=".exe"/>
          </then>
          <else>
            <property name="ext_exe" value=""/>
          </else>
        </if>
      </then>
      <else>
        <property name="ext_static" value=".lib"/>
        <property name="ext_dynamic" value=".dll"/>
        <property name="ext_exe" value=".exe"/>
      </else>
    </if>
  </target>

  <target name="Libraries" depends="initArch, init">
    <subant target="" inheritall="true">
      <fileset dir="${PACKAGE_DIR}" 
        includes="${Libs}"/>
    </subant>
  </target>

  <target name="Tools" depends="Libraries">
    <subant target="" inheritall="true">
      <fileset dir="${PACKAGE_DIR}" includes="*/build.xml" 
        excludes="${Libs}"/>
    </subant>
  </target>

  <target name="clean" depends="initArch">
    <subant target="clean" inheritall="true">
      <fileset dir="${PACKAGE_DIR}" includes="*/build.xml"/>
    </subant>
  </target>

  <target name="cleanall" depends="initArch">
    <subant target="cleanall" inheritall="true">
      <fileset dir="${PACKAGE_DIR}" includes="*/build.xml"/>
    </subant>
    <delete dir="${LIB_DIR}"/>
  </target>
</project>
