common/tools/BuildEnvXML.pm
changeset 358 61571c10c5d0
equal deleted inserted replaced
357:881cbdd44910 358:61571c10c5d0
       
     1 ## @file BuildEnvXML.pm
       
     2 # @ingroup userscripts clientscripts
       
     3 #
       
     4 # Uses information which is defined in the BuildEnvXML.xml to set up a machine
       
     5 # specific environment, such as path for compilers. 
       
     6 #
       
     7 # @todo Document this script.
       
     8 #
       
     9 # Copyright (c) 2009 Symbian Foundation  Ltd. All rights reserved.
       
    10 #
       
    11 
       
    12 package BuildEnvXML;
       
    13 use strict;
       
    14 use XML::DOM;
       
    15 use Cwd;
       
    16 use Exporter;
       
    17 our @ISA = qw(Exporter);
       
    18 our @EXPORT_OK = qw(&getToolEnvironment &getLastErrorMsg &setDbgFile);
       
    19 
       
    20 my $sToolEnv  = "";
       
    21 my $sLocalDir = cwd();
       
    22 my $hParser      = XML::DOM::Parser->new();
       
    23 my $sErrorMsg = "";
       
    24 my $toolSummary = "";
       
    25 my $execLog = "";
       
    26 my $dbgfile = "";
       
    27 
       
    28 sub getToolEnvironment
       
    29 {
       
    30  my $sBuildEnvXML = shift;
       
    31  my (@aCat) = @_;
       
    32 
       
    33  my $hXMLfile     = $hParser->parsefile ($sBuildEnvXML);
       
    34  my $hNodes = $hXMLfile->getElementsByTagName ("context");
       
    35  my $hSystemTools, my $hP4Tools, my $hCBRTools, my $sCat, my $nNumNodes;
       
    36  
       
    37  if ($dbgfile ne "")
       
    38  {
       
    39 	print "Logging output to file $dbgfile\n";
       
    40 	open DBGFILE, ">>$dbgfile" or die; 
       
    41  }
       
    42 
       
    43  $sErrorMsg = "";
       
    44  $sToolEnv  = "";
       
    45  my $nCatDepth = scalar(@aCat);
       
    46  subTrace("Context Depth: $nCatDepth\n");
       
    47  my $nLevel = ($nCatDepth-1);
       
    48  $sCat = $aCat[$nLevel];
       
    49  $nNumNodes = $hNodes->getLength;
       
    50 
       
    51  my $hParentNode, my $sParentId;
       
    52  for (my $nI = 0; $nI < $nNumNodes ; $nI++)
       
    53  {
       
    54      my $hNode = $hNodes->item ($nI);
       
    55      my $hId = $hNode->getAttributeNode ("id");
       
    56      # Discriminating Stages
       
    57      subTrace("Context found: \"".$hId->getValue()."\"");
       
    58      if (uc($hId->getValue()) eq uc($sCat))
       
    59      {
       
    60      	subTrace("---> requested! analyzing...");
       
    61      	$hParentNode = $hNode->getParentNode;
       
    62 	     for (;$nLevel>0;$nLevel--)
       
    63 	     {
       
    64 		    $sParentId = $hParentNode->getAttributeNode("id")->getValue;
       
    65 	    	subTrace("...from Parent context= ".$sParentId);
       
    66 		    if ($sParentId ne $aCat[$nLevel-1]){ $nLevel =-1;}
       
    67 		    $hParentNode = $hParentNode->getParentNode;
       
    68 	     };
       
    69 	     if (($nLevel>-1)&&($hParentNode->getNodeName ne "context"))
       
    70 	     {
       
    71 		     $hSystemTools = $hNode->getElementsByTagName ("tool", 0);
       
    72 		     $nI = $nNumNodes;
       
    73 	     }
       
    74      }
       
    75  }
       
    76  if (!defined $hSystemTools)
       
    77  {
       
    78 	 subTrace("No Stage found for category $sCat !");
       
    79 	 subTrace("Program Terminated.");
       
    80 	 exit(0);
       
    81  }
       
    82  subTrace(""); 
       
    83  subTrace("----------------------");
       
    84  subTrace("Number of $sCat tools: ".$hSystemTools->getLength);
       
    85  for (my $nI = 0; $nI < $hSystemTools->getLength; $nI++)
       
    86  {
       
    87      my $hTool     = $hSystemTools->item ($nI);
       
    88      my $hToolName = $hTool->getAttributeNode ("name");
       
    89      subTrace("");
       
    90      subTrace("----------------------");
       
    91      subTrace("Verifying tool: ".$hToolName->getValue());
       
    92 	 my $isToolNotFound = "true";
       
    93 
       
    94      # Location element
       
    95      my $hToolLocation = $hTool->getElementsByTagName("location")->item(0);
       
    96      my $hToolLocationPath, my $hToolLocationStrict;
       
    97      if (defined $hToolLocation)
       
    98      {
       
    99              $hToolLocationPath   = $hToolLocation->getAttributeNode("value");
       
   100              $hToolLocationStrict = $hToolLocation->getAttributeNode("strict");
       
   101 	     subTrace("Expected Location: ".$hToolLocationPath->getValue());    
       
   102      }
       
   103 
       
   104      # Verify element
       
   105 	 my $hToolVerifys           = $hTool->getElementsByTagName("verify");
       
   106 	 my $nNumOfVerifys			= $hToolVerifys->getLength;
       
   107 	 for (my $nZ = 0; $nZ < $nNumOfVerifys; $nZ++)
       
   108 	 {
       
   109 		 my $hToolVerify            = $hTool->getElementsByTagName("verify")->item($nZ);
       
   110 		 my $hToolVerifyVersion     = $hToolVerify->getAttributeNode("version");
       
   111 		 my $hToolVerifyShowStopper = $hToolVerify->getAttributeNode("showstopper");
       
   112 		 subTrace("Expected Version: ".$hToolVerifyVersion->getValue());    
       
   113 		 subPrint("\nVerifying tool: ".$hToolName->getValue()." (version ".$hToolVerifyVersion->getValue().")\n");
       
   114 		 
       
   115 		 # Tool checks
       
   116 		 my $hToolChecks = $hToolVerify->getElementsByTagName("check");
       
   117 		 my $nNumOfChecks= $hToolChecks->getLength;
       
   118 		 my $nCheckNum;
       
   119 		 for (my $nJ = 0; $nJ < $nNumOfChecks; $nJ++)
       
   120 		 {
       
   121 			 $nCheckNum = $nJ+1;
       
   122 			 subTrace("");
       
   123 			 subTrace("Check #$nCheckNum:");
       
   124 			 subPrint("Check #$nCheckNum");
       
   125 			 my $hCheck = $hToolChecks->item($nJ);
       
   126 			 my $hCheckCmd    = $hCheck->getAttributeNode ("execute");
       
   127 			 my $hCheckVar    = $hCheck->getAttributeNode ("var");
       
   128 			 my $hCheckValues = $hCheck->getAttributeNode ("values");
       
   129 			 my $hCheckLog    = $hCheck->getAttributeNode ("log");
       
   130 			 my $hCheckExclu  = $hCheck->getAttributeNode ("exclusive");
       
   131 			 my $hCheckName   = $hCheck->getAttributeNode ("name");
       
   132 			 my $hCheckLocator= $hCheck->getAttributeNode ("locator");
       
   133 			 my $hCheckSilent = $hCheck->getAttributeNode ("silent");
       
   134 			 my $sCommand, my $sPattern, my $sToolCmd, my $sOut, my $sOutLocal, my $sCheckName;
       
   135 			 my $versionFound = "";
       
   136 			 my $sMsg = "";
       
   137 			 # If Check is exclusive, we will only test this one:
       
   138 			 if ($hCheckExclu->getValue() eq "true")
       
   139 			 {
       
   140 				 $nJ = $nNumOfChecks;
       
   141 				 subTrace("This check is exclusive: the remaining checks will not be tested");
       
   142 			 }
       
   143 			 # Preparing system command to execute
       
   144 			 if (defined $hCheckCmd) 
       
   145 			 {
       
   146 				 $sCommand = $hCheckCmd->getValue();
       
   147 				 if (defined $hToolLocation)
       
   148 				 {
       
   149 					 $sToolCmd= $hToolLocationPath->getValue();
       
   150 					 $sCommand =~ s/\%location\%/$sToolCmd/g;
       
   151 				 }
       
   152 				 
       
   153 				 # Execute the command
       
   154 				 subTrace("Check cmd: ".$sCommand."");
       
   155 				 $sOut = `$sCommand 2>&1`;
       
   156 				 $sOutLocal = $sOut;
       
   157 				 
       
   158 				 # Check if tool cannot be found at expected location
       
   159 				 if ($sOut =~/(not\srecognized)|(system\scannot\sfind)/)
       
   160 				 {
       
   161 					 $sOutLocal = "";
       
   162 					 subTrace("Tool cannot be found at location: ".$hToolLocationPath->getValue());
       
   163 					 subLogErrorMsg($hToolLocationStrict->getValue(), $hToolName->getValue()." CANNOT BE FOUND IN THE EXPECTED LOCATION: "
       
   164 								 .$hToolLocationPath->getValue());
       
   165 					 if ($hToolLocationStrict->getValue() eq "true")
       
   166 					 {
       
   167 						 subTrace("LOCATION STRICT activated, program terminated");
       
   168 						 $sMsg .= "[ERROR] wrong location\n";
       
   169 						 #return "";
       
   170 					 }
       
   171 					 else
       
   172 					 {
       
   173 						 $sMsg .= "[WARNING] wrong location\n";
       
   174 					 }	 
       
   175 				 }
       
   176 				 else
       
   177 				 {
       
   178 					 if ((defined $hToolLocation)&&(defined $hCheckCmd))
       
   179 					 {
       
   180 						$sCommand =~ /(.*)(\\).*$/i;
       
   181 						my $sTmp  = $1;
       
   182 						$sTmp =~ s/^\s+//;
       
   183 						$sTmp =~ s/\s+$//;
       
   184 						$sToolEnv = $sTmp.";".$sToolEnv;
       
   185 						subTrace("Tool path OK.");
       
   186 						subTrace("New appender: \"".$sToolEnv."\"");
       
   187 					 }
       
   188 					 $isToolNotFound = "false";
       
   189 				 }
       
   190 				 
       
   191 				 # Check if tool can be found with the Environment PATH
       
   192 				 $sCommand =~ s/.*(\\)(.*)$/\2/i;
       
   193 				 subTrace("Try command from ENV PATH: ".$sCommand);
       
   194 				 if (!defined $hCheckName) 
       
   195 				 {
       
   196 					$sCheckName = $sCommand;
       
   197 				 }
       
   198 				 else
       
   199 				 {
       
   200 					$sCheckName = $hCheckName->getValue();
       
   201 				 }
       
   202 				 subPrint(" [$sCheckName]: ");
       
   203 				 $sOut = `$sCommand 2>&1`;
       
   204 				 if ($sOut =~/(not\srecognized)|(system\scannot\sfind)/)
       
   205 				 {
       
   206 					 $sOut = "";
       
   207 					 my $type = "ERROR";
       
   208 					 if ($isToolNotFound eq "false") {$type = "WARNING";}
       
   209 					 subTrace("$type: ".$hToolName->getValue()." CANNOT BE FOUND IN THE SYSTEM PATH");
       
   210 					 subLogErrorMsg($isToolNotFound, $hToolName->getValue()." CANNOT BE FOUND IN THE SYSTEM PATH");
       
   211 					 $sMsg .= "[$type] NOT FOUND IN SYSTEM PATH\n";
       
   212 					 if ($isToolNotFound eq "true") {next;}
       
   213 					 #return "";
       
   214 				 }
       
   215 			 }
       
   216 			 else
       
   217 			 {	 # Check by verifying an env variable
       
   218 				 subPrint(" [".$hCheckVar->getValue()."]: ");
       
   219 				 $sCommand = "ECHO %".$hCheckVar->getValue()."%";
       
   220 				 subTrace("Check var: ".$hCheckVar->getValue());		     
       
   221 				 $sOut = `$sCommand 2>&1`;
       
   222 				 $sOut =~ s/^\s*//;
       
   223 				 $sOut =~ s/\s*$//;
       
   224 			 }
       
   225 			 
       
   226 			 # Log the output if necessary 
       
   227 			 if ($hCheckLog->getValue() eq "true")
       
   228 			 {
       
   229 					 subTrace("command: $sCommand");
       
   230 					 subTrace("logged output: ");
       
   231 					 subLog("Verifying \"".$hToolName->getValue()." [version ".$hToolVerifyVersion->getValue().
       
   232 					 "]\" -- check #$nCheckNum:");
       
   233 					 subLog("Executing: $sCommand");
       
   234 					 subLog($sOut);
       
   235 			 }
       
   236 			 
       
   237 			 # Perform the check against given values
       
   238 			 if (defined $hCheckValues)
       
   239 			 {
       
   240 				 my $sOutCopy = $sOut;
       
   241 				 my $bResultOk = "false";
       
   242 				 $sPattern = $hCheckValues->getValue();
       
   243 				 if (defined $hCheckLocator)
       
   244 				 {
       
   245 					 my $sLocator = $hCheckLocator->getValue();
       
   246 					 $sOut =~ s/.*$sLocator.*/\1/msi;
       
   247 					 subTrace("Searching in locator: ".$sOut);
       
   248 					 $sOutLocal =~ s/.*$sLocator.*/\1/msi;
       
   249 				 }
       
   250 				 else
       
   251 				 {
       
   252 					$sOut =~ s/.*($sPattern).*/\1/smi;
       
   253 					$sOutLocal =~ s/.*($sPattern).*/\1/smi;
       
   254 				 }
       
   255 				 
       
   256 				# Log the current version found
       
   257 				$versionFound = $sOut.$sOutLocal;
       
   258 				if ($sOut eq $sOutLocal)
       
   259 				{
       
   260 					$versionFound = $sOut;
       
   261 				}
       
   262 				elsif (($sOut.$sOutLocal ne $sOut) and ($sOut.$sOutLocal ne $sOutLocal) and ($sOut ne $sOutLocal))
       
   263 				{
       
   264 					$versionFound = "$sOut or $sOutLocal [Value Mismatch]";
       
   265 				}
       
   266 				if ($versionFound eq "") {$versionFound = "not found";}
       
   267 				 
       
   268 				 #$sPattern =~ s/(\\|\/|\.|\*|\[|\]|\(|\)|\$|\{|\})/\\\1/g;
       
   269 				 #$sPattern = "($sPattern)";
       
   270 				 subTrace("Check against pattern: ".$sPattern);
       
   271 				 # Check the Environment Variable
       
   272 				 if (defined $hCheckVar)
       
   273 				 {
       
   274 				    $sOut = $sOutCopy;
       
   275 					my $valueTested = $hCheckVar->getValue();
       
   276 					if ($sOut =~ /%$valueTested%/) {$sOut = "not defined!";}
       
   277 					elsif ( $sOut =~ m/$sPattern/smi )
       
   278 					{
       
   279 						$bResultOk = "true";
       
   280 						if ($hCheckSilent->getValue() eq "true") { $sOut = "ok. ";}
       
   281 					}
       
   282 					$versionFound = "$sOut";
       
   283 				 } # Or Check the command executed (for version checking...)
       
   284 				 elsif ( ( ( $sOut =~ s/.*($sPattern).*/\1/smi ) and (( $sOutLocal =~ s/.*($sPattern).*/\1/smi ) or ($sOutLocal eq "")) ) || 
       
   285 				      ( ( $sOutLocal =~ s/.*($sPattern).*/\1/smi ) and ($sOut eq "") ) )
       
   286 				 {
       
   287 					$bResultOk = "true";
       
   288 					subTrace("Value OK");
       
   289 					if ($versionFound eq "" || $hCheckSilent->getValue() eq "true") { $versionFound = "ok. ";}
       
   290 					
       
   291 				 }
       
   292 				 elsif (!defined $hCheckLocator)
       
   293 				 {
       
   294 					$versionFound = "";
       
   295 				 }
       
   296 
       
   297 				 # Log the warnings/errors regarding version checking
       
   298 				 if ($bResultOk eq "false")
       
   299 				 {
       
   300 					 if ($versionFound eq "") { $versionFound = "wrong value. ";}
       
   301 					 subTrace("ERROR: VALUE CHECK FAILED");
       
   302 					 subLogErrorMsg($hToolVerifyShowStopper->getValue(), $hToolName->getValue()." VALUE INCORRECT");
       
   303 					 if ($hToolVerifyShowStopper->getValue() eq "true")
       
   304 					 {
       
   305 						 $sMsg .= "[ERROR] WRONG VALUE\n";
       
   306 						 subTrace("SHOWSTOPPER RAISED: program should be terminated");
       
   307 					 }
       
   308 					 else
       
   309 					 {
       
   310 						 subTrace("No Showstopper, continuing analysis...");
       
   311 						 $sMsg .= "[WARNING] Wrong Value\n";
       
   312 					 }
       
   313 					 
       
   314 				 }
       
   315 			 }
       
   316 			 chomp($versionFound);
       
   317 			 &subPrint ("$versionFound\n$sMsg");
       
   318 			 &printToolSummary;
       
   319 			 &printLog;
       
   320 		 }		
       
   321 	 }
       
   322 
       
   323      
       
   324  }
       
   325  close DBGFILE;
       
   326  return $sToolEnv;
       
   327 }
       
   328 
       
   329 sub subTrace
       
   330 {
       
   331   my $sString = $_[0];
       
   332   if ($dbgfile ne "")
       
   333   {
       
   334 	print DBGFILE "$sString\n";
       
   335   }
       
   336 }
       
   337 
       
   338 sub subPrint
       
   339 {
       
   340   my $sString = $_[0];
       
   341   $toolSummary .= "$sString";
       
   342 }
       
   343 
       
   344 sub printToolSummary
       
   345 {
       
   346   print $toolSummary."\n";
       
   347   $toolSummary="";
       
   348 }
       
   349 
       
   350 sub subLog
       
   351 {
       
   352   my $sString = $_[0];
       
   353   $execLog .= "[LOG] $sString\n";
       
   354   
       
   355   if ($dbgfile ne "")
       
   356   {
       
   357 	print DBGFILE "[LOG] $sString\n";
       
   358   }
       
   359 }
       
   360 
       
   361 sub printLog
       
   362 {
       
   363   #print $execLog;
       
   364   $execLog="";
       
   365 }
       
   366 
       
   367 sub subLogErrorMsg
       
   368 {
       
   369   my $isError = shift;
       
   370   my $msg = shift;
       
   371   my $type = "WARNING";
       
   372   if ($isError eq "true") {$type = "ERROR";} 
       
   373   $sErrorMsg = $sErrorMsg."\n[$type] ".$msg;
       
   374 }
       
   375 
       
   376 sub getLastErrorMsg
       
   377 {
       
   378   return $sErrorMsg;
       
   379 }
       
   380 
       
   381 sub setDbgFile
       
   382 {
       
   383   $dbgfile = shift;
       
   384 }
       
   385 
       
   386 1;