WebKitTools/Scripts/check-for-webkit-framework-include-consistency
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 #!/usr/bin/env ruby
       
     2 
       
     3 # Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
       
    15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    16 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    18 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    24 # THE POSSIBILITY OF SUCH DAMAGE.
       
    25 
       
    26 
       
    27 base_directory = ENV['TARGET_BUILD_DIR']
       
    28 
       
    29 unless base_directory
       
    30   throw "Unable to find TARGET_BUILD_DIR in the environment!"
       
    31 end
       
    32 
       
    33 Dir.chdir base_directory
       
    34 
       
    35 $PERMITTED_INCLUDE_TYPES = { :public => [ :public ], :private => [ :public, :private ] }
       
    36 
       
    37 $HEADER_NAMES_TO_TYPE = { }
       
    38 $HEADERS_BY_TYPE = { :public => [], :private => [] }
       
    39 
       
    40 $error_printed = false
       
    41 
       
    42 def print_error msg
       
    43   $error_printed = true
       
    44   STDERR.puts "ERROR: #{msg}"
       
    45 end
       
    46 
       
    47 def build_header_maps
       
    48   all_headers = `find WebKit.framework/Versions/A/{,Private}Headers -type f -name '*.h'`.split
       
    49 
       
    50   all_headers.each do |header|
       
    51     if /\/Headers\/(.*)/.match(header)
       
    52       $HEADER_NAMES_TO_TYPE[$1] = :public
       
    53       $HEADERS_BY_TYPE[:public] << header
       
    54     elsif /\/PrivateHeaders\/(.*)/.match(header)
       
    55       $HEADER_NAMES_TO_TYPE[$1] = :private
       
    56       $HEADERS_BY_TYPE[:private] << header
       
    57     else
       
    58       print_error "Unknown header type: #{header}"
       
    59     end
       
    60   end
       
    61 end
       
    62 
       
    63 def resolve_include(header, included_header, permitted_types)
       
    64   # Ignore includes that aren't in the typical framework style.
       
    65   return unless /<([^\/]+)\/(.*)>/.match(included_header)
       
    66 
       
    67   framework, included_header_name = [$1, $2]
       
    68 
       
    69   # Ignore includes that aren't related to other WebKit headers.
       
    70   return unless framework =~ /^Web/
       
    71 
       
    72   # A header of any type including a WebCore header is a recipe for disaster.
       
    73   if framework == "WebCore"
       
    74     # <rdar://problem/7718826> WebKeyGenerator.h should not include a WebCore header
       
    75     return if header =~ /\/WebKeyGenerator.h$/ and included_header_name == "WebCoreKeyGenerator.h"
       
    76 
       
    77     print_error "#{header} included #{included_header}!"
       
    78     return
       
    79   end
       
    80 
       
    81   header_type = $HEADER_NAMES_TO_TYPE[included_header_name]
       
    82 
       
    83   if not header_type
       
    84     print_error "#{header} included #{included_header} but I could not find a header of that name!"
       
    85   elsif not permitted_types.member?(header_type)
       
    86     print_error "#{header} included #{included_header} which is #{header_type}!"
       
    87   end
       
    88 end
       
    89 
       
    90 def verify_includes(header, permitted_types)
       
    91   File.open(header) do |file|
       
    92     file.each_line do |line|
       
    93       if /#(include|import) (.*)/.match(line)
       
    94         resolve_include(header, $2, permitted_types)
       
    95       end
       
    96     end
       
    97   end
       
    98 end
       
    99 
       
   100 build_header_maps
       
   101 
       
   102 $HEADERS_BY_TYPE.each do |header_type, headers|
       
   103   permitted_types = $PERMITTED_INCLUDE_TYPES[header_type]
       
   104   headers.each do |header|
       
   105     verify_includes header, permitted_types
       
   106   end
       
   107 end
       
   108 
       
   109 exit 1 if $error_printed