<?xml version="1.0" encoding="UTF-8"?>
<project name="SF-COMMON-CONFIG" xmlns:hlm="http://www.nokia.com/helium">

    <property environment="env"/> <!-- make environment variables available via env -->
    
    <dirname property="sf.common.config.dir" file="${ant.file.SF-COMMON-CONFIG}"/>
    
    <!-- Import common properties -->
    <import file="${sf.common.config.dir}/common_props.ant.xml" />
    
    <!-- setup Helium internal properties from their equivalent in the project spec -->
    <property name="build.name" value="${sf.spec.job.name}"/>
    <property name="core.build.version" value="${sf.spec.job.codeline}"/>
    <property name="build.number" value="${sf.spec.job.number}"/>
    <property name="build.drive" value="${sf.spec.job.drive}"/>
    <property name="email.from" value="${sf.spec.email.from}"/>
    <property name="local.free.space" value="${sf.spec.job.freespace}"/>
    <property name="network.free.space" value="${sf.spec.publish.network.freespace}"/>
    <property name="network.drive" value="${sf.spec.publish.networkdrive}"/>
    <property name="prep.root.dir" value="${sf.spec.job.rootdir}"/>
    <property name="publish" value="${sf.spec.publish.enable}"/>
    <property name="publish.root.dir" value="${sf.spec.publish.rootdir}"/>
    <property name="diamonds.host" value="${sf.spec.publish.diamonds.server}"/>
    <property name="diamonds.port" value="${sf.spec.publish.diamonds.port}"/>
    <property name="diamonds.path" value="${sf.spec.publish.diamonds.path}"/>
    <property name="build.family" value="${sf.spec.job.name}"/>
    <property name="build.system" value="${sf.spec.build.system}"/>
    <property name="base_release.path" value="${sf.spec.baseline.location}"/>
    <property name="base_release.getenv_options" value="${sf.spec.baseline.getenv_options}"/>
    <property name="ccm.user.password" value="set_to_any_value_to_skip_scm_checks__for_helium_5"/>
	<property name="ats.drive" value="${sf.spec.ats_worker.drive}"/>
    
    <if>
      <isset property="sf.spec.sbs.numberofjobs"/>
      <then>
        <property name="number.of.threads" value="${sf.spec.sbs.numberofjobs}"/>
      </then>
    </if>
    
    <property name="sf.diamonds.tags" value="_${sf.spec.job.codeline},${sf.spec.publish.diamonds.tag}"/>
    
    <!-- SF-specific Helium properties not meant to be exposed in the project spec -->
    <property name="diamonds.listener.configuration.file" location="${sf.common.config.dir}/diamonds/config.xml.ftl"/>
    
    <!-- Import all core HELIUM targets -->
    <import file="${helium.dir}/helium.ant.xml" />
    
    <!-- Import common references -->
    <import file="${sf.common.config.dir}/common_refs.ant.xml" />
             
    <!-- Import functionality distributed into other file(s) -->
    <import file="${sf.common.config.dir}/build.postbuild.xml"/>
	
	<!-- Import test functionality distributed into other file(s) -->
    <import file="${sf.common.config.dir}/build.test.xml"/>
             
    <!-- setup conditional Helium internal properties -->
    <if><istrue value="${sf.spec.sourcesync.usecache}"/>
        <then>
            <property name="sf.spec.sourcesync.sourcespecdir" value="${temp.build.dir}"/>
        </then>
        <else>
            <property name="sf.spec.sourcesync.sourcespecdir" value="${sf.project.location}"/>
        </else>
    </if>
    
    <!-- conditional import of generated source spec if available -->
    <if><available  file="${temp.build.dir}/source-spec.ant.xml" />
        <then>
            <echo message="Generated source spec found, importing..." />
            <import file="${temp.build.dir}/source-spec.ant.xml"/>
        </then>
    </if>
    
    <target name="sf-prep" depends="prep-drive,init-build-area,check-tool-dependencies,create-bom,log-build-env">
        <echo>[SF-PREP]</echo>
        <!-- Test for the disk space we would like for this build -->
	<fail>
            <condition>
                <not>
                    <or>
                        <isset property="sf.suppress.drive.space.check"/>
                        <hasfreespace partition="${build.drive}" needed="${sf.drive.space.needed}"/>
                    </or>
                </not>
            </condition>
