sbsv1_os/e32toolp/e32util/epocaif.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Wrapper to support the EPOC AIF Compiler
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 
       
    20 use FindBin;		# for FindBin::Bin
       
    21 use File::Copy;		# for copy()
       
    22 use Cwd;		# for cwd
       
    23 use File::Basename;	# for basename()
       
    24 
       
    25 my $PerlBinPath;	# fully qualified pathname of the directory containing this script
       
    26 my $curdrive="x";	    	# will be initialised when first needed
       
    27 
       
    28 # establish the path to the Perl binaries
       
    29 BEGIN {
       
    30 	require 5.005_03;		# check user has a version of perl that will cope
       
    31 	$PerlBinPath = $FindBin::Bin;	# X:/epoc32/tools
       
    32 	$PerlBinPath =~ s/\//\\/g;	# X:\epoc32\tools
       
    33 }
       
    34 use lib $PerlBinPath;
       
    35 use E32Variant;         # for variant specific macros
       
    36 use Pathutl;
       
    37 use Preprocessor;
       
    38 
       
    39 sub print_usage
       
    40 	{
       
    41 #........1.........2.........3.........4.........5.........6.........7.....
       
    42 	print <<USAGE_EOF;
       
    43 
       
    44 Usage:
       
    45   epocaif [options] srcfile [-o outputfile] [-t tmpdir] [-b "bmps" | -m mbm] [-l "TargetPath:CWD"]
       
    46 
       
    47 
       
    48 The available options are
       
    49 
       
    50    -Ixxx  -- C++ preprocessor arguments
       
    51    -o	  -- output AIF file name including path
       
    52    -t	  -- tempory directory for intermediate files
       
    53    -b	  -- list of bitmaps Eg., "-b/c8\\location\\bmp1 /c8\\location\\bmp2.."
       
    54    -m	  -- compiled MBM file (alternative to -b)
       
    55    -l	  -- if specified, captures all source to \\epoc32\\localisation\\...
       
    56 
       
    57 The aif resource file is then passed through the C++ preprocessor, using any 
       
    58 specified preprocessor arguments, and then compiled with RCOMP.EXE to 
       
    59 generate a compiled resource.
       
    60 The -m or -b option is used to generate a suitable MBM file, if required.
       
    61 The MBM and the compiled resource are then combined to produce an AIF file.
       
    62 
       
    63 
       
    64 USAGE_EOF
       
    65 	}
       
    66 
       
    67 #-------------------------------------------------------
       
    68 # Process commandline arguments
       
    69 #
       
    70 # Can't use the Getopt package because it doesn't like the -D and -I style options
       
    71 #
       
    72 my $sourcefile="";
       
    73 my $opt_o="";
       
    74 my $opt_h="";	
       
    75 my $tmpdir="";
       
    76 my $opt_v=1;
       
    77 my $opt_b="";
       
    78 my $opt_l="";
       
    79 my $opt_m="";
       
    80 my $TrgPath;
       
    81 my $xipaif=1;
       
    82 
       
    83 my $exe = &PreprocessorToUseExe();
       
    84 my $cpp_spec= "$exe -undef -C ";	    # preserve comments
       
    85 
       
    86 my $errors = 0;
       
    87 while (@ARGV)
       
    88 	{
       
    89 	my $arg = shift @ARGV;
       
    90 
       
    91 	if ($arg =~ /^-I-$/)
       
    92 		{
       
    93 		$cpp_spec .= "-I- ";
       
    94 		next;
       
    95 		}
       
    96 	if ($arg =~ /^-I(.*)$/)
       
    97 		{
       
    98 		$cpp_spec .= "-I ";
       
    99 		if ($1 eq "")
       
   100 		    {
       
   101 		    $arg = shift @ARGV;
       
   102 		    }
       
   103 		else
       
   104 		    {
       
   105 		    $arg = $1;
       
   106 		    }
       
   107 		$cpp_spec .= quoted_path($arg)." ";
       
   108 		next;
       
   109 		}
       
   110 	if ($arg =~ /^-v$/)
       
   111 		{
       
   112 		$opt_v =1;
       
   113 		next;
       
   114 		}
       
   115 	if ($arg =~ /^-o(.*)$/)
       
   116 		{
       
   117 		$opt_o =$1;
       
   118 		$TrgPath = $opt_o;
       
   119 		next;
       
   120 		}
       
   121 
       
   122 	if ($arg =~ /^-t(.*)\\?$/)
       
   123 		{
       
   124 		$tmpdir =$1;
       
   125 		next;
       
   126 		}
       
   127 	if ($arg =~ /^-b(.*)$/)
       
   128 		{
       
   129 		$opt_b =$1;
       
   130 		next;
       
   131 		}	
       
   132 	if ($arg =~ /^-m(.*)$/)
       
   133 		{
       
   134 		$opt_m =$1;
       
   135 		next;
       
   136 		}	
       
   137 	if ($arg =~ /^-l(.*)$/)
       
   138 		{
       
   139 		$opt_l =$1;
       
   140 		next;
       
   141 		}	
       
   142 
       
   143 	if ($arg =~ /^-/)
       
   144 		{
       
   145 		print "Unknown arg: $arg\n";
       
   146 		$errors++;
       
   147 		next;
       
   148 		}
       
   149 	$sourcefile=$arg;
       
   150 	}
       
   151 
       
   152 if ($opt_m ne "" && $opt_b ne "")
       
   153 	{
       
   154 	print "Can't specify both -m and -b\n";
       
   155 	$errors++;
       
   156 	}
       
   157 if ($errors || $sourcefile eq "")
       
   158 	{
       
   159 	print_usage();
       
   160 	exit 1;
       
   161 	}
       
   162 
       
   163 my $rss_base = basename($sourcefile);
       
   164 my ($rssfile) = split(/\./, $rss_base);	    # remove extension
       
   165 my $rpp_name = "$tmpdir\\$rssfile.rpp";
       
   166 my $outputfile="$tmpdir\\AIF.RSC";	
       
   167 my $headerfile=$opt_h;
       
   168 
       
   169 if ($opt_v)
       
   170 	{
       
   171 	print "* Source file:   $sourcefile\n";
       
   172 	print "* Resource file: $outputfile\n" if ($outputfile ne "");
       
   173 	}
       
   174 
       
   175 $opt_o = "-o\"$outputfile\"" if ($outputfile ne "");
       
   176 $opt_h = "-h\"$headerfile\"" if ($headerfile ne "");
       
   177 
       
   178 
       
   179 #-------------------------------------------------------
       
   180 # Run the preprocessor
       
   181 #
       
   182 
       
   183 my $variantMacroHRHFile = Variant_GetMacroHRHFile();
       
   184 if($variantMacroHRHFile){
       
   185     my $variantFilePath = Path_Split('Path',$variantMacroHRHFile);
       
   186     chop ( $variantFilePath );
       
   187     $cpp_spec .= " -I \"" . &Path_RltToWork($variantFilePath) . "\" -include " . &Path_RltToWork($variantMacroHRHFile) . " "; 
       
   188 }
       
   189 
       
   190 $cpp_spec .= "-I $PerlBinPath\\..\\include ";	# extra path to support shared tools
       
   191 $cpp_spec .= "-D_UNICODE ";
       
   192 
       
   193 $cpp_spec .= quoted_path($sourcefile) ." -o ". quoted_path($rpp_name);
       
   194 
       
   195 print "* $cpp_spec\n" if ($opt_v);
       
   196 system($cpp_spec);
       
   197 
       
   198 my $cpp_status = $?;
       
   199 die "* cpp failed\n" if ($cpp_status != 0);
       
   200 
       
   201 
       
   202 #-------------------------------------------------------
       
   203 # Copy source to epoc32\localisation
       
   204 #
       
   205 
       
   206 if ($opt_l ne "")
       
   207 {
       
   208 use lockit_info;
       
   209 	my ($rssfile, $FileType) = split(/\./, basename($TrgPath));
       
   210 	&Lockit_SrcFile($rssfile, $rpp_name, $opt_l, $FileType, $opt_b);
       
   211 }
       
   212 
       
   213 #-------------------------------------------------------
       
   214 # Merge rls strings to rpp
       
   215 #
       
   216 &Merge_rls_string($rpp_name);
       
   217 
       
   218 #-------------------------------------------------------
       
   219 # Run the resource compiler
       
   220 #
       
   221 
       
   222 my $rcomp_spec = "rcomp -u ";
       
   223 $rcomp_spec .= "-:$tmpdir\\_dump_of_resource_ "; # causes Rcomp to dump each resource (uncompressed and unpadded) in $tmpdir\\_dump_of_resource_1, $tmpdir\\_dump_of_resource_2, etc
       
   224 $rcomp_spec .= "$opt_o $opt_h -s\"$rpp_name\" -i\"$sourcefile\"";
       
   225 
       
   226 print "* $rcomp_spec\n" if ($opt_v);
       
   227 system($rcomp_spec);
       
   228 if ($? != 0)
       
   229 	{
       
   230 	print "* RCOMP failed - deleting output files\n";
       
   231 	unlink $outputfile if ($outputfile ne "");
       
   232 	unlink $headerfile if ($headerfile ne "");
       
   233 	exit 1;
       
   234 	}
       
   235 print "* deleting $rpp_name\n" if ($opt_v);
       
   236 unlink $rpp_name;
       
   237 
       
   238 #-------------------------------------------------------
       
   239 # Run bmconv, if needed
       
   240 #
       
   241 
       
   242 if ($opt_b ne "")
       
   243 	{
       
   244 	print "* bmconv /q $tmpdir\\AIF.MBM $opt_b\n" if ($opt_v);
       
   245 	system("bmconv /q $tmpdir\\AIF.MBM $opt_b");
       
   246 	if ($? != 0)
       
   247 		{
       
   248 		print "* BMCONV failed\n";
       
   249 		exit 1;
       
   250 		}
       
   251 	print "* bmconv /q /s $tmpdir\\AIF_XIP.MBM $opt_b\n" if ($opt_v);
       
   252 	system("bmconv /q /s $tmpdir\\AIF_xip.MBM $opt_b");
       
   253 	
       
   254 	if ($? != 0)
       
   255 		{
       
   256 		print "* BMCONV failed\n";
       
   257 		exit 1;
       
   258 		}
       
   259 	}
       
   260 elsif ($opt_m ne "")
       
   261 	{
       
   262 	print "* copy $opt_m $tmpdir\\AIF.MBM\n" if ($opt_v); 
       
   263 	copy($opt_m, "$tmpdir\\AIF.MBM");
       
   264 	# no xip file genarated 
       
   265 	$xipaif=0;
       
   266 	}
       
   267 else
       
   268 	{
       
   269 	# no bitmap specified - this is legitimate
       
   270 	unlink("$tmpdir\\AIF.MBM");
       
   271 	unlink("$tmpdir\\AIF_xip.MBM");
       
   272 	}
       
   273 
       
   274 #-------------------------------------------------------
       
   275 # Get the from UID from the first four bytes of "$tmpdir\\_dump_of_resource_1"
       
   276 #
       
   277 
       
   278 open(DUMP_OF_RESOURCE_1, "< $tmpdir\\_dump_of_resource_1") or die("* Can't open dump file\n");
       
   279 binmode(DUMP_OF_RESOURCE_1);
       
   280 my $data;
       
   281 my $numberOfBytesRead=read(DUMP_OF_RESOURCE_1, $data, 4);
       
   282 defined($numberOfBytesRead) or die("* Can't read from dump file\n");
       
   283 ($numberOfBytesRead>=4) or die("* Dump file too short\n");
       
   284 my $uid=(unpack('V', $data))[0];
       
   285 undef($data);
       
   286 undef($numberOfBytesRead);
       
   287 close(DUMP_OF_RESOURCE_1) or die("* Can't close dump file\n");
       
   288 
       
   289 #-------------------------------------------------------
       
   290 # Produce the AIF file from the RSC and MBM files
       
   291 #
       
   292 
       
   293 my $uidcrc = "uidcrc.exe 0x101fb032 0 ".sprintf('0x%08x', $uid)." $tmpdir\\out.aif";
       
   294 my $uidcrc_xip = "uidcrc.exe 0x101fb032 0 ".sprintf('0x%08x', $uid)." $tmpdir\\out_xip.aif";
       
   295 
       
   296 print "* $uidcrc\n" if ($opt_v);
       
   297 system($uidcrc);
       
   298 if ($? != 0)
       
   299 	{
       
   300 	print "* UIDCRC failed\n";
       
   301 	exit 1;
       
   302 	}
       
   303 if ($xipaif ne 0)
       
   304 	{	
       
   305 	print "* $uidcrc\n" if ($opt_v);
       
   306 	system($uidcrc_xip);
       
   307 	if ($? != 0)
       
   308 		{
       
   309 		print "* UIDCRC failed\n";
       
   310 		exit 1;
       
   311 		}
       
   312 	}
       
   313 
       
   314 
       
   315 open(OUT_AIF, ">> $tmpdir\\out.aif") or die("* Can't open temporary file\n");
       
   316 binmode(OUT_AIF);
       
   317 
       
   318 if ($xipaif ne 0)
       
   319 	{
       
   320 	open(OUTXIP_AIF, ">> $tmpdir\\out_xip.aif") or die("* Can't open temporary file\n");
       
   321 	binmode(OUTXIP_AIF);
       
   322 	}
       
   323 
       
   324 print "* Writing length of the RSC-block\n" if ($opt_v);
       
   325 my $lengthOfRscBlock=-s("$tmpdir\\aif.rsc");
       
   326 my $numberOfPaddingBytes=(4-($lengthOfRscBlock%4))%4;
       
   327 print(OUT_AIF pack('V', $lengthOfRscBlock));
       
   328 if ($xipaif ne 0)
       
   329 	{
       
   330 	print(OUTXIP_AIF pack('V', $lengthOfRscBlock));
       
   331 	}
       
   332 print "* Appending the RSC-block\n" if ($opt_v);
       
   333 &appendFile(\*OUT_AIF, "$tmpdir\\aif.rsc");
       
   334 if ($xipaif ne 0)
       
   335 	{
       
   336 	&appendFile(\*OUTXIP_AIF, "$tmpdir\\aif.rsc");
       
   337 	}
       
   338 # append any necessary padding bytes so that the file-offset of the start of the MBM-block is a multiple of 4-bytes
       
   339 print(OUT_AIF ('_' x $numberOfPaddingBytes));
       
   340 if ($xipaif ne 0)
       
   341 	{
       
   342 	print(OUTXIP_AIF ('_' x $numberOfPaddingBytes));
       
   343 	}
       
   344 if (-e("$tmpdir\\aif.mbm"))
       
   345 	{
       
   346 	print "* Appending the MBM-block\n" if ($opt_v);
       
   347 	&appendFile(\*OUT_AIF, "$tmpdir\\aif.mbm");
       
   348 	}
       
   349 if (-e("$tmpdir\\aif_xip.mbm"))
       
   350 	{
       
   351 	print "* Appending the XIPMBM-block\n" if ($opt_v);
       
   352 	&appendFile(\*OUTXIP_AIF, "$tmpdir\\aif_xip.mbm");
       
   353 	}
       
   354 
       
   355 close(OUT_AIF) or die("* Can't close temporary file\n");
       
   356 if ($xipaif ne 0)
       
   357 	{
       
   358 	close(OUTXIP_AIF) or die("* Can't close temporary file\n");
       
   359 	}
       
   360 print "* copy $tmpdir\\out.aif $TrgPath\n" if ($opt_v);
       
   361 copy("$tmpdir\\out.aif", "$TrgPath");
       
   362 if ($xipaif ne 0)
       
   363 	{
       
   364 	my $basepath = &Path_Split('Path', $TrgPath);
       
   365 	my $ext=&Path_Split('Ext',  $TrgPath);
       
   366 	my $basename = basename($TrgPath, $ext);
       
   367 	my $xip="_xip";
       
   368 	print "* copy $tmpdir\\out_xip.aif $basepath$basename$xip$ext\n" if ($opt_v);
       
   369 	copy("$tmpdir\\out_xip.aif", "$basepath$basename$xip$ext");
       
   370 	}
       
   371 unlink("$tmpdir\\_dump_of_resource_*");
       
   372 unlink("$tmpdir\\aif.rsc");
       
   373 unlink("$tmpdir\\aif.mbm");
       
   374 unlink("$tmpdir\\out.aif");
       
   375 unlink("$tmpdir\\aif_xip.mbm");
       
   376 unlink("$tmpdir\\out_xip.aif");
       
   377 exit 0;
       
   378 
       
   379 #-------------------------------------------------------
       
   380 # Subroutine: convert path into something acceptable to CPP.EXE
       
   381 #
       
   382 
       
   383 sub quoted_path
       
   384     {
       
   385     my ($arg) = @_;
       
   386     return "\"$arg\"" if ($arg !~ /^\\[^\\]/);	# not an absolute path
       
   387     if ($curdrive eq "x")
       
   388 		{
       
   389 		$curdrive="";
       
   390 		$curdrive=$1 if (cwd =~ /^(.:)/);	
       
   391 		}
       
   392     return "\"$curdrive$arg\"";
       
   393     }
       
   394 
       
   395 #-------------------------------------------------------
       
   396 # Subroutine: Merge the rls strings in the rpp file specified
       
   397 #
       
   398 sub Merge_rls_string
       
   399 	{
       
   400 	my ($rppfile) = @_;
       
   401 
       
   402 	my $line;
       
   403 	my $StringId;
       
   404 	my $key;
       
   405 	my $value;
       
   406 	my $StringToSubstitute;
       
   407 	my %ResourceString;
       
   408 	
       
   409 	print "* merging text strings to $rppfile\n" if ($opt_v);
       
   410 	
       
   411 	open NEWRPP, ">$rppfile.new" or die "* Can't write to $rppfile.new";
       
   412 	open RPP, "$rppfile" or die "* Can't open $rppfile";
       
   413 	
       
   414 	while ($line = <RPP>) 	{
       
   415 		while (($StringId, $StringToSubstitute)=each %ResourceString)
       
   416 		{
       
   417 			$line=~s/\b$StringId\b/$StringToSubstitute/g if ($line !~ /^rls_string/);
       
   418 		}
       
   419 	
       
   420 		# find quoted "" strings
       
   421 		if($line =~ /^rls_string\s+(\S+)\s+(.*$)/)
       
   422 		{
       
   423 			my $text = $2;
       
   424 			$key = $1;
       
   425 			$line=~s/(.*)/\/\/$1/;
       
   426 			my $substr_count = 0;
       
   427 			if(!exists $ResourceString{$key})
       
   428 			{
       
   429 			SUBSTR:	while (1)
       
   430 			{
       
   431 				# find quoted "" strings e.g. "hello"
       
   432 				if($text =~ /^(\s*\"(.*?\\.)*.*?\")/)		
       
   433 				{
       
   434 					$value = $1;
       
   435 					$text = $';
       
   436 					++$substr_count;
       
   437 				}
       
   438 	
       
   439 				# find quoted '' strings. e.g. 'world'
       
   440 				elsif($text =~ /^(\s*\'(.*?\\.)*.*?\')/)			
       
   441 				{
       
   442 					$value = $1;
       
   443 					$text = $';
       
   444 					++$substr_count;
       
   445 				}
       
   446 	
       
   447 				# find hex strings e.g. <0x34><0x45><0x65>
       
   448 				elsif($text =~ /^(\s*(<.*?>)+)/)		
       
   449 				{
       
   450 					$value = $1;
       
   451 					$text = $';
       
   452 					++$substr_count;
       
   453 				}
       
   454 	
       
   455 				# find c comment e.g. /*hello world*/ (may exist between strings)
       
   456 				elsif($text =~ /^(\s*\/\*.*?\*\/)/)		
       
   457 				{
       
   458 					$text = $';
       
   459 					next SUBSTR; # ignore embedded comment
       
   460 				}
       
   461 	
       
   462 				# find c++ comment e.g. //hello world (may exist after strings)
       
   463 				elsif($text =~ /^(\s*\/\/.*$)/)		
       
   464 				{
       
   465 					$text = $';
       
   466 					next SUBSTR; # ignore trailing comment
       
   467 				}
       
   468 	
       
   469 				# exit search
       
   470 				else
       
   471 				{
       
   472 					if ($substr_count == 0)
       
   473 					{
       
   474 						warn("WARNING: rls_string $key either has incorrect syntax or no value\n");
       
   475 					}
       
   476 					last SUBSTR;
       
   477 				}
       
   478 			$ResourceString{$key} .= $value;
       
   479 			}
       
   480 		  	}
       
   481 		}
       
   482 		print NEWRPP $line;
       
   483 	}
       
   484 
       
   485 	close RPP;
       
   486 	close NEWRPP;
       
   487 	copy ("$rppfile.new", "$rppfile");
       
   488 	unlink ("$rppfile.new");
       
   489 	}
       
   490 
       
   491 #-------------------------------------------------------
       
   492 # Subroutine: Append a file into the open (binary) file already opened
       
   493 #
       
   494 sub appendFile
       
   495 	{
       
   496 	my $fileHandleOfTarget=shift;
       
   497 	my $fileNameOfSource=shift;
       
   498 	open(SOURCE, "< $fileNameOfSource") or die("* Can't open $fileNameOfSource\n");
       
   499 	binmode(SOURCE);
       
   500 	for (;;)
       
   501 		{
       
   502 		my $data;
       
   503 		my $numberOfBytesRead=read(SOURCE, $data, 1024);
       
   504 		defined($numberOfBytesRead) or die("* Can't read from $fileNameOfSource\n");
       
   505 		if ($numberOfBytesRead==0)
       
   506 			{
       
   507 			last;
       
   508 			}
       
   509 		print($fileHandleOfTarget $data);
       
   510 		}
       
   511 	close(SOURCE) or die("* Can't close $fileNameOfSource\n");
       
   512 	}
       
   513