Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: Source/bindings/scripts/generate-bindings.pl

Issue 18190004: Add Python flow to bindings generation, move dummy-generating IDL files over (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Revised Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 # This script is a temporary hack.
25 # Files are generated in the source directory, when they really should go
26 # to the DerivedSources directory.
27 # This should also eventually be a build rule driven off of .idl files
28 # however a build rule only solution is blocked by several radars:
29 # <rdar://problems/4251781&4251785>
30
31 use strict;
32
33 use File::Path;
34 use File::Basename;
35 use Getopt::Long;
36 use Text::ParseWords;
37 use Cwd;
38
39 use IDLParser;
40 use CodeGeneratorV8;
41 use IDLSerializer;
42
43 my @idlDirectories;
44 my $outputDirectory;
45 my $defines;
46 my $filename;
47 my $preprocessor;
48 my $verbose;
49 my $supplementalDependencyFile;
50 my $additionalIdlFiles;
51 my $idlAttributesFile;
52 my $writeFileOnlyIfChanged;
53
54 GetOptions('include=s@' => \@idlDirectories,
55 'outputDir=s' => \$outputDirectory,
56 'defines=s' => \$defines,
57 'filename=s' => \$filename,
58 'preprocessor=s' => \$preprocessor,
59 'verbose' => \$verbose,
60 'supplementalDependencyFile=s' => \$supplementalDependencyFile,
61 'additionalIdlFiles=s' => \$additionalIdlFiles,
62 'idlAttributesFile=s' => \$idlAttributesFile,
63 'write-file-only-if-changed=s' => \$writeFileOnlyIfChanged);
64
65 my $targetIdlFile = $ARGV[0];
66
67 die('Must specify input file.') unless defined($targetIdlFile);
68 die('Must specify output directory.') unless defined($outputDirectory);
69 $defines = "" unless defined($defines);
70
71 $targetIdlFile = Cwd::realpath($targetIdlFile);
72 if ($verbose) {
73 print "$targetIdlFile\n";
74 }
75 my $targetInterfaceName = fileparse(basename($targetIdlFile), ".idl");
76
77 my $idlFound = 0;
78 my @supplementedIdlFiles;
79 if ($supplementalDependencyFile) {
80 # The format of a supplemental dependency file:
81 #
82 # Window.idl P.idl Q.idl R.idl
83 # Document.idl S.idl
84 # Event.idl
85 # ...
86 #
87 # The above indicates that Window.idl is supplemented by P.idl, Q.idl and R. idl,
88 # Document.idl is supplemented by S.idl, and Event.idl is supplemented by no IDLs.
89 # The IDL that supplements another IDL (e.g. P.idl) never appears in the dep endency file.
90 open FH, "< $supplementalDependencyFile" or die "Cannot open $supplementalDe pendencyFile\n";
91 while (my $line = <FH>) {
92 my ($idlFile, @followingIdlFiles) = split(/\s+/, $line);
93 if ($idlFile and basename($idlFile) eq basename($targetIdlFile)) {
94 $idlFound = 1;
95 # We sort the supplemental IDL files so that the corresponding code is generated
96 # in a consistent order. This is important for the bindings tests.
97 @supplementedIdlFiles = sort @followingIdlFiles;
98 }
99 }
100 close FH;
101
102 # $additionalIdlFiles is list of IDL files which should not be included in
103 # DerivedSources*.cpp (i.e. they are not described in the supplemental
104 # dependency file) but should generate .h and .cpp files.
105 if (!$idlFound and $additionalIdlFiles) {
106 my @idlFiles = shellwords($additionalIdlFiles);
107 $idlFound = grep { $_ and basename($_) eq basename($targetIdlFile) } @id lFiles;
108 }
109
110 if (!$idlFound) {
111 # We generate empty .h and .cpp files just to tell build scripts that .h and .cpp files are created.
112 generateEmptyHeaderAndCpp($targetInterfaceName, $outputDirectory);
113 exit 0;
114 }
115 }
116
117 # Parse the target IDL file.
118 my $targetParser = IDLParser->new(!$verbose);
119 my $targetDocument = $targetParser->Parse($targetIdlFile, $defines, $preprocesso r);
120
121 if ($idlAttributesFile) {
122 my $idlAttributes = loadIDLAttributes($idlAttributesFile);
123 checkIDLAttributes($idlAttributes, $targetDocument, basename($targetIdlFile) );
124 }
125
126 foreach my $idlFile (@supplementedIdlFiles) {
127 next if $idlFile eq $targetIdlFile;
128
129 my $interfaceName = fileparse(basename($idlFile), ".idl");
130 my $parser = IDLParser->new(!$verbose);
131 my $document = $parser->Parse($idlFile, $defines, $preprocessor);
132
133 foreach my $interface (@{$document->interfaces}) {
134 if (!$interface->isPartial || $interface->name eq $targetInterfaceName) {
135 my $targetDataNode;
136 foreach my $interface (@{$targetDocument->interfaces}) {
137 if ($interface->name eq $targetInterfaceName) {
138 $targetDataNode = $interface;
139 last;
140 }
141 }
142 die "Not found an interface ${targetInterfaceName} in ${targetInterf aceName}.idl." unless defined $targetDataNode;
143
144 # Support for attributes of partial interfaces.
145 foreach my $attribute (@{$interface->attributes}) {
146 # Record that this attribute is implemented by $interfaceName.
147 $attribute->extendedAttributes->{"ImplementedBy"} = $interfaceNa me if $interface->isPartial;
148
149 # Add interface-wide extended attributes to each attribute.
150 applyInterfaceExtendedAttributes($interface, $attribute->extende dAttributes);
151
152 push(@{$targetDataNode->attributes}, $attribute);
153 }
154
155 # Support for methods of partial interfaces.
156 foreach my $function (@{$interface->functions}) {
157 # Record that this method is implemented by $interfaceName.
158 $function->extendedAttributes->{"ImplementedBy"} = $interfaceNam e if $interface->isPartial;
159
160 # Add interface-wide extended attributes to each method.
161 applyInterfaceExtendedAttributes($interface, $function->extended Attributes);
162
163 push(@{$targetDataNode->functions}, $function);
164 }
165
166 # Support for constants of partial interfaces.
167 foreach my $constant (@{$interface->constants}) {
168 # Record that this constant is implemented by $interfaceName.
169 $constant->extendedAttributes->{"ImplementedBy"} = $interfaceNam e if $interface->isPartial;
170
171 # Add interface-wide extended attributes to each constant.
172 applyInterfaceExtendedAttributes($interface, $constant->extended Attributes);
173
174 push(@{$targetDataNode->constants}, $constant);
175 }
176 } else {
177 die "$idlFile is not a supplemental dependency of $targetIdlFile. Th ere maybe a bug in the the supplemental dependency generator (preprocess_idls.py ).\n";
178 }
179 }
180 }
181
182 # FIXME: This code will be removed once IDLParser.pm and CodeGeneratorV8.pm
183 # are connected via JSON files. See http://crbug.com/242795
184 $targetDocument = deserializeJSON(serializeJSON($targetDocument));
185
186 # Generate desired output for the target IDL file.
187 my @dependentIdlFiles = ($targetDocument->fileName(), @supplementedIdlFiles);
188 my $codeGenerator = CodeGeneratorV8->new($targetDocument, \@idlDirectories, $pre processor, $defines, $verbose, \@dependentIdlFiles, $writeFileOnlyIfChanged);
189 my $interfaces = $targetDocument->interfaces;
190 foreach my $interface (@$interfaces) {
191 print "Generating bindings code for IDL interface \"" . $interface->name . " \"...\n" if $verbose;
192 $codeGenerator->GenerateInterface($interface);
193 $codeGenerator->WriteData($interface, $outputDirectory);
194 }
195
196 sub generateEmptyHeaderAndCpp
197 {
198 my ($targetInterfaceName, $outputDirectory) = @_;
199
200 my $headerName = "V8${targetInterfaceName}.h";
201 my $cppName = "V8${targetInterfaceName}.cpp";
202 my $contents = "/*
203 This file is generated just to tell build scripts that $headerName and
204 $cppName are created for ${targetInterfaceName}.idl, and thus
205 prevent the build scripts from trying to generate $headerName and
206 $cppName at every build. This file must not be tried to compile.
207 */
208 ";
209 open FH, "> ${outputDirectory}/${headerName}" or die "Cannot open $headerNam e\n";
210 print FH $contents;
211 close FH;
212
213 open FH, "> ${outputDirectory}/${cppName}" or die "Cannot open $cppName\n";
214 print FH $contents;
215 close FH;
216 }
217
218 sub loadIDLAttributes
219 {
220 my $idlAttributesFile = shift;
221
222 my %idlAttributes;
223 open FH, "<", $idlAttributesFile or die "Couldn't open $idlAttributesFile: $ !";
224 while (my $line = <FH>) {
225 chomp $line;
226 next if $line =~ /^\s*#/;
227 next if $line =~ /^\s*$/;
228
229 if ($line =~ /^\s*([^=\s]*)\s*=?\s*(.*)/) {
230 my $name = $1;
231 $idlAttributes{$name} = {};
232 if ($2) {
233 foreach my $rightValue (split /\|/, $2) {
234 $rightValue =~ s/^\s*|\s*$//g;
235 $rightValue = "VALUE_IS_MISSING" unless $rightValue;
236 $idlAttributes{$name}{$rightValue} = 1;
237 }
238 } else {
239 $idlAttributes{$name}{"VALUE_IS_MISSING"} = 1;
240 }
241 } else {
242 die "The format of " . basename($idlAttributesFile) . " is wrong: li ne $.\n";
243 }
244 }
245 close FH;
246
247 return \%idlAttributes;
248 }
249
250 sub checkIDLAttributes
251 {
252 my $idlAttributes = shift;
253 my $document = shift;
254 my $idlFile = shift;
255
256 foreach my $interface (@{$document->interfaces}) {
257 checkIfIDLAttributesExists($idlAttributes, $interface->extendedAttribute s, $idlFile);
258
259 foreach my $attribute (@{$interface->attributes}) {
260 checkIfIDLAttributesExists($idlAttributes, $attribute->extendedAttri butes, $idlFile);
261 }
262
263 foreach my $function (@{$interface->functions}) {
264 checkIfIDLAttributesExists($idlAttributes, $function->extendedAttrib utes, $idlFile);
265 foreach my $parameter (@{$function->parameters}) {
266 checkIfIDLAttributesExists($idlAttributes, $parameter->extendedA ttributes, $idlFile);
267 }
268 }
269 }
270 }
271
272 sub applyInterfaceExtendedAttributes
273 {
274 my $interface = shift;
275 my $extendedAttributes = shift;
276
277 foreach my $extendedAttributeName (keys %{$interface->extendedAttributes}) {
278 next if $extendedAttributeName eq "ImplementedAs";
279 $extendedAttributes->{$extendedAttributeName} = $interface->extendedAttr ibutes->{$extendedAttributeName};
280 }
281 }
282
283 sub checkIfIDLAttributesExists
284 {
285 my $idlAttributes = shift;
286 my $extendedAttributes = shift;
287 my $idlFile = shift;
288
289 my $error;
290 OUTER: for my $name (keys %$extendedAttributes) {
291 if (!exists $idlAttributes->{$name}) {
292 $error = "Unknown IDL attribute [$name] is found at $idlFile.";
293 last OUTER;
294 }
295 if ($idlAttributes->{$name}{"*"}) {
296 next;
297 }
298 for my $rightValue (split /\s*[|&]\s*/, $extendedAttributes->{$name}) {
299 if (!exists $idlAttributes->{$name}{$rightValue}) {
300 $error = "Unknown IDL attribute [$name=" . $extendedAttributes-> {$name} . "] is found at $idlFile.";
301 last OUTER;
302 }
303 }
304 }
305 if ($error) {
306 die "IDL ATTRIBUTE CHECKER ERROR: $error
307 If you want to add a new IDL attribute, you need to add it to bindings/scripts/I DLAttributes.txt and add explanations to the Blink IDL document (http://chromium .org/blink/webidl).
308 ";
309 }
310 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698