2 # Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # A script which searches for headers included by WebKit2 files
27 # and generates forwarding headers for these headers.
30 use Cwd qw(abs_path realpath);
33 use File::Path qw(mkpath);
34 use File::Spec::Functions;
37 my $srcRoot = realpath(File::Spec->catfile(dirname(abs_path($0)), "../.."));
38 my @platformPrefixes = ("ca", "cf", "cocoa", "Cocoa", "CoordinatedGraphics", "curl", "gtk", "ios", "mac", "soup", "win", "wpe");
39 my @frameworks = ("JavaScriptCore", "WebCore", "WebKit");
44 my $verbose = 0; # enable it for debugging purpose
51 'include-path=s' => \@incFromRoot,
52 'output=s' => \$outputDirectory,
53 'platform=s' => \@platform
58 foreach my $prefix (@platformPrefixes) {
59 push(@skippedPrefixes, $prefix) if grep($_ =~ "$prefix", @platform) == 0;
62 foreach (@frameworks) {
64 @frameworkHeaders = ();
67 foreach (@incFromRoot) { find(\&collectNeededHeaders, abs_path($_) ); };
68 find(\&collectFrameworkHeaderPaths, File::Spec->catfile($srcRoot, $framework));
69 createForwardingHeadersForFramework();
72 sub collectNeededHeaders {
73 my $filePath = $File::Find::name;
75 if ($filePath =~ '\.h$|\.cpp$|\.c$') {
76 open(FILE, "<$file") or die "Could not open $filePath.\n";
78 if (m/^#.*<$framework\/(.*\.h)/) {
79 $neededHeaders{$1} = 1;
86 sub collectFrameworkHeaderPaths {
87 my $filePath = $File::Find::name;
89 if ($filePath =~ '\.h$' && $filePath !~ "ForwardingHeaders" && grep{$file eq $_} keys %neededHeaders) {
90 my $headerPath = substr($filePath, length(File::Spec->catfile($srcRoot, $framework)) + 1 );
91 push(@frameworkHeaders, $headerPath) unless (grep($headerPath =~ "$_/", @skippedPrefixes) || $headerPath =~ "config.h");
95 sub createForwardingHeadersForFramework {
96 my $targetDirectory = File::Spec->catfile($outputDirectory, $framework);
97 mkpath($targetDirectory);
98 foreach my $header (@frameworkHeaders) {
99 my $headerName = basename($header);
101 # If we found more headers with the same name, exit immediately.
102 my @headers = grep($_ =~ "/$headerName\$", @frameworkHeaders);
104 print("ERROR: Can't create $headerName forwarding header, because there are more headers with the same name:\n");
105 foreach (@headers) { print " - $_\n" };
109 my $forwardingHeaderPath = File::Spec->catfile($targetDirectory, $headerName);
110 my $expectedIncludeStatement = "#include \"$framework/$header\"";
111 my $foundIncludeStatement = 0;
113 $foundIncludeStatement = <EXISTING_HEADER> if open(EXISTING_HEADER, "<$forwardingHeaderPath");
114 chomp($foundIncludeStatement);
116 if (! $foundIncludeStatement || $foundIncludeStatement ne $expectedIncludeStatement) {
117 print "[Creating forwarding header for $framework/$header]\n" if $verbose;
118 open(FORWARDING_HEADER, ">$forwardingHeaderPath") or die "Could not open $forwardingHeaderPath.";
119 print FORWARDING_HEADER "$expectedIncludeStatement\n";
120 close(FORWARDING_HEADER);
123 close(EXISTING_HEADER);