<?xml version="1.0" encoding="UTF-8"?>
<!-- 
============================================================================ 
Name        : rombuild.ant.xml 
Part of     : Helium 

Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
This component and the accompanying materials are made available
under the terms of the License "Eclipse Public License v1.0"
which accompanies this distribution, and is available
at the URL "http://www.eclipse.org/legal/epl-v10.html".

Initial Contributors:
Nokia Corporation - initial contribution.

Contributors:

Description:

============================================================================
-->
<project name="rombuild" xmlns:hlm="http://www.nokia.com/helium">
    <description>
        ROM image creation using iMaker.
        Flash config file generation.
    </description>
    <import file="./trace_images/tracebuild.ant.xml" />
    <import file="imaker.ant.xml" />

    <!-- should be overriden - default configuration -->
    <!-- <property name="rombuild.makefile.name" value="mc_imaker_conf.mk" /> -->
    <property name="imaker.command" location="${build.drive}\epoc32\tools\imaker.cmd" />
    <property name="ee.makefile.target" value="ee_roms" />
    <property name="subcon.makefile.target" value="subcon_roms" />
    <property name="trace.makefile.target" value="trace_roms" />
    <property name="ee_roms.log.file" value="${build.log.dir}/${build.id}_ee_roms.log" />
    <property name="trace.log.file" value="${build.log.dir}/${build.id}_trace_roms.log" />
    <property name="subcon.log.file" value="${build.log.dir}/${build.id}_subcon_roms.log" />

    <!-- Properties for image-creation target which is being used by IDOs -->
    <property name="image_creation.copyto" value="${build.output.dir}/images" />
    <property name="image_creation.drive" value="${build.drive}" />
    <!-- Property for variant-image-creation -->
    <property name="variant.image.targetpath" value="${build.output.dir}/variant_images" />

    <!-- That macro runs iMaker using product the configuration generated by Helium. -->
    <macrodef name="rombuildImakerMacro" uri="http://www.nokia.com/helium">
        <attribute name="product" />
        <attribute name="target" />
        <sequential>
            <var name="rom.product.path" unset="true" />
            <if>
                <istrue value="${rombuild.using.var}" />
                <then>
                    <script language="jython" setbeans="false">
                        <![CDATA[
import imaker
import os
if project.getProperty('build.drive') == None :
    raise Exception("'build.drive' property is not defined")
os.chdir(project.getProperty('build.drive') + os.sep)
project.setProperty('rom.product.path', imaker.get_product_dir(r'@{product}'))
                        ]]>
                    </script>
                </then>
                <else>
                    <exec executable="python" outputproperty="rom.product.path" dir="${build.drive}/" failonerror="${failonerror}">
                        <arg line="${helium.dir}/tools/localisation/get_product_path.py @{product}" />
                    </exec>
                    <exec executable="python" outputproperty="rom.product.platform" dir="${build.drive}/" failonerror="${failonerror}">
                        <arg line="${helium.dir}/tools/localisation/get_product_platform.py @{product}" />
                    </exec>
                </else>
            </if>
            <if>
                <and>
                    <equals arg1="${build.system}" arg2="ec-helium" />
                    <isset property="rom.dryrun" />
                </and>
                <then>
                    <echo message="target to run @{target}-dryrun" />
                    <exec executable="${imaker.command}" dir="${build.drive}/" output="${build.drive}/dryrun_out.txt" append="true" failonerror="${failonerror}">
                        <arg line="-p@{product} -f /epoc32/rom/${rom.product.path}/${rombuild.makefile.name}" />
                        <arg value="@{target}-dryrun" />
                    </exec>
                </then>
                <else>
                    <if>
                        <istrue value="${rombuild.using.var}" />
                        <then>
                            <exec executable="${imaker.command}" dir="${build.drive}/" failonerror="${failonerror}">
                                <arg line="-p@{product} -f /epoc32/rom/${rom.product.path}/${rombuild.makefile.name}" />
                                <arg value="@{target}" />
                            </exec>
                        </then>
                        <else>
                            <exec executable="${imaker.command}" dir="${build.drive}/" failonerror="${failonerror}">
                                <arg line="-p@{product} -c${rom.product.platform} -f /epoc32/rom/${rom.product.path}/${rombuild.makefile.name}" />
                                <arg value="@{target}" />
                            </exec>
                        </else>
                    </if>
                </else>
            </if>
        </sequential>
    </macrodef>


    <!-- Create engineering english image using ImageCreation.pl. -->
    <target name="image-creation" if="image_creation.copyto">
        <tstamp>
            <format property="image.archive.date" pattern="ddmmyy'_'HHmmss" />
        </tstamp>

        <move todir="${build.drive}/archives/images/${image.archive.date}" failonerror="false">
            <fileset dir="${build.output.dir}/images/" />
        </move>
        <mkdir dir="${build.output.dir}/images" />
        <exec executable="perl" dir="${build.drive}/" failonerror="true">
            <arg value="${helium.dir}/tools/compile/ImageCreation.pl" />
            <arg value="-copyto" />
            <arg value="${image_creation.copyto}" />
            <arg value="-MyTraces" />
            <arg value="${image_creation.MyTraces}" />
            <arg value="-Drive" />
            <arg value="${image_creation.drive}" />
            <arg value="-product" />
            <arg value="${image_creation.product}" />
            <arg value="-type" />
            <arg value="${image_creation.type}" />
        </exec>
    </target>

    <!-- Create variant image using Creator.pl tools.
    -->
    <target name="variant-image-creation" if="variant.image.hw">
        <tstamp>
            <format property="image.archive.date" pattern="ddmmyy'_'HHmmss" />
        </tstamp>

        <move todir="${build.drive}/archives/variant_images/${image.archive.date}" failonerror="false">
            <fileset dir="${build.output.dir}/variant_images/" />
        </move>
        <mkdir dir="${build.output.dir}/variant_images" />
        <exec executable="perl" dir="${build.drive}/" failonerror="true">
            <arg value="${build.drive}/Creator.pl" />
            <arg value="-hw" />
            <arg value="${variant.image.hw}" />
            <arg value="-lang" />
            <arg value="${variant.image.lang}" />
            <arg value="-core" />
            <arg value="${variant.image.core}" />
            <arg value="-variant" />
            <arg value="${variant.image.variantcode}" />
            <arg value="-type" />
            <arg value="${variant.image.type}" />
            <arg value="-sw" />
            <arg value="${variant.image.sw}" />
            <arg value="-targetpath" />
            <arg value="${variant.image.targetpath}" />
        </exec>
    </target>

    <!-- This targets call the image-creation target to be reexecuted in a build.
        <deprecated>Should be done diferrently, e.g use imaker in new </deprecated>
    -->
    <target name="rerun-image-creation">
        <runtarget target="image-creation" />
    </target>

    <!-- This targets call the variant-image-creation target to be reexecuted in a build.
        <deprecated>Should be done diferrently, e.g use imaker in new </deprecated>
    -->
    <target name="rerun-variant-image-creation">
        <runtarget target="variant-image-creation" />
    </target>

    <!-- Preparing rom image configuration expanding properties, flattening the xinclude stucture
             and finally expanding properties again. 
    -->
    <target name="rombuild-prepare-input" depends="init-build-area">
        <property name="rombuild.config.file.parsed" location="${build.output.dir}/rom_config.xml.parsed" />
        <copy file="${rombuild.config.file}" tofile="${rombuild.config.file.parsed}.x1" overwrite="true">
            <filterchain>
                <expandproperties />
                <tokenfilter>
                    <replacestring from="http://www.w3.org/2003/XInclude" to="http://www.w3.org/2001/XInclude" />
                </tokenfilter>
            </filterchain>
        </copy>
        <!--<xcluder in="${rombuild.config.file.parsed}.x1" out="${rombuild.config.file.parsed}.xinclude" />-->
        <xinclude file="${rombuild.config.file.parsed}.x1" tofile="${rombuild.config.file.parsed}.xinclude" />
        <xmltask source="${rombuild.config.file.parsed}.xinclude" dest="${rombuild.config.file.parsed}.x2">
            <attr path="//*" attr="xml:base" remove="true" />
        </xmltask>
        <copy file="${rombuild.config.file.parsed}.x2" tofile="${rombuild.config.file.parsed}" overwrite="true">
            <filterchain>
                <expandproperties />
            </filterchain>
        </copy>
    </target>


    <!-- Creates ee-images
     -->
    <target name="ee-roms" depends="rombuild-create-makefile" unless="no.ncp">
        <hlm:startSpecificLogMacro name="${ee_roms.log.file}" />
        <hlm:preECImakerMacro />
        <for list="${product.list}" delimiter="," param="product.name">
            <sequential>
                <hlm:rombuildImakerMacro product="@{product.name}" target="${ee.makefile.target}" />
            </sequential>
        </for>
        <hlm:ecImakerMacro makefile.target="${ee.makefile.target}" />
        <hlm:stopSpecificLogMacro name="${ee_roms.log.file}" />
        <hlm:metadatarecord database="${metadata.dbfile}">
            <hlm:textmetadatainput>
                <fileset casesensitive="false" file="${ee_roms.log.file}" />
                <metadatafilterset refid="filterset.ee.roms" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>
        <hlm:generateBuildStatus file="${build.id}_ee_roms.log" />
        <!-- Todo: metadata: assertions for metadata parsing here -->
        <hlm:publishMacro />
    </target>


    <!-- Creates subcon certified rom images with imaker tool -->
    <target name="subcon-roms" depends="rombuild-create-makefile" unless="no.ncp">
        <hlm:startSpecificLogMacro name="${subcon.log.file}" />
        <mkdir dir="${subcon.build.drive}/output/development_flash_images/engineering_english/${product.name}/subcon" />
        <hlm:preECImakerMacro />
        <for list="${product.list}" delimiter="," param="product.name">
            <sequential>
                <hlm:rombuildImakerMacro product="@{product.name}" target="${subcon.makefile.target}" />
            </sequential>
        </for>
        <hlm:ecImakerMacro makefile.target="${ee.makefile.target}" />
        <hlm:stopSpecificLogMacro name="${subcon.log.file}" />
        <hlm:metadatarecord database="${metadata.dbfile}">
            <hlm:textmetadatainput>
                <fileset casesensitive="false" file= "${subcon.log.file}"/>
                <metadatafilterset refid="filterset.subcon.roms" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>
        <hlm:generateBuildStatus file="${build.id}_subcon_roms.log" />
        <!-- Todo: metadata: insert assertions for metadata parsing here -->
        <hlm:publishMacro />
    </target>

    <!-- Private: Internal target to export iMaker Helium additions. -->
    <target name="rombuild-export-features">
        <copy todir="${build.drive}/epoc32/rom/config" overwrite="true">
            <fileset dir="${helium.dir}/tools/rombuild/features" casesensitive="false">
                <include name="*.mk" />
                <include name="unittest/*.mk" />
            </fileset>
        </copy>
    </target>

    <!-- iMaker related targets -->
    <target name="rombuild-create-makefile" depends="rombuild-export-features,rombuild-prepare-input">
        <!-- Just generate the makefile for all the product available in product list. -->
        <hlm:python>
