add-include 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/perl -w
  2. # Copyright 2009 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. # 1. Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. # Helper script to add includes to source files.
  25. use strict;
  26. my $headerPattern = '[\"<][A-Za-z][A-Za-z0-9_/]+(\.h)?[\">]'; # " Make Xcode formatter happy.
  27. my $headerToAdd = shift @ARGV or die;
  28. $headerToAdd =~ /^([A-Za-z][A-Za-z0-9]+)\.h$/ or die "Header to add must be a .h file: $headerToAdd.\n";
  29. sub includesParagraph;
  30. FILE: for my $filename (@ARGV) {
  31. unless ($filename =~ /(\w+)\.cpp$/) { print STDERR "Command line args must be .cpp files: $filename.\n"; next FILE; }
  32. my $base = $1;
  33. my $sawConfig = 0;
  34. my $sawSelfInclude = 0;
  35. my $pastIncludes = 0;
  36. my %includes;
  37. my $beforeIncludes = "";
  38. my $afterIncludes = "";
  39. my $currentCondition = "";
  40. my $entireFileCondition = "";
  41. unless (open INPUT, "<", $filename) { print STDERR "File does not exist: $filename\n"; next FILE; }
  42. while (my $line = <INPUT>) {
  43. if ($line =~ /^\s*#(include|import)\s*($headerPattern)\s*\n/) {
  44. my $include = $2;
  45. if ($pastIncludes) { print STDERR "Saw more includes after include section in $filename, line $.\n"; next FILE; }
  46. if ($include eq "\"config.h\"") {
  47. $sawConfig = 1;
  48. } else {
  49. unless ($sawConfig) { print STDERR "First include must be config.h in $filename, line $.\n"; next FILE; }
  50. if ($include eq "\"$base.h\"") {
  51. $sawSelfInclude = 1;
  52. } else {
  53. unless ($sawSelfInclude) { print STDERR "Second include must be $base.h in $filename, line $.\n"; next FILE; }
  54. $includes{$currentCondition}{$include} = 1;
  55. }
  56. }
  57. } else {
  58. if ($sawConfig && !$pastIncludes) {
  59. if ($line =~ /^\s*#\s*if\s+(.+?)\s*$/) {
  60. my $condition = $1;
  61. if (!$sawSelfInclude) {
  62. $entireFileCondition = $1;
  63. next;
  64. }
  65. unless ($currentCondition eq "") { print STDERR "Nested #if in include section in $filename, line $.\n"; next FILE; }
  66. $currentCondition = $condition;
  67. next;
  68. }
  69. if ($line =~ /^\s*#\s*endif\s*$/) {
  70. unless ($currentCondition ne "") { print STDERR "Extra #endif in include section in $filename, line $.\n"; next FILE; }
  71. $currentCondition = "";
  72. next;
  73. }
  74. }
  75. if (!$sawConfig) {
  76. $beforeIncludes .= $line;
  77. } else {
  78. $pastIncludes = 1 if $line !~ /^\s*$/;
  79. if ($pastIncludes) {
  80. unless ($currentCondition eq "") { print STDERR "Unterminated #if in include section in $filename, line $.\n"; next FILE; }
  81. $afterIncludes .= $line;
  82. }
  83. }
  84. }
  85. }
  86. close INPUT or die;
  87. $includes{""}{"\"$headerToAdd\""} = 1;
  88. $beforeIncludes =~ s/\n+$//;
  89. $afterIncludes =~ s/^\n+//;
  90. my $contents = $beforeIncludes;
  91. $contents .= "\n\n#include \"config.h\"\n";
  92. $contents .= "\n#if $entireFileCondition\n" if $entireFileCondition ne "";
  93. $contents .= "#include \"$base.h\"\n\n";
  94. for my $condition (sort keys %includes) {
  95. $contents .= "#if $condition\n" unless $condition eq "";
  96. $contents .= includesParagraph($includes{$condition});
  97. $contents .= "#endif\n" unless $condition eq "";
  98. $contents .= "\n";
  99. }
  100. $contents .= $afterIncludes;
  101. unless (open OUTPUT, ">", $filename) { print STDERR "Could not open file for writing: $filename\n"; next FILE; };
  102. print OUTPUT $contents;
  103. close OUTPUT or die;
  104. }
  105. sub includesParagraph()
  106. {
  107. my ($includes) = @_;
  108. my $paragraph = "";
  109. for my $include (sort keys %{$includes}) {
  110. $paragraph .= "#include $include\n";
  111. }
  112. return $paragraph;
  113. }