[Mac] Add Build Phase to Check Headers for Inappropriate Macros (Platform.h macros)
authorjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 18 Dec 2012 21:36:37 +0000 (21:36 +0000)
committerjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 18 Dec 2012 21:36:37 +0000 (21:36 +0000)
https://bugs.webkit.org/show_bug.cgi?id=104279

Reviewed by David Kilzer.

Add a build phase script that checks a framework's header files for
Platform.h and Compiler.h macros. Also add some tests for the script.

* Scripts/check-for-inappropriate-macros-in-external-headers: Added.
Script to be used in build phases to check for inappropriate macros in headers.

* Scripts/test-webkitruby: Added.
Script that can be used to run all webkitruby tests.

* Scripts/test-webkit-scripts:
Also run the ruby tests.

* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/fake-data-failing-expected.txt: Added.
* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/pass-expected.txt: Added.
* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/passing-expected.txt: Added.
* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Fail.h: Added.
* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Pass.h: Added.
* Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/run-test.rb: Added.
Tests for the new script.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@138064 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/check-for-inappropriate-macros-in-external-headers [new file with mode: 0755]
Tools/Scripts/test-webkit-scripts
Tools/Scripts/test-webkitruby [new file with mode: 0755]
Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/fake-data-failing-expected.txt [new file with mode: 0644]
Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/pass-expected.txt [new file with mode: 0644]
Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Fail.h [new file with mode: 0644]
Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Pass.h [new file with mode: 0644]
Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/run-test.rb [new file with mode: 0755]

index fe1fb87..a12637a 100644 (file)
@@ -1,3 +1,29 @@
+2012-12-18  Joseph Pecoraro  <pecoraro@apple.com>
+
+        [Mac] Add Build Phase to Check Headers for Inappropriate Macros (Platform.h macros)
+        https://bugs.webkit.org/show_bug.cgi?id=104279
+
+        Reviewed by David Kilzer.
+
+        Add a build phase script that checks a framework's header files for
+        Platform.h and Compiler.h macros. Also add some tests for the script.
+
+        * Scripts/check-for-inappropriate-macros-in-external-headers: Added.
+        Script to be used in build phases to check for inappropriate macros in headers.
+
+        * Scripts/test-webkitruby: Added.
+        Script that can be used to run all webkitruby tests.
+
+        * Scripts/test-webkit-scripts:
+        Also run the ruby tests.
+
+        * Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/fake-data-failing-expected.txt: Added.
+        * Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/pass-expected.txt: Added.
+        * Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Fail.h: Added.
+        * Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Pass.h: Added.
+        * Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/run-test.rb: Added.
+        Tests for the new script.
+
 2012-12-18  Dominic Mazzoni  <dmazzoni@google.com>
 
         AX: support clickPoint in DRT for chromium
