<?xml version="1.0" encoding="UTF-8"?>
<!-- 
============================================================================ 
Name        : common.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:

============================================================================
-->
<!--* 
Common targets.
        
@package framework
    
-->
<project name="common" xmlns:hlm="http://www.nokia.com/helium" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
    <description>
        Common targets for all Helium tools.
    </description>
    
    <!--=======================================================================
    Preset definitions -->
    <presetdef name="preset.exec">
        <exec>
            <!-- Note the extra ':' required at the start of the value. This
            is required for the Symbian tools to match the env variable. -->
            <env key="ARMV5VER" value=":${arm.compiler.version}"/>

            <!-- Tools configuration -->
            <env key="LOGONSERVER" value="${logon.server}"/>
        </exec>
    </presetdef>
    
    
    <presetdef name="preset.mail">
        <mail from="${email.from}" messagemimetype="text/html" mailhost="${email.smtp.server}" failonerror="false"/>
    </presetdef>
    
    
    <!-- Outputs diagnostics information for the Helium configuration.
    
    This contains:
    * The current configuration of properties.
    * The Ant diagnostics information.
    -->
    <target name="diagnostics" description="Print diagnostics about Helium">
        <echo>
-------- Helium diagnostics report --------
Helium version ${helium.version}
            
-------------------------------------------
 Helium configuration
-------------------------------------------
        </echo>
        <runtarget target="config"/>
        <diagnostics/>
        <echo>
            
