3 # Copyright (C) 2005 Apple Computer, Inc.
4 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
6 # This file is part of WebKit
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.
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.
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.
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>
43 my $outputHeadersDirectory;
49 my $writeDependencies;
51 my $supplementalDependencyFile;
52 my $additionalIdlFilesList;
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);
67 my $targetIdlFile = $ARGV[0];
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);
73 if (!$outputHeadersDirectory) {
74 $outputHeadersDirectory = $outputDirectory;
76 $targetIdlFile = Cwd::realpath($targetIdlFile);
78 print "$generator: $targetIdlFile\n";
80 my $targetInterfaceName = fileparse(basename($targetIdlFile), ".idl");
83 my @supplementedIdlFiles;
84 if ($supplementalDependencyFile) {
85 # The format of a supplemental dependency file:
87 # DOMWindow.idl P.idl Q.idl R.idl
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)) {
100 @supplementedIdlFiles = @followingIdlFiles;
105 # The file $additionalIdlFilesList contains one IDL file per line:
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";
116 $idlFound = grep { $_ and basename($_) eq basename($targetIdlFile) } @idlFiles;
121 my $codeGen = CodeGenerator->new(\@idlDirectories, $generator, $outputDirectory, $outputHeadersDirectory, 0, $preprocessor, $writeDependencies, $verbose);
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);
129 # Parse the target IDL file.
130 my $targetParser = IDLParser->new(!$verbose);
131 my $targetDocument = $targetParser->Parse($targetIdlFile, $defines, $preprocessor);
133 foreach my $idlFile (@supplementedIdlFiles) {
134 next if $idlFile eq $targetIdlFile;
136 my $interfaceName = fileparse(basename($idlFile), ".idl");
137 my $parser = IDLParser->new(!$verbose);
138 my $document = $parser->Parse($idlFile, $defines, $preprocessor);
140 foreach my $dataNode (@{$document->classes}) {
141 if ($dataNode->extendedAttributes->{"Supplemental"} and $dataNode->extendedAttributes->{"Supplemental"} eq $targetInterfaceName) {
143 foreach my $class (@{$targetDocument->classes}) {
144 if ($class->name eq $targetInterfaceName) {
145 $targetDataNode = $class;
149 die "Not found an interface ${targetInterfaceName} in ${targetInterfaceName}.idl." unless defined $targetDataNode;
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;
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};
161 push(@{$targetDataNode->attributes}, $attribute);
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;
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};
174 push(@{$targetDataNode->functions}, $function);
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;
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};
187 push(@{$targetDataNode->constants}, $constant);
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);
197 sub generateEmptyHeaderAndCpp
199 my ($prefix, $targetInterfaceName, $outputHeadersDirectory, $outputDirectory) = @_;
201 my $headerName = "${prefix}${targetInterfaceName}.h";
202 my $cppName = "${prefix}${targetInterfaceName}.cpp";
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.
210 open FH, "> ${outputHeadersDirectory}/${headerName}" or die "Cannot open $headerName\n";
214 open FH, "> ${outputDirectory}/${cppName}" or die "Cannot open $cppName\n";