First public contribution.
3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
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".
10 # Initial Contributors:
11 # Nokia Corporation - initial contribution.
18 # rom.pl - Build a rom
20 # Pre-processes the .oby/iby files then invokes rombuild.exe
21 # (or other specified builder)
23 # First, read our config file
29 #Getopt::Long::Configure("ignore_case");
31 my $param_count = scalar(@ARGV);
32 my $result = GetOptions (\%opts, "assp=s",
55 my (@assps, @builds, %variants, @templates, %flags, %insts, %zip, %builder);
68 my $debug = $opts{_debug};
70 $quiet = $opts{quiet} unless $debug;
74 # discover where this script is running from to set the $toroot and $e32path variables
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)
90 # Raptor style: Drive letter and trailing \\ removed
93 $ENV{EPOCROOT} = $EpocRoot;
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);
101 # The cpp needed a drive apparently
102 my $cwd = Cwd::cwd();
104 $drive = "$1:" unless $drive;
105 print "epocroot = >$EpocRoot<\n";
106 print "drive = >$drive<\n";
111 unless ($fp0 =~ /^([A-Za-z]:)?\\/) {
119 my @path = split(/\\/, $cwd);
121 $toroot = ('..\\') x @path;
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;
136 Plat_Init($toolpath);
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";
150 my $cppflags="-P -undef -traditional -lang-c++ -nostdinc -iwithprefixbefore $rombuildpath -I $rombuildpath -I $drive$Epoc32Path ";
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)
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";
166 if($param_count == 0 || $opts{'help'} || !$result) {
171 # Now check that the options we have make sense
176 print "Starting directory: ", Cwd::cwd(), "\n";
179 \tTYPE: $opts{'type'}
180 \tVARIANT: $opts{'variant'}
181 \tINSTRUCTION SET: $opts{'inst'}
182 \tBUILD: $opts{'build'}
183 \tMODULES: $opts{'modules'}
187 #Pick out the type file
190 if (-e "$opts{'type'}.oby") {
191 $skel="$opts{'type'}.oby";
192 } elsif (-e "$rombuildpath\\$opts{'type'}.oby") {
193 $skel="$rombuildpath\\$opts{'type'}.oby";
195 die "Can't find type file for type $opts{'type'}, $!";
198 print "Using type file $skel\n" if !$quiet;
200 # If clean is specified, zap all the image and .oby files
203 unlink glob("*.img");
205 unlink "rombuild.log";
206 unlink glob("*.rofs");
207 unlink "rofsbuild.log";
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
214 my $k = $opts{kerneltrace};
216 if ($opts{assp}=~/^m(\S+)/i) {
218 $kbdir="kbarm" if (lc $1 eq 'eig');
220 $kbdir="kb$opts{assp}";
222 $single=1 if ($opts{assp}=~/^s(\S+)/i);
225 # Hackery to cope with old compiler
226 if ($main eq 'MARM') {
235 open(X, "$skel") || die "Can't open type file $skel, $!";
236 open(OUT, "> rom1.tmp") || die "Can't open output file, $!";
238 # First output the ROM name
239 print OUT "\nromname=$romname\n";
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/;
257 # Use cpp to pull in include chains and replace 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;
269 foreach (@{$opts{'define'}}) {
270 my @array=split(/,/,$_);
272 $defines.="-D ".uc $_." ";
273 $pagedCode = 1 if $_ eq 'PAGED_CODE';
277 if ($opts{'modules'}) {
278 my @array=split(/,/,$opts{'modules'});
280 $defines.="-D ".uc $_." ";
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{$_}." ";
295 $defines.="-D SINGLE " if ($single);
299 return 1 if ($build =~ /^ARMV/i);
300 my @customizations = Plat_Customizations('ARMV5');
301 return 1 if (grep /$build/, @customizations);
305 $defines.="-D RVCT " if (IsRVCTBuild($main));
307 print "Using defines $defines\n" if !$quiet;
311 if($opts{'build'}=~/^u/i) {
313 $cppcmd = "$Epoc32Path/gcc/bin/cpp $cppflags -D UNICODE $defines rom1.tmp rom2.tmp";
315 $cppcmd = "cpp $cppflags $defines rom1.tmp rom2.tmp";
317 print "Executing CPP:\n\t$cppcmd\n" if $debug;
318 $ret = system($cppcmd);
319 die "ERROR EXECUTING CPP\n" if $ret;
321 # Zap any ## marks REMS or blank lines
323 cleanup("rom2.tmp", "rom3.tmp", $k);
325 # scan tmp file and generate auxiliary files, if required
326 open TMP, "rom3.tmp" or die("Can't open rom3.tmp\n");
330 if ($line=~/\s*gentestpaged/i) {
332 if ($line=~/\s*gentestnonpaged/i) {
333 genfile("nonpaged"); }
336 parsePatchData("rom3.tmp", "rom4.tmp");
338 # break down the oby file into rom, rofs and extensions oby files
341 my $dumpfile="rom.oby";
345 open DUMPFILE, ">$dumpfile" or die("Can't create $dumpfile\n");
347 open TMP, "rom4.tmp" or die("Can't open rom4.tmp\n");
350 if ($line=~/^\s*rofsname/i)
352 close DUMPFILE; # close rom.oby or previous rofs#/extension#.oby
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";
360 open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
363 if ($line=~/^\s*coreimage/i)
365 close DUMPFILE; # close rofs.oby
366 if ($oby_index ne 1) {
368 die "Must specify ROFS image before ROFS extension\n";
371 $name =~ s/coreimage\s*=\s*//i; # read core rofs name
372 $name =~ s/\s*$//g; # remove trailing \n
373 if ($name ne $corerofsname) {
375 die "This extension does not relate to previous ROFS\n";
377 $oby_index=33; # open window
378 my $dumpfile="extension".$extension.".oby";
380 open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
383 if ($line=~/^\s*extensionrofs/i)
385 $oby_index=3 if ($oby_index eq 2);
388 if (($oby_index eq 2) && !($line=~/^\s*$/)) {
390 die "Bad ROFS extension specification\n";
392 print DUMPFILE $line;
393 $oby_index=2 if ($oby_index eq 33); # close window
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;
402 open(IN, 'rom4.tmp') || die "Can't read rom4.tmp";
403 open(ROM, '>rom.oby') || die "Can't write to rom.oby";
405 if ($oby_index >= 1 && $pagedCode) {
406 open(ROFS, '>>rofs0.oby') || die "Can't append to rofs0.oby";
411 if(($oby_index >= 1) && ($pagedCode) && ($line=~/^\s*data\s*=/) && !($line=~/\.*paging_unmovable\s*/)) {
415 $line=~s/paging_unmovable//;
423 if ($oby_index >= 1 && $pagedCode) {
430 foreach (@{$flags{$opts{'assp'}}}) {
434 if($opts{'noheader'}) {
435 $flags.=" -no-header";
438 if($opts{'compress'}) {
439 $flags.=" -compress";
442 my $builder = $opts{'rombuilder'};
443 $builder = "rombuild" unless ($builder);
447 print "$builder $flags -type-safe-link -S rom.oby 2>&1\n\n";
449 open(Y, "$builder $flags -type-safe-link -S rom.oby 2>&1 |") ||
450 die "Can't start $builder command, $!";
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);
463 print "\nGenerated .oby file is rom.oby\n" if !$quiet;
464 print "\nGenerated image file is $romname\n" if (!$nerrors);
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");
477 print "$rofsbuilder $image returned $?\n";
480 rename "rofsbuild.log", "rofs$i.log"
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");
491 print "$rofsbuilder $image returned $?\n";
494 rename "rofsbuild.log", "extension$i.log"
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";
503 print "\n\n Errors during $rofsbuilder!!\n\nLeaving tmp files\n";
505 unlink glob("*.tmp") if !$debug;
507 if ($opts{zip} or $zip{$opts{assp}}) {
508 my $zipname=$romname;
509 $zipname =~ s/\.(\w+)$/\.zip/i;
511 system("zip $zipname $romname");
514 my $logname=$romname;
515 $logname =~ s/\.(\w+)$/\.log/i;
516 my $obyname=$romname;
517 $obyname =~ s/\.(\w+)$/\.oby/i;
520 system("rename rombuild.log $logname");
521 system("rename rom.oby $obyname");
522 system("maksym $logname");
525 if ($nerrors || $nwarnings || $rerrors) {
532 ################################ Subroutines ##################################
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.
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
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
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
561 Options may be specified as a short abbreviation
562 e.g. -b udeb instead of --build udeb
568 my ($in, $out, $k) = @_;
569 my ($line, $lastblank);
571 open(OUTPUT_FILE, "> $out") or die "Cannot open $out for output";
572 open(INPUT_FILE, "< $in") or die "Cannot open for $in input";
574 while ($line=<INPUT_FILE>) {
577 # file=\epoc32\... ==> file=%EPOCROOT%\epoc32\...
578 $line =~ s/(=\s*)\\epoc32/\1${EpocRoot}Epoc32/i;
580 # Now compress blank lines down to one
586 # This is the first blank line
588 print OUTPUT_FILE $line;
593 if ($k and $line=~/^\s*kerneltrace/i) {
594 $line = "kerneltrace $k\n";
596 print OUTPUT_FILE $line if !($line=~/^\s*REM\s+/i);
613 return $SmpKernelDirs{lc $kdir};
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'});
623 if ($opts{'modules'}) {
624 $additional="_".$opts{modules};
625 $additional=~ s/,/_/ig;
627 my $build=lc $opts{build};
628 my $inst=uc $opts{'inst'};
629 if ($inst eq "MARM") {
630 # Hackery to cope with old compiler
637 if ($main eq "THUMB") {
642 if ($main eq "ARMI" or $main eq "THUMB") {
648 $kmain = $opts{'xabi'};
649 $kmain = $main unless ($kmain);
654 $romname=$opts{name};
656 $romname=uc($opts{variant}.$additional.$main);
657 if ($build=~/^\w*DEB$/i) {
664 sub lookupFileInfo($$)
666 my ($infile, $fullname) = @_;
668 my ($name, $ext) = $fullname =~ /^(.+)\.(\w+)$/ ? ($1, $2) : ($fullname, undef);
670 open TMP, $infile or die("Can't open $infile\n");
674 if(/^\s*(\S+)\s*=\s*(\S+)\s+(\S+)/i)
676 my ($src, $dest) = ($2, $3);
678 my $destFullname = $dest =~ /^.*\\(.+)$/ ? $1 : $dest;
679 my ($destName, $destExt) = $destFullname =~ /^(.+?)\.(\w+)$/ ? ($1, $2) : ($destFullname, undef);
681 if ($destName eq $name && (!$ext || $ext eq $destExt))
684 return ($src, $dest);
689 die "patchdata: Can't find file $fullname\n";
692 sub lookupSymbolInfo($$)
694 my ($file, $name) = @_;
696 open TMP, $file or die "Can't read $file\n";
698 # ignore local symbols.
699 while (<TMP> !~ /Global Symbols/) { }
703 if (/^\s*(\S+)\s+(\S+)\s+data\s+(\S+)/i)
705 my ($symbol, $addr, $size) = ($1, $2, $3);
706 if ($symbol eq $name)
709 return ($addr, $size);
713 # This is a quick fix for RVCT 3.1, which uses the text "(EXPORTED)"
714 # in the map file. Here is an example:
716 # KHeapMinCellSize (EXPORTED) 0x0003d81c Data 4 mem.o(.constdata)
718 elsif (/^\s*(\S+)\s+\(exported\)\s+(\S+)\s+data\s+(\S+)/i)
720 my ($symbol, $addr, $size) = ($1, $2, $3);
721 if ($symbol eq $name)
724 return ($addr, $size);
729 die "patchdata: Can't find symbol $name\n";
732 sub parsePatchData($$)
734 my ($infile, $outfile) = @_;
736 open IN, $infile or die("Can't read $infile\n");
737 open OUT, ">$outfile" or die("Can't write $outfile\n");
742 if ($line =~ /^\s*patchdata\s+(.+?)\s*$/i)
744 if ($1 !~ /(\S+)\s*@\s*(\S+)\s+(\S+)\s*$/)
746 die "Bad patchdata command: $line\n";
749 my ($file, $symbol, $value) = (lc $1, $2, $3);
750 my ($srcFile, $destFile) = lookupFileInfo($infile, $file);
751 my ($index, $elementSize) = (undef, undef);
752 if ($symbol =~ s/:(\d+)\[(\d+)\]$//)
754 ($index, $elementSize) = ($2, $1);
755 $index = hex($index) if $index =~ /^0x/i;
758 if ($srcFile =~ /\\armv5(smp)?\\/i)
760 my ($symbolAddr, $symbolSize) = lookupSymbolInfo("$srcFile.map", $symbol);
766 $bytes = 1, $max = 0xff if $elementSize == 8;
767 $bytes = 2, $max = 0xffff if $elementSize == 16;
768 $bytes = 4, $max = 0xffffffff if $elementSize == 32;
769 die("patchdata: invalid element size $elementSize: $line\n") unless defined($bytes);
771 if ($bytes > 1 && (($symbolSize & ($bytes-1)) != 0))
773 die("patchdata: unexpected symbol size $symbolSize for array $symbol ($elementSize-bit elements)\n");
776 if ($index >= int($symbolSize / $bytes))
778 die("patchdata: index $index out of bounds for $symbol of $symbolSize bytes ($elementSize-bit elements)\n");
781 $symbolAddr = hex($symbolAddr) if $symbolAddr =~ /^0x/i;
782 $symbolAddr += $index * $bytes;
783 $symbolAddr = sprintf("0x%x", $symbolAddr);
785 $symbolSize = $bytes;
787 elsif ($symbolSize == 1) { $max = 0xff; }
788 elsif ($symbolSize == 2) { $max = 0xffff; }
789 elsif ($symbolSize == 4) { $max = 0xffffffff; }
790 else { die "patchdata: Unexpected symbol size $symbolSize for $symbol\n"; }
792 $value = hex($value) if $value =~ /^0x/i;
795 print("Warning: Value overflow of $symbol\n");
798 $value = sprintf("0x%08x", $value);
800 $line = "patchdata $destFile addr $symbolAddr $symbolSize $value\n";
818 if($_[0] eq 'paged') {
819 my $file='gentestpaged.txt';
821 open(OUTFILE, ">$file") or die "Can't open output file, $!";
822 for(my $i=0;$i<50000;++$i) {
823 if(($i >5) && ($i % 40 ==0)) {
827 if(($i+$count) % 5 ==0) {
828 print OUTFILE "SATOR ";
830 if(($i+$count) % 5 ==1) {
831 print OUTFILE "AREPO ";
833 if(($i+$count) % 5 ==2) {
834 print OUTFILE "TENET ";
836 if(($i+$count) % 5 ==3) {
837 print OUTFILE "OPERA ";
839 if(($i+$count) % 5 ==4) {
840 print OUTFILE "ROTAS ";
844 my $file='gentestnonpaged.txt';
846 open(OUTFILE, ">$file") or die "Can't open output file, $!";
847 for(my $i=0;$i<20000;++$i) {
848 if(($i >5) && ($i % 40 ==0)) {
852 if(($i+$count) % 4 ==0) {
853 print OUTFILE "STEP ";
855 if(($i+$count) % 4 ==1) {
856 print OUTFILE "TIME ";
858 if(($i+$count) % 4 ==2) {
859 print OUTFILE "EMIT ";
861 if(($i+$count) % 4 ==3) {
862 print OUTFILE "PETS ";
870 # Tell emacs that this is a perl script even 'though it has a .bat extension