sbsv1/abld/genutil/prepfile.pm
changeset 599 fa7a3cc6effd
equal deleted inserted replaced
596:9f25be3da657 599:fa7a3cc6effd
       
     1 # Copyright (c) 1997-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 # module for preprocessing makmake-style project files
       
    15 # 
       
    16 #
       
    17 
       
    18 
       
    19 package Prepfile;
       
    20 
       
    21 require Exporter;
       
    22 @ISA=qw(Exporter);
       
    23 
       
    24 @EXPORT=qw(
       
    25 	Prepfile_SetVerbose Prepfile_SetUpperCase Prepfile_ProcessL Prepfile_SetCmdOptions
       
    26 );
       
    27 
       
    28 
       
    29 use Checkgcc;
       
    30 use Pathutl;
       
    31 use Preprocessor;
       
    32 
       
    33 my %Mode=(
       
    34 	Verbose=>0,
       
    35 	UpperCase=>0,
       
    36         CmdOptions=>''
       
    37 );
       
    38 
       
    39 sub Prepfile_SetVerbose () {
       
    40 	$Mode{Verbose}=1;
       
    41 }
       
    42 
       
    43 sub Prepfile_SetUpperCase () {
       
    44 	$Mode{UpperCase}=1;
       
    45 }
       
    46 
       
    47 sub Prepfile_SetCmdOptions ($) {
       
    48         $Mode{CmdOptions} = shift;
       
    49         $Mode{CmdOptions} .= ' ' if $Mode{CmdOptions};
       
    50 }
       
    51 
       
    52 sub Prepfile_ProcessL ($$;$@) {
       
    53 # use the C++ preprocessor to process a file.  The function fills the data structure specified
       
    54 # by the first argument, an array reference, according to the contents of the second argument -
       
    55 # a filehandle.  Any other arguments are assumed to be MACROS and are defined for preprocessing.
       
    56 
       
    57 	my ($ArrayRef,$FILE,$VariantHRHFile,@Macros)=@_;
       
    58 	die "\nERROR: Project File \"$FILE\" not found\n" unless -e $FILE;
       
    59 
       
    60 	my $exe = &PreprocessorToUseExe();
       
    61  	my $cpp = "$exe.EXE $Mode{CmdOptions}-undef -nostdinc -+ ";
       
    62 	my @CppCall;
       
    63  	push @CppCall, $cpp;
       
    64 
       
    65 	push @CppCall, join '',	"-I ",&Path_PrefixWithDriveAndQuote("$ENV{EPOCROOT}epoc32\\include"),
       
    66 							" -I .",
       
    67 							" -I ",&Path_PrefixWithDriveAndQuote(&Path_Split('Path',$FILE));
       
    68 
       
    69 	my $Macro;
       
    70 	# add CL macros to the CPP call for preprocessing of the file
       
    71 	foreach $Macro (@Macros) {
       
    72 		$Macro=uc $Macro;					 # add the underscores so we can tell what has been
       
    73 		push @CppCall, "-D $Macro=_____$Macro"; # expanded and remove the space CPP puts in most of the time
       
    74 	}
       
    75 	#if a variant file is used
       
    76 	if(defined($VariantHRHFile)){
       
    77         my $variantFilePath = Path_Split('Path',$VariantHRHFile);
       
    78 	chop( $variantFilePath );
       
    79         push @CppCall, " -I " . &Path_PrefixWithDriveAndQuote($variantFilePath) . " -include " . &Path_PrefixWithDriveAndQuote($VariantHRHFile); 
       
    80 	}
       
    81 	# all macros made upper case and suppress user-expansion of macros for nefarious purposes
       
    82 
       
    83 	push @CppCall, &Path_PrefixWithDriveAndQuote($FILE);
       
    84 	if ($Mode{'Verbose'}) {
       
    85 		print "@CppCall\n";
       
    86 	}
       
    87 	my $CPPPIPE;
       
    88 	open CPPPIPE,"@CppCall |" or die "ERROR: Can't invoke CPP.EXE\n";
       
    89 
       
    90 	# read the processed output
       
    91 	#--------------------------
       
    92 
       
    93 	my $LineNum=0;
       
    94 	my $FileName;
       
    95 	while (<CPPPIPE>) {
       
    96 
       
    97 		# skip blank lines
       
    98 		if (/^\s*$/o) {
       
    99 			$LineNum++;
       
   100 			next;
       
   101 		}
       
   102 
       
   103 		my @Tmp=();
       
   104 
       
   105 	    # Process the file information lines that cpp inserts.
       
   106 		# (note that we don't currently do anything with the
       
   107 		# number cpp sometimes puts out after the filename -
       
   108 		# it's something to do with inclusion nesting levels)
       
   109 		if (/^# (\d+) "(.*)"( \d+)?/o) {
       
   110 			$LineNum = scalar $1;
       
   111 			my $CurFile=$2;
       
   112 			$CurFile=~s-\\\\-\\-go;
       
   113 			$CurFile=~s-\\\/-\\-go;
       
   114 			$CurFile=~s-\/\\-\\-go;
       
   115 			$CurFile=~s-\/\/-\\-go;
       
   116 			$CurFile=~s-\/-\\-go;
       
   117 			$CurFile=~s-(.:)--go;
       
   118 
       
   119 			$CurFile=&Path_AbsToWork($CurFile);
       
   120 			@Tmp=('#', $CurFile);
       
   121 			push @{$ArrayRef}, [ @Tmp ];
       
   122 			next;
       
   123 		}
       
   124 
       
   125 		# Process file data
       
   126 		push @Tmp, $LineNum;
       
   127 		# get rid of the space that cpp puts in most of the time after macro expansion (CPP help doesn't say exactly when!)
       
   128 		# don't upper case everything until you've done this.
       
   129 		
       
   130 		foreach $Macro (@Macros) {
       
   131 			s/\/ _____$Macro /\/$Macro/g;
       
   132 			s/_____$Macro /$Macro/g;
       
   133 			s/_____$Macro/$Macro/g;
       
   134 		}
       
   135 		if(/^macro/i)
       
   136 		{
       
   137 			#Parse and Store the mmp file statement by retaining double quotes
       
   138 			while (/("([^\t\n\r\f]+)"|([^ \t\n\r\f]+))/go) {
       
   139 			        my $Flag = $2 ? $2 : $3;
       
   140 				if($Flag =~ m/\\\"/) { 
       
   141 					$Flag =~ s/\"\\/\\/g;
       
   142 					$Flag =~ s/\""/\"/g;
       
   143 				}
       
   144 				push @Tmp, $Flag;
       
   145 			}
       
   146 		}
       
   147 		else
       
   148 		{
       
   149 			#Parse and Store the mmp file statement by removing double quotes
       
   150 			while (/("([^"\t\n\r\f]+)"|([^ "\t\n\r\f]+))/go) {
       
   151 				push @Tmp, $2 ? $2 : $3;
       
   152 			}
       
   153 		}
       
   154 		push @{$ArrayRef}, [ @Tmp ];
       
   155 		$LineNum++;
       
   156 	}		
       
   157 
       
   158 	if ($Mode{UpperCase}) {
       
   159 #		upper-case all the data
       
   160 		my $Line;
       
   161 		my $Item;
       
   162 		foreach $Line (@{$ArrayRef}) {
       
   163 			foreach $Item (@$Line) {
       
   164 				$Item=uc $Item;
       
   165 			}
       
   166 		}
       
   167 	}
       
   168 	close CPPPIPE or die $! ? "ERROR: Error closing $exe.exe pipe ($!)\n\t@CppCall\n"
       
   169  				: "ERROR: $exe.exe returned non-zero exit status ($?)\n\t@CppCall\n";
       
   170 }
       
   171 
       
   172 1;