import os

import configuration
import rom

os.chdir(r'${build.drive}/')
configBuilder = configuration.NestedConfigurationBuilder(open(r'${rombuild.config.file.parsed}', 'r'))
configSet = configBuilder.getConfiguration()
for product in r'${product.list}'.split(','):
    builder = rom.IMakerRomBuilder(configSet, product, (r'${rombuild.using.var}'.lower() == "true"))
    builder.build()
        </hlm:python>
    </target>




    <!-- Creates trace rom images -->
    <target name="trace-roms" depends="rombuild-create-makefile" unless="no.ncp">
        <hlm:startSpecificLogMacro name="${trace.log.file}" />
        <hlm:preECImakerMacro />
        <for list="${product.list}" delimiter="," param="product.name">
            <sequential>
                <hlm:rombuildImakerMacro product="@{product.name}" target="${trace.makefile.target}" />
            </sequential>
        </for>
        <hlm:ecImakerMacro makefile.target="${ee.makefile.target}" />
        <hlm:stopSpecificLogMacro name="${trace.log.file}" />
        <!-- Todo: Make sure that logfilter is added to the filterset -->
        <hlm:metadatarecord database="${metadata.dbfile}">
            <hlm:textmetadatainput>
                <fileset casesensitive="false" file="${trace.log.file}" />
                <metadatafilterset refid="filterset.trace.rom" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>
        <hlm:generateBuildStatus file="${build.id}_trace_roms.log" />
        <!-- Todo: metadata: insert assertions for metadata parsing here -->
        <hlm:publishMacro />
    </target>

    <!-- Single product configuration support
        <deprecated>Please consider using new iMaker integration.</deprecated>
    -->
    <target name="ee-roms-product" depends="rombuild-create-makefile" unless="no.ncp">
        <sequential>
            <hlm:preECImakerMacro />
            <hlm:rombuildImakerMacro product="${product.name}" target="${ee.makefile.target}" />
            <hlm:ecImakerMacro makefile.target="${ee.makefile.target}" />
        </sequential>
    </target>

    <!-- Used for parallel rom image creation using dry run approach -->
    <macrodef name="preECImakerMacro" uri="http://www.nokia.com/helium">
        <sequential>
            <delete file="${build.drive}/dryrun_out.txt" failonerror="false" />
        </sequential>
    </macrodef>

    <!-- Target to run imaker in parallel rom image creation for S60 3.2 based products -->
    <macrodef name="ecImakerMacro" uri="http://www.nokia.com/helium">
        <attribute name="makefile.target" />
        <sequential>
            <if>
                <and>
                    <equals arg1="${build.system}" arg2="ec-helium" />
                    <isset property="rom.dryrun" />
                </and>
                <then>
                    <exec executable="python" dir="${build.drive}/" failonerror="${failonerror}">
                        <arg line="${helium.dir}/tools/rombuild/dryrun_parser.py ${build.drive}/dryrun_out.txt ${build.drive}/@{makefile.target}.make @{makefile.target}" />
                    </exec>
                    <antcall target="compile-ec-helium">
                        <param name="skip.compile-ec-patch" value="yes" />
                        <param name="sysdef.configuration" value="@{makefile.target}" />
                        <param name="ec.makefile.file" location="${build.drive}/@{makefile.target}.make" />
                    </antcall>
                </then>
            </if>
        </sequential>
    </macrodef>

    <!--Private: Internal iMaker macro. -->
    <macrodef name="internalImakerMacro" uri="http://www.nokia.com/helium">
        <attribute name="product" />
        <attribute name="target" />
        <attribute name="makefile" default="" />
        <attribute name="cmd" default="" />
        <sequential>
            <var name="rom.product.path" unset="true" />
            <script language="jython" setbeans="false">
                <![CDATA[
import imaker
import os
os.chdir(project.getProperty('build.drive') + os.sep)
project.setProperty('rom.product.path', imaker.get_product_dir(r'@{product}'))
              ]]>
            </script>
            <if>
                <equals arg1="@{makefile}" arg2="" />
                <then>
                    <var name="var.makefile" value="" />
                </then>
                <else>
                    <var name="var.makefile" value="-f ${rom.product.path}/@{makefile}" />
                </else>
            </if>
            <exec executable="${imaker.command}" dir="${build.drive}" failonerror="${failonerror}">
                <arg line="-p@{product} @{cmd}" />
                <arg line="${var.makefile}" />
                <arg line="@{cmd}" />
                <arg value="@{target}" />
            </exec>
        </sequential>
    </macrodef>

    <!-- Timestamp used in flash config file generation -->
    <tstamp>
        <format property="fota.release.date" pattern="dd-MM-yy" />
    </tstamp>

    <!-- Create flash configration files -->
    <target name="flash-config-file" if="flash.config.enabled">
        <runtarget target="rombuild-prepare-input" />
        <runtarget target="rombuild-create-makefile" />

        <property name="flash.config.makefile.target" value="flash_config" />
        <property name="flash.config.makefile.name" value="mc_flash_config.mk" />
        <hlm:python>