diff --git a/Tools/Scripts/check-for-inappropriate-macros-in-external-headers b/Tools/Scripts/check-for-inappropriate-macros-in-external-headers
new file mode 100755 (executable)
index 0000000..7e6c31f
--- /dev/null
@@ -0,0 +1,77 @@
+#!/usr/bin/env ruby
+
+# Copyright (C) 2012 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+# THE POSSIBILITY OF SUCH DAMAGE.
+
+# This script checks that the given headers in the framework build product do
+# not contain Platform.h and Compiler.h macros such as PLATFORM, COMPILER, etc.
+# This is meant to limit the exposure of the WTF headers, ensuring that if
+# clients include these headers they would not also need WTF's Platform.h.
+
+base_directory = ENV['TARGET_BUILD_DIR'] or throw "Unable to find TARGET_BUILD_DIR in the environment!"
+project_name = ENV['PROJECT_NAME'] or throw "Unable to find PROJECT_NAME in the environment!"
+$is_shallow_bundle = (ENV['SHALLOW_BUNDLE'] || "NO").upcase == "YES"
+
+$error_printed = false
+
+def print_error(msg)
+  $error_printed = true
+  STDERR.puts "ERROR: #{msg}"
+end
+
+def framework_headers_for_path(framework, path)
+  full_path = File.join Dir.pwd, framework, $is_shallow_bundle ? "" : "Versions/A/", path
+  if File.directory? full_path
+    Dir.glob "#{full_path}/**/*.h"
+  elsif File.exists? full_path
+    [full_path]
+  else
+    print_error "path '#{full_path}' for argument '#{path}' does not exist."
+    [] # Return an empty list so we can continue to check the other paths.
+  end
+end
+
+def verify_macros_in_header(header)
+  File.open(header) do |file|
+    file.each_line.with_index do |line, index|
+      # Check for the common macros from Platform.h and Compiler.h.
+      # NOTE: Negative lookahead (?!error) prevents matching "#error WebKit was not available prior to Mac OS X 10.2".
+      # NOTE: Negative lookahead (?!:2) prevents matching OS2 in macros like "defined(__OS2__)".
+      if match = /^\s*#(?!error).*?\b(PLATFORM|CPU|HAVE|OS(?!2)|USE|ENABLE|COMPILER)/.match(line)
+        print_error "'#{header}:#{index+1}' included forbidden macro '#{match[1]}' => '#{line.chomp}'"
+      end
+    end
+  end
+end
+
+
+Dir.chdir base_directory
+
+framework = "#{project_name}.framework"
+ARGV.each do |path|
+  framework_headers_for_path(framework, path).each do |header|
+    verify_macros_in_header(header)
+  end
+end
+
+exit 1 if $error_printed
index 781e8ce..baba059 100755 (executable)
@@ -28,7 +28,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-"""Run unit tests of WebKit's Perl and Python scripts."""
+"""Run unit tests of WebKit's Perl, Python, and Ruby scripts."""
 
 # The docstring above is passed as the "description" to the OptionParser
 # used in this script's __main__ block.
@@ -72,11 +72,12 @@ class ScriptsTester(object):
         self.run_test_script('Perl scripts', self.script_path('test-webkitperl'))
         self.run_test_script('Python scripts', self.script_path('test-webkitpy'),
                              ['--all'] if options.all else None)
+        self.run_test_script('Ruby scripts', self.script_path('test-webkitruby'))
 
         # FIXME: Display a cumulative indication of success or failure.
         #        In addition, call sys.exit() with 0 or 1 depending on that
         #        cumulative success or failure.
-        print('Note: Perl and Python results appear separately above.')
+        print('Note: Perl, Python, and Ruby results appear separately above.')
 
 
 if __name__ == '__main__':
