sbsv1/abld/e32util/featurevariantparser.pm
changeset 607 378360dbbdba
parent 599 fa7a3cc6effd
equal deleted inserted replaced
591:22486c9c7b15 607:378360dbbdba
       
     1 #
       
     2 # Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 
       
    17 # Module FEATUREVARIANTPARSER. Parses .VAR files and returns key variables.
       
    18 
       
    19 # The following hashes can be used with this module:
       
    20 
       
    21 # NAME 				-> Returns the name of the variant file (without the extension)
       
    22 
       
    23 # FULLPATH 			-> Returns the full path of the variant file (including the extension)
       
    24 
       
    25 # VALID 			-> Set to 1 if the variant file is valid, otherwise set to 0
       
    26 
       
    27 # VIRTUAL 			-> Set to 1 if the variant is a grouping node, otherwise set to 0
       
    28 
       
    29 # ROM_INCLUDES 		-> Returns a pointer to the list of ROM_INCLUDES (including Parent nodes).
       
    30 
       
    31 # BUILD_INCLUDES 	-> Returns a pointer to the list of BUILD_INCLUDES (including Parent nodes).
       
    32 
       
    33 # VARIANT_HRH 		-> Returns the full VARIANT_HRH file path used by the VAR file.
       
    34 
       
    35 # PARENTS			-> Returns a pointer to the list of all the parent nodes, starting with immediate parent
       
    36 
       
    37 # CHILDREN			-> Returns a pointer to the list of all the children nodes.
       
    38 
       
    39 # USAGE : The GetVariant method should only be called using featurevariantparser->GetVariant(var_name, directory(optional) );
       
    40 # If the directory for the VAR file is not supplied,the default directory will be searched for var_name.var
       
    41 
       
    42 
       
    43 package featurevariantparser;
       
    44 use File::Spec;
       
    45 
       
    46 my @buildinclude;
       
    47 my @rominclude;
       
    48 my @parents;
       
    49 my @childNodes;
       
    50 my $virtual;
       
    51 my $childNodeStatus;
       
    52 my $varianthrh;
       
    53 
       
    54 my $defaultDir = "$ENV{EPOCROOT}epoc32\\tools\\variant";
       
    55 
       
    56 my $dir;
       
    57 my $fullpath;
       
    58 my $fulldir;
       
    59 
       
    60 my $pathregex = '.+[^\s]'  ;   # Regex to match all characters (including \ or /), excluding whitespaces.
       
    61 
       
    62 our $verbose = 0;
       
    63 
       
    64 # Wrapper function to return all the correct variables
       
    65 # Arguments : (Variant Name, Variant Directory(optional))
       
    66 # Returns a Hash.
       
    67 #
       
    68 # Note: This has to return a copy of all the data - no references!
       
    69 #  There are package globals that are reused on a call to this function
       
    70 #  so references would go out of date after repeated calls to GetVariant
       
    71 #  This package should have been written using objects - too late now
       
    72 
       
    73 sub GetVariant
       
    74 {
       
    75 
       
    76     @buildinclude    = ();
       
    77     @rominclude      = ();
       
    78     @parents         = ();
       
    79     @childNodes      = ();
       
    80     $dir             = "";
       
    81     $fullpath        = "";
       
    82     $varianthrh      = "";
       
    83     $virtual         = 0;
       
    84     $childNodeStatus = 0;
       
    85     
       
    86     my $parnodes = "";
       
    87     my %data;
       
    88     my $children  = "";
       
    89     my $romincs   = "";
       
    90     my $buildincs = "";
       
    91 
       
    92     $data{'VALID'} = 0;
       
    93 
       
    94     my ( $empty, $varname, $dirname ) = @_;
       
    95 
       
    96     my $fullvarpath = ReturnFullVariantPath( $varname, $dirname );
       
    97 
       
    98     if ( $dirname )
       
    99     {
       
   100         $fulldir = $dirname;
       
   101     }
       
   102     else
       
   103     {
       
   104         $fulldir = $defaultDir;
       
   105     }
       
   106 
       
   107     $data{'FULLPATH'} = "$fullvarpath";
       
   108     $data{'NAME'}     = "$varname";
       
   109 
       
   110     # If the variant file exists, check the syntax and setup variables.
       
   111     if ( FileExists($fullvarpath) )
       
   112     {
       
   113 
       
   114         if ( CheckVarFileSyntax( $fullvarpath, $varname ) )
       
   115         {
       
   116             $data{'VALID'} = 1;
       
   117         }
       
   118     }
       
   119     else
       
   120     {
       
   121         print "ERROR: $fullpath" . " does not exist\n";
       
   122     }
       
   123 
       
   124     my $count = 0;
       
   125 
       
   126     # If VAR file is valid, setup all other variables.
       
   127     if ( $data{'VALID'} )
       
   128     {
       
   129 
       
   130         $romincs   = FindRomInclude($fullvarpath);
       
   131         $buildincs = FindBuildInclude($fullvarpath);
       
   132         $children  = FindChildNodes($fullvarpath);
       
   133         $parnodes  = FindParentNodes($fullvarpath);
       
   134 
       
   135         # Remove empty elements from the BUILD_INCLUDE list     
       
   136         @$buildincs = grep /\S/, @$buildincs;
       
   137 
       
   138         # Fix paths for all BUILD_INCLUDES
       
   139         for ( my $i = 0 ; $i < scalar(@$buildincs) ; $i++ )
       
   140         {
       
   141             @$buildincs[$i] = FixPaths( @$buildincs[$i] );
       
   142         }
       
   143 
       
   144         # Remove empty elements from the ROM_INCLUDE list
       
   145 		@$romincs = grep /\S/, @$romincs;
       
   146 
       
   147         # Fix paths for all ROM_INCLUDES
       
   148         for ( my $i = 0 ; $i < scalar(@$romincs) ; $i++ )
       
   149         {
       
   150             @$romincs[$i] = FixPaths( @$romincs[$i] );
       
   151         }
       
   152 
       
   153         # Remove empty elements from the CHILDREN list
       
   154 		@$children = grep /\S/, @$children;
       
   155 		
       
   156         # Remove empty elements from the PARENT list
       
   157 		@$parnodes = grep /\S/, @$parnodes;
       
   158 
       
   159         $data{'BUILD_INCLUDES'} = CloneList($buildincs);        
       
   160         $data{'ROM_INCLUDES'}   = CloneList($romincs);
       
   161         $data{'PARENTS'}        = CloneList($parnodes);
       
   162         $data{'CHILDREN'}       = CloneList($children);
       
   163         $data{'VARIANT_HRH'}    = $varianthrh;
       
   164         $data{'VIRTUAL'}        = $virtual;
       
   165     }
       
   166 
       
   167     # If variant file is not valid, return reference to a blank array
       
   168     else
       
   169     {
       
   170         $data{'BUILD_INCLUDES'} = [];
       
   171         $data{'ROM_INCLUDES'}   = [];
       
   172         $data{'VARIANT_HRH'}    = "";
       
   173         $data{'PARENTS'}        = [];
       
   174         $data{'CHILDREN'}       = [];
       
   175     }
       
   176 
       
   177     return %data;
       
   178 }
       
   179 
       
   180 # Helper method that clones a reference to a simple list
       
   181 sub CloneList
       
   182     {
       
   183     my $ref = shift;
       
   184     
       
   185     # Check the reference is a list
       
   186     die "Not a list ref" if ref($ref) ne 'ARRAY';
       
   187     
       
   188     # Create a new list object
       
   189     my @list;
       
   190     foreach my $entry ( @$ref )
       
   191         {
       
   192         # Only clone lists of scalars
       
   193         die "Not a scalar" if ref($entry);
       
   194         
       
   195         # Add the entry to the new list
       
   196         push @list, $entry;
       
   197         }
       
   198     
       
   199     # return a reference to the copy    
       
   200     return \@list;
       
   201     }
       
   202     
       
   203 # Method to correct all the slashes, and also append EPOCROOT if the path begins with a \ or /
       
   204 # If path doesn't start with \ or /, returns an abosulte canonical path
       
   205 sub FixPaths
       
   206 {
       
   207 
       
   208     my $arr = $_[0];
       
   209 
       
   210     if ( $arr =~ m/^\// )
       
   211     {
       
   212        $arr =~ s/^\/?//;
       
   213         return File::Spec->canonpath( "$ENV{EPOCROOT}" . "$arr" );
       
   214     }
       
   215 
       
   216     elsif ( $arr =~ m/^\\/ )
       
   217     {
       
   218         $arr =~ s/^\\?//;
       
   219         return File::Spec->canonpath( "$ENV{EPOCROOT}" . "$arr" );
       
   220     }
       
   221 
       
   222     else
       
   223     {
       
   224         return File::Spec->rel2abs( File::Spec->canonpath("$arr") );
       
   225     }
       
   226 
       
   227 }
       
   228 
       
   229 # Method to construct a full variant path from the variant file and directory
       
   230 sub ReturnFullVariantPath
       
   231 {
       
   232 
       
   233     my $vardirectory = $_[1];
       
   234     my $varname      = $_[0];
       
   235 
       
   236     # Check if a directory is supplied
       
   237     if ($vardirectory)
       
   238     {
       
   239         $dir = "$vardirectory";
       
   240     }
       
   241 
       
   242     else
       
   243     {
       
   244         $dir = $defaultDir;
       
   245     }
       
   246     my $filename = "$varname" . "\.var";
       
   247     $fullpath = File::Spec->catfile( File::Spec->rel2abs($dir), $filename );
       
   248 
       
   249     if ( !File::Spec->file_name_is_absolute($fullpath) )
       
   250     {
       
   251         $fullpath = File::Spec->rel2abs($fullpath);
       
   252     }
       
   253 
       
   254     return $fullpath;
       
   255 }
       
   256 
       
   257 # Method to find the BUILDINCLUDE values of the VAR file.
       
   258 sub FindBuildInclude
       
   259 {
       
   260 
       
   261     my $filename = $_[0];
       
   262 
       
   263     my $parentNodes;
       
   264 
       
   265     # Construct a list of parent nodes if node is a child
       
   266     if ($childNodeStatus)
       
   267     {
       
   268         $parentNodes = FindParentNodes("$filename");
       
   269     }
       
   270 
       
   271     if ($parentNodes)
       
   272     {
       
   273 
       
   274         # Go through and build the list of all parent BUILD_INCLUDES
       
   275         for ( my $i = scalar(@$parentNodes) - 1 ; $i >= 0 ; $i-- )
       
   276         {
       
   277 
       
   278             my $tmp = ReturnFullVariantPath( @$parentNodes[$i], $fulldir );
       
   279             open( NEWHANDLE, "<$tmp" );
       
   280             while (<NEWHANDLE>)
       
   281             {
       
   282                 if (/BUILD_INCLUDE/)
       
   283                 {
       
   284                     ExtractBuildIncludeValue($_);
       
   285                 }
       
   286             }
       
   287             close(NEWHANDLE);
       
   288         }
       
   289     }
       
   290 
       
   291     # Append the BUILD_INCLUDES of the VAR file in the end
       
   292     open( NEWHANDLE, "<$filename" );
       
   293 
       
   294     while (<NEWHANDLE>)
       
   295     {
       
   296         if (/BUILD_INCLUDE/)
       
   297         {
       
   298             ExtractBuildIncludeValue($_);
       
   299         }
       
   300     }
       
   301     close(NEWHANDLE);
       
   302 
       
   303     undef(@parents);    # Flush out parent array
       
   304 
       
   305     return \@buildinclude;
       
   306 
       
   307 }
       
   308 
       
   309 # Method to extract the BUILD_INCLUDE value of a node.
       
   310 sub ExtractBuildIncludeValue
       
   311 {
       
   312 
       
   313 # If modifier append is found, push the buildinclude to the end of the array list.
       
   314     if (/^BUILD_INCLUDE\s+append\s+($pathregex)/)
       
   315     {
       
   316         push( @buildinclude, ($1) );
       
   317 
       
   318     }
       
   319 
       
   320 # If modifier prepend is found, push the buildinclude to the beginning of the array list.
       
   321     if (/^BUILD_INCLUDE\s+prepend\s+($pathregex)/)
       
   322     {
       
   323         unshift( @buildinclude, ($1) );
       
   324     }
       
   325 
       
   326 #If keyword set is found, then empty the buildinclude variable and push the new value
       
   327     if (/^BUILD_INCLUDE\s+set\s+($pathregex)/)
       
   328     {
       
   329         undef(@buildinclude);
       
   330         push( @buildinclude, ($1) );
       
   331     }
       
   332 
       
   333 }
       
   334 
       
   335 # Method to find the ROMINCLUDE values of the VAR file.
       
   336 sub FindRomInclude
       
   337 {
       
   338 
       
   339     my $filename = $_[0];
       
   340 
       
   341     my $parentNodes;
       
   342 
       
   343     # Construct a list of parent nodes if node is a child
       
   344     if ($childNodeStatus)
       
   345     {
       
   346         $parentNodes = FindParentNodes("$filename");
       
   347     }
       
   348 
       
   349     if ($parentNodes)
       
   350     {
       
   351 
       
   352         # Go through and build the list of all parent ROM_INCLUDES
       
   353         for ( my $i = scalar(@$parentNodes) - 1 ; $i >= 0 ; $i-- )
       
   354         {
       
   355             my $t = ReturnFullVariantPath( @$parentNodes[$i], $fulldir );
       
   356             open( NEWHANDLE, "<$t" );
       
   357 
       
   358             while (<NEWHANDLE>)
       
   359             {
       
   360                 if (/ROM_INCLUDE/)
       
   361                 {
       
   362                     ExtractRomIncludeValue($_);
       
   363                 }
       
   364             }
       
   365             close(NEWHANDLE);
       
   366         }
       
   367     }
       
   368 
       
   369     # Append the ROM_INCLUDES of the VAR file in the end
       
   370     open( NEWHANDLE, "<$filename" );
       
   371 
       
   372     while (<NEWHANDLE>)
       
   373     {
       
   374         if (/ROM_INCLUDE/)
       
   375         {
       
   376             ExtractRomIncludeValue($_);
       
   377         }
       
   378     }
       
   379 
       
   380     undef(@parents);    # Flush out parent array;
       
   381     return \@rominclude;
       
   382 
       
   383 }
       
   384 
       
   385 # Method to extract the ROM_INCLUDE value of a node.
       
   386 sub ExtractRomIncludeValue
       
   387 {
       
   388 
       
   389 # If modifier append is found, push the rominclude to the end of the array list.
       
   390     if (/^ROM_INCLUDE\s+append\s+($pathregex)/)
       
   391     {
       
   392         push( @rominclude, ($1) );
       
   393     }
       
   394 
       
   395 # If modifier prepend is found, push the rominclude to the beginning of the array list.
       
   396     if (/^ROM_INCLUDE\s+prepend\s+($pathregex)/)
       
   397     {
       
   398         unshift( @rominclude, ($1) );
       
   399     }
       
   400 
       
   401 # If keyword set is found, then empty the rominclude variable and push the new value
       
   402     if (/^ROM_INCLUDE\s+set\s+($pathregex)/)
       
   403     {
       
   404         undef(@rominclude);
       
   405         push( @rominclude, ($1) );
       
   406     }
       
   407 
       
   408 }
       
   409 
       
   410 # Method to find the immediate parent node of a child node
       
   411 sub ExtractExtendsValue
       
   412 {
       
   413 
       
   414     $_[0] =~ m/^EXTENDS\s+(\w+)/;
       
   415     return $1;
       
   416 }
       
   417 
       
   418 # Extract the value of the VARIANT keyword
       
   419 sub ExtractVariantValue
       
   420 {
       
   421 
       
   422     $_[0] =~ m/^VARIANT\s+(\w+)/;
       
   423     return $1;
       
   424 }
       
   425 
       
   426 # Extracts the value of the HRH file from the VARIANT_HRH line supplied
       
   427 sub ExtractHrhValue
       
   428 {
       
   429 
       
   430     $_[0] =~ m/^VARIANT_HRH\s+($pathregex)/;
       
   431     return $1;
       
   432 
       
   433 }
       
   434 
       
   435 # Finds if the variant file is a group node
       
   436 # Note: This method is only a supplementary method, not actually used during this module
       
   437 #       Provides a quick way to check is any VAR file is grouping node or not without loading the entire file.
       
   438 sub IsVirtual
       
   439 {
       
   440 
       
   441     my $filename = $_[0];
       
   442 
       
   443     open( READHANDLE, "<$filename" );
       
   444     while (<READHANDLE>)
       
   445     {
       
   446         if (/^VIRTUAL\s*$/)
       
   447         {
       
   448             close(READHANDLE);
       
   449             return 1;
       
   450         }
       
   451     }
       
   452     close(READHANDLE);
       
   453     return 0;
       
   454 }
       
   455 
       
   456 # Constructs a list of Parent nodes for a given Child node.
       
   457 sub FindParentNodes
       
   458 {
       
   459 
       
   460     my $filename   = $_[0];
       
   461     my $hasparents = 0;
       
   462 
       
   463     open( READHANDLE, "<$filename" );
       
   464     while (<READHANDLE>)
       
   465     {
       
   466         if (/EXTENDS/)
       
   467         {
       
   468             $hasparents = 1;
       
   469             push( @parents, ExtractExtendsValue($_) );
       
   470 
       
   471         }
       
   472     }
       
   473 
       
   474     close(READHANDLE);
       
   475 
       
   476     if ( $hasparents == 1 )
       
   477     {
       
   478         FindParentNodes(
       
   479             ReturnFullVariantPath( @parents[ scalar(@parents) - 1 ], $fulldir )
       
   480         );
       
   481     }
       
   482     else
       
   483     {
       
   484         return \@parents;
       
   485     }
       
   486 
       
   487 }
       
   488 
       
   489 # Constructs a list of Child nodes for a given Parent node (full path or .var path required)
       
   490 sub FindChildNodes
       
   491 {
       
   492 
       
   493     my $var = ReturnNativeVarName("$_[0]");
       
   494 
       
   495     my $tmpname    = "";
       
   496     my @childarray = ();
       
   497 
       
   498     opendir( DIR, $fulldir );
       
   499 
       
   500     while ( defined( my $file = readdir(DIR) ) )
       
   501     {
       
   502 
       
   503         if ( $file =~ m/\.var$/ )
       
   504         {
       
   505             $tmpname = $file;
       
   506             $tmpname =~ s/\.var$//;
       
   507 
       
   508             open( FILEHANDLE, ReturnFullVariantPath( $tmpname, $fulldir ) );
       
   509 
       
   510             while (<FILEHANDLE>)
       
   511             {
       
   512 
       
   513                 if (/^EXTENDS/)
       
   514                 {
       
   515                     if ( lc $var eq lc ExtractExtendsValue($_) )
       
   516                     {
       
   517                         push( @childarray, $tmpname );
       
   518                     }
       
   519                 }
       
   520 
       
   521             }
       
   522             close(FILEHANDLE);
       
   523         }
       
   524     }
       
   525 
       
   526     close(DIR);
       
   527 
       
   528     foreach my $child (@childarray)
       
   529     {
       
   530         push( @childNodes, $child );
       
   531     }
       
   532 
       
   533     foreach my $child (@childarray)
       
   534     {
       
   535         FindChildNodes( ReturnFullVariantPath( $child, $fulldir ) );
       
   536     }
       
   537 
       
   538     return \@childNodes;
       
   539 }
       
   540 
       
   541 # Method to return all the buildable (i.e. No syntax erros and non-virtual) list of
       
   542 # variants that can be built in the specified directory. If no directory is specified,
       
   543 # the default location is searched.
       
   544 
       
   545 # Usage: GetBuildableFeatureVariants(<Directory>)
       
   546 
       
   547 sub GetBuildableFeatureVariants
       
   548 {
       
   549 
       
   550     my $empty = shift;
       
   551     my $dir   = shift;
       
   552     my @list;
       
   553     my $fulldir;
       
   554 
       
   555     if ( $dir )
       
   556     {
       
   557         $fulldir = $dir;
       
   558     }
       
   559     else
       
   560     {
       
   561         $fulldir = $defaultDir;
       
   562     }
       
   563 
       
   564     opendir( DIR, $fulldir );
       
   565 
       
   566     while ( defined( my $file = readdir(DIR) ) )
       
   567     {
       
   568 
       
   569         if ( $file =~ m/\.var$/ )
       
   570         {
       
   571 
       
   572             $file =~ s/\.var$//;
       
   573 
       
   574             my $fullpath = ReturnFullVariantPath( $file, $fulldir );
       
   575 
       
   576             if ( CheckVarFileSyntax( $fullpath, $file )
       
   577                 && !IsVirtual($fullpath) )
       
   578             {
       
   579                 push( @list, $file );
       
   580 
       
   581             }
       
   582         }
       
   583     }
       
   584 
       
   585     return sort @list;
       
   586 
       
   587 }
       
   588 
       
   589 # Method to return a list of the valid and non-virtual children of a given VAR node, in a given location.
       
   590 # If the calling var file is non-virtual, it is returned as the only element of the list If not, then
       
   591 # the method parses though and finds all buildable children
       
   592 
       
   593 # USAGE: ResolveFeatureVariant(<varname>,<dirame>);
       
   594 
       
   595 sub ResolveFeatureVariant
       
   596 {
       
   597 
       
   598     my $empty   = shift;
       
   599     my $varfile = shift;
       
   600     my $dir     = shift;
       
   601     my $fulldir;
       
   602     my @list;
       
   603 
       
   604     if ( !$dir eq "" )
       
   605     {
       
   606         $fulldir = $dir;
       
   607     }
       
   608     else
       
   609     {
       
   610         $fulldir = $defaultDir;
       
   611     }
       
   612     my $fullpath = ReturnFullVariantPath( $varfile, $fulldir );
       
   613 
       
   614     if ( CheckVarFileSyntax( $fullpath, $varfile ) && !IsVirtual($fullpath) )
       
   615     {
       
   616         push( @list, $varfile );
       
   617         return @list;
       
   618     }
       
   619 
       
   620     my %variant = GetVariant( 0, $varfile, $fulldir );
       
   621     my $child = $variant{'CHILDREN'};
       
   622 
       
   623     foreach my $item (@$child)
       
   624     {
       
   625 
       
   626         my $fullpath = ReturnFullVariantPath( $item, $fulldir );
       
   627 
       
   628         if ( CheckVarFileSyntax( $fullpath, $item ) && !IsVirtual($fullpath) )
       
   629         {
       
   630             push( @list, $item );
       
   631         }
       
   632     }
       
   633 
       
   634     return @list;
       
   635 
       
   636 }
       
   637 
       
   638 # Method to return all valid (no syntax errors only) list of variants
       
   639 # in the specified directory. If no directory is specified,the default location is searched.
       
   640 
       
   641 # Usage: GetValidVariants(<Directory>)
       
   642 
       
   643 sub GetValidVariants
       
   644 {
       
   645 
       
   646     my $empty = shift;
       
   647     my $dir   = shift;
       
   648     my @list;
       
   649     my $fulldir;
       
   650 
       
   651     if ( !$dir eq "" )
       
   652     {
       
   653         $fulldir = $dir;
       
   654     }
       
   655     else
       
   656     {
       
   657         $fulldir = $defaultDir;
       
   658     }
       
   659 
       
   660     opendir( DIR, $fulldir );
       
   661 
       
   662     while ( defined( my $file = readdir(DIR) ) )
       
   663     {
       
   664 
       
   665         if ( $file =~ m/\.var$/ )
       
   666         {
       
   667 
       
   668             $file =~ s/\.var$//;
       
   669 
       
   670             my $fullpath = ReturnFullVariantPath( $file, $fulldir );
       
   671 
       
   672             if ( CheckVarFileSyntax( $fullpath, $file ) )
       
   673             {
       
   674 
       
   675                 push( @list, $file );
       
   676 
       
   677             }
       
   678         }
       
   679     }
       
   680 
       
   681     return sort @list;
       
   682 }
       
   683 
       
   684 # Returns the Variant name from a complete path, without the .var extension
       
   685 sub ReturnNativeVarName
       
   686 {
       
   687 
       
   688     my $tmp = "$_[0]";
       
   689     $tmp =~ /^.*[\\|\/](.*)\.var$/i;  
       
   690     return $1;
       
   691 
       
   692 }
       
   693 
       
   694 # Checks if a file passed to this function exists.
       
   695 sub FileExists
       
   696 {
       
   697 	return -e $_[0];
       
   698 
       
   699 }
       
   700 
       
   701 # Checks if the default variant file (default.var) exists in the directory supplied.
       
   702 # If no directory is supplied, looks under the default location.
       
   703 # Should only be called directly from the featurevariantparser module reference, not from any methods within the module.
       
   704 
       
   705 # USAGE: featurevariantparser->DefaultExists(<DirName>);
       
   706 # <DirName> : Optional -- Specifies which directory to look under to find the default.var file. Can be relative or absolute.
       
   707 
       
   708 sub DefaultExists
       
   709 {
       
   710 
       
   711     my $dirToSearch = "";
       
   712     if ( $_[1] )
       
   713     {
       
   714         $dirToSearch = $_[1];
       
   715     }
       
   716     else
       
   717     {
       
   718         $dirToSearch = $defaultDir;
       
   719     }
       
   720 
       
   721     $dirToSearch =
       
   722       File::Spec->canonpath(
       
   723         File::Spec->rel2abs( File::Spec->canonpath("$dirToSearch") ) );
       
   724 
       
   725     return ( -e "$dirToSearch/default.var" );
       
   726 
       
   727 }
       
   728 
       
   729 # Checks the variant file for the correct syntax and reports any errors
       
   730 # Also sets up some variables(VIRTUAL ,VARIANT_HRH and VARIANT) whilst file is being parsed.
       
   731 
       
   732 # Usage: CheckVarFileSyntaxt(<fullpath>,<varfile>) . Note: <varfile> without .var
       
   733 sub CheckVarFileSyntax
       
   734 {
       
   735 
       
   736     my $fullpath          = $_[0];
       
   737     my $varname           = $_[1];
       
   738     my $varianthrhpresent = 0;
       
   739 
       
   740     open( READVAR, "<$fullpath" );
       
   741     my $exp  = "#";
       
   742     my $line = "";
       
   743 
       
   744     while (<READVAR>)
       
   745     {
       
   746         $line = $.;
       
   747 
       
   748 	# Checks for a valid argument supplied to EXTENDS keyword. Checks for one and only one argument supplied.
       
   749         if (/^EXTENDS/)
       
   750         {
       
   751             if ( !m/^EXTENDS\s+./ )
       
   752             {
       
   753                 print "\nERROR: Invalid format supplied to argument EXTENDS on line "
       
   754                   . "$."
       
   755                   . " in file "
       
   756                   . "$fullpath";
       
   757                 return 0;
       
   758             }
       
   759             my $str = ExtractExtendsValue($_);
       
   760             if ( $str =~ /\s+/ )
       
   761             {
       
   762                 print "\nERROR: Cannot extend from two nodes. Error in line "
       
   763                   . "$."
       
   764                   . " in file "
       
   765                   . "$fullpath";
       
   766                 return 0;
       
   767             }
       
   768 
       
   769             $childNodeStatus = 1;
       
   770 
       
   771         }
       
   772 
       
   773         # Checks for the grammar of BUILD_INCLUDE, i.e. KEYWORD MODIFIER VALUE
       
   774         elsif (/^BUILD_INCLUDE/)
       
   775         {
       
   776 
       
   777           if (!m/^BUILD_INCLUDE\s+(append|prepend|set)\s+$pathregex/)
       
   778             {
       
   779                 print "\nERROR: Invalid syntax supplied to keyword BUILD_INCLUDE on line "
       
   780                   . "$."
       
   781                   . " in file "
       
   782                   . "$fullpath";
       
   783                 return 0;
       
   784             }
       
   785             
       
   786 		 if (m/^BUILD_INCLUDE\s+(append|prepend|set)\s+$pathregex\s+$pathregex/)
       
   787             {
       
   788                 print "\nERROR: Too many arguments supplied to keyword BUILD_INCLUDE on line "
       
   789                   . "$."
       
   790                   . " in file "
       
   791                   . "$fullpath";
       
   792                 return 0;
       
   793             }
       
   794         }
       
   795 
       
   796         # Checks for the grammar of ROM_INCLUDE, i.e. KEYWORD MODIFIER VALUE
       
   797         elsif (/^ROM_INCLUDE/)
       
   798         {
       
   799 
       
   800 		if (!m/^ROM_INCLUDE\s+(append|prepend|set)\s+$pathregex/)
       
   801             {
       
   802                 print "\nERROR: Invalid syntax supplied to keyword ROM_INCLUDE on line "
       
   803                   . "$."
       
   804                   . " in file "
       
   805                   . "$fullpath";
       
   806                 return 0;
       
   807             }
       
   808 
       
   809        if (m/^ROM_INCLUDE\s+(append|prepend|set)\s+$pathregex\s+$pathregex/)
       
   810             {
       
   811                 print "\nERROR: Too many arguments supplied to keyword ROM_INCLUDE on line "
       
   812                   . "$."
       
   813                   . " in file "
       
   814                   . "$fullpath";
       
   815                 return 0;
       
   816             }
       
   817         }
       
   818 
       
   819         # Checks for a valid VARIANT name
       
   820         elsif (/^VARIANT[^_HRH]/)
       
   821         {
       
   822             if ( !m/^VARIANT\s+\w+/ )
       
   823             {
       
   824                 print "\nERROR: VARIANT name not specified on line " . "$."
       
   825                   . " in file "
       
   826                   . "$fullpath";
       
   827                 return 0;
       
   828             }
       
   829             if ( uc("$varname") ne uc( ExtractVariantValue($_) ) )
       
   830             {
       
   831                 print "\nERROR: VARIANT filename does not match variant name specified on line "
       
   832                   . "$line"
       
   833                   . " in file "
       
   834                   . "$fullpath"
       
   835                   . "\nVariant value extracted from the VAR file is " . "$_";
       
   836             }
       
   837 
       
   838         }
       
   839 
       
   840         # Checks that keyword VIRTUAL is declared correctly
       
   841         elsif (/^VIRTUAL/)
       
   842         {
       
   843             if (m/^VIRTUAL\s+\w+/)
       
   844             {
       
   845                 print "\nERROR: Invalid declaration of VIRTUAL on line " . "$."
       
   846                   . " in file "
       
   847                   . "$fullpath";
       
   848                 return 0;
       
   849             }
       
   850 
       
   851             $virtual = 1;
       
   852         }
       
   853 
       
   854         # Checks if VARIANT_HRH is declared correctly.
       
   855         elsif (/^VARIANT_HRH/)
       
   856         {
       
   857             $varianthrhpresent = 1;
       
   858             my $lineno = $.;
       
   859             if ( !m/^VARIANT_HRH\s+./ )
       
   860             {
       
   861                 print "\nERROR: Invalid format supplied to argument VARIANT_HRH on line "
       
   862                   . "$lineno"
       
   863                   . " in file "
       
   864                   . "$fullpath";
       
   865                 return 0;
       
   866             }
       
   867 
       
   868             my $str = ExtractHrhValue($_);
       
   869             if ( $str =~ /\s+/ )
       
   870             {
       
   871                 print "\nERROR: Cannot have 2 or more hrh files. Error in line "
       
   872                   . "$lineno"
       
   873                   . " in file "
       
   874                   . "$fullpath";
       
   875                 return 0;
       
   876             }
       
   877 
       
   878             if ( !FileExists( FixPaths($str) ) )
       
   879             {
       
   880                 print "\nERROR: VARIANT HRH file : "
       
   881                   . FixPaths($str)
       
   882                   . " specified on line "
       
   883                   . "$lineno"
       
   884                   . " does not exist";
       
   885                 return 0;
       
   886             }
       
   887 
       
   888             $varianthrh = FixPaths( ExtractHrhValue($_) );
       
   889 
       
   890         }
       
   891         
       
   892         # If none of the valid keywords are found
       
   893         else
       
   894         {
       
   895 
       
   896             # Do nothing if a comment or blank line is found
       
   897             if ( (m/$exp\s+\S/) || (m/$exp\S/) || ( !m/./ ) || (m/^\n/) )
       
   898             {
       
   899             }
       
   900 
       
   901             # Unsupported keyword
       
   902             else
       
   903             {
       
   904 
       
   905                 print "\nERROR: Invalid keyword " . '"' . "$_" . '"'
       
   906                   . " found on line " . "$."
       
   907                   . " in file "
       
   908                   . "$fullpath";
       
   909                 return 0;
       
   910             }
       
   911         }
       
   912     }
       
   913 
       
   914     close(READVAR);
       
   915 
       
   916     # If no HRH file defined, check if the default one exists
       
   917     if ( !$varianthrhpresent )
       
   918     {
       
   919         print "\nINFO: No VARIANT_HRH defined in VAR file, using $ENV{EPOCROOT}epoc32\\include\\variant\\$varname\.hrh" if ($verbose);
       
   920         my $str =
       
   921           ExtractHrhValue(
       
   922             "VARIANT_HRH $ENV{EPOCROOT}epoc32\\include\\variant\\$varname\.hrh"
       
   923           );
       
   924 
       
   925         if ( !FileExists($str) )
       
   926         {
       
   927             print "\nERROR: VARIANT HRH file : " . "$str " . "does not exist\n";
       
   928             return 0;
       
   929         }
       
   930         else
       
   931         {
       
   932             $varianthrh = $str;
       
   933         }
       
   934     }
       
   935     return 1;
       
   936 }
       
   937 
       
   938 1;
       
   939