kernel/eka/rombuild/rom.pl
changeset 0 a41df078684a
child 31 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 #!perl
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of the License "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description:
       
    16 #
       
    17 
       
    18 # rom.pl - Build a rom
       
    19 #
       
    20 # Pre-processes the .oby/iby files then invokes rombuild.exe
       
    21 # (or other specified builder)
       
    22 
       
    23 # First, read our config file
       
    24 
       
    25 use strict;
       
    26 use Getopt::Long;
       
    27 use Cwd;
       
    28 
       
    29 #Getopt::Long::Configure("ignore_case");
       
    30 my %opts=();
       
    31 my $param_count = scalar(@ARGV);
       
    32 my $result = GetOptions (\%opts, "assp=s",
       
    33 						 "inst=s",
       
    34 						 "type=s",
       
    35 						 "variant=s",
       
    36 						 "build=s", 
       
    37 						 "conf=s",
       
    38 						 "name=s",
       
    39 						 "modules=s",
       
    40 						 "xabi=s",
       
    41 						 "clean",
       
    42 						 "quiet",
       
    43 						 "help",
       
    44 						 "_debug",
       
    45 						 "zip",
       
    46 						 "symbol",
       
    47 						 "noheader",
       
    48 						 "kerneltrace=s",
       
    49 						 "rombuilder=s",
       
    50 						 "define=s@",
       
    51 						 "rofsbuilder=s",
       
    52 						 "compress"
       
    53 						 );
       
    54 
       
    55 my (@assps, @builds, %variants, @templates, %flags, %insts, %zip, %builder);
       
    56 my $main;
       
    57 my $kmain;
       
    58 my $toroot;
       
    59 my $e32path;
       
    60 my $rombuildpath;
       
    61 my $euserdir;
       
    62 my $elocldir;
       
    63 my $kbdir;
       
    64 my $romname;
       
    65 my $single;
       
    66 my $smain;
       
    67 my $pagedCode;
       
    68 my $debug = $opts{_debug};
       
    69 my $quiet;
       
    70 $quiet = $opts{quiet} unless $debug;
       
    71 
       
    72 my $drive;
       
    73 
       
    74 # discover where this script is running from to set the $toroot and $e32path variables
       
    75 
       
    76 my $toolpath;
       
    77 my $Epoc32Path;
       
    78 my $EpocRoot;
       
    79 my $drive;
       
    80 my $BasePath;
       
    81 
       
    82 BEGIN {
       
    83 
       
    84 	$EpocRoot = $ENV{EPOCROOT};
       
    85 	die "ERROR: Must set the EPOCROOT environment variable.\n" if (!defined($EpocRoot));
       
    86 	$EpocRoot =~ s-/-\\-go;	# for those working with UNIX shells
       
    87 	die "ERROR: EPOCROOT must be an absolute path, " .
       
    88 		"not containing a drive letter.\n" if ($EpocRoot !~ /^\\/);
       
    89 	die "ERROR: EPOCROOT must not be a UNC path.\n" if ($EpocRoot =~ /^\\\\/);
       
    90 	die "ERROR: EPOCROOT must end with a backslash.\n" if ($EpocRoot !~ /\\$/);
       
    91 	die "ERROR: EPOCROOT must specify an existing directory.\n" if (!-d $EpocRoot);
       
    92 
       
    93 	# The cpp needed a drive apparently
       
    94 	my $cwd = Cwd::cwd();
       
    95 	$cwd =~ /^(.)/;
       
    96 	$drive = "$1:";
       
    97 
       
    98 	my $fp0 = $0;
       
    99 	$cwd =~ s/\//\\/g;
       
   100 	$fp0 =~ s/\//\\/g;
       
   101 	unless ($fp0 =~ /^([A-Za-z]:)?\\/) {
       
   102 		if ($cwd =~ /\\$/) {
       
   103 			$fp0 = "$cwd$fp0";
       
   104 		} else {
       
   105 			$fp0 = "$cwd\\$fp0";
       
   106 		}
       
   107 	}
       
   108 
       
   109 	my @path = split(/\\/, $cwd);
       
   110 	shift(@path);
       
   111 	$toroot = ('..\\') x @path;
       
   112 	$e32path = $fp0;
       
   113 	$e32path =~ s/\\kernelhwsrv\\kernel\\eka\\rombuild\\rom\.pl$//i;
       
   114 	$e32path =~ s/^[A-Za-z]://;
       
   115 	$rombuildpath = $toroot."os\\kernelhwsrv\\kernel\\eka\\rombuild";
       
   116 	$Epoc32Path = $toroot;
       
   117 	$Epoc32Path =~ s/\\$//;
       
   118 	$Epoc32Path .= $EpocRoot . "epoc32";
       
   119 	$toolpath = "$Epoc32Path\\tools\\";
       
   120 	push @INC, $toolpath;
       
   121 	$BasePath = $toroot;
       
   122 	$BasePath =~ s/\\$/$e32path\\/;
       
   123 }
       
   124 
       
   125 use E32Plat;
       
   126 {
       
   127         Plat_Init($toolpath);
       
   128 }
       
   129 
       
   130 if ($debug) {
       
   131 	print "EpocRoot = $EpocRoot\n";
       
   132 	print "Epoc32Path = $Epoc32Path\n";
       
   133 	print "drive = $drive\n";
       
   134 	print "toolpath = $toolpath\n";
       
   135 	print "toroot = $toroot\n";
       
   136 	print "e32path = $e32path\n";
       
   137 	print "rombuildpath = $rombuildpath\n";
       
   138 	print "BasePath = $BasePath\n";
       
   139 }
       
   140 
       
   141 my $cppflags="-P -undef -traditional -lang-c++ -nostdinc -iwithprefixbefore $rombuildpath -I $rombuildpath -I $Epoc32Path ";
       
   142 
       
   143 # Include variant hrh file defines when processing oby and ibys with cpp
       
   144 # (Code copied from \\EPOC\master\cedar\generic\tools\romkit\tools\buildrom.pm -
       
   145 # it used relative path to the working dir but we are using absolute path seems neater)
       
   146 use E32Variant;
       
   147 use Pathutl;
       
   148 my $variantMacroHRHFile = Variant_GetMacroHRHFile();
       
   149 if($variantMacroHRHFile){
       
   150 	# Using absolute paths so must include drive letter otherwise cpp will fail
       
   151 	# Also adding the directory containing the HRH file to main includes paths in
       
   152 	# case it includes files
       
   153 	my $variantFilePath = Path_Split('Path',$variantMacroHRHFile);
       
   154 	$cppflags .= " -I $drive$variantFilePath -include $drive$variantMacroHRHFile";
       
   155 }
       
   156 
       
   157 if($param_count == 0 || $opts{'help'} || !$result) {
       
   158 	usage();
       
   159 	exit 0;
       
   160 }
       
   161 
       
   162 # Now check that the options we have make sense
       
   163 
       
   164 checkopts();
       
   165 
       
   166 if (!$quiet) {
       
   167 	print "Starting directory: ", Cwd::cwd(), "\n";
       
   168 	print <<EOF;
       
   169 OPTIONS:
       
   170 \tTYPE: $opts{'type'}
       
   171 \tVARIANT: $opts{'variant'}
       
   172 \tINSTRUCTION SET: $opts{'inst'}
       
   173 \tBUILD: $opts{'build'}
       
   174 \tMODULES: $opts{'modules'}
       
   175 EOF
       
   176 }
       
   177 
       
   178 #Pick out the type file
       
   179 my $skel;
       
   180 
       
   181 if (-e "$opts{'type'}.oby") {
       
   182 	$skel="$opts{'type'}.oby";
       
   183 } elsif (-e "$rombuildpath\\$opts{'type'}.oby") {
       
   184 	$skel="$rombuildpath\\$opts{'type'}.oby";
       
   185 } else {
       
   186 	die "Can't find type file for type $opts{'type'}, $!";
       
   187 }
       
   188 
       
   189 print "Using type file $skel\n" if !$quiet;
       
   190 
       
   191 # If clean is specified, zap all the image and .oby files
       
   192 
       
   193 if($opts{'clean'}) {
       
   194 	unlink glob("*.img");
       
   195 	unlink "rom.oby";
       
   196 	unlink "rombuild.log";
       
   197 	unlink glob("*.rofs");
       
   198 	unlink "rofsbuild.log";
       
   199 }
       
   200 
       
   201 # Now pre-pre-process this file to point to the right places for .ibys
       
   202 # Unfortunately cpp won't do macro replacement in #include strings, so
       
   203 # we have to do it by hand
       
   204 
       
   205 my $k = $opts{kerneltrace};
       
   206 
       
   207 if ($opts{assp}=~/^m(\S+)/i) {
       
   208 	$kbdir="kb$1";
       
   209 	$kbdir="kbarm" if (lc $1 eq 'eig');
       
   210 } else {
       
   211 	$kbdir="kb$opts{assp}";
       
   212 }
       
   213 $single=1 if ($opts{assp}=~/^s(\S+)/i);
       
   214 
       
   215 if ($single) {
       
   216 	# Hackery to cope with old compiler
       
   217 	if ($main eq 'MARM') {
       
   218 		$smain='SARM';
       
   219 	} else {
       
   220 		$smain="S$main";
       
   221 	}
       
   222 } else {
       
   223 	$smain=$main;
       
   224 }
       
   225 
       
   226 open(X, "$skel") || die "Can't open type file $skel, $!";
       
   227 open(OUT, "> rom1.tmp") || die "Can't open output file, $!";
       
   228 
       
   229 # First output the ROM name
       
   230 print OUT "\nromname=$romname\n";
       
   231 while(<X>) {
       
   232 	s/\#\#ASSP\#\#/$opts{'assp'}/;
       
   233 	s/\#\#VARIANT\#\#/$opts{'variant'}/;
       
   234 	s/\#\#BUILD\#\#/$opts{'build'}/;
       
   235 	s/\#\#MAIN\#\#/$main/;
       
   236 	s/\#\#KMAIN\#\#/$kmain/;
       
   237 	s/\#\#E32PATH\#\#/$e32path/;
       
   238 	s/\#\#BASEPATH\#\#/$BasePath/;
       
   239 	s/\#\#EUSERDIR\#\#/$euserdir/;
       
   240 	s/\#\#ELOCLDIR\#\#/$elocldir/;
       
   241 	s/\#\#KBDIR\#\#/$kbdir/;
       
   242 	print OUT;
       
   243 }
       
   244 
       
   245 close X;
       
   246 close OUT;
       
   247 
       
   248 # Use cpp to pull in include chains and replace defines
       
   249 
       
   250 my $defines = "";
       
   251 $defines .= "-D MAIN=$main ";
       
   252 $defines .= "-D KMAIN=$kmain ";
       
   253 $defines .= "-D EUSERDIR=$euserdir ";
       
   254 $defines .= "-D ELOCLDIR=$elocldir ";
       
   255 $defines .= "-D E32PATH=$e32path ";
       
   256 $defines .= "-D BASEPATH=$BasePath ";
       
   257 $defines .= "-D EPOCROOT=$EpocRoot ";
       
   258 $defines .= "-D SMAIN=$smain " if $smain;
       
   259 
       
   260 foreach (@{$opts{'define'}}) {
       
   261 	my @array=split(/,/,$_);
       
   262 	foreach (@array) {
       
   263 		$defines.="-D ".uc $_." ";
       
   264 		$pagedCode = 1 if $_ eq 'PAGED_CODE';
       
   265 		}
       
   266 	}
       
   267 
       
   268 if ($opts{'modules'}) {
       
   269 	my @array=split(/,/,$opts{'modules'});
       
   270 	foreach (@array) {
       
   271 		$defines.="-D ".uc $_." ";
       
   272 		}
       
   273 	}
       
   274 
       
   275 foreach (keys %opts) {
       
   276 	next if ($_ eq 'name');
       
   277 	next if ($_ eq 'modules');
       
   278 	next if ($_ eq 'zip');
       
   279 	next if ($_ eq 'symbol');
       
   280 	next if ($_ eq 'kerneltrace');
       
   281 	next if ($_ eq 'define');
       
   282 	$defines.="-D ".uc $_."=".$opts{$_}." ";
       
   283 	$defines.="-D ".uc $_."_".$opts{$_}." ";
       
   284 }
       
   285 
       
   286 $defines.="-D SINGLE " if ($single);
       
   287 
       
   288 sub IsRVCTBuild($) {
       
   289     my ($build)=@_;
       
   290     return 1 if ($build =~ /^ARMV/i);
       
   291 	my @customizations = Plat_Customizations('ARMV5');
       
   292 	return 1 if (grep /$build/, @customizations);
       
   293 	return 0;
       
   294 }
       
   295 
       
   296 $defines.="-D RVCT " if (IsRVCTBuild($main));
       
   297 
       
   298 print "Using defines $defines\n" if !$quiet;
       
   299 
       
   300 my $ret=1;
       
   301 my $cppcmd;
       
   302 if($opts{'build'}=~/^u/i) {
       
   303 	# Unicode build
       
   304 	$cppcmd = "cpp $cppflags -D UNICODE $defines rom1.tmp rom2.tmp";
       
   305 } else {
       
   306 	$cppcmd = "cpp $cppflags $defines rom1.tmp rom2.tmp";
       
   307 }
       
   308 print "Executing CPP:\n\t$cppcmd\n" if $debug;
       
   309 $ret = system($cppcmd);
       
   310 die "ERROR EXECUTING CPP\n" if $ret;
       
   311 
       
   312 # Zap any ## marks REMS or blank lines
       
   313 
       
   314 cleanup("rom2.tmp", "rom3.tmp", $k);
       
   315 
       
   316 # scan tmp file and generate auxiliary files, if required
       
   317 open TMP, "rom3.tmp" or die("Can't open rom3.tmp\n");
       
   318 my $line;
       
   319 while ($line=<TMP>)
       
   320 	{
       
   321 	if ($line=~/\s*gentestpaged/i) {
       
   322 		genfile("paged");	}
       
   323 	if ($line=~/\s*gentestnonpaged/i) {
       
   324 		genfile("nonpaged");	}
       
   325 	}
       
   326 
       
   327 parsePatchData("rom3.tmp", "rom4.tmp");
       
   328 
       
   329 # break down the oby file into rom, rofs and extensions oby files
       
   330 
       
   331 my $oby_index =0;
       
   332 my $dumpfile="rom.oby";
       
   333 my $rofs=0;
       
   334 my $extension=0;
       
   335 my $corerofsname="";
       
   336 open DUMPFILE, ">$dumpfile" or die("Can't create $dumpfile\n");
       
   337 my $line;
       
   338 open TMP, "rom4.tmp" or die("Can't open rom4.tmp\n");
       
   339 while ($line=<TMP>)
       
   340 	{
       
   341 	if ($line=~/^\s*rofsname/i)
       
   342 		{
       
   343 		close DUMPFILE;							# close rom.oby or previous rofs#/extension#.oby
       
   344 		$oby_index=1;
       
   345 		$corerofsname=$line;
       
   346 		$corerofsname =~ s/rofsname\s*=\s*//i;		# save core rofs name
       
   347 		$corerofsname =~ s/\s*$//g; 			# remove trailing \n
       
   348 		unlink $corerofsname || print "unable to delete $corerofsname";
       
   349 		my $dumpfile="rofs".$rofs.".oby";
       
   350 		$rofs++;
       
   351 		open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
       
   352 		}
       
   353 
       
   354 	if ($line=~/^\s*coreimage/i)
       
   355 		{
       
   356 		close DUMPFILE;							# close rofs.oby
       
   357 		if ($oby_index ne 1) {
       
   358 			close TMP;
       
   359 			die "Must specify ROFS image before ROFS extension\n";
       
   360 		}
       
   361 		my $name=$line;
       
   362 		$name =~ s/coreimage\s*=\s*//i;		# read core rofs name
       
   363 		$name =~ s/\s*$//g; 			# remove trailing \n
       
   364 		if ($name ne $corerofsname) {
       
   365 			close TMP;
       
   366 			die "This extension does not relate to previous ROFS\n";
       
   367 		}
       
   368 		$oby_index=33;						# open window
       
   369 		my $dumpfile="extension".$extension.".oby";
       
   370 		$extension++;
       
   371 		open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
       
   372 		}
       
   373 
       
   374 	if ($line=~/^\s*extensionrofs/i)
       
   375 		{
       
   376 		$oby_index=3 if ($oby_index eq 2);
       
   377 		}
       
   378 
       
   379 	if (($oby_index eq 2) && !($line=~/^\s*$/)) {
       
   380 		close TMP;
       
   381 		die "Bad ROFS extension specification\n";
       
   382 	}
       
   383 	print DUMPFILE $line;
       
   384 	$oby_index=2 if ($oby_index eq 33);		# close window
       
   385 	}
       
   386 close DUMPFILE;
       
   387 close TMP;
       
   388 
       
   389 # For paged roms that use rofs, move all data= lines in rom which are not 'paging_unmovable' to rofs, so that paged ram-loaded code
       
   390 # is automatically put into rofs
       
   391 rename('rom.oby', 'rom4.tmp') || die;
       
   392 
       
   393 open(IN, 'rom4.tmp') || die "Can't read rom4.tmp";
       
   394 open(ROM, '>rom.oby') || die "Can't write to rom.oby";
       
   395 
       
   396 if ($oby_index >= 1 && $pagedCode)	{
       
   397 	open(ROFS, '>>rofs0.oby') || die "Can't append to rofs0.oby";
       
   398 }
       
   399 
       
   400 while ($line=<IN>)
       
   401 {
       
   402 	if(($oby_index >= 1) && ($pagedCode) && ($line=~/^\s*data\s*=/) && !($line=~/\.*paging_unmovable\s*/)) {
       
   403 		print ROFS $line;
       
   404 	}
       
   405 	else {
       
   406 		$line=~s/paging_unmovable//;
       
   407 		print ROM $line;
       
   408 	}
       
   409 }
       
   410 
       
   411 close IN;
       
   412 close ROM;
       
   413 
       
   414 if ($oby_index >= 1 && $pagedCode)	{
       
   415 	close ROFS;
       
   416 }
       
   417 	unlink 'rom4.tmp';
       
   418 
       
   419 my $flags;
       
   420 
       
   421 foreach (@{$flags{$opts{'assp'}}}) {
       
   422 	$flags.=" -$_";
       
   423 }
       
   424 
       
   425 if($opts{'noheader'}) {
       
   426 	$flags.=" -no-header";
       
   427 }
       
   428 
       
   429 if($opts{'compress'}) {
       
   430 	$flags.=" -compress";
       
   431 }
       
   432 
       
   433 my $builder = $opts{'rombuilder'};
       
   434 $builder = "rombuild" unless ($builder);
       
   435 
       
   436 
       
   437 
       
   438 print "$builder $flags -type-safe-link -S rom.oby 2>&1\n\n";
       
   439 
       
   440 open(Y, "$builder $flags -type-safe-link -S rom.oby 2>&1 |") || 
       
   441 	die "Can't start $builder command, $!";
       
   442 
       
   443 my $nerrors=0;
       
   444 my $nwarnings=0;
       
   445 
       
   446 while(<Y>) {
       
   447 	my $error=(/^error:/i);
       
   448 	my $warning=(/^warning:/i);
       
   449 	print if ($error or $warning or !$quiet);
       
   450 	$nerrors++ if ($error);
       
   451 	$nwarnings++ if ($warning);
       
   452 }
       
   453 
       
   454 print "\nGenerated .oby file is rom.oby\n" if !$quiet;
       
   455 print "\nGenerated image file is $romname\n" if (!$nerrors);
       
   456 
       
   457 my$rerrors;
       
   458 my $rofsbuilder;
       
   459 if ($rofs) {
       
   460 	$rofsbuilder = $opts{'rofsbuilder'};
       
   461 	$rofsbuilder = "rofsbuild" unless ($rofsbuilder);
       
   462 	for(my $i=0;$i<$rofs;++$i) {
       
   463 		print "Executing $rofsbuilder on main rofs\n" if !$quiet;
       
   464 		my $image="rofs".$i.".oby";
       
   465 		system("$rofsbuilder $image");
       
   466 		if ($? != 0)
       
   467 			{
       
   468 			print "$rofsbuilder $image returned $?\n";
       
   469 			$rerrors++;
       
   470 			}
       
   471 		rename "rofsbuild.log", "rofs$i.log"
       
   472 		}
       
   473 }
       
   474 
       
   475 if ($rofs and $extension) {
       
   476 	for(my $i=0;$i<$extension;++$i) {
       
   477 		print "Executing $rofsbuilder on extension rofs\n" if !$quiet;
       
   478 		my $image="extension".$i.".oby";
       
   479 		system("$rofsbuilder $image");
       
   480 		if ($? != 0)
       
   481 			{
       
   482 			print "$rofsbuilder $image returned $?\n";
       
   483 			$rerrors++;
       
   484 			}
       
   485 		rename "rofsbuild.log", "extension$i.log"
       
   486 		}
       
   487 }
       
   488 
       
   489 if ($nerrors) {
       
   490 	print "\n\n Errors found during $builder!!\n\nLeaving tmp files\n";
       
   491 } elsif ($nwarnings) {
       
   492 	print "\n\n Warnings during $builder!!\n\nLeaving tmp files\n";
       
   493 } elsif ($rerrors) {
       
   494 	print "\n\n Errors during $rofsbuilder!!\n\nLeaving tmp files\n";
       
   495 } else {
       
   496 	unlink glob("*.tmp") if !$debug;
       
   497 }
       
   498 if ($opts{zip} or $zip{$opts{assp}}) {
       
   499 	my $zipname=$romname;
       
   500 	$zipname =~ s/\.(\w+)$/\.zip/i;
       
   501 	unlink $zipname;
       
   502 	system("zip $zipname $romname");
       
   503 }
       
   504 if ($opts{symbol}) {
       
   505 	my $logname=$romname;
       
   506 	$logname =~ s/\.(\w+)$/\.log/i;
       
   507 	my $obyname=$romname;
       
   508 	$obyname =~ s/\.(\w+)$/\.oby/i;
       
   509 	unlink $logname;
       
   510 	unlink $obyname;
       
   511 	system("rename rombuild.log $logname");
       
   512 	system("rename rom.oby $obyname");
       
   513 	system("maksym $logname");
       
   514 }
       
   515 
       
   516 if ($nerrors || $nwarnings || $rerrors) {
       
   517 	exit 4;
       
   518 }	
       
   519 	
       
   520 exit 0;
       
   521 
       
   522 
       
   523 ################################ Subroutines  ##################################
       
   524 
       
   525 sub usage {
       
   526 	print <<EOT;
       
   527 
       
   528 rom <options>
       
   529 
       
   530 Generate a rom image for the specified target, along with a rom.oby file
       
   531 that can be fed to (a) rombuild to regenerate the image.
       
   532 
       
   533 The following options are required:
       
   534   --variant=<variant>         e.g. --variant=assabet
       
   535   --inst=<instruction set>    e.g. --inst=arm4
       
   536   --build=<build>             e.g. --build=udeb
       
   537   --type=<type of rom>  
       
   538          tshell for a text shell rom
       
   539          e32tests for a rom with e32tests
       
   540          f32tests for rom with f32tests
       
   541          alltests for all the tests
       
   542 
       
   543 The following are optional:
       
   544   --name=<image name>               Give image file specified name
       
   545   --noheader                        Pass -no-header option on to rombuild
       
   546   --help                            This help message.
       
   547   --clean                           Remove existing generated files first
       
   548   --quiet                           Be less verbose
       
   549   --modules=<comma separated list>  List of additional modules for this ROM
       
   550   --define=<comma separated list>   List of CPP macros to define
       
   551 
       
   552 Options may be specified as a short abbreviation 
       
   553 e.g. -b udeb instead of --build udeb
       
   554 
       
   555 EOT
       
   556 }
       
   557 
       
   558 sub cleanup($$$) {
       
   559 	my ($in, $out, $k) = @_;
       
   560 	my ($line, $lastblank);
       
   561 
       
   562 	open(OUTPUT_FILE, "> $out") or die "Cannot open $out for output";
       
   563 	open(INPUT_FILE, "< $in") or die "Cannot open for $in input";
       
   564   
       
   565 	while ($line=<INPUT_FILE>) {
       
   566 		$line=~s/##//g;
       
   567 
       
   568 		# file=\epoc32\...  ==> file=%EPOCROOT%\epoc32\...
       
   569 		$line =~ s/(=\s*)\\epoc32/\1${EpocRoot}Epoc32/i;
       
   570 
       
   571 		# Now compress blank lines down to one
       
   572 	
       
   573 		if($line=~/^\s*$/) {
       
   574 			if($lastblank) {
       
   575 				# Do nothing
       
   576 			} else {
       
   577 				# This is the first blank line
       
   578 				$lastblank=1;
       
   579 				print OUTPUT_FILE $line;
       
   580 			}
       
   581 		} else {
       
   582 			# Not blank
       
   583 			$lastblank=0;
       
   584 			if ($k and $line=~/^\s*kerneltrace/i) {
       
   585 				$line = "kerneltrace $k\n";
       
   586 			}
       
   587 			print OUTPUT_FILE $line if !($line=~/^\s*REM\s+/i);
       
   588 		}
       
   589 	}
       
   590 	close(INPUT_FILE);
       
   591 	close(OUTPUT_FILE);
       
   592 }
       
   593 
       
   594 sub IsSmp($) {
       
   595 	my %SmpKernelDirs=(
       
   596 		'x86smp' => 1,
       
   597 		'x86gmp' => 1,
       
   598 		'arm4smp' => 1,
       
   599 		'armv4smp' => 1,
       
   600 		'armv5smp' => 1
       
   601 	);
       
   602 
       
   603 	my ($kdir) = @_;
       
   604 	return $SmpKernelDirs{lc $kdir};
       
   605 }
       
   606 
       
   607 sub checkopts {
       
   608 	unless($opts{variant}) { die "No Variant specified"; }
       
   609 	$opts{'build'}="UDEB" unless($opts{'build'});
       
   610 	$opts{'type'}="TSHELL" unless($opts{'type'});
       
   611 	$opts{'inst'}="ARM4" unless($opts{'inst'});
       
   612 
       
   613 	my $additional;
       
   614 	if ($opts{'modules'}) {
       
   615 		$additional="_".$opts{modules};
       
   616 		$additional=~ s/,/_/ig;
       
   617 	}
       
   618 	my $build=lc $opts{build};
       
   619 	my $inst=uc $opts{'inst'};
       
   620 	if ($inst eq "MARM") {
       
   621 		# Hackery to cope with old compiler
       
   622 		$main="MARM";
       
   623 		$euserdir="MARM";
       
   624 		$elocldir="MARM";
       
   625 	}
       
   626 	else {
       
   627 		$main=$inst;
       
   628 		if ($main eq "THUMB") {
       
   629 			$euserdir="ARMI";
       
   630 		} else {
       
   631 			$euserdir=$main;
       
   632 		}
       
   633 		if ($main eq "ARMI" or $main eq "THUMB") {
       
   634 			$elocldir="ARM4";
       
   635 		} else {
       
   636 			$elocldir=$main;
       
   637 		}
       
   638 	}
       
   639 	$kmain = $opts{'xabi'};
       
   640 	$kmain = $main unless ($kmain);
       
   641 	if (IsSmp($kmain)) {
       
   642 		$euserdir = $kmain;
       
   643 	}
       
   644 	if ($opts{name}) {
       
   645 		$romname=$opts{name};
       
   646 	} else {
       
   647 		$romname=uc($opts{variant}.$additional.$main);
       
   648 		if ($build=~/^\w*DEB$/i) {
       
   649 			$romname.='D';
       
   650 		}
       
   651 		$romname.='.IMG';
       
   652 	}
       
   653 }
       
   654 
       
   655 sub lookupFileInfo($$)
       
   656 {
       
   657 	my ($infile, $fullname) = @_;
       
   658 
       
   659 	my ($name, $ext) = $fullname =~ /^(.+)\.(\w+)$/ ? ($1, $2) : ($fullname, undef);
       
   660 
       
   661 	open TMP, $infile or die("Can't open $infile\n");
       
   662 	while(<TMP>)
       
   663 	{
       
   664 		$_ = lc;
       
   665 		if(/^\s*(\S+)\s*=\s*(\S+)\s+(\S+)/i)
       
   666 		{
       
   667 			my ($src, $dest) = ($2, $3);
       
   668 
       
   669 			my $destFullname = $dest =~ /^.*\\(.+)$/ ? $1 : $dest;
       
   670 			my ($destName, $destExt) = $destFullname =~ /^(.+?)\.(\w+)$/ ? ($1, $2) : ($destFullname, undef);
       
   671 
       
   672 			if ($destName eq $name && (!$ext || $ext eq $destExt))
       
   673 			{
       
   674 				close TMP;
       
   675 				return ($src, $dest);
       
   676 			}
       
   677 		}
       
   678 	}
       
   679 
       
   680 	die "patchdata: Can't find file $fullname\n";
       
   681 }
       
   682 
       
   683 sub lookupSymbolInfo($$)
       
   684 {
       
   685 	my ($file, $name) = @_;
       
   686 
       
   687 	open TMP, $file or die "Can't read $file\n";
       
   688 
       
   689 	# ignore local symbols.
       
   690 	while (<TMP> !~ /Global Symbols/) { }
       
   691 
       
   692 	while (<TMP>)
       
   693 	{
       
   694 		if (/^\s*(\S+)\s+(\S+)\s+data\s+(\S+)/i)
       
   695 		{
       
   696 			my ($symbol, $addr, $size) = ($1, $2, $3);
       
   697 			if ($symbol eq $name)
       
   698 			{
       
   699 				close TMP;
       
   700 				return ($addr, $size);
       
   701 			}
       
   702 		}
       
   703 
       
   704 		# This is a quick fix for RVCT 3.1, which uses the text "(EXPORTED)"
       
   705 		# in the map file. Here is an example:
       
   706 		#
       
   707 		# KHeapMinCellSize (EXPORTED) 0x0003d81c Data 4 mem.o(.constdata)
       
   708 		#
       
   709 		elsif (/^\s*(\S+)\s+\(exported\)\s+(\S+)\s+data\s+(\S+)/i)
       
   710 		{
       
   711 			my ($symbol, $addr, $size) = ($1, $2, $3);
       
   712 			if ($symbol eq $name)
       
   713 			{
       
   714 				close TMP;
       
   715 				return ($addr, $size);
       
   716 			}
       
   717 		}
       
   718 	}
       
   719 
       
   720 	die "patchdata: Can't find symbol $name\n";
       
   721 }
       
   722 
       
   723 sub parsePatchData($$)
       
   724 {
       
   725 	my ($infile, $outfile) = @_;
       
   726 
       
   727 	open IN, $infile or die("Can't read $infile\n");
       
   728 	open OUT, ">$outfile" or die("Can't write $outfile\n");
       
   729 
       
   730 	my $line;
       
   731 	while($line = <IN>)
       
   732 	{
       
   733 		if ($line =~ /^\s*patchdata\s+(.+?)\s*$/i)
       
   734 		{
       
   735 			if ($1 !~ /(\S+)\s*@\s*(\S+)\s+(\S+)\s*$/)
       
   736 			{
       
   737 				die "Bad patchdata command: $line\n";
       
   738 			}
       
   739 
       
   740 			my ($file, $symbol, $value) = (lc $1, $2, $3);
       
   741 			my ($srcFile, $destFile) = lookupFileInfo($infile, $file);
       
   742 			my ($index, $elementSize) = (undef, undef);
       
   743 			if ($symbol =~ s/:(\d+)\[(\d+)\]$//)
       
   744 			{
       
   745 				($index, $elementSize) = ($2, $1);
       
   746 				$index = hex($index) if $index =~ /^0x/i;
       
   747 			}
       
   748 
       
   749 			if ($srcFile =~ /\\armv5(smp)?\\/i)
       
   750 			{
       
   751 				my ($symbolAddr, $symbolSize) = lookupSymbolInfo("$srcFile.map", $symbol);
       
   752 
       
   753 				my $max;
       
   754 				if (defined($index))
       
   755 				{
       
   756 					my $bytes;
       
   757 					$bytes = 1, $max = 0xff       if $elementSize ==  8;
       
   758 					$bytes = 2, $max = 0xffff     if $elementSize == 16;
       
   759 					$bytes = 4, $max = 0xffffffff if $elementSize == 32;
       
   760 					die("patchdata: invalid element size $elementSize: $line\n") unless defined($bytes);
       
   761 
       
   762 					if ($bytes > 1 && (($symbolSize & ($bytes-1)) != 0))
       
   763 					{
       
   764 						die("patchdata: unexpected symbol size $symbolSize for array $symbol ($elementSize-bit elements)\n");
       
   765 					}
       
   766 
       
   767 					if ($index >= int($symbolSize / $bytes))
       
   768 					{
       
   769 						die("patchdata: index $index out of bounds for $symbol of $symbolSize bytes ($elementSize-bit elements)\n");
       
   770 					}
       
   771 
       
   772 					$symbolAddr = hex($symbolAddr) if $symbolAddr =~ /^0x/i;
       
   773 					$symbolAddr += $index * $bytes;
       
   774 					$symbolAddr = sprintf("0x%x", $symbolAddr);
       
   775 
       
   776 					$symbolSize = $bytes;
       
   777 				}
       
   778 				elsif ($symbolSize == 1) { $max = 0xff; }
       
   779 				elsif ($symbolSize == 2) { $max = 0xffff; }
       
   780 				elsif ($symbolSize == 4) { $max = 0xffffffff; }
       
   781 				else { die "patchdata: Unexpected symbol size $symbolSize for $symbol\n"; }
       
   782 
       
   783 				$value = hex($value) if $value =~ /^0x/i;
       
   784 				if ($value > $max)
       
   785 				{
       
   786 					print("Warning:  Value overflow of $symbol\n");
       
   787 					$value &= $max;
       
   788 				}					
       
   789 				$value = sprintf("0x%08x", $value);
       
   790 
       
   791 				$line = "patchdata $destFile addr $symbolAddr $symbolSize $value\n";
       
   792 			}
       
   793 			else
       
   794 			{
       
   795 				$line = "";
       
   796 			}
       
   797 
       
   798 		}
       
   799 
       
   800 		print OUT $line;
       
   801 	}
       
   802 
       
   803 	close IN;
       
   804 	close OUT;
       
   805 }
       
   806 
       
   807 sub genfile {
       
   808 	my $count=0;
       
   809 	if($_[0] eq 'paged') {
       
   810 		my $file='gentestpaged.txt';
       
   811 		unlink $file;
       
   812 		open(OUTFILE, ">$file") or die "Can't open output file, $!";
       
   813 		for(my $i=0;$i<50000;++$i) {
       
   814 			if(($i >5) && ($i % 40 ==0)) {
       
   815 			print OUTFILE "\n";
       
   816 			$count++;
       
   817 			} 
       
   818 			if(($i+$count) % 5 ==0) {
       
   819 			print OUTFILE "SATOR ";
       
   820 			}
       
   821 			if(($i+$count) % 5 ==1) {
       
   822 			print OUTFILE "AREPO ";
       
   823 			}
       
   824 			if(($i+$count) % 5 ==2) {
       
   825 			print OUTFILE "TENET ";
       
   826 			}
       
   827 			if(($i+$count) % 5 ==3) {
       
   828 			print OUTFILE "OPERA ";
       
   829 			}
       
   830 			if(($i+$count) % 5 ==4) {
       
   831 			print OUTFILE "ROTAS ";
       
   832 			}
       
   833 		}
       
   834 	} else {
       
   835 		my $file='gentestnonpaged.txt';
       
   836 		unlink $file;
       
   837 		open(OUTFILE, ">$file") or die "Can't open output file, $!";
       
   838 		for(my $i=0;$i<20000;++$i) {
       
   839 			if(($i >5) && ($i % 40 ==0)) {
       
   840 			print OUTFILE "\n";
       
   841 			$count++;
       
   842 			} 
       
   843 			if(($i+$count) % 4 ==0) {
       
   844 			print OUTFILE "STEP ";
       
   845 			}
       
   846 			if(($i+$count) % 4 ==1) {
       
   847 			print OUTFILE "TIME ";
       
   848 			}
       
   849 			if(($i+$count) % 4 ==2) {
       
   850 			print OUTFILE "EMIT ";
       
   851 			}
       
   852 			if(($i+$count) % 4 ==3) {
       
   853 			print OUTFILE "PETS ";
       
   854 			}
       
   855 		}
       
   856 	}
       
   857 }
       
   858 
       
   859 __END__
       
   860 
       
   861 # Tell emacs that this is a perl script even 'though it has a .bat extension
       
   862 # Local Variables:
       
   863 # mode:perl
       
   864 # tab-width:4
       
   865 # End:
       
   866