bldsystemtools/commonbldutils/clean.pl
branchRCL_3
changeset 5 d90029decf65
parent 4 a9d4531388d0
child 6 5e522efbae7b
equal deleted inserted replaced
4:a9d4531388d0 5:d90029decf65
     1 #
       
     2 # Copyright (c) 2009 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 use strict;
       
    18 
       
    19 use Getopt::Long;
       
    20 use File::Path;
       
    21 use File::Spec::Functions;
       
    22 
       
    23 my $gRealTimeBuildErrors = 0;
       
    24 sub RealTimeBuildError($)
       
    25 {
       
    26 	$gRealTimeBuildErrors++;
       
    27 	print STDERR "ERROR: RealTimeBuild: ", @_, "\n";
       
    28 	return "RealTimeBuild error";
       
    29 }
       
    30 
       
    31 # Process the commandline
       
    32 my ($iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose) = ProcessCommandLine();
       
    33 
       
    34 my ($iDirList)= ProcessList($iDataSource, $iSrc, $iMRPSrc, $platform, $iVerbose);
       
    35 my @delete;
       
    36 
       
    37 search_dir($iSrc, $iDirList, \@delete, $iVerbose);
       
    38 
       
    39 foreach my $leftover (keys %$iDirList)
       
    40 {
       
    41   print "REMARK: LEFTOVER: $leftover ($$iDirList{$leftover})\n";
       
    42 }
       
    43 
       
    44 if ($gRealTimeBuildErrors && !$iDummy)
       
    45 {
       
    46   print STDERR "\nWARNING: Files will NOT be deleted, because of earlier real time build errors\n\n";
       
    47   $iDummy = 1;
       
    48 }
       
    49 
       
    50 foreach my $delete (@delete)
       
    51 {
       
    52   if ($iDummy)
       
    53   {
       
    54     print "REMARK: $delete is not referenced by any MRP\n";
       
    55   } else {
       
    56     # Delete the files or directories
       
    57     # make sure it is not read only
       
    58     #Convert back to \ for dos command
       
    59     $delete =~ s#\/#\\#g;
       
    60     system("attrib -r /s /d \"$delete\"");
       
    61     my $deletenum = rmtree($delete);
       
    62     if ($deletenum == 0)
       
    63     {
       
    64       RealTimeBuildError("failed to deleted $delete");
       
    65     } elsif (-d "$delete") {
       
    66       RealTimeBuildError("failed to deleted directory $delete"); #Because it still exists
       
    67     } elsif ($deletenum > 1) {
       
    68       print "REMARK: deleted $deletenum files in directory $delete as they are not referenced by any MRP\n";
       
    69     } else {
       
    70       print "REMARK: deleted $delete as it is not referenced by any MRP\n";
       
    71     }
       
    72   }
       
    73 }
       
    74 
       
    75 sub search_dir
       
    76 { 
       
    77   my ($dir, $iDirList, $delete, $iVerbose) = @_; 
       
    78   my @flist;
       
    79   
       
    80   print "Processing $dir\n" if ($iVerbose);
       
    81   if (opendir(DIRH,"$dir"))
       
    82   {
       
    83     @flist=readdir(DIRH); 
       
    84     closedir DIRH; 
       
    85     ENTRY: foreach my $entry (@flist)
       
    86     { 
       
    87       # ignore . and .. : 
       
    88       next if ($entry eq "." || $entry eq "..");
       
    89       my $partial_match;
       
    90       # Check entry again $iDirList for matches
       
    91       foreach my $sourceline (keys %$iDirList)
       
    92       {
       
    93         if ($sourceline =~ m#^$dir/$entry$#i)
       
    94         {
       
    95           # Exact match delete entry in %$iDirList
       
    96           # Check to see if something has already partial matched
       
    97           print "REMARK: $dir/$entry is probably covered more than once\n" if ($partial_match);
       
    98           if ($iVerbose)
       
    99           {
       
   100             if (-d "$dir/$entry")
       
   101             {
       
   102               print "Keeping directory $dir/$entry ($$iDirList{$sourceline})\n";
       
   103             } else {
       
   104               print "Keeping file $dir/$entry ($$iDirList{$sourceline})\n";
       
   105             }
       
   106           }
       
   107           delete $$iDirList{$sourceline};
       
   108           # No more processing required
       
   109           next ENTRY;
       
   110         }
       
   111         # Check to see if there is reference to inside this directory
       
   112         if ($sourceline =~ m#^$dir/$entry/#i)
       
   113         {
       
   114           # something reference this as a directory need more processing
       
   115           $partial_match = 1 if (-d "$dir/$entry");
       
   116         }
       
   117       }
       
   118       if ($partial_match)
       
   119       {
       
   120         search_dir("$dir/$entry", $iDirList, $delete, $iVerbose) if (-d "$dir/$entry");
       
   121         next ENTRY;
       
   122       }
       
   123       # No match place on deletion list
       
   124       push @$delete, "$dir/$entry";
       
   125       print "Marking $dir/$entry for delete\n" if ($iVerbose);
       
   126     }
       
   127   }else{ 
       
   128     RealTimeBuildError("can not read directory $dir"); 
       
   129   } 
       
   130 }
       
   131 
       
   132 # ProcessList
       
   133 #
       
   134 # Inputs
       
   135 # $iDataSource - ref to array of files to process
       
   136 # $iSrc - real location of source files
       
   137 # $iMRPSrc - where the mrp thinks they are
       
   138 #
       
   139 # Outputs
       
   140 #
       
   141 # Description
       
   142 # This function processes mrp files
       
   143 sub ProcessList
       
   144 {
       
   145   my ($iDataSource, $iSrc, $iMRPSrc, $platform, $iVerbose) = @_;
       
   146   
       
   147   my %Sources;
       
   148   my @ComponentList;
       
   149   my %mrpHash;
       
   150   
       
   151   # Need the dir swap
       
   152   $iDataSource =~ s/^$iMRPSrc/$iSrc/;
       
   153   # Read the options.txt
       
   154   open OPTIONS, $iDataSource or die RealTimeBuildError("Cannot open $iDataSource $!");
       
   155   while(<OPTIONS>)
       
   156   {
       
   157     if (/^GT\+Techview baseline mrp location:\s*(\S+)\s*$/i)
       
   158     {
       
   159       $mrpHash{lc $1} = $1;
       
   160       next;
       
   161     }
       
   162     if (/^GT only baseline mrp location:\s*(\S+)\s*$/i)
       
   163     {
       
   164       $mrpHash{lc $1} = $1;
       
   165       next;
       
   166     }
       
   167     if (/^Strong crypto mrp location:\s*(\S+)\s*$/i)
       
   168     {
       
   169       $mrpHash{lc $1} = $1;
       
   170       next;
       
   171     }
       
   172     if (/^Techview component list:\s*(\S+)\s*$/i)
       
   173     {
       
   174       push @ComponentList, $1;
       
   175       next;
       
   176     }
       
   177     if (/^GT component list:\s*(\S+)\s*$/i)
       
   178     {
       
   179       push @ComponentList, $1;
       
   180       next;
       
   181     }
       
   182   }
       
   183   close OPTIONS;
       
   184   for (my $i = 0; $i < scalar(@ComponentList); $i++)
       
   185   {
       
   186     # Fix path
       
   187     $ComponentList[$i] =~ s#\\#\/#g;
       
   188     # Need the dir swap
       
   189     $ComponentList[$i] =~ s/^$iMRPSrc/$iSrc/;
       
   190     open IN, $ComponentList[$i] or die RealTimeBuildError("Cannot open ".$ComponentList[$i]." $!");
       
   191     while(<IN>)
       
   192     {
       
   193       my ($mrp) = /^\s*\S+\s+(\S+)\s*$/;
       
   194       $mrpHash{lc $mrp} = $mrp;
       
   195     }
       
   196     close IN;
       
   197   }
       
   198 
       
   199   my @mrpList = sort values %mrpHash;
       
   200   for (my $i = 0; $i < scalar(@mrpList); $i++)
       
   201   {
       
   202     # Fix path
       
   203     $mrpList[$i] =~ s#\\#\/#g;
       
   204     # Need the dir swap
       
   205     $mrpList[$i] =~ s/^$iMRPSrc/$iSrc/i;
       
   206     # Fix the CustKit / Devkit and techviewexamplesdk mrp locations
       
   207     $mrpList[$i] =~ s#^/product/CustKit#$iSrc/os/unref/orphan/cedprd/CustKit#i;
       
   208     $mrpList[$i] =~ s#^/product/DevKit#$iSrc/os/unref/orphan/cedprd/DevKit#i;
       
   209     $mrpList[$i] =~ s#^/product/techviewexamplesdk#$iSrc/os/unref/orphan/cedprd/techviewexamplesdk#i;
       
   210     $Sources{"$iSrc/os/unref/orphan/cedprd/SuppKit"} = "clean.pl";
       
   211     $Sources{"$iSrc/os/unref/orphan/cedprd/tools"} = "clean.pl";
       
   212     $Sources{"$iSrc/os/buildtools/toolsandutils/productionbldtools"} = "clean.pl";
       
   213     
       
   214     if (open MRP, $mrpList[$i])
       
   215     {
       
   216       my $mrpfile = $mrpList[$i];
       
   217       my $mrpfile_in_source = 0;
       
   218       
       
   219       while(<MRP>)
       
   220       {
       
   221         my $dir;
       
   222         if (/^\s*source\s+(\S.*\S)\s*$/i)		# must allow for spaces in names
       
   223         {
       
   224           my $origdir = $1;
       
   225           $dir = $origdir;
       
   226          
       
   227           #Find any relative paths and add them to the end of the mrp location to create a full path
       
   228           if (($dir =~ /\.\\/)||($dir =~ /\.\.\\/)||($dir !~ /\\/))
       
   229           {
       
   230 		  $dir =~ s#\.\.#\.#g;		# .. becomes .	
       
   231 		  $dir =~ s#^\.#\\\.#g;		# add an extra \ incase one is not present at start of path, canonpath wil cleanup multiple \
       
   232 	  
       
   233 		  $dir = "\\".$dir if ($dir !~ /\\/);	#add \ to start of path if source line only specifies a file
       
   234 		  
       
   235 		  # Fix paths to /
       
   236 		  $dir =~ s#\\#\/#g;
       
   237 		  
       
   238 		  $dir =~ s/$iMRPSrc/$iSrc/i if ($dir =~ /^$iMRPSrc/);
       
   239 		  
       
   240 		  $dir = $iSrc.$dir if ($dir !~ /^$iMRPSrc/);
       
   241 		  
       
   242 		  $dir = canonpath($dir);
       
   243           }
       
   244 	  
       
   245 	  # Fix paths to /
       
   246 	  $dir =~ s#\\#\/#g;
       
   247 	  
       
   248           # Remove any / from the end of the sourceline just in case the directory was ended in one
       
   249           $dir =~ s#\/$##;
       
   250           # Need the dir swap
       
   251           $dir =~ s/^$iMRPSrc/$iSrc/i;
       
   252           # Fix the CustKit / Devkit and techviewexamplesdk mrp locations
       
   253           $dir =~ s#^/product/CustKit#$iSrc/os/unref/orphan/cedprd/CustKit#i;
       
   254           $dir =~ s#^/product/DevKit#$iSrc/os/unref/orphan/cedprd/DevKit#i;
       
   255           $dir =~ s#^/product/techviewexamplesdk#$iSrc/os/unref/orphan/cedprd/techviewexamplesdk#i;
       
   256           
       
   257           if ($mrpfile =~ /^$dir$/i || $mrpfile =~ /^$dir\//i) {
       
   258             # mrpfile covered by source statements
       
   259             $mrpfile_in_source = 1;
       
   260           }
       
   261 
       
   262           # ignore location of release notes
       
   263           next if ($dir =~ /^\/component_defs/i);
       
   264           
       
   265           if (!-e $dir) {
       
   266             # CBR tools consider missing source as a fatal error
       
   267             RealTimeBuildError("$dir does not exist (listed as source in $mrpfile)");
       
   268           } elsif (!defined $Sources{$dir}) {
       
   269             $Sources{$dir} = $mrpfile;
       
   270           } else {
       
   271             print "REMARK: $origdir in $mrpfile is already defined in $Sources{$dir}\n";
       
   272           }
       
   273         }
       
   274       }
       
   275       close MRP;
       
   276       print "REMARK: $mrpList[$i] does not include itself as source\n" if (!$mrpfile_in_source);
       
   277     } else {
       
   278       RealTimeBuildError("Cannot open ".$mrpList[$i]." $!");
       
   279     }
       
   280   }
       
   281   return \%Sources;
       
   282 }
       
   283 
       
   284 # ProcessCommandLine
       
   285 #
       
   286 # Inputs
       
   287 #
       
   288 # Outputs
       
   289 # @iDataSource array of multiple (txt file(s) to process)
       
   290 # $iSrc - real location of files
       
   291 # $iMRPSrc - where the mrp thinks they are
       
   292 # @iDummy - do not delete anything
       
   293 #
       
   294 # Description
       
   295 # This function processes the commandline
       
   296 
       
   297 sub ProcessCommandLine {
       
   298   my ($iHelp, $iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose);
       
   299   GetOptions('h' => \$iHelp, 'o=s' =>\$iDataSource, 's=s' =>\$iSrc, 'm=s' =>\$iMRPSrc, 'p=s' =>\$platform,'n' => \$iDummy, 'v' => \$iVerbose);
       
   300 
       
   301   if (($iHelp) || (!defined $iSrc) || (!defined $iMRPSrc) || (!defined $platform))
       
   302   {
       
   303     Usage();
       
   304   }
       
   305 
       
   306   die RealTimeBuildError("Source directory $iSrc must be an absolute path with no drive letter") if ($iSrc !~ m#^[\\\/]#);
       
   307   die RealTimeBuildError("Source directory $iMRPSrc must be an absolute path with no drive letter") if ($iMRPSrc !~ m#^[\\\/]#);
       
   308   # Fix the paths
       
   309   $iSrc =~ s#\\#\/#g;
       
   310   $iMRPSrc =~ s#\\#\/#g;
       
   311   $iDataSource =~ s#\\#\/#g;
       
   312   if (! -d "$iSrc")
       
   313   {
       
   314     die RealTimeBuildError("$iSrc is not a directory") ;
       
   315   }
       
   316 
       
   317   # Need the dir swap
       
   318   $iDataSource =~ s/^$iMRPSrc/$iSrc/i;
       
   319   if (! -e "$iDataSource")
       
   320   {
       
   321     die RealTimeBuildError("Cannot open $iDataSource");
       
   322   }
       
   323 
       
   324   return($iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose);
       
   325 }
       
   326 
       
   327 # Usage
       
   328 #
       
   329 # Output Usage Information.
       
   330 #
       
   331 
       
   332 sub Usage {
       
   333   print <<USAGE_EOF;
       
   334 
       
   335   Usage: clean.pl [options]
       
   336 
       
   337   options:
       
   338 
       
   339   -h  help
       
   340   -o  options.txt to process
       
   341   -s  Source directory to process
       
   342   -m  MRP source directory
       
   343   -p  platform of product (beech or cedar)
       
   344   -n  Not do anything (dummy run)
       
   345   -v  Verbose
       
   346   
       
   347   Note:
       
   348   Due to CustKit using the clean-src directory (%clean-src%) for source
       
   349   The files need to be deleted from the %clean-src% directory
       
   350   This tool substitutes directory specified by -m with the directory
       
   351   specified by -s in all locations
       
   352   This means that options.txt and the component lists and mrp's must be
       
   353   referenced in the directory
       
   354   specified by -m
       
   355 
       
   356 USAGE_EOF
       
   357   exit 1;
       
   358 }