1#!/usr/bin/perl -w
2# Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
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.
13#
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.
25#
26# A script which searches for headers included by WebKit2 files
27# and generates forwarding headers for these headers.
28
29use strict;
30use Cwd qw(abs_path realpath);
31use File::Find;
32use File::Basename;
33use File::Path qw(mkpath);
34use File::Spec::Functions;
35
36my $srcRoot = realpath(File::Spec->catfile(dirname(abs_path($0)), "../.."));
37my $incFromRoot = abs_path($ARGV[0]);
38my @platformPrefixes = ("cf", "Cocoa", "CoordinatedGraphics", "curl", "efl", "gtk", "mac", "soup", "win");
39my @frameworks = ("JavaScriptCore", "WebCore", "WebKit");
40my @skippedPrefixes;
41my @frameworkHeaders;
42my $framework;
43my $frameworkDirectoryName;
44my %neededHeaders;
45
46shift;
47my $outputDirectory = $ARGV[0];
48shift;
49my $platform  = $ARGV[0];
50
51foreach my $prefix (@platformPrefixes) {
52    push(@skippedPrefixes, $prefix) unless ($prefix =~ $platform);
53}
54
55foreach (@frameworks) {
56    $framework = $_;
57    $frameworkDirectoryName = ($framework eq "WebKit") ? "WebKit2" : $framework;
58    @frameworkHeaders = ();
59    %neededHeaders = ();
60
61    find(\&collectNeededHeaders, $incFromRoot);
62    find(\&collectFrameworkHeaderPaths, File::Spec->catfile($srcRoot, $frameworkDirectoryName));
63    createForwardingHeadersForFramework();
64}
65
66sub collectNeededHeaders {
67    my $filePath = $File::Find::name;
68    my $file = $_;
69    if ($filePath =~ '\.h$|\.cpp$|\.c$|\.mm$') {
70        open(FILE, "<$file") or die "Could not open $filePath.\n";
71        while (<FILE>) {
72           if (m/^#.*<$framework\/(.*\.h)/) {
73               $neededHeaders{$1} = 1;
74           }
75        }
76        close(FILE);
77    }
78}
79
80sub collectFrameworkHeaderPaths {
81    my $filePath = $File::Find::name;
82    my $file = $_;
83    if ($filePath =~ '\.h$' && $filePath !~ "ForwardingHeaders" && grep{$file eq $_} keys %neededHeaders) {
84        my $headerPath = substr($filePath, length(File::Spec->catfile($srcRoot, $frameworkDirectoryName)) + 1 );
85        push(@frameworkHeaders, $headerPath) unless (grep($headerPath =~ "$_/", @skippedPrefixes) || $headerPath =~ "config.h");
86    }
87}
88
89sub createForwardingHeadersForFramework {
90    my $targetDirectory = File::Spec->catfile($outputDirectory, $framework);
91    mkpath($targetDirectory);
92    foreach my $header (@frameworkHeaders) {
93        my $headerName = basename($header);
94
95        # If we found more headers with the same name, only generate a forwarding header for the current platform
96        if(grep($_ =~ "/$headerName\$", @frameworkHeaders) == 1 || $header =~ "/$platform/" ) {
97            my $forwardingHeaderPath = File::Spec->catfile($targetDirectory, $headerName);
98            my $expectedIncludeStatement = "#include \"$frameworkDirectoryName/$header\"";
99            my $foundIncludeStatement = 0;
100
101            $foundIncludeStatement = <EXISTING_HEADER> if open(EXISTING_HEADER, "<$forwardingHeaderPath");
102            chomp($foundIncludeStatement);
103
104            if (! $foundIncludeStatement || $foundIncludeStatement ne $expectedIncludeStatement) {
105                print "[Creating forwarding header for $framework/$header]\n";
106                open(FORWARDING_HEADER, ">$forwardingHeaderPath") or die "Could not open $forwardingHeaderPath.";
107                print FORWARDING_HEADER "$expectedIncludeStatement\n";
108                close(FORWARDING_HEADER);
109            }
110
111            close(EXISTING_HEADER);
112        }
113    }
114}
115