Java properties
---------------
        </echo>
        <script language="beanshell">
            properties = System.getProperties();
            properties.list(System.out);
        </script>
    </target>
    
    <!-- Sets the properties that are dependent on build.number -property. 
    
    The properties are:
    * build.version
    * build.id
    * prep.build.dir
    * publish.dir
    * publish.release.dir
     -->
    <target name="build-number">
        <fail unless="build.number" message="build.number property not defined" />
        <!-- Version of the build.
        @type string
        @scope private
        -->
        <property name="build.version" value="${core.build.version}.${build.number}"/>
        <!-- A unique ID for the build.
        @type string
        @scope private
        -->
        <property name="build.id" value="${build.name}_${build.version}"/>
        <!-- This is the directory where the build area is prepared. Once created it is substed to the build.drive.
        @type string
        @scope private
        -->
        <property name="prep.build.dir" location="${prep.root.dir}/${build.id}"/>
        <!-- Directory where the build is published (includes build.id)
        @type string
        -->
        <property name="publish.dir" location="${publish.root.dir}/${build.name}/builds/${core.build.version}/${build.id}"/>
        <!-- The directory where the release of this build is published to on the local network.
        @type string
        @scope private
        -->
        <property name="publish.release.dir" location="${publish.root.dir}/${build.name}/releases/${core.build.version}/${build.id}"/>
    </target>

    <!-- Variable set to configuration tool. 
        Currently supported configuration tool arguments are
        argument        :    ant variable name
        
        -master_conf    :    master_conf
        -confml         :    confml
        -impl            :    impl
        -iby            :    iby
        -ignore_errors    :    keepgoing(true - uses -ignore_errors, otherwise not, setting 
                            true generates cenrep incase of errors, and signals has to be configured to stop the build
                            in case of errors).
    -->    
    <hlm:argSet id="cnftool.refid">
        <hlm:arg name="path" value="${build.drive}/s60/tools/toolsextensions/configurationtool" />
        <hlm:arg name="master_conf" value="s60.confml" />
        <hlm:arg name="confml" value="\epoc32\rom\config\confml_data\s60" />
        <hlm:arg name="impl" value="\epoc32\rom\config\confml_data\s60" />
        <hlm:arg name="iby" value="\epoc32\rom\include\" />
        <hlm:arg name="keepgoing" value="false" />
    </hlm:argSet>

    <!-- Target to run configtool. See cnftool.refid for config tool parameters-->
    <target name="configtool" description="target to run configtool, refid is for mcl, overridden by changing cnftool.refid">
        <hlm:toolMacro name="configuration">
            <hlm:toolvarset refid="cnftool.refid"/>
        </hlm:toolMacro>
    </target>

    <!-- Finds the build manager's (current user) email address from their username. -->
    <target name="lookup-email" >
        <if>
            <isset property="email.ldap.server"/>
            <then>
                <if>
                    <not>
                        <isset property="email.from"/>
                    </not>
                    <then>
                        <hlm:ldap url="${email.ldap.server}" rootdn="${email.ldap.rootdn}" filter="uid=${env.USERNAME}" outputproperty="email.from" key="mail" failonerror="false"/>
                    </then>
                </if>
            </then>   
        </if>        
    </target>
    
    <!-- To authenticate the noe password (read from .netrc file) for ${env.USERNAME}. -->
    <target name="authenticate-user" if="authenticate.noe.user">
        <runtarget target="noe-password"/>

        <hlm:ldapauthenticate   url="${email.ldap.server}" 
                                rootdn="${email.ldap.rootdn}" 
                                searchdn="${ldap.organization.unit.rootdn}, ${ldap.people.rootdn}" 
                                filter="uid=${env.USERNAME}" 
                                outputproperty="is.authentication.sucess" 
                                key="employeeNumber"
                                password="${noe.password}"
                                />
        <if>
            <istrue value="${is.authentication.sucess}" />
            <then>
                <echo>noe authentication for user ${env.USERNAME} is success.</echo>              
            </then>
            <else>
                <fail message="noe authentication for user ${env.USERNAME} is failed."/>
            </else>
        </if>
    </target>
    
    <!-- Utility target to test mail sending from Helium. -->
    <target name="check-mail" depends="lookup-email">
        <preset.mail tolist="${email.from}" subject="mail-test"/>
    </target>

    <!-- A simple test target that prints a simple message -->
    <target name="hello">
        <echo message="Hello!"/>
        <if>
            <isset property="build.number"/>
            <then>
                <echo message="Ant libs found OK"/>
            </then>
        </if>
    </target>
    
    
    <!-- A simple test target that prints a simple message and is dependant upon another target to show how ANT works-->
    <target name="hi" depends="hello"/>
    
    
    <!-- A simple test target that prints a simple message -->
    <target name="fail">
        <fail message="Test build failure."/>
    </target>
    
    
    <!-- Print out the build properties -->
    <target name="config" description="Print out the build properties">
        <echoproperties>
            <propertyset negate="true">
                <propertyref prefix="java."/>
                <propertyref prefix="sun."/>
                <propertyref prefix="awt."/>
                <propertyset refid="password.list.ref"/>
            </propertyset>
        </echoproperties>
    </target>

    <!-- Prints out target dependencies.
    
    A <tt>target</tt> property should be defined on the command line to specify which target's dependencies to analyse.

    Example: <tt>hlm -Dtarget=compile-main deps</tt>
    -->
    <target name="deps">
        <fail unless="target" message="target property not defined, example: hlm -Dtarget=compile-main deps" />
        <hlm:dependencies target="${target}" format="nested"/>
    </target>
    
    
    <!-- Displays target dependencies in a text box. 
    
    A <tt>target</tt> property should be defined on the command line to specify which target's dependencies to analyse. -->
    <target name="execlist">
        <fail unless="target" message="target property not defined" />
        <hlm:record name="execlist.txt" action="start" emacsmode="true"/>
        <hlm:dependencies target="${target}" format="executable"/>
        <hlm:record name="execlist.txt" action="stop"/>
        <exec executable="notepad.exe">
            <arg value="execlist.txt"/>
        </exec>
        <delete file="execlist.txt"/>
    </target>
    
    
    <!-- Prints out Helium help dialog -->
    <target name="help">
        <if>
            <not>
                <available file="${database.file}"/>
            </not>
            <then>
                <antcall target="database"/>
            </then>
        </if>
        <if>
            <isset property="help.item"/>
            <then>
                <fmpp sourcefile="${helium.dir}/tools/common/templates/help_text.txt.ftl" outputfile="build/help_text.txt" quiet="true">
                    <data expandProperties="yes">
                        xml: xml(${database.file})
                        helpItem: ${help.item}
                    </data>
                </fmpp>
                <loadfile srcfile="build/help_text.txt" property="help.text"/>
                <echo>${help.text}</echo>                
            </then>
            <else>
                <echo>
