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