Insufficient space to run this build to completion. (Was looking for ${sf.drive.space.needed}.) To suppress this check, set the property sf.suppress.drive.space.check to any value.
        </fail>

        <tempfile property="prep.dynamic.config" suffix="ant.xml" destdir="${temp.build.dir}"/>

        <if>          
            <not><isset property="sf.suppress.buildenv.check"/></not>
            <then>
                <echo message="Checking build Environment. To suppress this check set property sf.suppress.buildenv.check to any value"/>
                <runtarget target="sf-check-env"/>
            </then>
        </if>
    </target>
        
    <target name="sf-check-env">
        <!-- Conditional location of BuildEnv.xml. Project always preferred to default -->
        <if><available file="${sf.project.location}/BuildEnv.xml"/>        
            <then>
                <property name="sf.buildenv.location" value="${sf.project.location}"/>
            </then>
            <else>
                <property name="sf.buildenv.location" value="${sf.common.config.dir}/tools"/>
            </else>
        </if>
        
        <echo message="Build Env against ${sf.buildenv.location}/BuildEnv.xml"/>
        <exec executable="perl" dir="${sf.common.config.dir}/tools" resultproperty="sf.checkenv.exit">
            <arg value="CheckBuildEnv.pl"/>
            <arg value="--xml=${sf.buildenv.location}/BuildEnv.xml"/>
            <arg value="--dbg=${build.log.dir}/BuildEnvironmentCheck.log"/>
            <arg value="--sbs='${sf.spec.sbs.config}'"/>
            <arg value="--tools='${sf.spec.sbs.tools.config}'"/>
            <arg value="Common"/>
        </exec>
        <antcall target="sf-log-to-brag">
            <param name="sf.brag.phase" value="Prebuild"/>
            <param name="sf.brag.step" value="Build Environment Check"/>
            <param name="sf.brag.log" value="${build.log.dir}/BuildEnvironmentCheck.log"/>
            <param name="sf.brag.id" value="BuildEnvironmentCheck"/>
        </antcall>
        <fail>
            <condition>
                <not>
                    <or>
                        <equals arg1="${sf.checkenv.exit}" arg2="0"/>
                        <isset property="sf.suppress.buildenv.check"/>
                    </or>
                </not>
            </condition>
