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