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