Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / bindings / scripts / generate-bindings.pl
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 Cwd;
37
38 use IDLParser;
39 use CodeGenerator;
40
41 my @idlDirectories;
42 my $outputDirectory;
43 my $outputHeadersDirectory;
44 my $generator;
45 my $defines;
46 my $filename;
47 my $prefix;
48 my $preprocessor;
49 my $writeDependencies;
50 my $verbose;
51 my $supplementalDependencyFile;
52 my $additionalIdlFilesList;
53
54 GetOptions('include=s@' => \@idlDirectories,
55            'outputDir=s' => \$outputDirectory,
56            'outputHeadersDir=s' => \$outputHeadersDirectory,
57            'generator=s' => \$generator,
58            'defines=s' => \$defines,
59            'filename=s' => \$filename,
60            'prefix=s' => \$prefix,
61            'preprocessor=s' => \$preprocessor,
62            'verbose' => \$verbose,
63            'write-dependencies' => \$writeDependencies,
64            'supplementalDependencyFile=s' => \$supplementalDependencyFile,
65            'additionalIdlFilesList=s' => \$additionalIdlFilesList);
66
67 my $targetIdlFile = $ARGV[0];
68
69 die('Must specify input file.') unless defined($targetIdlFile);
70 die('Must specify generator') unless defined($generator);
71 die('Must specify output directory.') unless defined($outputDirectory);
72
73 if (!$outputHeadersDirectory) {
74     $outputHeadersDirectory = $outputDirectory;
75 }
76 $targetIdlFile = Cwd::realpath($targetIdlFile);
77 if ($verbose) {
78     print "$generator: $targetIdlFile\n";
79 }
80 my $targetInterfaceName = fileparse(basename($targetIdlFile), ".idl");
81
82 my $idlFound = 0;
83 my @supplementedIdlFiles;
84 if ($supplementalDependencyFile) {
85     # The format of a supplemental dependency file:
86     #
87     # DOMWindow.idl P.idl Q.idl R.idl
88     # Document.idl S.idl
89     # Event.idl
90     # ...
91     #
92     # The above indicates that DOMWindow.idl is supplemented by P.idl, Q.idl and R.idl,
93     # Document.idl is supplemented by S.idl, and Event.idl is supplemented by no IDLs.
94     # The IDL that supplements another IDL (e.g. P.idl) never appears in the dependency file.
95     open FH, "< $supplementalDependencyFile" or die "Cannot open $supplementalDependencyFile\n";
96     while (my $line = <FH>) {
97         my ($idlFile, @followingIdlFiles) = split(/\s+/, $line);
98         if ($idlFile and basename($idlFile) eq basename($targetIdlFile)) {
99             $idlFound = 1;
100             @supplementedIdlFiles = @followingIdlFiles;
101         }
102     }
103     close FH;
104
105     # The file $additionalIdlFilesList contains one IDL file per line:
106     # P.idl
107     # Q.idl
108     # ...
109     # These IDL files are ones which should not be included in DerivedSources*.cpp
110     # (i.e. they are not described in the supplemental dependency file)
111     # but should generate .h and .cpp files.
112     if (!$idlFound and $additionalIdlFilesList) {
113         open FH, "< $additionalIdlFilesList" or die "Cannot open $additionalIdlFilesList\n";
114         my @idlFiles = <FH>;
115         chomp(@idlFiles);
116         $idlFound = grep { $_ and basename($_) eq basename($targetIdlFile) } @idlFiles;
117         close FH;
118     }
119
120     if (!$idlFound) {
121         my $codeGen = CodeGenerator->new(\@idlDirectories, $generator, $outputDirectory, $outputHeadersDirectory, 0, $preprocessor, $writeDependencies, $verbose);
122
123         # We generate empty .h and .cpp files just to tell build scripts that .h and .cpp files are created.
124         generateEmptyHeaderAndCpp($codeGen->FileNamePrefix(), $targetInterfaceName, $outputHeadersDirectory, $outputDirectory);
125         exit 0;
126     }
127 }
128
129 # Parse the target IDL file.
130 my $targetParser = IDLParser->new(!$verbose);
131 my $targetDocument = $targetParser->Parse($targetIdlFile, $defines, $preprocessor);
132
133 foreach my $idlFile (@supplementedIdlFiles) {
134     next if $idlFile eq $targetIdlFile;
135
136     my $interfaceName = fileparse(basename($idlFile), ".idl");
137     my $parser = IDLParser->new(!$verbose);
138     my $document = $parser->Parse($idlFile, $defines, $preprocessor);
139
140     foreach my $dataNode (@{$document->classes}) {
141         if ($dataNode->extendedAttributes->{"Supplemental"} and $dataNode->extendedAttributes->{"Supplemental"} eq $targetInterfaceName) {
142             my $targetDataNode;
143             foreach my $class (@{$targetDocument->classes}) {
144                 if ($class->name eq $targetInterfaceName) {
145                     $targetDataNode = $class;
146                     last;
147                 }
148             }
149             die "Not found an interface ${targetInterfaceName} in ${targetInterfaceName}.idl." unless defined $targetDataNode;
150
151             # Support [Supplemental] for attributes.
152             foreach my $attribute (@{$dataNode->attributes}) {
153                 # Record that this attribute is implemented by $interfaceName.
154                 $attribute->signature->extendedAttributes->{"ImplementedBy"} = $interfaceName;
155
156                 # Add interface-wide extended attributes to each attribute.
157                 foreach my $extendedAttributeName (keys %{$dataNode->extendedAttributes}) {
158                     next if ($extendedAttributeName eq "Supplemental");
159                     $attribute->signature->extendedAttributes->{$extendedAttributeName} = $dataNode->extendedAttributes->{$extendedAttributeName};
160                 }
161                 push(@{$targetDataNode->attributes}, $attribute);
162             }
163
164             # Support [Supplemental] for methods.
165             foreach my $function (@{$dataNode->functions}) {
166                 # Record that this method is implemented by $interfaceName.
167                 $function->signature->extendedAttributes->{"ImplementedBy"} = $interfaceName;
168
169                 # Add interface-wide extended attributes to each method.
170                 foreach my $extendedAttributeName (keys %{$dataNode->extendedAttributes}) {
171                     next if ($extendedAttributeName eq "Supplemental");
172                     $function->signature->extendedAttributes->{$extendedAttributeName} = $dataNode->extendedAttributes->{$extendedAttributeName};
173                 }
174                 push(@{$targetDataNode->functions}, $function);
175             }
176
177             # Support [Supplemental] for constants.
178             foreach my $constant (@{$dataNode->constants}) {
179                 # Record that this constant is implemented by $interfaceName.
180                 $constant->extendedAttributes->{"ImplementedBy"} = $interfaceName;
181
182                 # Add interface-wide extended attributes to each constant.
183                 foreach my $extendedAttributeName (keys %{$dataNode->extendedAttributes}) {
184                     next if ($extendedAttributeName eq "Supplemental");
185                     $constant->extendedAttributes->{$extendedAttributeName} = $dataNode->extendedAttributes->{$extendedAttributeName};
186                 }
187                 push(@{$targetDataNode->constants}, $constant);
188             }
189         }
190     }
191 }
192
193 # Generate desired output for the target IDL file.
194 my $codeGen = CodeGenerator->new(\@idlDirectories, $generator, $outputDirectory, $outputHeadersDirectory, 0, $preprocessor, $writeDependencies, $verbose);
195 $codeGen->ProcessDocument($targetDocument, $defines);
196
197 sub generateEmptyHeaderAndCpp
198 {
199     my ($prefix, $targetInterfaceName, $outputHeadersDirectory, $outputDirectory) = @_;
200
201     my $headerName = "${prefix}${targetInterfaceName}.h";
202     my $cppName = "${prefix}${targetInterfaceName}.cpp";
203     my $contents = "/*
204     This file is generated just to tell build scripts that $headerName and
205     $cppName are created for ${targetInterfaceName}.idl, and thus
206     prevent the build scripts from trying to generate $headerName and
207     $cppName at every build. This file must not be tried to compile.
208 */
209 ";
210     open FH, "> ${outputHeadersDirectory}/${headerName}" or die "Cannot open $headerName\n";
211     print FH $contents;
212     close FH;
213
214     open FH, "> ${outputDirectory}/${cppName}" or die "Cannot open $cppName\n";
215     print FH $contents;
216     close FH;
217 }