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
27base_directory = ENV['TARGET_BUILD_DIR'] or throw "Unable to find TARGET_BUILD_DIR in the environment!"
28is_shallow_bundle = (ENV['SHALLOW_BUNDLE'] || "NO").upcase == "YES"
29
30unless base_directory
31  throw "Unable to find TARGET_BUILD_DIR in the environment!"
32end
33
34Dir.chdir base_directory
35
36$PERMITTED_INCLUDE_TYPES = { :public => [ :public ], :private => [ :public, :private ] }
37
38$HEADER_NAMES_TO_TYPE = { }
39$HEADERS_BY_TYPE = { :public => [], :private => [] }
40
41$error_printed = false
42
43def print_error msg
44  $error_printed = true
45  STDERR.puts "ERROR: #{msg}"
46end
47
48def build_header_maps is_shallow_bundle
49  current_version_path = is_shallow_bundle ? "" : "Versions/A/"
50  all_headers = `find WebKit.framework/#{current_version_path}{,Private}Headers -type f -name '*.h'`.split
51
52  all_headers.each do |header|
53    if /\/Headers\/(.*)/.match(header)
54      $HEADER_NAMES_TO_TYPE[$1] = :public
55      $HEADERS_BY_TYPE[:public] << header
56    elsif /\/PrivateHeaders\/(.*)/.match(header)
57      $HEADER_NAMES_TO_TYPE[$1] = :private
58      $HEADERS_BY_TYPE[:private] << header
59    else
60      print_error "Unknown header type: #{header}"
61    end
62  end
63end
64
65def resolve_include(header, included_header, permitted_types)
66  # Ignore includes that aren't in the typical framework style.
67  return unless /<([^\/]+)\/(.*)>/.match(included_header)
68
69  framework, included_header_name = [$1, $2]
70
71  # Ignore includes that aren't related to other WebKit headers.
72  return unless framework =~ /^Web/
73
74  # A header of any type including a WebCore header is a recipe for disaster.
75  if framework == "WebCore"
76    # <rdar://problem/7718826> WebKeyGenerator.h should not include a WebCore header
77    return if header =~ /\/WebKeyGenerator.h$/ and included_header_name == "WebCoreKeyGenerator.h"
78
79    print_error "#{header} included #{included_header}!"
80    return
81  end
82
83  header_type = $HEADER_NAMES_TO_TYPE[included_header_name]
84
85  if not header_type
86    print_error "#{header} included #{included_header} but I could not find a header of that name!"
87  elsif not permitted_types.member?(header_type)
88    print_error "#{header} included #{included_header} which is #{header_type}!"
89  end
90end
91
92def verify_includes(header, permitted_types)
93  File.open(header) do |file|
94    file.each_line do |line|
95      if /#(include|import) (.*)/.match(line)
96        resolve_include(header, $2, permitted_types)
97      end
98    end
99  end
100end
101
102build_header_maps is_shallow_bundle
103
104$HEADERS_BY_TYPE.each do |header_type, headers|
105  permitted_types = $PERMITTED_INCLUDE_TYPES[header_type]
106  headers.each do |header|
107    verify_includes header, permitted_types
108  end
109end
110
111exit 1 if $error_printed
112