check-for-inappropriate-macros-in-external-headers 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012 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. # This script checks that the given headers in the framework build product do
  25. # not contain Platform.h and Compiler.h macros such as PLATFORM, COMPILER, etc.
  26. # This is meant to limit the exposure of the WTF headers, ensuring that if
  27. # clients include these headers they would not also need WTF's Platform.h.
  28. base_directory = ENV['TARGET_BUILD_DIR'] or throw "Unable to find TARGET_BUILD_DIR in the environment!"
  29. project_name = ENV['PROJECT_NAME'] or throw "Unable to find PROJECT_NAME in the environment!"
  30. $is_shallow_bundle = (ENV['SHALLOW_BUNDLE'] || "NO").upcase == "YES"
  31. $error_printed = false
  32. def print_error(msg)
  33. $error_printed = true
  34. STDERR.puts "ERROR: #{msg}"
  35. end
  36. def framework_headers_for_path(framework, path)
  37. full_path = File.join Dir.pwd, framework, $is_shallow_bundle ? "" : "Versions/A/", path
  38. if File.directory? full_path
  39. Dir.glob "#{full_path}/**/*.h"
  40. elsif File.exists? full_path
  41. [full_path]
  42. else
  43. print_error "path '#{full_path}' for argument '#{path}' does not exist."
  44. [] # Return an empty list so we can continue to check the other paths.
  45. end
  46. end
  47. def verify_macros_in_header(header)
  48. File.open(header) do |file|
  49. file.each_line.with_index do |line, index|
  50. # Check for the common macros from Platform.h and Compiler.h.
  51. # NOTE: Negative lookahead (?!error) prevents matching "#error WebKit was not available prior to Mac OS X 10.2".
  52. # NOTE: Negative lookahead (?!:2) prevents matching OS2 in macros like "defined(__OS2__)".
  53. if match = /^\s*#(?!error).*?\b(PLATFORM|CPU|HAVE|OS(?!2)|USE|ENABLE|COMPILER)/.match(line)
  54. print_error "'#{header}:#{index+1}' included forbidden macro '#{match[1]}' => '#{line.chomp}'"
  55. end
  56. end
  57. end
  58. end
  59. Dir.chdir base_directory
  60. framework = "#{project_name}.framework"
  61. ARGV.each do |path|
  62. framework_headers_for_path(framework, path).each do |header|
  63. verify_macros_in_header(header)
  64. end
  65. end
  66. exit 1 if $error_printed