Usage:
hlm [target] [-D&lt;property>=&lt;value>] [-f &lt;ant_build_file>] [-h] [-p -v]

[target]                        Run Ant target
[-D&lt;property>=&lt;value>]          Set an Ant property
[-f &lt;ant_build_file>]           Use another Ant build file
[-h]                            Print Ant help text
[-p -v]                         List all Ant targets

Variable properties for helium:
-Dsysdef.configuration=default set build configuration, default value is 'default'
-Dbuild.system=ebs             set build system, default value is 'ebs'
                                  - possible values are 'ebs' and 'ec'

Usage examples:
hlm                            build the default build target
hlm -Dbuild.system=ec-helium   use electric cloud build system
                </echo>                
            </else>
        </if>
    </target>
        
    
    <!-- Automates deletion of old work areas.
    
    <tt>prep.root.dir</tt> is the path where work areas are typically stored. This command
    scans that directory for sub-directories that match a pattern based on the build name.
    A dialog is displayed listing the directories as checkboxes. Check each build area 
    directory to delete it.
    -->
    <target name="clean-pc">
        <dirset id="prep.build.dirs" dir="${prep.root.dir}">
            <include name="*_*"/>
            <include name="subcon_*_*"/>
        </dirset>
        <pathconvert pathsep="," property="prep.build.dirs.path">
            <dirset refid="prep.build.dirs"/>
        </pathconvert>
        <fmpp sourceFile="${helium.dir}/tools/common/templates/clean_pc.ant.ftl"
              outputFile="${cache.dir}/clean_pc.ant.xml">
            <data expandProperties="yes">
                prepRootDir: ${prep.root.dir}
                buildAreaDirs: [${prep.build.dirs.path}]
            </data>
        </fmpp>
        <ant antfile="${cache.dir}/clean_pc.ant.xml"/>
        <delete file="${cache.dir}/clean_pc.ant.xml"/>
    </target>
    
    
    <!-- Displays the current version of Helium -->
    <target name="version" description="Displays the current version of Helium">
        <echo message="Helium version: ${helium.version}" />
    </target>
        
    <!-- Checks the Ant configuration against a Helium data model.
    @deprecated Implementation of the feature has changed, and the target is not needed anymore. Instead use antlint target
    -->
    <target name="check">
        <hlm:antlint>
            <fileset id="antlint.files" dir="${helium.dir}/tools">
                <include name="**/*.ant.xml"/>
                <include name="**/build.xml"/>
                <include name="**/*.antlib.xml"/>
                <exclude name="tests/**"/>
                <exclude name="**/test/test_*.xml"/>
                <exclude name="external/**"/>
                <exclude name="**/tests/data/**"/>
                <exclude name="build/**"/>
            </fileset>
            <hlm:checkPropertyTypeAndValueMismatch severity="error" />
            <hlm:antlintConsoleReporter />            
        </hlm:antlint>
    </target>
    
    
    <!-- Generates an Ant XML database file.
        
    This lists all the targets and information about them. -->
    <target name="database">
        <hlm:databaseMacro file="${database.file}" scope="private"/>
    </target>
    
    
    <!-- Looks for lint-style issues with the Ant files in Helium, using the antlint task. -->
    <target name="antlint">
        <delete dir="${helium.build.dir}/jep"/>
        <delete dir="${helium.build.dir}/python"/>
        <delete dir="${helium.build.dir}/beanshell"/>
        <delete file="${helium.build.dir}/test_jython.xml"/>
        <mkdir dir="${helium.build.dir}/report/antlint" />
        
        <pathconvert pathsep="${path.separator}" property="python.tools.path">
            <fileset dir="${helium.dir}/nokia_builder" includes="tools/pylint/**/*.egg" />
            <dirset dir="${helium.dir}/nokia_builder" includes="tools/pylint/**/*.egg" />
        </pathconvert>
        <hlm:antlint>
            <fileset id="antlint.files" dir="${helium.dir}/tools">
                <include name="**/*.ant.xml"/>
                <include name="**/build.xml"/>
                <include name="**/*.antlib.xml"/>
                <exclude name="tests/**"/>
                <exclude name="**/test/test_*.xml"/>
                <exclude name="external/**"/>
                <exclude name="**/tests/data/**"/>
                <exclude name="build/**"/>
            </fileset>
            <hlm:checkTabCharacter severity="error" />
            <hlm:checkPropertyName severity="warning" regexp="([a-z0-9[\\d\\_\\.\\@\\{\\}\\$]]*)" />
            <hlm:checkTargetName severity="warning" regexp="([a-z0-9[\\d\\-]]*)" />
            <hlm:checkIndentation severity="error" />
            <hlm:checkPresetDefMacroDefName severity="warning" regexp="([a-z0-9][a-zA-Z0-9]*)" />       
            <hlm:checkProjectName severity="warning" regexp="([a-z0-9[\\d\\.\\_\\-]]*)" />
            <hlm:checkDescription severity="warning" />
            <hlm:checkFileName severity="warning" regexp="^build.xml$|ant.xml$|antlib.xml$" />
            <hlm:checkRunTarget severity="warning" />
            <hlm:checkAntCall severity="warning" />
            <hlm:checkScriptSize severity="warning" />
            <hlm:checkUseOfIfInTargets severity="warning" />
            <hlm:checkJythonScript severity="error"/>
            <hlm:checkScriptCondition severity="warning" />
            <hlm:checkUseOfEqualsTask severity="warning" />
            <hlm:checkScriptDefAttributes severity="error" />
            <hlm:checkScriptDefStyle severity="warning" />
            <hlm:checkScriptDefName severity="warning" regexp="([a-z0-9][a-zA-Z0-9]*)"/>
            <hlm:checkDuplicateNames severity="warning" />
            <hlm:checkTryCatchBlock severity="warning" />
            <hlm:checkPropertyTypeAndValueMismatch severity="error" />
            <hlm:checkVariableTask severity="error" />
            
            <!-- external command executors -->
            <hlm:checkstyleExecutor outputDir="${helium.build.dir}/beanshell" config="builder/java/config/java_checkstyle_config.xml">
                <formatter type="plain"/>
            </hlm:checkstyleExecutor>
            <hlm:pylintExecutor outputDir="${helium.build.dir}/python">
                <env key="PYTHONPATH" value="${python.tools.path}"/>
                <arg file="${helium.dir}/nokia_builder/tools/pylint/bin/pylint-script.py"/>
                <arg value="--output-format=parseable"/>
                <arg line="--rcfile=${helium.dir}/nokia_builder/config/pylintrc.txt"/>
            </hlm:pylintExecutor>

            <!-- Reporters -->
            <hlm:antlintCheckstyleReporter file="${helium.build.dir}/report/antlint/antlint_report.xml" />
            <hlm:antlintConsoleReporter />            
            
        </hlm:antlint>

        <for param="file">
            <path>
                <fileset dir="${helium.dir}">
                    <include name="**/*.ftl"/>
                    <include name="**/*.rst"/>
                    <exclude name="**/*.mk.ftl"/>
                    <exclude name="build/**/*"/>
                </fileset>
            </path>
            <sequential>
                <loadfile srcfile="@{file}" property="tabs.in.ftl">
                    <filterchain>
                        <linecontainsregexp>
                            <regexp pattern="\t"/>
                        </linecontainsregexp>
                    </filterchain>
                </loadfile>
                <fail if="tabs.in.ftl" message="@{file} has tabs" />
            </sequential>
        </for>
    </target>
   
    
    <!-- This target can be use to clean up after a build finished or failed.
    
    This contains:
    * Unsubst build drive if the build finished.
    * Can be used to do other cleanup stuff too.
    -->
    <target name="cleanup-all">
        <!-- unsubst build drive after build finished if the property "unsubst.after.build" defined as "yes" -->
        <if>
            <and>
                <isset property="unsubst.after.build"/>
                <istrue value="${unsubst.after.build}" />
            </and>
            <then>
                <echo> Removing build drive: ${build.drive} </echo>
                <hlm:unsubst drive="${build.drive}"/>
            </then>
        </if>                
    </target>
    
    
    <!-- Macro test target. -->
    <target name="macro-test">
        <hlm:fooMacro/>
    </target>

   <!--* @property hlm.final.target
   This property defines a target that will be run after build completion, whatever the 
   status of the build is.   
   @type string
   @scope public
   @since 10.0
   -->

</project>
    



