group/cleaner.pl
branchRCL_3
changeset 23 13a33d82ad98
parent 0 822a42b6c3f1
equal deleted inserted replaced
22:826cea16efd9 23:13a33d82ad98
       
     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 the License "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:  Cleaner script to perform more thorough cleanup than abld reallyclean#
       
    15 
       
    16 use strict;
       
    17 use constant DEBUG => 0;
       
    18 
       
    19 my $iCleanup_file = "cleanup.txt";
       
    20 my @iErrors = ();
       
    21 my $iTag_platform = "__PLATFORM__";
       
    22 my $iTag_target = "__TARGET__";
       
    23 my @iPlatforms = ( "winscw", "armv5" );
       
    24 my @iTargets = ( "udeb", "urel" );
       
    25 my $iSlowClean = 0;
       
    26 my $iTest = 0;
       
    27 
       
    28 # ----------------------------------------------------------------------------
       
    29 # Main
       
    30 # ----------------------------------------------------------------------------
       
    31 
       
    32 my $iCommand = shift @ARGV || "";
       
    33 
       
    34 if ( $iCommand eq "clean" ) {
       
    35 
       
    36     print "Running cleanup...\n";
       
    37     clean();
       
    38 
       
    39 } elsif ( $iCommand eq "slowclean" ) {
       
    40 
       
    41     $iSlowClean = 1;
       
    42     clean();
       
    43 
       
    44 } elsif ( $iCommand eq "test" ) {
       
    45 
       
    46     print "Running test...\n";
       
    47     $iTest = 1;
       
    48     clean();
       
    49 
       
    50 } elsif ( $iCommand eq "init" ) {
       
    51 
       
    52     init_environment();
       
    53 
       
    54 } elsif ( $iCommand eq "generate" ) {
       
    55 
       
    56     generate();
       
    57 
       
    58 } else {
       
    59     print "Usage: $0 <command>\n";
       
    60     print "Commands:\n";
       
    61     print "  init      Initializes the build environment by calling 'bldmake bldfiles' and 'abld makefile'.\n";
       
    62     print "            This needs to be run only if the environment has not been built yet and 'abld build -what' fails.\n";
       
    63     print "  generate  Generates a configuration file of releasables to be cleaned.\n";
       
    64     print "  clean     Cleans the build quickly by deleting all matching files from the configuration file.\n";
       
    65     print "  slowclean Slower and more thorough clean that deletes all similar files in addition to the ones normal clean deletes.\n";
       
    66     print "            Example: Cleanup list contains file 'binary.dll'. Slowclean will also delete files named\n";
       
    67     print "            'binary.dll.foo' and 'binary.dll.bar'\n";
       
    68     print "  test      Runs a test to see what files would be deleted on a real cleanup run.\n";
       
    69     print "            Shows only files that would not be deleted by 'abld reallyclean'.\n";
       
    70     exit 0;
       
    71 }
       
    72 
       
    73 # Print possible errors
       
    74 print join "\n", @iErrors;
       
    75 
       
    76 exit 0;
       
    77 
       
    78 # ----------------------------------------------------------------------------
       
    79 # Subroutines
       
    80 # ----------------------------------------------------------------------------
       
    81 
       
    82 
       
    83 # ----------------------------------------------------------------------------
       
    84 # Initializes the environment to be able to call 'abld build -what'
       
    85 # ----------------------------------------------------------------------------
       
    86 #
       
    87 sub init_environment {
       
    88     # Initialize the environment in order to get "abld build -what" to run properly
       
    89     print "Initializing build environment...\n";
       
    90 
       
    91     print "Calling 'bldmake bldfiles'\n";
       
    92     system "bldmake bldfiles";
       
    93 
       
    94     print "Calling 'abld export'\n";
       
    95     system "abld export";
       
    96 
       
    97     print "Calling 'abld makefile winscw'\n";
       
    98     system "abld makefile winscw";
       
    99 
       
   100     print "Calling 'abld makefile armv5'\n";
       
   101     system "abld makefile armv5";
       
   102 
       
   103     print "Done.\n";
       
   104 }
       
   105 
       
   106 
       
   107 # ----------------------------------------------------------------------------
       
   108 # Generates the cleanup list by calling 'abld build -what' and parsing it
       
   109 # ----------------------------------------------------------------------------
       
   110 #
       
   111 sub generate {
       
   112     print "Attempting to generate the cleanup list...\n";
       
   113 
       
   114     # Get the list of releasables from the build tools
       
   115     open my $in, "abld build -what 2>&1 |" or die "Cannot open pipe! $!";
       
   116 
       
   117     my %parsed;
       
   118     while ( <$in> ) {
       
   119         if ( /No such file or directory/  ) {
       
   120             push @iErrors, "Unable to generate cleanup list. The environment is not ready. Run 'init' before running 'generate'";
       
   121             last;
       
   122         }
       
   123 
       
   124         $_ = lc $_;
       
   125         next unless /^\\epoc32/;
       
   126 
       
   127         s/release\\(?:winscw|armv5)\\(?:udeb|urel)/release\\$iTag_platform\\$iTag_target/i;
       
   128 
       
   129         # Cenrep file. Add the corresponding .cre file to the cleanup list
       
   130         if ( /(.+?winscw\\c\\private\\10202be9\\)(.{8})\.txt/ ) {
       
   131             $parsed{ $1 . "persists\\" . $2 . ".cre\n" }++;
       
   132         }
       
   133 
       
   134         $parsed{ $_ }++;
       
   135     }
       
   136 
       
   137     close $in;
       
   138 
       
   139     if ( @iErrors > 0 ) {
       
   140         return;
       
   141     }
       
   142 
       
   143     open my $out, ">$iCleanup_file" or die "Cannot open file $iCleanup_file! $!";
       
   144     print $out join "", sort keys %parsed;
       
   145     close $out;
       
   146 }
       
   147 
       
   148 
       
   149 # ----------------------------------------------------------------------------
       
   150 # Cleans up the environment by deleting all files found from the cleanup list
       
   151 # Cleaning is done by finding all files that match the string in the list in
       
   152 # so all similarly named files are also deleted. (CodeWarrior temporary files etc )
       
   153 # ----------------------------------------------------------------------------
       
   154 #
       
   155 sub clean {
       
   156 
       
   157     if ( !-e $iCleanup_file ) {
       
   158         push @iErrors, "Cleanup file $iCleanup_file not found! Run 'generate' to generate it.";
       
   159         return;
       
   160     }
       
   161 
       
   162     open my $in, "$iCleanup_file" or die "Cannot open cleanup file $iCleanup_file! $!";
       
   163     my @releasables = <$in>;
       
   164     close $in;
       
   165 
       
   166     my $total = scalar @releasables;
       
   167     print "$total rules found from cleanup list...\n";
       
   168 
       
   169     my @deleted = ();
       
   170     my $progress = 0;
       
   171     foreach ( @releasables ) {
       
   172         $progress++;
       
   173 
       
   174         next if $_ eq "";
       
   175 
       
   176         # Found __PLATFORM__ tag. Substitute it with all known platforms and add them back to the array
       
   177         if ( /$iTag_platform/ ) {
       
   178             foreach my $platform ( @iPlatforms ) {
       
   179                 my $new_releasable = $_;
       
   180                 $new_releasable =~ s/$iTag_platform/$platform/;
       
   181                 debug( "Adding releasable: $new_releasable" );
       
   182                 push @releasables, $new_releasable;
       
   183                 $total++; # Adjust the total amount of files to be deleted
       
   184             }
       
   185 
       
   186             # Move on to the next round
       
   187             next;
       
   188         }
       
   189 
       
   190         # Found __TARGET__ tag. Substitute it with all known targets and add them back to the array
       
   191         if ( /$iTag_target/ ) {
       
   192             foreach my $target ( @iTargets ) {
       
   193                 my $new_releasable = $_;
       
   194                 $new_releasable =~ s/$iTag_target/$target/;
       
   195                 debug( "Adding releasable: $new_releasable" );
       
   196                 push @releasables, $new_releasable;
       
   197                 $total++; # Adjust the total amount of files to be deleted
       
   198             }
       
   199 
       
   200             # Move on to the next round
       
   201             next;
       
   202         }
       
   203 
       
   204         # At this point there is nothing to substitute. Find all files matching the releasable name and delete them
       
   205         chomp; # Get rid of newline at the end
       
   206 
       
   207         my $releasable = lc $_;
       
   208         my @files_found;
       
   209         if ( $iSlowClean == 1 ) {
       
   210             @files_found = glob "$_*";
       
   211         } else {
       
   212             push @files_found, $releasable;
       
   213         }
       
   214         foreach my $file ( @files_found ) {
       
   215             next unless $file =~ /^\\epoc32/; # Some kind of safeguard, just in case
       
   216 
       
   217             if ( $iTest == 1 ) {
       
   218 
       
   219                 # If the file is not one of the releasables, it would not be deleted by reallyclean
       
   220                 # So add it to the list of files that would only be deleted by this script
       
   221 
       
   222                 $file = lc $file;
       
   223                 if ( $file ne $releasable || $file =~ /\.cre$/ ) {
       
   224                     push @deleted, $file;
       
   225                 }
       
   226 
       
   227             } else {
       
   228 
       
   229                 my $err = 0;
       
   230                 unlink $file or ( $err = -1 );
       
   231                 if ( $err ne -1 ) {
       
   232                     push @deleted, $file;
       
   233                 }
       
   234             }
       
   235         }
       
   236 
       
   237         # Report progress
       
   238         my $percent = ( $progress / $total ) * 100;
       
   239         printf "\r  ( %d / %d ) % .1f %%", $progress, $total, $percent;
       
   240     }
       
   241 
       
   242     if ( @deleted > 0 ) {
       
   243         print "\n\nSummary:\n";
       
   244         foreach my $deleted ( sort @deleted ) {
       
   245             if ( $iTest ) {
       
   246                 print "Would be deleted: $deleted\n";
       
   247             } else {
       
   248                 print "Deleted: $deleted\n";
       
   249             }
       
   250         }
       
   251     } else {
       
   252         print "\n\nNothing to be done.\n";
       
   253     }
       
   254 }
       
   255 
       
   256 
       
   257 # ----------------------------------------------------------------------------
       
   258 # Debug output. Prints a string if the debug flag is defined
       
   259 # ----------------------------------------------------------------------------
       
   260 #
       
   261 sub debug {
       
   262     my $msg = shift;
       
   263     if ( DEBUG ) {
       
   264         print $msg;
       
   265     }
       
   266 }