generate-forwarding-headers.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use strict;
  29. use Cwd qw(abs_path realpath);
  30. use File::Find;
  31. use File::Basename;
  32. use File::Path qw(mkpath);
  33. use File::Spec::Functions;
  34. my $srcRoot = realpath(File::Spec->catfile(dirname(abs_path($0)), "../.."));
  35. my $incFromRoot = abs_path($ARGV[0]);
  36. my @platformPrefixes = ("blackberry", "cf", "CoordinatedGraphics", "curl", "efl", "gtk", "mac", "qt", "soup", "win", "manx");
  37. my @frameworks = ("JavaScriptCore", "WebCore", "WebKit2");
  38. my @skippedPrefixes;
  39. my @frameworkHeaders;
  40. my $framework;
  41. my %neededHeaders;
  42. shift;
  43. my $outputDirectory = $ARGV[0];
  44. shift;
  45. my $platform = $ARGV[0];
  46. foreach my $prefix (@platformPrefixes) {
  47. push(@skippedPrefixes, $prefix) unless ($prefix =~ $platform);
  48. }
  49. foreach (@frameworks) {
  50. $framework = $_;
  51. @frameworkHeaders = ();
  52. %neededHeaders = ();
  53. find(\&collectNeededHeaders, $incFromRoot);
  54. find(\&collectFameworkHeaderPaths, File::Spec->catfile($srcRoot, $framework));
  55. createForwardingHeadersForFramework();
  56. }
  57. sub collectNeededHeaders {
  58. my $filePath = $File::Find::name;
  59. my $file = $_;
  60. if ($filePath =~ '\.h$|\.cpp$|\.c$|\.mm$') {
  61. open(FILE, "<$file") or die "Could not open $filePath.\n";
  62. while (<FILE>) {
  63. if (m/^#.*<$framework\/(.*\.h)/) {
  64. $neededHeaders{$1} = 1;
  65. }
  66. }
  67. close(FILE);
  68. }
  69. }
  70. sub collectFameworkHeaderPaths {
  71. my $filePath = $File::Find::name;
  72. my $file = $_;
  73. if ($filePath =~ '\.h$' && $filePath !~ "ForwardingHeaders" && grep{$file eq $_} keys %neededHeaders) {
  74. my $headerPath = substr($filePath, length(File::Spec->catfile($srcRoot, $framework)) + 1 );
  75. push(@frameworkHeaders, $headerPath) unless (grep($headerPath =~ "$_/", @skippedPrefixes) || $headerPath =~ "config.h");
  76. }
  77. }
  78. sub createForwardingHeadersForFramework {
  79. my $targetDirectory = File::Spec->catfile($outputDirectory, $framework);
  80. mkpath($targetDirectory);
  81. foreach my $header (@frameworkHeaders) {
  82. my $headerName = basename($header);
  83. # If we found more headers with the same name, only generate a forwarding header for the current platform
  84. if(grep($_ =~ "/$headerName\$", @frameworkHeaders) == 1 || $header =~ "/$platform/" ) {
  85. my $forwardingHeaderPath = File::Spec->catfile($targetDirectory, $headerName);
  86. my $expectedIncludeStatement = "#include \"$framework/$header\"";
  87. my $foundIncludeStatement = 0;
  88. $foundIncludeStatement = <EXISTING_HEADER> if open(EXISTING_HEADER, "<$forwardingHeaderPath");
  89. chomp($foundIncludeStatement);
  90. if (! $foundIncludeStatement || $foundIncludeStatement ne $expectedIncludeStatement) {
  91. print "[Creating forwarding header for $framework/$header]\n";
  92. open(FORWARDING_HEADER, ">$forwardingHeaderPath") or die "Could not open $forwardingHeaderPath.";
  93. print FORWARDING_HEADER "$expectedIncludeStatement\n";
  94. close(FORWARDING_HEADER);
  95. }
  96. close(EXISTING_HEADER);
  97. }
  98. }
  99. }