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