Build Environment Check failed (${sf.checkenv.exit}). To stop this failure aborting the build set property sf.suppress.buildenv.check to any value.
        </fail>
    </target>

    <target name="sf-summary" depends="">
        <echo>[SF-SUMMARY]</echo>
        <echo message="Generating build summary"/>
        <exec executable="perl" dir="${sf.common.config.dir}/tools/summary" failonerror="true">
          <arg value="brag_script.pl"/>
          <arg value="--buildid=${sf.spec.job.name}_${sf.spec.job.codeline}.${sf.spec.job.number}"/>
          <arg value="--basedir=${sf.spec.job.rootdir}"/>
        </exec>
    </target>
    
    <target name="sf-build-all" depends="sf-prebuild-noprep,sf-build-noprep,sf-postbuild">
        <echo>[SF-BUILD-ALL]</echo>
    </target>
    
    <target name="sf-build-and-pack" depends="sf-build-noprep,sf-postbuild">
        <echo>[SF-BUILD-AND-PACK]</echo>
    </target>

    <target name="sf-build" depends="sf-prebuild,sf-build-noprep,sf-postbuild">
        <echo>[SF-BUILD]</echo>
    </target>
    
    <target name="sf-generate-source-spec">
        <!-- Generate the sources.csv if hg cache is activated -->
        <if><istrue value="${sf.spec.sourcesync.usecache}"/>
            <then>
                <echo message="Generating Hg local cache..." />
                <exec executable="perl" dir="${build.log.dir}" failonerror="true" output="${build.log.dir}/${build.id}_hgcache.log">
                    <arg value="${sf.common.config.dir}/tools/cachefiles.pl"/>
                    <arg value="${sf.spec.sourcesync.cachelocation}"/>
                    <arg value="${sf.project.location}/${sf.spec.sourcesync.sourcespecfile}"/>
                    <arg value="${temp.build.dir}/${sf.spec.sourcesync.sourcespecfile}"/>
                </exec>
            </then>
        </if>

        <!-- TODO: 1. Same file name souce-spec.ant.xml is used for all packages
        for multiple package builds, this needs to be linked with package name. -->
        <!-- TODO: 2. Change fmpp data to be a full property rather than relative path -->
        <delete file="${temp.build.dir}/source-spec.ant.xml"/>
        
        <fmpp sourceFile="${sf.common.config.dir}/templates/source-spec.ant.xml.ftl"
              outputFile="${temp.build.dir}/source-spec.ant.xml">
              <data expandProperties="yes">
                    ant: antProperties()
                    data: csv(${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}, {separator:','})
              </data>
        </fmpp>
    </target>
    
    <target name="generate-layers">
       <echo message="canno-file:${canonical.sysdef.file}"/>
       <echo message="raptor-filters:raptor_${sysdef.configuration}"/>

       <!-- All we want is a sysdef with the config name appended, so just copy it -->
       <echo message="INFO: Skip GenXML and copy sysdef to sysdef+config name"/>
       <copy file="${canonical.sysdef.file}" tofile="${build.drive}/output/build/canonical_system_definition_${sysdef.configuration}_temp.xml"/>

        <copy file="${build.drive}/output/build/canonical_system_definition_${sysdef.configuration}_temp.xml" tofile="${build.drive}/output/build/canonical_system_definition_${sysdef.configuration}.xml">
        <filterchain>
            <linecontainsregexp negate="true">
                <regexp pattern="^\s*$"/>
            </linecontainsregexp>
        </filterchain>
        </copy>
        <copy file="${build.drive}/output/build/canonical_system_definition_${sysdef.configuration}.xml" todir="${build.log.dir}/BOM/"/>
        <delete file="${build.drive}/output/build/canonical_system_definition_${sysdef.configuration}_temp.xml"/>
    </target>

    <target name="sf-prebuild" depends="sf-prep,sf-prebuild-noprep"/>
    <target name="sf-prebuild-noprep" depends="sf-prebuild-announce,sf-diamonds-connect,sf-diamonds-tag-build,sf-getenvs,sf-syncsource,sf-diamondize-bom"/>
    
    <target name="sf-prebuild-announce">
        <echo>[SF-PREBUILD]</echo>
    </target>
    
    <target name="sf-diamonds-connect">
      <if>
        <istrue value="${sf.spec.publish.enable}"/>
        <then>
          <echo>[SF-DIAMONDS-CONNECT]</echo>
          <runtarget target="diamonds"/>
          <echo message="Save Diamonds build id value (${diamonds.build.id}) in a file"/>
          <echo message="diamonds.build.id=${diamonds.build.id}" file="${build.log.dir}/diamonds_build_id.properties"/>
        </then>
      </if>
    </target>

    <target name="sf-diamonds-tag-build">
      <if>
        <istrue value="${sf.spec.publish.enable}"/>
        <then>
          <echo>[SF-DIAMONDS-TAG-BUILD]</echo>
          <echo message="Generate ${build.drive}/output/logs/sf-diamonds-tag-build.xml with Diamonds tags for this build"/>
          <exec executable="perl" dir="${sf.common.config.dir}/tools" failonerror="true">
            <arg value="${sf.common.config.dir}/tools/generate_diamonds_tags_xml.pl"/>
            <arg value="--tags=${sf.diamonds.tags}"/>
            <arg value="--output=${build.drive}/output/logs/sf-diamonds-tag-build.xml"/>
          </exec>
        </then>
      </if>
    </target>
    
    <target name="sf-record-proj-conf-bom">
        <!-- create BOM dir -->
        <mkdir dir="${build.drive}/output/logs/BOM"/>
        
        <!-- record config and project information in BOM files -->
        <exec executable="hg" dir="${sf.config.dir}" outputproperty="sf.job.bom.config.repo">
            <arg value="showconfig"/>
            <arg value="paths.default"/>
        </exec>
        <propertyregex input="${sf.job.bom.config.repo}" regexp="\\" replace="/" global="true" property="sf.job.bom.config.repo" override="true"/>
        <exec executable="hg" dir="${sf.config.dir}" outputproperty="sf.job.bom.config.checksum">
            <arg value="identify"/>
            <arg value="-i"/>
        </exec>
        <echo message="${sf.job.bom.config.repo},sf-config,${sf.job.bom.config.checksum}" file="${build.drive}/output/logs/BOM/config.csv"/>

        <exec executable="hg" dir="${sf.project.location}" outputproperty="sf.job.bom.project.repo">
            <arg value="showconfig"/>
            <arg value="paths.default"/>
        </exec>
        <propertyregex input="${sf.job.bom.project.repo}" regexp="\\" replace="/" global="true" property="sf.job.bom.project.repo" override="true"/>
        <exec executable="hg" dir="${sf.project.location}" outputproperty="sf.job.bom.project.checksum">
            <arg value="identify"/>
            <arg value="-i"/>
        </exec>
	<echo message="${sf.job.bom.project.repo},build/config,${sf.job.bom.project.checksum}" file="${build.drive}/output/logs/BOM/project.csv"/>
    </target>

    <target name="sf-getenvs">
        <!-- create BOM dir -->
        <mkdir dir="${build.drive}/output/logs/BOM"/>
        
        <!-- tools baseline must come before epoc baseline to allow clean listing -->
        <touch file="${build.drive}/output/logs/BOM/toolsbaseline.txt"/>
        <if>
            <istrue value="${sf.spec.toolsbaseline.enable}" />
            <then>

                <!-- record toolsbaseline information in BOM file -->
                <echo message="${sf.spec.toolsbaseline.location}" file="${build.drive}/output/logs/BOM/toolsbaseline.txt"/>

                <!-- wrapper around preparation-getenv but with diff params -->
                <runtarget target="sf-getenv-tools"/>
                
                <echo message="INFO Getting tools environment listing"/>
                <exec executable="perl" dir="${build.log.dir}/" failonerror="true" output="${build.log.dir}/listdir_${build.id}_toolsbaseline.log">
                    <arg value="${sf.common.config.dir}/tools/listdir.pl"/>
                    <arg value="${build.drive}/epoc32"/>
                </exec>
            </then>
        </if>        

        <touch file="${build.drive}/output/logs/BOM/baseline.txt"/>
        <if>
            <istrue value="${sf.spec.baseline.enable}" />
            <then>
                <!-- record baseline information in BOM file -->
                <echo message="${sf.spec.baseline.location}" file="${build.drive}/output/logs/BOM/baseline.txt"/>
                
                <!-- run internal target preparation-getenv -->
                <runtarget target="preparation-getenv"/>
            </then>
        </if>
    </target>

    <target name="sf-getenv-tools">
        <antcall target="preparation-getenv" inheritAll="false">
            <param name="base_release.path"           value="${sf.spec.toolsbaseline.location}"/>
            <param name="base_release.getenv_options" value="${sf.spec.toolsbaseline.getenv_options}"/>
        </antcall>
    </target>

    <target name="sf-syncsource">
        <touch file="${build.log.dir}/BOM/sources.csv"/> 
        <if>
            <istrue value="${sf.spec.sourcesync.enable}" />
            <then>
                <runtarget target="sf-get-source"/>                
                <runtarget target="sf-package-source"/>                
                <runtarget target="sf-unpack-rnd"/>
            </then>
        </if>        
    </target>

    <!-- generate dir list using passed location and name 
    if a baseline list is available then generate deltas too -->

    <target name="sf-list-dir">
        <property name="sf.currentlist.name"   value="${sf.list.name}"/> 
        <property name="sf.dir.location" value="${build.drive}/epoc32"/> 
        <property name="sf.dir.exclude" value="${build.drive}/epoc32/build"/> 
    
        <if>
            <istrue value="${sf.spec.dirlist.enable}"/>
            <then>
                <echo message="Dirlist name: ${sf.currentlist.name} requested for ${sf.dir.location} excluding ${sf.dir.exclude}"/>
                <exec executable="python" dir="${build.log.dir}/" failonerror="true" output="${build.log.dir}/listdir_${build.id}_${sf.currentlist.name}.log">
                    <arg value="${sf.common.config.dir}/tools/listdir.py"/>
                    <arg value="${sf.dir.location}"/>
                    <arg value="${sf.dir.exclude}"/>
                </exec>
            </then>
        </if>
    </target>
    
    <target name="sf-delta-dir">
        <property name="sf.currentlist_a.name"   value="${sf.list_a.name}"/> 
        <property name="sf.currentlist_b.name"   value="${sf.list_b.name}"/>
        <property name="sf.dir.location" value="${build.drive}/epoc32"/> 
        
        <if>
            <istrue value="${sf.spec.dirdelta.enable}"/>
            <then>
                <echo message="Delta requested for ${sf.currentlist_a.name} vs ${sf.currentlist_b.name} "/>
                <exec executable="perl" dir="${build.log.dir}/" failonerror="true" output="${build.log.dir}/listdir_${build.id}_${sf.currentlist_b.name}_delta.log">
                    <arg value="${sf.common.config.dir}/tools/difflist.pl"/>
                    <arg value="${build.log.dir}/listdir_${build.id}_${sf.currentlist_a.name}.log"/>
                    <arg value="${build.log.dir}/listdir_${build.id}_${sf.currentlist_b.name}.log"/>
                </exec>
            </then>
        </if>
    </target>
  
    <target name="sf-intersect-dir">
        <property name="sf.currentlist_a.name"   value="${sf.list_a.name}"/> 
        <property name="sf.currentlist_b.name"   value="${sf.list_b.name}"/>
        <property name="sf.dir.location" value="${build.drive}/epoc32"/> 
    
        <if>
            <istrue value="${sf.spec.dirdelta.enable}"/>
            <then>
                <echo message="Inersection requested for ${sf.currentlist_a.name} vs ${sf.currentlist_b.name} "/>
                <exec executable="perl" dir="${build.log.dir}/" failonerror="true" output="${build.log.dir}/listdir_${build.id}_${sf.currentlist_b.name}_intersect.log">
                    <arg value="${sf.common.config.dir}/tools/difflist.pl"/>
                    <arg value="${build.log.dir}/listdir_${build.id}_${sf.currentlist_a.name}.log"/>
                    <arg value="${build.log.dir}/listdir_${build.id}_${sf.currentlist_b.name}.log"/>
                    <arg value="-I"/> <!-- run difflist in intersect mode -->
                </exec>
            </then>
        </if>
    </target>

    <!-- This target is overridden in platform builds, but used directly in package builds -->
    <target name="sf-build-noprep" depends="sf-truclean,sf-model-from-project,sf-compile,sf-postbuild">
        <echo>[SF-BUILD-NOPREP]</echo>
    </target>
    
    <target name="create-canonical-sysdef-file">
      <copy file="${build.drive}/${sf.spec.systemdefinition.location}" tofile="${canonical.sysdef.file}" failonerror="true" verbose="true"/>
    </target>
    
  <target name="sf-truclean">
    <if>
      <istrue value="${sf.spec.build.clean.enable}"/>
      <then>
        <echo message="Executing truclean step."/>
        <fmpp sourceFile="${sf.common.config.dir}/templates/truclean.ant.xml.ftl"
            outputFile="${temp.build.dir}/truclean.ant.xml">
          <data expandProperties="yes">
              ant: antProperties()
              data: csv(${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}, {separator:','})
          </data>
        </fmpp>
        <ant antfile="${temp.build.dir}/truclean.ant.xml"/>
      </then>
      <else>
        <echo message="Skipping truclean step."/>
      </else>
    </if>
  </target>
  
  <target name="sf-model-from-project">
    <if>
      <isset property="sf.spec.systemdefinition.source"/>
      <then>
        <echo message="Using ${sf.spec.systemdefinition.source} as model file"/>
        <echo message="Copying to ${build.drive}/definition.xml"/>
        <copy file="${sf.project.location}/${sf.spec.systemdefinition.source}" tofile="${build.drive}${sf.spec.systemdefinition.location}"/>
      </then>
      <else>
        <!-- Collate the system definition from the packages listed in sources.csv -->
        <echo message="Building system model from ${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}"/>
        <exec executable="perl" dir="${build.drive}/" output="${build.drive}${sf.spec.systemdefinition.location}" logError="true" failonerror="true">
          <arg value="${sf.common.config.dir}/tools/csvToSysDef.pl"/>
          <arg value="${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}"/>
          <arg value="${sf.config.dir}/../build/config/${sf.spec.job.codeline}"/>
        </exec>
      </else>
    </if>
  </target>
    
    <target name="sf-get-source" depends="sf-generate-source-spec">
        <ant antfile="${temp.build.dir}/source-spec.ant.xml" />
    </target>
    
    <target name="sf-diamondize-bom" depends="sf-record-proj-conf-bom">
        <fmpp sourceFile="${sf.common.config.dir}/templates/build-info.xml.ftl"
              outputFile="${build.drive}/output/logs/sf-diamondize-bom.xml">
            <data expandProperties="yes">
                ant: antProperties()
                config: csv(${build.drive}/output/logs/BOM/config.csv,{separator:',',headers:[loc,dst,rev]})
                project: csv(${build.drive}/output/logs/BOM/project.csv,{separator:',',headers:[loc,dst,rev]})
                baseline: slicedText(${build.drive}/output/logs/BOM/baseline.txt,{trim})
                toolsbaseline: slicedText(${build.drive}/output/logs/BOM/toolsbaseline.txt,{trim})
                sources: csv(${build.drive}/output/logs/BOM/sources.csv,{separator:',',headers:[loc,dst,type,rev,sysdef]})              
            </data>
        </fmpp>
        <!-- Make a copy in BOM dir -->
        <copy file="${build.drive}/output/logs/sf-diamondize-bom.xml" tofile="${build.log.dir}/BOM/build-info.xml"/>
    </target>

    <target name="sf-preprocess-package-config">
	<echo message="${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}"/>
        <exec executable="perl" dir="${build.drive}/" failonerror="false" output="${build.log.dir}/zipconfig.log">
            <arg value="${sf.common.config.dir}/tools/populateziptemplate.pl"/>
            <arg value="${sf.spec.sourcesync.sourcespecdir}/${sf.spec.sourcesync.sourcespecfile}"/>
            <arg value="${sf.common.config.dir}/templates/zip.cfg.xml.ftl.template"/>
            <arg value="${temp.build.dir}/zip.cfg.xml.ftl"/>
            <arg value="${build.log.dir}/rnd_excludefile.txt"/>
        </exec>
        <antcall target="sf-log-to-brag">
            <param name="sf.brag.phase" value="Prebuild"/>
            <param name="sf.brag.step" value="Preprocess package config"/>
            <param name="sf.brag.log" value="${build.log.dir}/zipconfig.log"/>
            <param name="sf.brag.id" value="PreprocessPackageConfig"/>
        </antcall>
    </target>
    
    <target name="sf-zip-content">
        <!-- If it's the file we use for everything, and it's not up to date... -->
        <if>
          <and>
            <equals arg1="${zip.config.file}" arg2="${temp.build.dir}/zip.cfg.xml.ftl"/>
            <not>
              <and>
                <uptodate targetfile="${temp.build.dir}/zip.cfg.xml.ftl">
                  <srcfiles dir="${sf.common.config.dir}">
                    <include name="tools/populateziptemplate.pl"/>
                    <include name="templates/zip.cfg.xml.ftl.template"/>
                  </srcfiles>
                </uptodate>
                <uptodate targetfile="${temp.build.dir}/zip.cfg.xml.ftl">
                  <srcfiles dir="${sf.spec.sourcesync.sourcespecdir}">
                    <include name="${sf.spec.sourcesync.sourcespecfile}"/>
                  </srcfiles>
                </uptodate>
              </and>
            </not>
          </and>
          <then>
            <!-- (Re)build the packaging config file -->      
            <runtarget target="sf-preprocess-package-config"/>
          </then>  
        </if>
        <if><available file="${zip.config.file}"/>
          <then>
            <runtarget target="preprocess-zip-config" />
            <property name="zip.${zip.target.name}.log.file" location="${build.log.dir}/${build.id}_${zip.target.name}_zip.log" />
            <property name="zips.${zip.target.name}.spec.name" value="${zip.target.name}" />
            <hlm:zipContentMacro type="${zip.target.name}" file="${zip.config.file}" />
          </then>
          <else>
            <echo message="ERROR ${zip.config.file} not available for zipping!"/>
          </else>
        </if>
    </target>
    
    <target name="sf-package-source" >
        <!-- Firstly zip up rnd by package -->
        <antcall target="sf-zip-content">
            <param name="zip.config.file" value="${temp.build.dir}/zip.cfg.xml.ftl"/>
            <param name="zip.target.name" value="rnd" />
        </antcall>
        <antcall target="sf-zip-content">
            <param name="zip.config.file" value="${temp.build.dir}/zip.cfg.xml.ftl"/>
            <param name="zip.target.name" value="rnd-internal" />
        </antcall>
        <if>
            <istrue value="${sf.spec.package.src.enable}"/>
            <then>
                <echo message="INFO Packaging Source Repos"/>
                <antcall target="sf-zip-content">
                    <param name="zip.config.file" value="${temp.build.dir}/zip.cfg.xml.ftl"/>
                    <param name="zip.target.name" value="src" />
                </antcall>
                <!-- Then zip up src zips by layer -->
                <antcall target="sf-zip-content">
                    <param name="zip.config.file" value="${temp.build.dir}/zip.cfg.xml.ftl"/>
                    <param name="zip.target.name" value="src-by-layer" />
                </antcall>
            </then>
        </if>
    </target>
    
    <!-- Unpack the rnd zips ready to be used in the build -->
    <target name="sf-unpack-rnd">
        <echo message="Unpacking any available RnD binaries"/>
        <for param="rndZip">
            <fileset dir="${build.drive}/output/zips/">
                <include name="binaries_*.zip"/> <!-- Internal rnd bins -->
                <include name="release/bin_*.zip"/> <!-- Releaseable rnd bins -->
            </fileset>
            <sequential>
                <exec executable="7za" dir="${build.drive}" output="${build.log.dir}/unzip_${build.id}_binaries_rnd.log" append="true">
                    <arg value="x"/>
                    <arg value="-y"/> <!-- Need to unpack in overwrite mode, due to TEF -->
                    <arg value="-i!epoc32\*"/> <!-- Only unzip items into the epoc32 tree -->
                    <arg value="@{rndZip}"/>
                </exec>
            </sequential>
	</for>
    </target>

  <target name="sf-compile-options">  
    <hlm:sbsMakeOptions engine="gmake" id="sbs.fullbuild.options"/>
    <hlm:sbsMakeOptions engine="gmake" id="sbs.toolsbuild.options"/>
  </target>
    
    <!-- Turn a log file into a BRAG XML file - parameters to be passed are sf.brag.* -->
    <target name="sf-log-to-brag">
        <mkdir dir="${build.log.dir}/summary"/>
        <echo message="Generating ${sf.brag.id}_BRAG.xml from ${sf.brag.log}"/>
        <exec executable="perl" failonerror="false" output="${build.log.dir}/summary/${sf.brag.id}_BRAG.xml" logError="true">
            <arg value="${sf.common.config.dir}/tools/brag/logToBRAG.pl"/>
	    <arg value="--phase=${sf.brag.phase}"/>
            <arg value="--step=${sf.brag.step}"/>
            <arg value="--rules=${sf.common.config.dir}/tools/brag/rules.${sf.brag.id}.tsv"/>
            <arg value="${sf.brag.log}"/>
        </exec>
        <!-- And copy the XSL so the output can be transformed by a browser -->
        <copy file="${sf.common.config.dir}/tools/brag/brag.xsl" toDir="${build.log.dir}/summary/"/>
    </target>
       

    <target name="sf-rombuild">
      <!-- TODO If ANT gets a better way to do conditions based on value then use it instead! -->
      <property name="sf.rombuild.dir" value="${build.drive}/sf/os/kernelhwsrv/kernel/eka/rombuild"/>
      
      <for list="${sf.spec.sbs.config}" delimiter="," param="sf.spec.sbs.config.variant">
          <sequential>
          <propertyregex property="udeb" input="@{sf.spec.sbs.config.variant}" regexp="(armv5$|armv5\.|armv5_udeb)" select="\1"/>
          <propertyregex property="urel" input="@{sf.spec.sbs.config.variant}" regexp="(armv5$|armv5\.|armv5_urel)" select="\1"/>
          </sequential>
      </for> 
      
      <condition property="sf.spec.sbs.config.expanded" value="udeb">
      <and><isset property="udeb"/><not><isset property="urel"/></not></and>
      </condition>
      
      <condition property="sf.spec.sbs.config.expanded" value="urel">
      <and><isset property="urel"/><not><isset property="udeb"/></not></and>
      </condition>
      
      <condition property="sf.spec.sbs.config.expanded" value="udeb,urel">
      <and><isset property="udeb"/><isset property="urel"/></and>
      </condition>
      
      <if><isset property="sf.spec.sbs.config.expanded"/>
      <then>
          <echo message="INFO Found armv5, building ${sf.spec.sbs.config.expanded}"/>      
          
          <for list="${sf.spec.referenceroms.list}" delimiter="," param="sf.spec.referenceroms.variant">
              <sequential>
              <for list="${sf.spec.sbs.config.expanded}" delimiter="," param="sf.spec.sbs.config.expanded.i">
                  <sequential>
                      <echo message="INFO Building textshell ROM variant @{sf.spec.sbs.config.expanded.i} @{sf.spec.referenceroms.variant}"/>
                      <exec executable="cmd" dir="${sf.rombuild.dir}" failonerror="false" 
                      output="${build.log.dir}/ROM_@{sf.spec.referenceroms.variant}_@{sf.spec.sbs.config.expanded.i}_out.log" logError="true">
                          <arg value="/c"/>
                          <arg value="rom --variant=@{sf.spec.referenceroms.variant} --inst=armv5 --build=@{sf.spec.sbs.config.expanded.i} --type=tshell"/>
                      </exec>
                      
                      <if><available  file="${sf.rombuild.dir}/ROMBUILD.LOG"/>
                      <then>
                          <move file="${sf.rombuild.dir}/ROMBUILD.LOG" tofile="${build.log.dir}/ROMBUILD_@{sf.spec.referenceroms.variant}_@{sf.spec.sbs.config.expanded.i}.log"/>
                      </then>
                      <else>
                          <echo message="ERROR Failed to build @{sf.spec.sbs.config.expanded.i} ROM for @{sf.spec.referenceroms.variant} variant"/>
                      </else>
                      </if>
                  </sequential>
             </for>
             </sequential>
          </for>
          
          <move todir="${build.drive}/epoc32/rom/"  verbose="true" failonerror="false">
            <fileset dir="${sf.rombuild.dir}">
              <include name="**/*.IMG"/>
            </fileset>
          </move>
      </then>
      <else><echo message="INFO ROM Build requested but no valid build variants defined by sf.spec.sbs.config. Skipping."/></else>
      </if>
    </target>

</project>

