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