import configuration
import flash_config
import os
os.chdir(r'${build.drive}/')
configBuilder = configuration.NestedConfigurationBuilder(open(r'${rombuild.config.file.parsed}', 'r'))
configSet = configBuilder.getConfiguration()
for product in r'${product.list}'.split(','):
    writer = flash_config.FlashConfigurationWriter(configSet, product)
    writer.write()
    </hlm:python>
        <for list="${product.list}" delimiter="," param="product.name">
            <sequential>
                <hlm:internalImakerMacro product="@{product.name}" target="${flash.config.makefile.target}" makefile="${flash.config.makefile.name}" />
            </sequential>
        </for>
    </target>

    <!-- The target ido-iby-copy copies the iby files from ido.src.root to ido.romtree
        Two properties that define from where the files are copied from and whre to. Defauls are 
        for "ido.src.tree" default value is "${build.drive}${ido.src.root}"  
        for "ido.romtree" default value is "${build.drive}${env.EPOCROOT}/epoc32/rom/include"  .
         The IDO should define the ido.src.root and ido.romtree to override the defauls  
         <deprecated>IDO must now move to the new sysdef structure and use ido-copy-iby.</deprecated>
    -->
    <target name="ido-iby-copy">
        <property name="ido.src.tree" value="${build.drive}${ido.src.root}" />
        <property name="ido.romtree" value="${build.drive}${env.EPOCROOT}/epoc32/rom/include" />
        <echo>New files</echo>
        <!-- the files are in rom folder -->
        <copy todir="${ido.romtree}" verbose="true" flatten="true">
            <fileset dir="${ido.src.tree}">
                <exclude name="**/internal/**" />
                <exclude name="**/tsrc/**" />
                <include name="**/rom/*.iby" />
            </fileset>
        </copy>
        <echo>Old files</echo>

        <copy todir="${ido.romtree}" verbose="true" flatten="true" overwrite="true">
            <fileset dir="${ido.src.tree}">
                <exclude name="**/internal/**" />
                <exclude name="**/tsrc/**" />
                <include name="**/rom/*.iby" />
            </fileset>
        </copy>
    </target>


    <!-- Two properties that define from where the files are copied and what platform is used.
         The IDO should define the ido.src.root and ido.cenrep.platform 
         The properties ido.cenrep.root and ido.cenrep.target mey be overriden by the user.
         <deprecated>IDO must now move to the new sysdef structure and use ido-create-cenrep.</deprecated>
     -->
    <target name="ido-cenrep" if="ido.cenrep.platform">
        <property name="ido.src.tree" value="${build.drive}${ido.src.root}" />
        <property name="ido.cenrep.root" value="${build.drive}${env.EPOCROOT}/epoc32/tools/cenrep" />
        <property name="ido.cenrep.target" value="${build.drive}${env.EPOCROOT}/epoc32/data/z/private/10202be9" />
        <echo>Generating cenrep files</echo>

        <copy todir="${ido.cenrep.root}" verbose="true" flatten="true">
            <fileset dir="${ido.src.tree}">
                <include name="**/cenrep/keys_*.xls" />
            </fileset>
        </copy>

        <!-- in cenrep dir execut 'generate_cenrep_inifile.pl -r %s -d .' % platform -->
        <exec executable="perl" dir="${ido.cenrep.root}" failonerror="true">
            <arg value="${ido.cenrep.root}/generate_cenrep_inifile.pl" />
            <arg value="-r" />
            <arg value="${ido.cenrep.platform}" />
            <arg value="-d" />
            <arg value="." />
        </exec>
        <!-- Copy generated files to target path -->
        <copy todir="${ido.cenrep.target}" verbose="true" flatten="true">
            <fileset dir="${ido.cenrep.root}">
                <include name="????????.txt" />
            </fileset>
        </copy>

    </target>

    <!--
        Implement a solution to create build information for imaker.
    -->
    <target name="rombuild-imaker-create-buildinfo">
        <property name="rombuild.buildinfo.template" location="${helium.dir}/tools/common/templates/imaker/image_conf_buildinfo.mk.ftl" />
        <property name="rombuild.buildinfo.output" location="${build.drive}${env.EPOCROOT}epoc32/rom/config/image_conf_buildinfo.mk" />
        <fmpp sourceFile="${rombuild.buildinfo.template}" outputFile="${rombuild.buildinfo.output}">
            <data expandProperties="yes">
                ant: antProperties()
            </data>
        </fmpp>
    </target>

</project>
