3 # Copyright 2009 Apple Inc. All rights reserved.
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 APPLE INC. ``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 APPLE INC. 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 # Helper script to add includes to source files.
30 my $headerPattern = '[\"<][A-Za-z][A-Za-z0-9_/]+(\.h)?[\">]'; # " Make Xcode formatter happy.
32 my $headerToAdd = shift @ARGV or die;
33 $headerToAdd =~ /^([A-Za-z][A-Za-z0-9]+)\.h$/ or die "Header to add must be a .h file: $headerToAdd.\n";
35 sub includesParagraph;
37 FILE: for my $filename (@ARGV) {
38 unless ($filename =~ /(\w+)\.cpp$/) { print STDERR "Command line args must be .cpp files: $filename.\n"; next FILE; }
43 my $sawSelfInclude = 0;
48 my $beforeIncludes = "";
49 my $afterIncludes = "";
51 my $currentCondition = "";
53 my $entireFileCondition = "";
55 unless (open INPUT, "<", $filename) { print STDERR "File does not exist: $filename\n"; next FILE; }
56 while (my $line = <INPUT>) {
57 if ($line =~ /^\s*#(include|import)\s*($headerPattern)\s*\n/) {
59 if ($pastIncludes) { print STDERR "Saw more includes after include section in $filename, line $.\n"; next FILE; }
60 if ($include eq "\"config.h\"") {
63 unless ($sawConfig) { print STDERR "First include must be config.h in $filename, line $.\n"; next FILE; }
64 if ($include eq "\"$base.h\"") {
67 unless ($sawSelfInclude) { print STDERR "Second include must be $base.h in $filename, line $.\n"; next FILE; }
68 $includes{$currentCondition}{$include} = 1;
72 if ($sawConfig && !$pastIncludes) {
73 if ($line =~ /^\s*#\s*if\s+(.+?)\s*$/) {
75 if (!$sawSelfInclude) {
76 $entireFileCondition = $1;
79 unless ($currentCondition eq "") { print STDERR "Nested #if in include section in $filename, line $.\n"; next FILE; }
80 $currentCondition = $condition;
83 if ($line =~ /^\s*#\s*endif\s*$/) {
84 unless ($currentCondition ne "") { print STDERR "Extra #endif in include section in $filename, line $.\n"; next FILE; }
85 $currentCondition = "";
90 $beforeIncludes .= $line;
92 $pastIncludes = 1 if $line !~ /^\s*$/;
94 unless ($currentCondition eq "") { print STDERR "Unterminated #if in include section in $filename, line $.\n"; next FILE; }
95 $afterIncludes .= $line;
102 $includes{""}{"\"$headerToAdd\""} = 1;
104 $beforeIncludes =~ s/\n+$//;
105 $afterIncludes =~ s/^\n+//;
107 my $contents = $beforeIncludes;
108 $contents .= "\n\n#include \"config.h\"\n";
109 $contents .= "\n#if $entireFileCondition\n" if $entireFileCondition ne "";
110 $contents .= "#include \"$base.h\"\n\n";
111 for my $condition (sort keys %includes) {
112 $contents .= "#if $condition\n" unless $condition eq "";
113 $contents .= includesParagraph($includes{$condition});
114 $contents .= "#endif\n" unless $condition eq "";
117 $contents .= $afterIncludes;
119 unless (open OUTPUT, ">", $filename) { print STDERR "Could not open file for writing: $filename\n"; next FILE; };
120 print OUTPUT $contents;
124 sub includesParagraph()
130 for my $include (sort keys %{$includes}) {
131 $paragraph .= "#include $include\n";