check-for-webkit-framework-include-consistency 3.6 KB

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