sbsv1_os/e32toolp/e32util/genshimsrc.bat
changeset 0 83f4b4db085c
child 2 99082257a271
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 @REM Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 @REM All rights reserved.
       
     3 @REM This component and the accompanying materials are made available
       
     4 @REM under the terms of the License "Eclipse Public License v1.0"
       
     5 @REM which accompanies this distribution, and is available
       
     6 @REM at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 @REM
       
     8 @REM Initial Contributors:
       
     9 @REM Nokia Corporation - initial contribution.
       
    10 @REM 
       
    11 @REM Contributors:
       
    12 @REM
       
    13 
       
    14 @goto invoke
       
    15 
       
    16 #!perl
       
    17 
       
    18 use strict;
       
    19 use FindBin;		# for FindBin::Bin
       
    20 use Getopt::Long;
       
    21 use Cwd;
       
    22 
       
    23 $_ = cwd;
       
    24 tr/\//\\/;
       
    25 my $Pwd = $_;
       
    26 
       
    27 my $PerlLibPath;    # fully qualified pathname of the directory containing our Perl modules
       
    28 
       
    29 # establish the path to the Perl binaries
       
    30 BEGIN {
       
    31 	require 5.005_03;				# check user has a version of perl that will cope
       
    32 # establish the path to the Perl libraries: currently the same directory as this script
       
    33 	$PerlLibPath = $FindBin::Bin;	# X:/epoc32/tools
       
    34 	$PerlLibPath =~ s/\//\\/g;	# X:\epoc32\tools
       
    35 	$PerlLibPath .= "\\";
       
    36 }
       
    37 use lib $PerlLibPath;
       
    38 
       
    39 use Defutl;
       
    40 use Genutl;
       
    41 
       
    42 my %opts = ();
       
    43 
       
    44 my $result = GetOptions(\%opts,
       
    45 			"srcpath:s",
       
    46 			"defpath:s",
       
    47 			"name:s",
       
    48 			"version:s",
       
    49 			"alignstack",
       
    50 			"help",
       
    51 		       );
       
    52 
       
    53 Usage() if(!$result || $opts{'help'} || @ARGV < 1);
       
    54 
       
    55 my $srcPath = $opts{"srcpath"} || $Pwd;
       
    56 my $defPath = $opts{"defpath"} || $srcPath;
       
    57 my $outputName = $opts{"name"} || "shim";
       
    58 my %Version;
       
    59 if ($opts{version}) {
       
    60 	%Version = &Genutl_StringToVersion($opts{version}) or die "Bad version number\n";
       
    61 } else {
       
    62 	$Version{major} = 0;
       
    63 	$Version{minor} = 0;
       
    64 }
       
    65 
       
    66 my $infile = pop @ARGV;
       
    67 
       
    68 my @DefData;
       
    69 eval { &Def_ReadFileL(\@DefData, $infile); } ;
       
    70 die $@ if $@;
       
    71 
       
    72 sub WriteFunction($$$) {
       
    73   my ($ordinal, $branchTarget, $comment) = @_;
       
    74   my $fnName = "export_at_ordinal_$ordinal";
       
    75   if ($branchTarget =~ /^\"(.*)\"$/) {
       
    76 	  $branchTarget = $1;
       
    77 	}
       
    78   if ($comment =~ /(null)/) {
       
    79     $comment = $branchTarget;
       
    80   }
       
    81 
       
    82   print CPP <<FUNCTION_DEFINITION;
       
    83 
       
    84 EXPORT_C __NAKED__ int $fnName()
       
    85 //
       
    86 // $comment
       
    87 //
       
    88 	{
       
    89 	asm("B $branchTarget ");
       
    90 	}
       
    91 
       
    92 FUNCTION_DEFINITION
       
    93 }
       
    94 
       
    95 sub WriteFunctionAligned($$$) {
       
    96   my ($ordinal, $branchTarget, $comment) = @_;
       
    97   my $fnName = "export_at_ordinal_$ordinal";
       
    98   if ($branchTarget =~ /^\"(.*)\"$/) {
       
    99 	  $branchTarget = $1;
       
   100 	}
       
   101   if ($comment =~ /(null)/) {
       
   102     $comment = $branchTarget;
       
   103   }
       
   104 
       
   105   print CPP <<FUNCTION_DEFINITION2;
       
   106 
       
   107 EXPORT_C __NAKED__ int $fnName()
       
   108 //
       
   109 // $comment
       
   110 //
       
   111 	{
       
   112 	asm("stmfd sp!, {r4,lr} ");
       
   113 	asm("mov r4, sp ");
       
   114 	asm("bic sp, sp, #4 ");
       
   115 	asm("bl $branchTarget ");
       
   116 	asm("mov sp, r4 ");
       
   117 	asm("ldmfd sp!, {r4,pc} ");
       
   118 	}
       
   119 
       
   120 FUNCTION_DEFINITION2
       
   121 }
       
   122 
       
   123 sub WriteCppHeader($) {
       
   124   my ($filename) = @_;
       
   125   print CPP <<FILE_HEADER;
       
   126 //
       
   127 // $filename - generated by GENSHIMSRC.BAT
       
   128 //
       
   129 
       
   130 #include <e32def.h>
       
   131 #include <e32const.h>
       
   132 #include <cpudefs.h>
       
   133 
       
   134 FILE_HEADER
       
   135 }
       
   136 
       
   137 sub WriteExport($$$) {
       
   138   my ($ordinal, $export, $comment) = @_;
       
   139 
       
   140   if ($comment =~ /(null)/) {
       
   141     $comment = $export;
       
   142   }
       
   143   printf DEF "\t${export}=export_at_ordinal_${ordinal}__Fv \@ $ordinal NONAME \; $comment\n";
       
   144 }
       
   145 
       
   146 my $cppFile = "$srcPath\\$outputName\.cia";
       
   147 die "ERROR: Couldn't open $cppFile\n" unless open CPP, ">$cppFile";
       
   148 my $vstring = &Genutl_VersionToFileAugment(%Version);
       
   149 my $defFile = "$defPath\\${outputName}$vstring\.def";
       
   150 die "ERROR: Couldn't open $defFile\n" unless open DEF, ">$defFile";
       
   151 
       
   152 WriteCppHeader(uc "${outputName}.cia");
       
   153 printf DEF "EXPORTS\n";
       
   154 
       
   155 foreach my $defData (@DefData){
       
   156   my $ord = $$defData{Ordinal};
       
   157   if ($ord) {
       
   158 	my $name = $$defData{Name};
       
   159 	my $comment = $$defData{Comment};
       
   160 	if ($opts{alignstack}) {
       
   161 		WriteFunctionAligned($ord, $name, $comment);
       
   162 	} else {
       
   163 		WriteFunction($ord, $name, $comment);
       
   164 	}
       
   165 	WriteExport($ord, $name, $comment);
       
   166   }
       
   167 }
       
   168 
       
   169 close CPP;
       
   170 close DEF;
       
   171 
       
   172 #####################################################################
       
   173 sub Usage
       
   174 {
       
   175 	print <<EOT;
       
   176 
       
   177 genshimsrc
       
   178 
       
   179 	Generate source for a shim DLL and its associated deffile from a supplied deffile
       
   180 
       
   181 Usage:
       
   182 	genshimsrc [options] deffile
       
   183 
       
   184 Where:
       
   185 	[deffile]     The source deffile
       
   186 
       
   187 Options:
       
   188 	--srcpath         the path for the output source file (defaults to CWD)
       
   189 	--defpath         the path for the output DEF file (defaults to srcpath)
       
   190 	--name            the name to use for the output files (defaults to shim)
       
   191 	--version         the version to use for the output DEF file (defaults to 0.0)
       
   192 	--alignstack      use shims which align the stack to an 8 byte boundary
       
   193 EOT
       
   194 	exit 1;
       
   195 }
       
   196 
       
   197 1;
       
   198 
       
   199 __END__
       
   200 
       
   201 # Tell emacs that this is a perl script even 'though it has a .bat extension
       
   202 # Local Variables:
       
   203 # mode:perl
       
   204 # tab-width:4
       
   205 # End:
       
   206 
       
   207 :invoke
       
   208 @perl -x -S genshimsrc.bat %1 %2 %3 %4 %5 %6 %7 %8 %9