Home | History | Annotate | Download | only in Scripts
      1 #!/usr/bin/perl -w
      2 
      3 # Copyright 2009 Apple Inc. 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 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.
     25 
     26 # Helper script to add includes to source files.
     27 
     28 use strict;
     29 
     30 my $headerPattern = '[\"<][A-Za-z][A-Za-z0-9_/]+(\.h)?[\">]'; # " Make Xcode formatter happy.
     31 
     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";
     34 
     35 sub includesParagraph;
     36 
     37 FILE: for my $filename (@ARGV) {
     38     unless ($filename =~ /(\w+)\.cpp$/) { print STDERR "Command line args must be .cpp files: $filename.\n"; next FILE; }
     39 
     40     my $base = $1;
     41 
     42     my $sawConfig = 0;
     43     my $sawSelfInclude = 0;
     44 
     45     my $pastIncludes = 0;
     46     my %includes;
     47 
     48     my $beforeIncludes = "";
     49     my $afterIncludes = "";
     50 
     51     my $currentCondition = "";
     52 
     53     my $entireFileCondition = "";
     54 
     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/) {
     58             my $include = $2;
     59             if ($pastIncludes) { print STDERR "Saw more includes after include section in $filename, line $.\n"; next FILE; }
     60             if ($include eq "\"config.h\"") {
     61                 $sawConfig = 1;
     62             } else {
     63                 unless ($sawConfig) { print STDERR "First include must be config.h in $filename, line $.\n"; next FILE; }
     64                 if ($include eq "\"$base.h\"") {
     65                     $sawSelfInclude = 1;
     66                 } else {
     67                     unless ($sawSelfInclude) { print STDERR "Second include must be $base.h in $filename, line $.\n"; next FILE; }
     68                     $includes{$currentCondition}{$include} = 1;
     69                 }
     70             }
     71         } else {
     72             if ($sawConfig && !$pastIncludes) {
     73                 if ($line =~ /^\s*#\s*if\s+(.+?)\s*$/) {
     74                     my $condition = $1;
     75                     if (!$sawSelfInclude) {
     76                         $entireFileCondition = $1;
     77                         next;
     78                     }
     79                     unless ($currentCondition eq "") { print STDERR "Nested #if in include section in $filename, line $.\n"; next FILE; }
     80                     $currentCondition = $condition;
     81                     next;
     82                 }
     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 = "";
     86                     next;
     87                 }
     88             }
     89             if (!$sawConfig) {
     90                 $beforeIncludes .= $line;
     91             } else {
     92                 $pastIncludes = 1 if $line !~ /^\s*$/;
     93                 if ($pastIncludes) {
     94                     unless ($currentCondition eq "") { print STDERR "Unterminated #if in include section in $filename, line $.\n"; next FILE; }
     95                     $afterIncludes .= $line;
     96                 }
     97             }
     98         }
     99     }
    100     close INPUT or die;
    101 
    102     $includes{""}{"\"$headerToAdd\""} = 1;
    103 
    104     $beforeIncludes =~ s/\n+$//;
    105     $afterIncludes =~ s/^\n+//;
    106 
    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 "";
    115         $contents .= "\n";
    116     }
    117     $contents .= $afterIncludes;
    118 
    119     unless (open OUTPUT, ">", $filename) { print STDERR "Could not open file for writing: $filename\n"; next FILE; };
    120     print OUTPUT $contents;
    121     close OUTPUT or die;
    122 }
    123 
    124 sub includesParagraph()
    125 {
    126     my ($includes) = @_;
    127 
    128     my $paragraph = "";
    129 
    130     for my $include (sort keys %{$includes}) {
    131         $paragraph .= "#include $include\n";
    132     }
    133 
    134     return $paragraph;
    135 }
    136