diff --git a/Tools/Scripts/test-webkitruby b/Tools/Scripts/test-webkitruby
new file mode 100755 (executable)
index 0000000..cd04a0a
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/env ruby
+
+# Copyright (C) 2012 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+# THE POSSIBILITY OF SUCH DAMAGE.
+
+$exit_code = 0;
+
+Dir.chdir File.dirname(__FILE__)
+Dir.glob("./webkitruby/*/*.rb").each do |test|
+  puts %x{ '#{test}' }
+  $exit_code = 1 if $?.exitstatus != 0
+end
+
+exit $exit_code
diff --git a/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/fake-data-failing-expected.txt b/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/fake-data-failing-expected.txt
new file mode 100644 (file)
index 0000000..32e966f
--- /dev/null
@@ -0,0 +1,11 @@
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:2' included forbidden macro 'PLATFORM' => '#if PLATFORM(MAC)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:4' included forbidden macro 'CPU' => '#if CPU(X86)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:6' included forbidden macro 'OS' => '#if OS(DARWIN)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:8' included forbidden macro 'COMPILER' => '#if COMPILER(CLANG)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:10' included forbidden macro 'ENABLE' => '#if ENABLE(FEATURE)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:12' included forbidden macro 'HAVE' => '#if HAVE(FEATURE)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:14' included forbidden macro 'USE' => '#if USE(FEATURE)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:16' included forbidden macro 'COMPILER' => '#if COMPILER_SUPPORTS(FEATURE)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:18' included forbidden macro 'COMPILER' => '#if COMPILER_QUIRK(FEATURE)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:23' included forbidden macro 'PLATFORM' => '  #if PLATFORM(X)'
+ERROR: '--stripped--/Fake.framework/Headers/Fail.h:28' included forbidden macro 'PLATFORM' => '#if defined(ignored) && PLATFORM(X)'
diff --git a/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/pass-expected.txt b/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/pass-expected.txt
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Fail.h b/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Fail.h
new file mode 100644 (file)
index 0000000..6b42092
--- /dev/null
@@ -0,0 +1,29 @@
+// Common macros that we want to catch.
+#if PLATFORM(MAC)
+#endif
+#if CPU(X86)
+#endif
+#if OS(DARWIN)
+#endif
+#if COMPILER(CLANG)
+#endif
+#if ENABLE(FEATURE)
+#endif
+#if HAVE(FEATURE)
+#endif
+#if USE(FEATURE)
+#endif
+#if COMPILER_SUPPORTS(FEATURE)
+#endif
+#if COMPILER_QUIRK(FEATURE)
+#endif
+
+// Indented.
+#if 1
+  #if PLATFORM(X)
+  #endif
+#endif
+
+// Conditionals, we don't evalute. We just check for the existence of the macro.
+#if defined(ignored) && PLATFORM(X)
+#endif
diff --git a/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Pass.h b/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/resources/Fake.framework/Headers/Pass.h
new file mode 100644 (file)
index 0000000..3a8a15d
--- /dev/null
@@ -0,0 +1,6 @@
+// A macro word in a #error should not matter, that is just a coincidence.
+#error PLATFORM
+
+// There are references to a OS2, but that is not the OS() macro.
+#if defined(__OS2__) || defined(OS2)
+#endif
diff --git a/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/run-test.rb b/Tools/Scripts/webkitruby/check-for-inappropriate-macros-in-external-headers-tests/run-test.rb
new file mode 100755 (executable)
index 0000000..e362ba3
--- /dev/null
@@ -0,0 +1,74 @@
+#!/usr/bin/env ruby
+
+# Copyright (C) 2012 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+# THE POSSIBILITY OF SUCH DAMAGE.
+
+# Testing Tools/Scripts/check-for-macros-in-external-headers
+$test_directory = File.dirname(__FILE__)
+$tool = File.expand_path(File.join($test_directory, '..', '..', 'check-for-inappropriate-macros-in-external-headers'))
+puts "Testing: Tools/Scripts/check-for-inappropriate-macros-in-external-headers"
+
+$was_failure = false
+
+def sanitized_output(output)
+  lines = output.split("\n").map { |line| line.sub(/\'(.*)?\/(.*)?\.framework/, "'--stripped--/\\2.framework") }
+  lines.join("\n") + (lines.empty? ? "" : "\n")
+end
+
+def run_test(config)
+  ENV['TARGET_BUILD_DIR'] = File.join($test_directory, 'resources')
+  ENV['PROJECT_NAME'] = config[:framework]
+  ENV['SHALLOW_BUNDLE'] = config[:shallow] ? 'YES' : 'NO'
+  output = sanitized_output %x{ #{$tool} #{config[:paths].join(' ')} 2>&1 }
+
+  if config[:expectedToPass] != ($?.exitstatus == 0)
+    pass = false
+  else
+    expected_output = File.read File.join($test_directory, config[:expectedOutput])
+    pass = output == expected_output
+  end
+
+  puts "#{pass ? "PASS" : "FAIL"} - #{config[:name]}"
+  $was_failure = true if !pass
+end
+
+[
+  {
+    :name => 'test_good_fake_data',
+    :framework => 'Fake',
+    :shallow => true,
+    :paths => ['Headers/Pass.h'],
+    :expectedToPass => true,
+    :expectedOutput => 'pass-expected.txt'
+  },
+  {
+    :name => 'test_bad_fake_data',
+    :framework => 'Fake',
+    :shallow => true,
+    :paths => ['Headers/Fail.h'],
+    :expectedToPass => false,
+    :expectedOutput => 'fake-data-failing-expected.txt'
+  }
+].each { |x| run_test(x) }
+
+exit 1 if $was_failure