OLD | NEW |
| (Empty) |
1 #!/usr/bin/perl -w | |
2 # | |
3 # Copyright (C) 2005 Apple Computer, Inc. | |
4 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com> | |
5 # | |
6 # This file is part of WebKit | |
7 # | |
8 # This library is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Library General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2 of the License, or (at your option) any later version. | |
12 # | |
13 # This library is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # Library General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Library General Public License | |
19 # along with this library; see the file COPYING.LIB. If not, write to | |
20 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 # Boston, MA 02110-1301, USA. | |
22 # | |
23 | |
24 use strict; | |
25 | |
26 use File::Path; | |
27 use File::Basename; | |
28 use Getopt::Long; | |
29 use Text::ParseWords; | |
30 use Cwd; | |
31 | |
32 use idl_parser; | |
33 use code_generator_v8; | |
34 use idl_serializer; | |
35 | |
36 my @idlDirectories; | |
37 my $outputDirectory; | |
38 my $preprocessor; | |
39 my $verbose; | |
40 my $interfaceDependenciesFile; | |
41 my $idlAttributesFile; | |
42 my $writeFileOnlyIfChanged; | |
43 | |
44 GetOptions('include=s@' => \@idlDirectories, | |
45 'outputDir=s' => \$outputDirectory, | |
46 'preprocessor=s' => \$preprocessor, | |
47 'verbose' => \$verbose, | |
48 'interfaceDependenciesFile=s' => \$interfaceDependenciesFile, | |
49 'idlAttributesFile=s' => \$idlAttributesFile, | |
50 'write-file-only-if-changed=s' => \$writeFileOnlyIfChanged); | |
51 | |
52 my $targetIdlFile = $ARGV[0]; | |
53 | |
54 die('Must specify input file.') unless defined($targetIdlFile); | |
55 die('Must specify output directory.') unless defined($outputDirectory); | |
56 | |
57 $targetIdlFile = Cwd::realpath($targetIdlFile); | |
58 if ($verbose) { | |
59 print "$targetIdlFile\n"; | |
60 } | |
61 my $targetInterfaceName = fileparse(basename($targetIdlFile), ".idl"); | |
62 | |
63 my @dependencyIdlFiles; | |
64 if ($interfaceDependenciesFile) { | |
65 # The format of the interface dependencies file: | |
66 # | |
67 # Window.idl P.idl Q.idl R.idl | |
68 # Document.idl S.idl | |
69 # Event.idl | |
70 # ... | |
71 # | |
72 # The above indicates that Window.idl depends on P.idl, Q.idl, and R.idl, | |
73 # Document.idl depends on S.idl, and Event.idl depends on no IDLs. | |
74 # A dependency IDL file (one that is depended on by another IDL, e.g. P.idl | |
75 # in the above) does not have its own entry in the dependency file. | |
76 open FH, "< $interfaceDependenciesFile" or die "Cannot open $interfaceDepend
enciesFile\n"; | |
77 while (my $line = <FH>) { | |
78 my ($idlFile, @followingIdlFiles) = split(/\s+/, $line); | |
79 if ($idlFile and basename($idlFile) eq basename($targetIdlFile)) { | |
80 # We sort the dependency IDL files so that the corresponding code is
generated | |
81 # in a consistent order. This is important for the bindings tests. | |
82 @dependencyIdlFiles = sort @followingIdlFiles; | |
83 } | |
84 } | |
85 close FH; | |
86 } | |
87 | |
88 # Parse the target IDL file. | |
89 my $targetParser = idl_parser->new(!$verbose); | |
90 my $targetDocument = $targetParser->Parse($targetIdlFile, $preprocessor); | |
91 | |
92 if ($idlAttributesFile) { | |
93 my $idlAttributes = loadIDLAttributes($idlAttributesFile); | |
94 checkIDLAttributes($idlAttributes, $targetDocument, basename($targetIdlFile)
); | |
95 } | |
96 | |
97 foreach my $idlFile (@dependencyIdlFiles) { | |
98 next if $idlFile eq $targetIdlFile; | |
99 | |
100 my $interfaceName = fileparse(basename($idlFile), ".idl"); | |
101 my $parser = idl_parser->new(!$verbose); | |
102 my $document = $parser->Parse($idlFile, $preprocessor); | |
103 | |
104 foreach my $interface (@{$document->interfaces}) { | |
105 if (!$interface->isPartial || $interface->name eq $targetInterfaceName)
{ | |
106 my $targetDataNode; | |
107 foreach my $interface (@{$targetDocument->interfaces}) { | |
108 if ($interface->name eq $targetInterfaceName) { | |
109 $targetDataNode = $interface; | |
110 last; | |
111 } | |
112 } | |
113 die "Not found an interface ${targetInterfaceName} in ${targetInterf
aceName}.idl." unless defined $targetDataNode; | |
114 | |
115 # Support for attributes of partial interfaces. | |
116 foreach my $attribute (@{$interface->attributes}) { | |
117 # Record that this attribute is implemented by $interfaceName. | |
118 $attribute->extendedAttributes->{"ImplementedBy"} = $interfaceNa
me; | |
119 | |
120 # Add interface-wide extended attributes to each attribute. | |
121 applyInterfaceExtendedAttributes($interface, $attribute->extende
dAttributes); | |
122 | |
123 push(@{$targetDataNode->attributes}, $attribute); | |
124 } | |
125 | |
126 # Support for methods of partial interfaces. | |
127 foreach my $function (@{$interface->functions}) { | |
128 # Record that this method is implemented by $interfaceName. | |
129 $function->extendedAttributes->{"ImplementedBy"} = $interfaceNam
e; | |
130 | |
131 # Add interface-wide extended attributes to each method. | |
132 applyInterfaceExtendedAttributes($interface, $function->extended
Attributes); | |
133 | |
134 push(@{$targetDataNode->functions}, $function); | |
135 } | |
136 | |
137 # Support for constants of partial interfaces. | |
138 foreach my $constant (@{$interface->constants}) { | |
139 # Record that this constant is implemented by $interfaceName. | |
140 $constant->extendedAttributes->{"ImplementedBy"} = $interfaceNam
e; | |
141 | |
142 # Add interface-wide extended attributes to each constant. | |
143 applyInterfaceExtendedAttributes($interface, $constant->extended
Attributes); | |
144 | |
145 push(@{$targetDataNode->constants}, $constant); | |
146 } | |
147 } else { | |
148 die "$idlFile is not a dependency of $targetIdlFile. There maybe a b
ug in the dependency computer (compute_dependencies.py).\n"; | |
149 } | |
150 } | |
151 } | |
152 | |
153 # Serialize to and from JSON to ensure Perl and Python parsers are equivalent, | |
154 # as part of porting compiler to Python. See http://crbug.com/242795 | |
155 $targetDocument = deserializeJSON(serializeJSON($targetDocument)); | |
156 | |
157 # Generate desired output for the target IDL file. | |
158 my @interfaceIdlFiles = ($targetDocument->fileName(), @dependencyIdlFiles); | |
159 my $codeGenerator = code_generator_v8->new($targetDocument, \@idlDirectories, $p
reprocessor, $verbose, \@interfaceIdlFiles, $writeFileOnlyIfChanged); | |
160 my $interfaces = $targetDocument->interfaces; | |
161 foreach my $interface (@$interfaces) { | |
162 print "Generating bindings code for IDL interface \"" . $interface->name . "
\"...\n" if $verbose; | |
163 $codeGenerator->GenerateInterface($interface); | |
164 $codeGenerator->WriteData($interface, $outputDirectory); | |
165 } | |
166 | |
167 sub loadIDLAttributes | |
168 { | |
169 my $idlAttributesFile = shift; | |
170 | |
171 my %idlAttributes; | |
172 open FH, "<", $idlAttributesFile or die "Couldn't open $idlAttributesFile: $
!"; | |
173 while (my $line = <FH>) { | |
174 chomp $line; | |
175 next if $line =~ /^\s*#/; | |
176 next if $line =~ /^\s*$/; | |
177 | |
178 if ($line =~ /^\s*([^=\s]*)\s*=?\s*(.*)/) { | |
179 my $name = $1; | |
180 $idlAttributes{$name} = {}; | |
181 if ($2) { | |
182 foreach my $rightValue (split /\|/, $2) { | |
183 $rightValue =~ s/^\s*|\s*$//g; | |
184 $rightValue = "VALUE_IS_MISSING" unless $rightValue; | |
185 $idlAttributes{$name}{$rightValue} = 1; | |
186 } | |
187 } else { | |
188 $idlAttributes{$name}{"VALUE_IS_MISSING"} = 1; | |
189 } | |
190 } else { | |
191 die "The format of " . basename($idlAttributesFile) . " is wrong: li
ne $.\n"; | |
192 } | |
193 } | |
194 close FH; | |
195 | |
196 return \%idlAttributes; | |
197 } | |
198 | |
199 sub checkIDLAttributes | |
200 { | |
201 my $idlAttributes = shift; | |
202 my $document = shift; | |
203 my $idlFile = shift; | |
204 | |
205 foreach my $interface (@{$document->interfaces}) { | |
206 checkIfIDLAttributesExists($idlAttributes, $interface->extendedAttribute
s, $idlFile); | |
207 | |
208 foreach my $attribute (@{$interface->attributes}) { | |
209 checkIfIDLAttributesExists($idlAttributes, $attribute->extendedAttri
butes, $idlFile); | |
210 } | |
211 | |
212 foreach my $function (@{$interface->functions}) { | |
213 checkIfIDLAttributesExists($idlAttributes, $function->extendedAttrib
utes, $idlFile); | |
214 foreach my $parameter (@{$function->parameters}) { | |
215 checkIfIDLAttributesExists($idlAttributes, $parameter->extendedA
ttributes, $idlFile); | |
216 } | |
217 } | |
218 } | |
219 } | |
220 | |
221 sub applyInterfaceExtendedAttributes | |
222 { | |
223 my $interface = shift; | |
224 my $extendedAttributes = shift; | |
225 | |
226 foreach my $extendedAttributeName (keys %{$interface->extendedAttributes}) { | |
227 next if $extendedAttributeName eq "ImplementedAs"; | |
228 $extendedAttributes->{$extendedAttributeName} = $interface->extendedAttr
ibutes->{$extendedAttributeName}; | |
229 } | |
230 } | |
231 | |
232 sub checkIfIDLAttributesExists | |
233 { | |
234 my $idlAttributes = shift; | |
235 my $extendedAttributes = shift; | |
236 my $idlFile = shift; | |
237 | |
238 my $error; | |
239 OUTER: for my $name (keys %$extendedAttributes) { | |
240 if (!exists $idlAttributes->{$name}) { | |
241 $error = "Invalid IDL attribute [$name] found in $idlFile."; | |
242 last; | |
243 } | |
244 # Check no argument first, since "*" means "some argument (not missing)"
. | |
245 if ($extendedAttributes->{$name} eq "VALUE_IS_MISSING" and not exists $i
dlAttributes->{$name}{"VALUE_IS_MISSING"}) { | |
246 $error = "Missing required argument for IDL attribute [$name] in fil
e $idlFile."; | |
247 last; | |
248 } | |
249 if (exists $idlAttributes->{$name}{"*"}) { | |
250 next; | |
251 } | |
252 for my $rightValue (split /\s*[|&]\s*/, $extendedAttributes->{$name}) { | |
253 if (!(exists $idlAttributes->{$name}{$rightValue})) { | |
254 $error = "Invalid IDL attribute value [$name=" . $extendedAttrib
utes->{$name} . "] found in $idlFile."; | |
255 last OUTER; | |
256 } | |
257 } | |
258 } | |
259 if ($error) { | |
260 die "IDL ATTRIBUTE CHECKER ERROR: $error | |
261 If you want to add a new IDL extended attribute, please add it to: | |
262 bindings/IDLExtendedAttributes.txt | |
263 and add an explanation to the Blink IDL documentation at: | |
264 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes | |
265 "; | |
266 } | |
267 } | |
OLD | NEW |